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\Collector\Metric\Metric; |
9
|
|
|
use Startwind\Inventorio\Metrics\Collector\Metric\Webserver\ApacheAccessLogMetric; |
10
|
|
|
use Startwind\Inventorio\Metrics\Collector\Metric\Webserver\ApacheErrorLogMetric; |
11
|
|
|
use Startwind\Inventorio\Metrics\Collector\Metric\Webserver\NginxAccessLogMetric; |
12
|
|
|
use Startwind\Inventorio\Metrics\Collector\Metric\Webserver\NginxErrorLogMetric; |
13
|
|
|
use Startwind\Inventorio\Metrics\Memory\Memory; |
14
|
|
|
|
15
|
|
|
class Collector |
16
|
|
|
{ |
17
|
|
|
private const MEMORY_KEY = 'net_traffic_history'; |
18
|
|
|
|
19
|
|
|
private array $metrics = []; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->metrics[] = new ApacheAccessLogMetric(); |
24
|
|
|
$this->metrics[] = new ApacheErrorLogMetric(); |
25
|
|
|
|
26
|
|
|
$this->metrics[] = new NginxAccessLogMetric(); |
27
|
|
|
$this->metrics[] = new NginxErrorLogMetric(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function collect(): array |
31
|
|
|
{ |
32
|
|
|
$runner = Runner::getInstance(); |
33
|
|
|
$system = System::getInstance(); |
34
|
|
|
|
35
|
|
|
$totalMem = (int)$runner->run("grep MemTotal /proc/meminfo | awk '{print $2}'")->getOutput() + 1; |
36
|
|
|
$freeMem = (int)$runner->run("grep MemAvailable /proc/meminfo | awk '{print $2}'")->getOutput(); |
37
|
|
|
$usedMem = $totalMem - $freeMem; |
38
|
|
|
$usedMemPercent = round(($usedMem / $totalMem) * 100, 2); |
39
|
|
|
|
40
|
|
|
$loadAvg = (float)$system->getLoadAverage()[1]; |
41
|
|
|
$cpuCores = (int)$runner->run("nproc")->getOutput(); |
42
|
|
|
|
43
|
|
|
$cpuUsagePercent = ($cpuCores > 0) |
44
|
|
|
? round(($loadAvg / $cpuCores) * 100, 1) |
45
|
|
|
: 0; |
46
|
|
|
|
47
|
|
|
$diskTotal = $system->getDiskTotalSpace('/'); |
48
|
|
|
$diskFree = $system->getDiskFreeSpace('/'); |
49
|
|
|
|
50
|
|
|
$diskUsedPercent = ($diskTotal > 0) |
51
|
|
|
? round((($diskTotal - $diskFree) / $diskTotal) * 100, 1) |
52
|
|
|
: 0; |
53
|
|
|
|
54
|
|
|
$metricResults = [ |
55
|
|
|
'memory-usage' => $usedMemPercent, |
56
|
|
|
'cpu-usage' => $cpuUsagePercent, |
57
|
|
|
'disk-usage' => $diskUsedPercent, |
58
|
|
|
'network-throughput-eth0' => $this->calculateNetworkThroughput('eth0') |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
foreach ($this->metrics as $metric) { |
62
|
|
|
/** @var Metric $metric */ |
63
|
|
|
if ($metric->isApplicable()) { |
64
|
|
|
$lastValue = Memory::getInstance()->getLastData($metric->getName(), -1); |
65
|
|
|
$currentValue = $metric->getValue($lastValue); |
66
|
|
|
if ($currentValue >= 0) Memory::getInstance()->addData($metric->getName(), $currentValue); |
67
|
|
|
$metricResults[$metric->getName()] = max(0, $currentValue); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $metricResults; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function calculateNetworkThroughput($interface): float |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$memory = Memory::getInstance(); |
77
|
|
|
|
78
|
|
|
$file = File::getInstance(); |
79
|
|
|
|
80
|
|
|
if (!$file->fileExists('/proc/net/dev')) return 0; |
81
|
|
|
|
82
|
|
|
$lines = $file->getContents('/proc/net/dev', true); |
83
|
|
|
foreach ($lines as $line) { |
84
|
|
|
if (strpos($line, $interface . ':') !== false) { |
85
|
|
|
$parts = preg_split('/\s+/', trim($line)); |
86
|
|
|
$rx = (int)$parts[1]; |
87
|
|
|
$tx = (int)$parts[9]; |
88
|
|
|
$total = $rx + $tx; |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (!isset($total)) return 0; |
94
|
|
|
|
95
|
|
|
$history = $memory->getData(self::MEMORY_KEY) ?? []; |
96
|
|
|
|
97
|
|
|
if (empty($history)) { |
98
|
|
|
$lastTotal = 0; |
99
|
|
|
} else { |
100
|
|
|
$lastTotal = end($history); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($total < $lastTotal || $lastTotal === 0) { |
|
|
|
|
104
|
|
|
$memory->addData(self::MEMORY_KEY, $total); |
105
|
|
|
return $total; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$throughput = $total - $lastTotal; |
109
|
|
|
|
110
|
|
|
$memory->addData(self::MEMORY_KEY, $total); |
111
|
|
|
|
112
|
|
|
return $throughput; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.