diff --git a/.gitignore b/.gitignore index 0dbdb364eaef9ae3f3dc2d4bb9d46bb7005026df..c5d3125d78bee91b8f934217aa8d0167271b0b05 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ yarn-error.log .env /volumes/ +/target/ monitor monitor-*.zip diff --git a/app/ServerInfo.php b/app/ServerInfo.php index dd77ed6223ee9fd94bb13d3b5f3c87e414fe608f..2d060a81f5c31a44a1c8187d1dd7aea933a7fb4d 100644 --- a/app/ServerInfo.php +++ b/app/ServerInfo.php @@ -5,186 +5,170 @@ namespace App; use Carbon\Carbon; /** - * Holds info about a server. + * Parse a record and store info about the server. */ class ServerInfo { - const REGEX_PRODUCT_NAME = "/^\s*Product Name: (.*)$/m"; - + + public $uptime; + public $uuid; + public $cpuinfo; + public $memory_total; + public $client_version; + public $lsb; + public $manufacturer; + public $product_name; + + private $parser; private $record; - + /** * @param Record $record */ public function __construct(Record $record) { + $this->parser = new ServerInfoParser(); $this->record = $record; + + $this->uptime = $this->parseUptime(); + $this->uuid = $this->parseUUID(); + $this->lsb = $this->parseLsb(); + $this->product_name = $this->parseProductName(); + $this->cpuinfo = $this->parseCpuinfo(); + $this->memory_total = $this->parseMemoryTotal(); + $this->client_version = $this->parseClientVersion(); } /** * Human readable uptime. * * @return string */ - public function uptime() : string + public function parseUptime() : string { if (! isset($this->record->data["upaimte"])) { return "unknown"; } - return $this->parseUptime($this->record->data["upaimte"]); + return $this->parser->parseUptime($this->record->data["upaimte"]); } - - public function parseUptime(string $string) : string + + public function uptime() : string { - $pieces = explode(' ', $string); - $uptime = \Carbon\Carbon::now()->subSeconds($pieces[0]); - return $uptime->diffForHumans(null, true); + return $this->uptime; } - public function uuid() + public function parseUuid() { if (! isset($this->record->data["system"])) { return "unknown"; } - - return $this->parseUUID($this->record->data["system"]); + + return $this->parser->parseUUID($this->record->data["system"]); } - - const UUID = "/\s*UUID: (.*)/m"; - - public function parseUUID(string $string) : string + + public function uuid() : string { - $matches = array(); - preg_match(self::UUID, $string, $matches); - if (! isset($matches[1])) { - return "unknown"; - } - return $matches[1]; + return $this->uuid; } - - public function cpuinfo() : array + public function parseCpuinfo() : array { if (! isset($this->record->data["cpu"])) { return ["threads" => 0, "cpu" => "unknown"]; } - return $this->parseCpuinfo($this->record->data["cpu"]); + return $this->parser->parseCpuinfo($this->record->data["cpu"]); } - - const CPU_INFO = "/^model name : (.+)$/m"; - public function parseCpuinfo(string $string) : array + public function cpuinfo() { - $matches = array(); - preg_match_all(self::CPU_INFO, $string, $matches); - - $result["threads"] = count($matches[0]); - $result["cpu"] = $matches[1][0]; - return $result; + return $this->cpuinfo; } - public function meminfo() + public function memoryTotalForHumans() { - return round($this->memoryTotal() / 1000 / 1000) . " GB"; + return round($this->memoryTotal() / 1024 / 1024) . " GB"; } /** * * @return int total memory (in KB) or 0 if not found... */ - public function memoryTotal() + public function parseMemoryTotal() { if (! isset($this->record->data["memory"])) { return 0; } - return $this->parseMeminfo($this->record->data["memory"]); + return $this->parser->parseMeminfo($this->record->data["memory"]); } - - const MEMINFO = "/^MemTotal:\\s+([0-9]+) kB$/m"; - public function parseMeminfo(string $string) + public function memoryTotal() { - $matches = array(); - preg_match(self::MEMINFO, $string, $matches); - $total = $matches[1]; - return $total; + return $this->memory_total; } - - public function lsb() + + public function parseLsb() { if (! isset($this->record->data["lsb"])) { return "unknown"; } - return $this->parseLsb($this->record->data["lsb"]); - } - - const LSB = "/^Description: (.+)$/m"; - public function parseLsb(string $string) : string - { - $matches = []; - preg_match(self::LSB, $string, $matches); - return $matches[1]; + return $this->parser->parseLsb($this->record->data["lsb"]); } - - - const REGEX_MANUFACTURER = "/^\s*Manufacturer: (.*)$/m"; - public function parseManufacturer(string $string) : string + public function lsb() { - $matches = []; - preg_match(self::REGEX_MANUFACTURER, $string, $matches); - - if (!isset($matches[1])) { - return "unkwnown"; - } - return $matches[1]; + return $this->lsb; } - public function manufacturer() + + public function parseManufacturer() { if (! isset($this->record->data["system"])) { return "unknown"; } - return $this->parseManufacturer($this->record->data["system"]); + return $this->parser->parseManufacturer($this->record->data["system"]); } - - - public function parseProductName(string $string) : string + + public function manufacturer() { - $matches = []; - preg_match(self::REGEX_PRODUCT_NAME, $string, $matches); - if (!isset($matches[1])) { - return "unkwnown"; - } - return $matches[1]; + return $this->manufacturer; } - public function productName() + public function parseProductName() { if (! isset($this->record->data["system"])) { return "unknown"; } - return $this->parseProductName($this->record->data["system"]); + return $this->parser->parseProductName($this->record->data["system"]); + } + + public function productName() + { + return $this->product_name; } - /** + /** * * @return \Carbon\Carbon */ - public function lastRecordTime() + public function lastRecordTime() : Carbon { return Carbon::createFromTimestamp($this->record->time); } - public function clientVersion() : string + public function parseClientVersion() : string { // $sensor = new \App\Sensor\ClientVersion(); // return $sensor->installedVersion([$this->record]); return ""; } + + public function clientVersion() : string + { + return $this->client_version; + } } diff --git a/app/ServerInfoParser.php b/app/ServerInfoParser.php new file mode 100644 index 0000000000000000000000000000000000000000..4a18ff945c064582eef51a4bd773aca36b3848fe --- /dev/null +++ b/app/ServerInfoParser.php @@ -0,0 +1,85 @@ +<?php + +namespace App; + +use Carbon\Carbon; + +/** + * Algorithms used to parse server information from string. + * + * @author tibo + */ +class ServerInfoParser +{ + public function parseUptime(string $string) : string + { + $pieces = explode(' ', $string); + $uptime = Carbon::now()->subSeconds($pieces[0]); + return $uptime->diffForHumans(null, true); + } + + public function parseUUID(string $string) : string + { + $REGEX = "/\s*UUID: (.*)/m"; + + $matches = array(); + preg_match($REGEX, $string, $matches); + if (! isset($matches[1])) { + return "unknown"; + } + return $matches[1]; + } + + public function parseCpuinfo(string $string) : array + { + $REGEX = "/^model name : (.+)$/m"; + $matches = array(); + preg_match_all($REGEX, $string, $matches); + + $result["threads"] = count($matches[0]); + $result["cpu"] = $matches[1][0]; + return $result; + } + + public function parseMeminfo(string $string) + { + $REGEX = "/^MemTotal:\\s+([0-9]+) kB$/m"; + $matches = array(); + preg_match($REGEX, $string, $matches); + $total = $matches[1]; + return $total; + } + + public function parseProductName(string $string) : string + { + $REGEX = "/^\s*Product Name: (.*)$/m"; + + $matches = []; + preg_match($REGEX, $string, $matches); + if (!isset($matches[1])) { + return "unkwnown"; + } + return $matches[1]; + } + + + public function parseManufacturer(string $string) : string + { + $REGEX = "/^\s*Manufacturer: (.*)$/m"; + $matches = []; + preg_match($REGEX, $string, $matches); + + if (!isset($matches[1])) { + return "unkwnown"; + } + return $matches[1]; + } + + public function parseLsb(string $string) : string + { + $REGEX = "/^Description: (.+)$/m"; + $matches = []; + preg_match($REGEX, $string, $matches); + return $matches[1]; + } +} diff --git a/resources/views/server/show.blade.php b/resources/views/server/show.blade.php index ba2681bfd8291a77caf1d18af31353600d55a029..fac0a63170b31d11adf4fd0fc71eee60b74bfe34 100644 --- a/resources/views/server/show.blade.php +++ b/resources/views/server/show.blade.php @@ -30,7 +30,7 @@ window.monitorServerToken = "{{ $server->read_token }}"; <p><small>{{ $server->info()->uuid() }}</small></p> <p>{{ $server->info()->cpuinfo()["cpu"] }} ({{ $server->info()->cpuinfo()["threads"] }} threads)</p> - <p>Memory: {{ $server->info()->meminfo() }}</p> + <p>Memory: {{ $server->info()->memoryTotalForHumans() }}</p> <p>{{ $server->info()->lsb() }}</p> @else diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php index ddee7f213e35a66618fc1bf888df01f02f381877..a2de136f4fbcf18d7599ecdaea4ced0a96487a14 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/ExampleTest.php @@ -6,11 +6,6 @@ use App\User; use App\Organization; use App\Server; use App\Record; -use App\ServerInfo; -use App\Sensor\Disks; -use App\Sensor\CPUtemperature; -use App\Sensor\USBtemperature; -use App\Sensor\Ifconfig; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -46,157 +41,6 @@ class ExampleTest extends TestCase ); } - /** - * @group ifconfig - * @group sensors - */ - public function testIfconfig() - { - $string = file_get_contents(__DIR__ . "/ifconfig"); - $sensor = new Ifconfig(new \App\Server()); - $interfaces = $sensor->parseIfconfig($string); - $this->assertEquals(2, count($interfaces)); - $this->assertEquals("enp0s31f6", $interfaces[0]->name); - $this->assertEquals("10.67.1.32", $interfaces[1]->address); - $this->assertEquals(1074590056, $interfaces[1]->rx); - $this->assertEquals(2074977132, $interfaces[1]->tx); - } - - /** - * Test parsing of ifconfig string from a ubuntu 18.04 server - * - * @group ifconfig - * @group sensors - */ - public function testIfconfig1804() - { - $string = file_get_contents(__DIR__ . "/ifconfig1804"); - $sensor = new Ifconfig(new \App\Server()); - $interfaces = $sensor->parseIfconfig($string); - $this->assertEquals(2, count($interfaces)); - $this->assertEquals("eno1", $interfaces[0]->name); - $this->assertEquals("172.20.0.8", $interfaces[1]->address); - $this->assertEquals(185252610, $interfaces[1]->rx); - $this->assertEquals(266912412, $interfaces[1]->tx); - } - - /** - * @group Disks - */ - - public function testDisksSensor() - { - $string = file_get_contents(__DIR__ . "/df"); - $sensor = new Disks(new Server()); - $disks = $sensor->parse($string); - $this->assertEquals(2, count($disks)); - $this->assertEquals("/dev/sda1", $disks[0]->filesystem); - $this->assertEquals(1128926648, $disks[0]->blocks); - } - - public function testNetstatListening() - { - $string = file_get_contents(__DIR__ . "/netstat-tcp"); - $sensor = new \App\Sensor\ListeningPorts(new Server()); - $ports = $sensor->parse($string); - $this->assertEquals(16, count($ports)); - $this->assertEquals("31933/cloud-backup-", $ports[4]->process); - $this->assertEquals(1024, $ports[4]->port); - $this->assertEquals("127.0.0.1", $ports[4]->bind); - } - - public function testSsacli() - { - $string = file_get_contents(__DIR__ . "/ssacli"); - $sensor = new \App\Sensor\Ssacli(new Server()); - $disks = $sensor->parse($string); - $this->assertEquals("OK", $disks[0]->status); - } - - public function testPerccli() - { - $string = file_get_contents(__DIR__ . "/perccli"); - $sensor = new \App\Sensor\Perccli(new Server()); - $disks = $sensor->parse($string); - $this->assertEquals("Onln", $disks[0]->status); - $this->assertEquals("SSD", $disks[0]->type); - $this->assertEquals("446.625 GB", $disks[0]->size); - } - - public function testUpdates() - { - $sensor = new \App\Sensor\Updates(new \App\Server()); - - $string1 = "6 packages can be updated. -2 updates are security updates."; - $status = $sensor->parse($string1); - $this->assertEquals(2, $status["security"]); - - $string2 = "1 package can be updated. -1 update is a security update. -"; - $status2 = $sensor->parse($string2); - $this->assertEquals(1, $status2["security"]); - } - - public function testMeminfo() - { - $string = file_get_contents(__DIR__ . "/meminfo"); - $mem_total = (new ServerInfo(new Record()))->parseMeminfo($string); - $this->assertEquals("15954328", $mem_total); - } - - /** - * @group cpuinfo - */ - public function testCpuinfo() - { - $string = file_get_contents(__DIR__ . "/cpuinfo"); - $cpuinfo = (new ServerInfo(new Record()))->parseCpuinfo($string); - $this->assertEquals(8, $cpuinfo["threads"]); - $this->assertEquals("Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz", $cpuinfo["cpu"]); - } - - /** - * @group uptime - */ - public function testUptime() - { - $string = "24439.45 190434.65"; - $uptime = (new ServerInfo(new Record()))->parseUptime($string); - $this->assertEquals("6 hours", $uptime); - } - - public function testUUID() - { - $uuid = (new ServerInfo(new Record()))->parseUUID(file_get_contents(__DIR__ . "/system")); - $this->assertEquals("74F7C34C-2924-11B2-A85C-DC427DCA7109", $uuid); - } - - /** - * @group cpuinfo - */ - public function testCpuinfoSingleCPU() - { - $string = file_get_contents(__DIR__ . "/cpuinfo_1cpu"); - $cpuinfo = (new ServerInfo(new Record()))->parseCpuinfo($string); - $this->assertEquals(1, $cpuinfo["threads"]); - $this->assertEquals("Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz", $cpuinfo["cpu"]); - } - - public function testManufacturer() - { - $string = file_get_contents(__DIR__ . "/system"); - $manufacturer = (new ServerInfo(new Record()))->parseManufacturer($string); - $this->assertEquals("LENOVO", $manufacturer); - } - - public function testProductName() - { - $string = file_get_contents(__DIR__ . "/system"); - $manufacturer = (new ServerInfo(new Record()))->parseProductName($string); - $this->assertEquals("20J60018MB", $manufacturer); - } public function testClientVersion() { @@ -205,16 +49,6 @@ class ExampleTest extends TestCase $this->assertStringMatchesFormat('%d.%d.%d', $client_version->latestVersion()); } - /** - * @group netstat - */ - public function testNetstat() - { - $string = file_get_contents(__DIR__ . "/netstat"); - $server = new \App\Server(); - $netstat = new \App\Sensor\Netstat($server); - $this->assertEquals(24004, $netstat->parse($string)->tcp_segments_retransmitted); - } /** * @group status-change @@ -281,41 +115,4 @@ class ExampleTest extends TestCase $change_detection_job->detectChangeForServer($server); } } - /** - * @group CPUtemp - */ - public function testCPUtemp() - { - $string = file_get_contents(__DIR__ . "/sensors"); - $sensor = new CPUtemperature(new Server()); - $CPUTEMPS = $sensor->parse($string); - $this->assertEquals(4, count($CPUTEMPS)); - $this->assertEquals("Core 3", $CPUTEMPS[3]->name); - $this->assertEquals("34.0", $CPUTEMPS[3]->value); - } - /** - * @group USBtemp - */ - public function testTEMPer() - { - $string = file_get_contents(__DIR__ . "/TEMPer"); - $TEMPer = new USBtemperature(new Server()); - $USBTemp = $TEMPer->parse($string); - $this->assertEquals("09", $USBTemp->part1); - $this->assertEquals("47", $USBTemp->part2); - $this->assertEquals("23", $USBTemp->temp[1]); - $this->assertEquals("75", $USBTemp->temp[2]); - } - /** - * @group multicpu - */ - public function testmultiCPUtemp() - { - $string = file_get_contents(__DIR__ . "/sensors"); - $sensor = new CPUtemperature(new Server()); - $CPUTEMPS = $sensor->parseCPUtemperature($string); - $this->assertEquals(4, count($CPUTEMPS)); - $this->assertEquals("Core 3", $CPUTEMPS[3]->name); - $this->assertEquals("34.0", $CPUTEMPS[3]->corevalue); - } } diff --git a/tests/Unit/SensorsTest.php b/tests/Unit/SensorsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6bfb28114d6cc301bd2bf86d4bacff16ef239139 --- /dev/null +++ b/tests/Unit/SensorsTest.php @@ -0,0 +1,165 @@ +<?php + +namespace Tests\Unit; + +use App\Server; +use App\Sensor\Disks; +use App\Sensor\CPUtemperature; +use App\Sensor\USBtemperature; +use App\Sensor\Ifconfig; +use App\Sensor\Updates; +use App\Sensor\Netstat; + +use Tests\TestCase; + +/** + * Description of SensorsTest + * + * @author tibo + */ +class SensorsTest extends TestCase +{ + /** + * @group ifconfig + * @group sensors + */ + public function testIfconfig() + { + $string = file_get_contents(__DIR__ . "/ifconfig"); + $sensor = new Ifconfig(new Server()); + $interfaces = $sensor->parseIfconfig($string); + $this->assertEquals(2, count($interfaces)); + $this->assertEquals("enp0s31f6", $interfaces[0]->name); + $this->assertEquals("10.67.1.32", $interfaces[1]->address); + $this->assertEquals(1074590056, $interfaces[1]->rx); + $this->assertEquals(2074977132, $interfaces[1]->tx); + } + + /** + * Test parsing of ifconfig string from a ubuntu 18.04 server + * + * @group ifconfig + * @group sensors + */ + public function testIfconfig1804() + { + $string = file_get_contents(__DIR__ . "/ifconfig1804"); + $sensor = new Ifconfig(new Server()); + $interfaces = $sensor->parseIfconfig($string); + $this->assertEquals(2, count($interfaces)); + $this->assertEquals("eno1", $interfaces[0]->name); + $this->assertEquals("172.20.0.8", $interfaces[1]->address); + $this->assertEquals(185252610, $interfaces[1]->rx); + $this->assertEquals(266912412, $interfaces[1]->tx); + } + + /** + * @group Disks + */ + + public function testDisksSensor() + { + $string = file_get_contents(__DIR__ . "/df"); + $sensor = new Disks(new Server()); + $disks = $sensor->parse($string); + $this->assertEquals(2, count($disks)); + $this->assertEquals("/dev/sda1", $disks[0]->filesystem); + $this->assertEquals(1128926648, $disks[0]->blocks); + } + + public function testNetstatListening() + { + $string = file_get_contents(__DIR__ . "/netstat-tcp"); + $sensor = new \App\Sensor\ListeningPorts(new Server()); + $ports = $sensor->parse($string); + $this->assertEquals(16, count($ports)); + $this->assertEquals("31933/cloud-backup-", $ports[4]->process); + $this->assertEquals(1024, $ports[4]->port); + $this->assertEquals("127.0.0.1", $ports[4]->bind); + } + + public function testSsacli() + { + $string = file_get_contents(__DIR__ . "/ssacli"); + $sensor = new \App\Sensor\Ssacli(new Server()); + $disks = $sensor->parse($string); + $this->assertEquals("OK", $disks[0]->status); + } + + public function testPerccli() + { + $string = file_get_contents(__DIR__ . "/perccli"); + $sensor = new \App\Sensor\Perccli(new Server()); + $disks = $sensor->parse($string); + $this->assertEquals("Onln", $disks[0]->status); + $this->assertEquals("SSD", $disks[0]->type); + $this->assertEquals("446.625 GB", $disks[0]->size); + } + + public function testUpdates() + { + $sensor = new Updates(new Server()); + + $string1 = "6 packages can be updated. +2 updates are security updates."; + $status = $sensor->parse($string1); + $this->assertEquals(2, $status["security"]); + + $string2 = "1 package can be updated. +1 update is a security update. +"; + $status2 = $sensor->parse($string2); + $this->assertEquals(1, $status2["security"]); + } + + + /** + * @group netstat + */ + public function testNetstat() + { + $string = file_get_contents(__DIR__ . "/netstat"); + $server = new Server(); + $netstat = new Netstat($server); + $this->assertEquals(24004, $netstat->parse($string)->tcp_segments_retransmitted); + } + + + /** + * @group CPUtemp + */ + public function testCPUtemp() + { + $string = file_get_contents(__DIR__ . "/sensors"); + $sensor = new CPUtemperature(new Server()); + $CPUTEMPS = $sensor->parse($string); + $this->assertEquals(4, count($CPUTEMPS)); + $this->assertEquals("Core 3", $CPUTEMPS[3]->name); + $this->assertEquals("34.0", $CPUTEMPS[3]->value); + } + /** + * @group USBtemp + */ + public function testTEMPer() + { + $string = file_get_contents(__DIR__ . "/TEMPer"); + $TEMPer = new USBtemperature(new Server()); + $USBTemp = $TEMPer->parse($string); + $this->assertEquals("09", $USBTemp->part1); + $this->assertEquals("47", $USBTemp->part2); + $this->assertEquals("23", $USBTemp->temp[1]); + $this->assertEquals("75", $USBTemp->temp[2]); + } + /** + * @group multicpu + */ + public function testmultiCPUtemp() + { + $string = file_get_contents(__DIR__ . "/sensors"); + $sensor = new CPUtemperature(new Server()); + $CPUTEMPS = $sensor->parseCPUtemperature($string); + $this->assertEquals(4, count($CPUTEMPS)); + $this->assertEquals("Core 3", $CPUTEMPS[3]->name); + $this->assertEquals("34.0", $CPUTEMPS[3]->corevalue); + } +} diff --git a/tests/Unit/ServerInfoParserTest.php b/tests/Unit/ServerInfoParserTest.php new file mode 100644 index 0000000000000000000000000000000000000000..59e2275fc3414b1a70abd5d279a3c54c94d61b69 --- /dev/null +++ b/tests/Unit/ServerInfoParserTest.php @@ -0,0 +1,74 @@ +<?php + +namespace Tests\Unit; + +use App\ServerInfoParser; + +use Tests\TestCase; + +/** + * Description of ServerInfoParserTest + * + * @author tibo + */ +class ServerInfoParserTest extends TestCase +{ + public function testMeminfo() + { + $string = file_get_contents(__DIR__ . "/meminfo"); + $mem_total = (new ServerInfoParser())->parseMeminfo($string); + $this->assertEquals("15954328", $mem_total); + } + + /** + * @group cpuinfo + */ + public function testCpuinfo() + { + $string = file_get_contents(__DIR__ . "/cpuinfo"); + $cpuinfo = (new ServerInfoParser())->parseCpuinfo($string); + $this->assertEquals(8, $cpuinfo["threads"]); + $this->assertEquals("Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz", $cpuinfo["cpu"]); + } + + /** + * @group uptime + */ + public function testUptime() + { + $string = "24439.45 190434.65"; + $uptime = (new ServerInfoParser())->parseUptime($string); + $this->assertEquals("6 hours", $uptime); + } + + public function testUUID() + { + $uuid = (new ServerInfoParser())->parseUUID(file_get_contents(__DIR__ . "/system")); + $this->assertEquals("74F7C34C-2924-11B2-A85C-DC427DCA7109", $uuid); + } + + /** + * @group cpuinfo + */ + public function testCpuinfoSingleCPU() + { + $string = file_get_contents(__DIR__ . "/cpuinfo_1cpu"); + $cpuinfo = (new ServerInfoParser())->parseCpuinfo($string); + $this->assertEquals(1, $cpuinfo["threads"]); + $this->assertEquals("Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz", $cpuinfo["cpu"]); + } + + public function testManufacturer() + { + $string = file_get_contents(__DIR__ . "/system"); + $manufacturer = (new ServerInfoParser())->parseManufacturer($string); + $this->assertEquals("LENOVO", $manufacturer); + } + + public function testProductName() + { + $string = file_get_contents(__DIR__ . "/system"); + $manufacturer = (new ServerInfoParser())->parseProductName($string); + $this->assertEquals("20J60018MB", $manufacturer); + } +}