|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Metrics\Collector; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Exec\File; |
|
6
|
|
|
use Startwind\Inventorio\Exec\Runner; |
|
7
|
|
|
use Startwind\Inventorio\Exec\System; |
|
8
|
|
|
use Startwind\Inventorio\Metrics\Memory\Memory; |
|
9
|
|
|
|
|
10
|
|
|
class Collector |
|
11
|
|
|
{ |
|
12
|
|
|
private const MEMORY_KEY = 'net_traffic_history'; |
|
13
|
|
|
|
|
14
|
|
|
public function collect(): array |
|
15
|
|
|
{ |
|
16
|
|
|
$runner = Runner::getInstance(); |
|
17
|
|
|
$system = System::getInstance(); |
|
18
|
|
|
|
|
19
|
|
|
$totalMem = (int)$runner->run("grep MemTotal /proc/meminfo | awk '{print $2}'")->getOutput(); |
|
20
|
|
|
$freeMem = (int)$runner->run("grep MemAvailable /proc/meminfo | awk '{print $2}'")->getOutput(); |
|
21
|
|
|
$usedMem = $totalMem - $freeMem; |
|
22
|
|
|
$usedMemPercent = round(($usedMem / $totalMem) * 100, 2); |
|
23
|
|
|
|
|
24
|
|
|
$loadAvg = (float)$system->getLoadAverage()[1]; |
|
25
|
|
|
$cpuCores = (int)$runner->run("nproc")->getOutput(); |
|
26
|
|
|
|
|
27
|
|
|
$cpuUsagePercent = ($cpuCores > 0) |
|
28
|
|
|
? round(($loadAvg / $cpuCores) * 100, 1) |
|
29
|
|
|
: 0; |
|
30
|
|
|
|
|
31
|
|
|
$diskTotal = $system->getDiskTotalSpace('/'); |
|
32
|
|
|
$diskFree = $system->getDiskFreeSpace('/'); |
|
33
|
|
|
|
|
34
|
|
|
$diskUsedPercent = ($diskTotal > 0) |
|
35
|
|
|
? round((($diskTotal - $diskFree) / $diskTotal) * 100, 1) |
|
36
|
|
|
: 0; |
|
37
|
|
|
|
|
38
|
|
|
return [ |
|
39
|
|
|
'memory-usage' => $usedMemPercent, |
|
40
|
|
|
'cpu-usage' => $cpuUsagePercent, |
|
41
|
|
|
'disk-usage' => $diskUsedPercent, |
|
42
|
|
|
'network-throughput-eth0' => $this->calculateNetworkThroughput('eth0') |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
function calculateNetworkThroughput($interface): float |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$memory = Memory::getInstance(); |
|
49
|
|
|
|
|
50
|
|
|
$file = new File(); |
|
51
|
|
|
|
|
52
|
|
|
if (!$file->fileExists('/proc/net/dev')) return 0; |
|
53
|
|
|
|
|
54
|
|
|
$lines = $file->getContents('/proc/net/dev', true); |
|
55
|
|
|
foreach ($lines as $line) { |
|
56
|
|
|
if (strpos($line, $interface . ':') !== false) { |
|
57
|
|
|
$parts = preg_split('/\s+/', trim($line)); |
|
58
|
|
|
$rx = (int)$parts[1]; |
|
59
|
|
|
$tx = (int)$parts[9]; |
|
60
|
|
|
$total = $rx + $tx; |
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!isset($total)) return 0; |
|
66
|
|
|
|
|
67
|
|
|
$history = $memory->getData(self::MEMORY_KEY) ?? []; |
|
68
|
|
|
|
|
69
|
|
|
if (empty($history)) { |
|
70
|
|
|
$lastTotal = 0; |
|
71
|
|
|
} else { |
|
72
|
|
|
$lastTotal = end($history); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($total < $lastTotal) { |
|
|
|
|
|
|
76
|
|
|
$memory->addData(self::MEMORY_KEY, $total); |
|
77
|
|
|
return $total; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$throughput = $total - $lastTotal; |
|
81
|
|
|
|
|
82
|
|
|
$memory->addData(self::MEMORY_KEY, $total); |
|
83
|
|
|
|
|
84
|
|
|
return $throughput; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.