Skip to content
Snippets Groups Projects
Commit e9411297 authored by Thibault Debatty's avatar Thibault Debatty
Browse files

use autodiscovery for sensors

parent 26082d49
No related branches found
No related tags found
No related merge requests found
Pipeline #13318 passed
<?php
namespace App;
use Illuminate\Support\Facades\File;
use Illuminate\Support\LazyCollection;
use Symfony\Component\Finder\SplFileInfo;
/**
* Implements sensor auto discovery and registration.
*
* Implements singleton pattern.
*
* @author tibo
*/
class SensorHandler
{
private array $sensors;
private function __construct()
{
$this->sensors = $this->autodiscover();
}
private static $instance;
public static function get() : SensorHandler
{
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
public function sensors() : array
{
return $this->sensors;
}
public function autodiscover() : array
{
$ROOT = __DIR__ . "/Sensor/";
return LazyCollection::make(File::allFiles($ROOT))->map(function (SplFileInfo $file) {
$interface_name = "\App\Sensor";
$class_name = '\App\Sensor\\' . $file->getFilenameWithoutExtension();
if (!is_a($class_name, $interface_name, true)) {
return;
}
return new $class_name;
})->filter()->toArray();
}
}
......@@ -76,25 +76,6 @@ class Server extends Model
private $records_1day = null;
private $info = null;
private static $sensors = [
\App\Sensor\LoadAvg::class,
\App\Sensor\MemInfo::class,
\App\Sensor\Ifconfig::class,
\App\Sensor\Netstat::class,
\App\Sensor\ListeningPorts::class,
\App\Sensor\Reboot::class,
\App\Sensor\Updates::class,
\App\Sensor\Disks::class,
\App\Sensor\Inodes::class,
\App\Sensor\Ssacli::class,
\App\Sensor\Perccli::class,
\App\Sensor\Date::class,
\App\Sensor\ClientVersion::class,
\App\Sensor\Heartbeat::class,
\App\Sensor\CPUtemperature::class,
\App\Sensor\USBtemperature::class
];
public function __construct(array $attributes = array())
{
$attributes["token"] = str_random(32);
......@@ -110,6 +91,11 @@ class Server extends Model
{
return $this->hasMany(Record::class);
}
public function sensors() : array
{
return SensorHandler::get()->sensors();
}
public function lastRecord() : ?Record
{
......@@ -179,13 +165,13 @@ class Server extends Model
$serverinfo = $this->info();
$this->reports = [];
foreach (self::$sensors as $sensor) {
foreach ($this->sensors() as $sensor) {
try {
$report = (new $sensor)->analyze($records, $serverinfo);
$report = $sensor->analyze($records, $serverinfo);
$this->reports[] = $report;
} catch (\Throwable $ex) {
Log::error('Sensor failed : ' . $ex->getTraceAsString());
$report = new Report($sensor, Status::unknown());
$report = new Report(get_class($sensor), Status::unknown());
$report->setHTML("<p>Agent crashed...</p>");
$this->reports[] = $report;
}
......
<?php
namespace Tests\Unit;
use App\SensorHandler;
use Tests\TestCase;
/**
* Description of SensorManagementTest
*
* @group sensor-manager
* @author tibo
*/
class SensorManagementTest extends TestCase
{
public function testAutodiscover()
{
$manager = SensorHandler::get();
var_dump($manager->autodiscover());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment