File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 55use DivineOmega \ServerInfo \Interfaces \MetricInterface ;
66use DivineOmega \ServerInfo \Metrics \ActiveHttpConnection ;
77use DivineOmega \ServerInfo \Metrics \ApacheServerRunning ;
8+ use DivineOmega \ServerInfo \Metrics \CpuUsagePercentage ;
89use DivineOmega \ServerInfo \Metrics \DiskUsagePercentage ;
910use DivineOmega \ServerInfo \Metrics \LoadAverages ;
1011use DivineOmega \ServerInfo \Metrics \MemoryUsagePercentage ;
@@ -35,6 +36,7 @@ class Metrics
3536 NginxServerRunning::class,
3637 ActiveHttpConnection::class,
3738 LoadAverages::class,
39+ CpuUsagePercentage::class,
3840 ];
3941
4042 public function __construct (Server $ server )
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace DivineOmega \ServerInfo \Metrics ;
4+
5+ class CpuUsagePercentage extends BaseMetric
6+ {
7+ public function populate ()
8+ {
9+ $ output = $ this ->connection
10+ ->run ('top -n 1 -b | grep "%Cpu" | cut -d "," -f 4 | cut -d " " -f 2 ' )
11+ ->getOutput ();
12+
13+ if (!is_numeric ($ output )) {
14+ $ this ->value = null ;
15+ return ;
16+ }
17+
18+ $ idleCpu = (float ) $ output ;
19+
20+ if ($ idleCpu < 0 || $ idleCpu > 100 ) {
21+ $ this ->value = null ;
22+ return ;
23+ }
24+
25+ $ this ->value = 100 - $ idleCpu ;
26+ }
27+
28+ public function getName ()
29+ {
30+ return 'cpu-usage-percentage ' ;
31+ }
32+
33+ }
You can’t perform that action at this time.
0 commit comments