Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
monitoring
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cylab
monitoring
Commits
e9411297
Commit
e9411297
authored
1 year ago
by
Thibault Debatty
Browse files
Options
Downloads
Patches
Plain Diff
use autodiscovery for sensors
parent
26082d49
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#13318
passed
1 year ago
Stage: test
Stage: deploy
Changes
3
Pipelines
6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/SensorHandler.php
+55
-0
55 additions, 0 deletions
app/SensorHandler.php
app/Server.php
+8
-22
8 additions, 22 deletions
app/Server.php
tests/Unit/SensorManagementTest.php
+22
-0
22 additions, 0 deletions
tests/Unit/SensorManagementTest.php
with
85 additions
and
22 deletions
app/SensorHandler.php
0 → 100644
+
55
−
0
View file @
e9411297
<?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
();
}
}
This diff is collapsed.
Click to expand it.
app/Server.php
+
8
−
22
View file @
e9411297
...
...
@@ -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
;
}
...
...
This diff is collapsed.
Click to expand it.
tests/Unit/SensorManagementTest.php
0 → 100644
+
22
−
0
View file @
e9411297
<?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
());
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment