-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIO.php
101 lines (93 loc) · 4.01 KB
/
AIO.php
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
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: text/html; charset=utf-8');
function getNetworkStatistics() {
$netDevFile = '/proc/net/dev';
$netDevContents = file_get_contents($netDevFile);
$lines = explode("\n", $netDevContents);
$networkData = [];
foreach ($lines as $line) {
if (strpos($line, ':') !== false) {
$parts = preg_split('/\s+/', trim($line));
$interface = rtrim($parts[0], ':');
$receivedBytes = $parts[1];
$transmittedBytes = $parts[9];
$networkData[$interface] = [
'receivedBytes' => $receivedBytes,
'transmittedBytes' => $transmittedBytes
];
}
}
return $networkData;
}
function getCurrentNetworkSpeed() {
$interfaceName = 'eth0';
$previousStatsFile = '/tmp/previous_stats.txt';
$networkStatistics = getNetworkStatistics();
if (isset($networkStatistics[$interfaceName])) {
$currentStats = $networkStatistics[$interfaceName];
$previousStats = [];
if (file_exists($previousStatsFile)) {
$previousStats = unserialize(file_get_contents($previousStatsFile));
}
$receivedDiff = $currentStats['receivedBytes'] - $previousStats['receivedBytes'];
$transmittedDiff = $currentStats['transmittedBytes'] - $previousStats['transmittedBytes'];
file_put_contents($previousStatsFile, serialize($currentStats));
$requestInterval = 1;
$receivedSpeed = $receivedDiff / $requestInterval;
$transmittedSpeed = $transmittedDiff / $requestInterval;
return [
'receivedSpeed' => $receivedSpeed,
'transmittedSpeed' => $transmittedSpeed
];
} else {
return false;
}
}
function f($d, $n){
$string = exec("grep '".$d."' /proc/meminfo");
$numb = explode(":", $string);
$outb = explode(" ", $numb[1]);
return $outb[$n];
}
function getCpuUtilization() {
$statFile = '/proc/stat';
$cpuDataFile = '/tmp/cpu_data.txt';
$statContents = file_get_contents($statFile);
$lines = explode("\n", $statContents);
$cpuLine = '';
foreach ($lines as $line) {
if (strpos($line, 'cpu ') === 0) {
$cpuLine = $line;
break;
}
}
$cpuData = sscanf($cpuLine, 'cpu %d %d %d %d %d %d %d %d %d %d');
$totalCpuTime = array_sum($cpuData);
$previousCpuData = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
if (file_exists($cpuDataFile)) {
$previousCpuData = file($cpuDataFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
$cpuTimeDiff = array_map(function ($current, $previous) {
return $current - $previous;
}, $cpuData, $previousCpuData);
$totalCpuTimeDiff = array_sum($cpuTimeDiff);
$idleCpuTimeDiff = $cpuTimeDiff[3];
$cpuUtilization = 100 - (($idleCpuTimeDiff / $totalCpuTimeDiff) * 100);
file_put_contents($cpuDataFile, implode(PHP_EOL, $cpuData));
return $cpuUtilization;
}
$networkSpeeds = getCurrentNetworkSpeed();
$stats["netspd"]["in"] = $networkSpeeds['receivedSpeed'];
$stats["netspd"]["out"] = $networkSpeeds['transmittedSpeed'];
$stats["time"] = microtime(true);
$stats["temp"] = exec("cat /sys/class/thermal/thermal_zone*/temp") / 1000;
// $stats["util"] = exec("mpstat 0,5 1 | awk -F \" \" '{print (100 - $12)}'");
$stats["util"] = getCpuUtilization();
$stats["memo"]["total"] = f("MemTotal", 8);
$stats["memo"]["avail"] = f("MemAvailable", 4);
$stats["uptime"] = strtotime(exec('uptime -s'));
echo json_encode($stats);
?>