-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjstats.php
More file actions
108 lines (85 loc) · 2.42 KB
/
Copy pathjstats.php
File metadata and controls
108 lines (85 loc) · 2.42 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
<?php
require_once "config.php";
session_start();
if (!isset($_SESSION['login']) || !($_SESSION['login'] === true)) {
header("Location: login.php");
}
// Get system stats
$output = "";
exec("sar -b -n DEV -q -r -u -S 1 $samples | grep Average", $output);
$moutput = array();
// Count number of lines
$lines = count($output);
// Convert output to a matrix
foreach ($output as $line) {
// Clean multiple spaces
$line = preg_replace("!\s+!", " ", $line);
// Explode line
$aline = explode(" ", $line);
// Append to matrix
$moutput[] = $aline;
}
// Init variables
$i = 0;
$metrics = array();
while ($i < $lines) {
// Get interesting metrics
switch ($moutput[$i][1]) {
case "CPU":
$metrics["CPU"] = 100-$moutput[$i+1][7];
$i++;
break;
case "tps":
$metrics["tps"] = (float)$moutput[$i+1][1];
$i++;
break;
case "kbmemfree":
$metrics["memused"] = (float)$moutput[$i+1][3];
$i++;
break;
case "kbswpfree":
$metrics["swpused"] = (float)$moutput[$i+1][3];
$i++;
break;
case "runq-sz":
$metrics["ldavg1"] = (float)$moutput[$i+1][3];
$i++;
break;
case $iface:
// rxkB/s txkB/s
$metrics["ifacerxkB"] = (float)$moutput[$i][4];
$metrics["ifacetxkB"] = (float)$moutput[$i][5];
break;
}
$i++;
}
// Get top processes
$output = "";
exec("top -b -n 1 | grep -A 10 PID", $output);
$index = 0;
foreach ($output as $line) {
// Clean multiple spaces
$line = preg_replace("!\s+!", " ", ltrim($line));
// Explode line
$aline = explode(" ", $line);
// Recover only columns 4, 5, 6, 7, 8, 9 and 11
$metrics["top$index"][0] = $aline[4];
$metrics["top$index"][1] = $aline[5];
$metrics["top$index"][2] = $aline[6];
$metrics["top$index"][3] = $aline[7];
$metrics["top$index"][4] = $aline[8];
$metrics["top$index"][5] = $aline[9];
$metrics["top$index"][6] = $aline[11];
$index++;
}
// Get uptime
$output = "";
exec("LANG=C uptime", $output);
$metrics['uptime'] = $output[0];
// Get ESTABLISHED connections
$output = "";
exec("netstat -tun | grep ESTABLISHED | wc -l", $output);
$metrics['connections'] = $output[0];
// Send JSON response
header('Content-Type: application/json;charset=UTF-8');
echo json_encode($metrics);