-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathServerMonitor.php
More file actions
200 lines (176 loc) · 6.88 KB
/
ServerMonitor.php
File metadata and controls
200 lines (176 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
class ServerMonitor {
static function getTemp() {
$obj = new stdClass();
$cmd = "cat /sys/class/thermal/thermal_zone0/temp";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get t° ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
$obj->title = "t° Raspberry";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
$obj->percent = 0;
$obj->percent = $output[0]/1000;
// $obj->percent = intval(self::getServerLoad());
}
return $obj;
}
static function getCpu() {
$obj = new stdClass();
$cmd = "cat /proc/cpuinfo";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get CPU ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
$obj->title = "";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
$obj->percent = 0;
$obj->percent = intval(self::getServerLoad());
// find model name
foreach ($output as $value) {
if (preg_match("/model name.+:(.*)/i", $value, $match)) {
$obj->title = $match[1];
break;
}
}
}
return $obj;
}
static function getMemory() {
$obj = new stdClass();
$cmd = "free";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get Memmory ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
$obj->title = "";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
$obj->memTotalBytes = 0;
$obj->memUsedBytes = 0;
$obj->memFreeBytes = 0;
if (preg_match("/Mem: *([0-9]+) *([0-9]+) *([0-9]+) */i", $output[1], $match)) {
$obj->memTotalBytes = $match[1]*1024;
$obj->memUsedBytes = $match[2]*1024;
$obj->memFreeBytes = $match[3]*1024;
$onePc = $obj->memTotalBytes / 100;
$obj->memTotal = self::humanFileSize($obj->memTotalBytes);
$obj->memUsed = self::humanFileSize($obj->memUsedBytes);
$obj->memFree = self::humanFileSize($obj->memFreeBytes);
$obj->percent = intval($obj->memUsedBytes / $onePc);
$obj->title = "Total: {$obj->memTotal} | Free: {$obj->memFree} | Used: {$obj->memUsed}";
}
}
return $obj;
}
static function getDisk() {
$obj = new stdClass();
$cmd = "df -h";
exec($cmd . " 2>&1", $output, $return_val);
if ($return_val !== 0) {
$obj->error = "Get Disk ERROR** " . print_r($output, true);
$obj->command = $cmd;
} else {
$obj->percent = 0;
foreach ($output as $value) {
if (preg_match("/([0-9]+)% \/$/i", $value, $match)) {
$obj->percent = intval($match[1]);
break;
}
}
$obj->title = "Usage of {$obj->percent}%";
$obj->success = 1;
$obj->output = $output;
$obj->command = $cmd;
}
return $obj;
}
static function humanFileSize($size, $unit = "") {
if ((!$unit && $size >= 1 << 30) || $unit == "GB")
return number_format($size / (1 << 30), 2) . "GB";
if ((!$unit && $size >= 1 << 20) || $unit == "MB")
return number_format($size / (1 << 20), 2) . "MB";
if ((!$unit && $size >= 1 << 10) || $unit == "KB")
return number_format($size / (1 << 10), 2) . "KB";
return number_format($size) . " bytes";
}
static private function _getServerLoadLinuxData() {
if (is_readable("/proc/stat")) {
$stats = @file_get_contents("/proc/stat");
if ($stats !== false) {
// Remove double spaces to make it easier to extract values with explode()
$stats = preg_replace("/[[:blank:]]+/", " ", $stats);
// Separate lines
$stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
$stats = explode("\n", $stats);
// Separate values and find line for main CPU load
foreach ($stats as $statLine) {
$statLineData = explode(" ", trim($statLine));
// Found!
if
(
(count($statLineData) >= 5) &&
($statLineData[0] == "cpu")
) {
return array(
$statLineData[1],
$statLineData[2],
$statLineData[3],
$statLineData[4],
);
}
}
}
}
return null;
}
// Returns server load in percent (just number, without percent sign)
static function getServerLoad() {
$load = null;
if (stristr(PHP_OS, "win")) {
$cmd = "wmic cpu get loadpercentage /all";
@exec($cmd, $output);
if ($output) {
foreach ($output as $line) {
if ($line && preg_match("/^[0-9]+\$/", $line)) {
$load = $line;
break;
}
}
}
} else {
if (is_readable("/proc/stat")) {
// Collect 2 samples - each with 1 second period
// See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen
$statData1 = self::_getServerLoadLinuxData();
sleep(1);
$statData2 = self::_getServerLoadLinuxData();
if
(
(!is_null($statData1)) &&
(!is_null($statData2))
) {
// Get difference
$statData2[0] -= $statData1[0];
$statData2[1] -= $statData1[1];
$statData2[2] -= $statData1[2];
$statData2[3] -= $statData1[3];
// Sum up the 4 values for User, Nice, System and Idle and calculate
// the percentage of idle time (which is part of the 4 values!)
$cpuTime = $statData2[0] + $statData2[1] + $statData2[2] + $statData2[3];
// Invert percentage to get CPU time, not idle time
$load = 100 - ($statData2[3] * 100 / $cpuTime);
}
}
}
return $load;
}
}