1
|
<?
|
2
|
|
3
|
if(Connection_Aborted()) {
|
4
|
exit;
|
5
|
}
|
6
|
|
7
|
require_once("config.inc");
|
8
|
|
9
|
function get_stats() {
|
10
|
$stats['cpu'] = cpu_usage();
|
11
|
$stats['mem'] = mem_usage();
|
12
|
$stats['uptime'] = get_uptime();
|
13
|
$stats['states'] = get_pfstate();
|
14
|
$stats['temp'] = get_temp();
|
15
|
|
16
|
$stats = join("|", $stats);
|
17
|
|
18
|
return $stats;
|
19
|
}
|
20
|
|
21
|
|
22
|
function get_uptime() {
|
23
|
$boottime = "";
|
24
|
$matches = "";
|
25
|
exec("/sbin/sysctl -n kern.boottime", $boottime);
|
26
|
preg_match("/sec = (\d+)/", $boottime[0], $matches);
|
27
|
$boottime = $matches[1];
|
28
|
$uptime = time() - $boottime;
|
29
|
|
30
|
if ($uptime > 60)
|
31
|
$uptime += 30;
|
32
|
$updays = (int)($uptime / 86400);
|
33
|
$uptime %= 86400;
|
34
|
$uphours = (int)($uptime / 3600);
|
35
|
$uptime %= 3600;
|
36
|
$upmins = (int)($uptime / 60);
|
37
|
|
38
|
$uptimestr = "";
|
39
|
if ($updays > 1)
|
40
|
$uptimestr .= "$updays days, ";
|
41
|
else if ($updays > 0)
|
42
|
$uptimestr .= "1 day, ";
|
43
|
$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
|
44
|
return $uptimestr;
|
45
|
}
|
46
|
|
47
|
function cpu_usage() {
|
48
|
$cpuTicks = explode(" ", `/sbin/sysctl -n kern.cp_time`);
|
49
|
sleep(1);
|
50
|
$cpuTicks2 = explode(" ", `/sbin/sysctl -n kern.cp_time`);
|
51
|
|
52
|
$diff = array();
|
53
|
$diff['user'] = ($cpuTicks2[0] - $cpuTicks[0]);
|
54
|
$diff['nice'] = ($cpuTicks2[1] - $cpuTicks[1]);
|
55
|
$diff['sys'] = ($cpuTicks2[2] - $cpuTicks[2]);
|
56
|
$diff['intr'] = ($cpuTicks2[3] - $cpuTicks[3]);
|
57
|
$diff['idle'] = ($cpuTicks2[4] - $cpuTicks[4]);
|
58
|
|
59
|
//echo "<!-- user: {$diff['user']} nice {$diff['nice']} sys {$diff['sys']} intr {$diff['intr']} idle {$diff['idle']} -->";
|
60
|
$totalDiff = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'] + $diff['idle'];
|
61
|
$totalused = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'];
|
62
|
if (isset($totalused)&&$totalused <= 0) {
|
63
|
$totalused = 0.001;
|
64
|
}
|
65
|
$cpuUsage = floor(100 * ($totalused / $totalDiff));
|
66
|
|
67
|
return $cpuUsage;
|
68
|
}
|
69
|
|
70
|
function get_pfstate() {
|
71
|
global $config;
|
72
|
$matches = "";
|
73
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
|
74
|
$maxstates="/{$config['system']['maximumstates']}";
|
75
|
else
|
76
|
$maxstates="/10000";
|
77
|
$curentries = `/sbin/pfctl -si |grep current`;
|
78
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
79
|
$curentries = $matches[1];
|
80
|
}
|
81
|
return $curentries . $maxstates;
|
82
|
}
|
83
|
|
84
|
function has_temp() {
|
85
|
if(`/sbin/dmesg -a | /usr/bin/grep net4801` <> "") {
|
86
|
/* Initialize hw monitor */
|
87
|
exec("/usr/local/sbin/env4801 -i");
|
88
|
return true;
|
89
|
}
|
90
|
|
91
|
/* should only reach here if there is no hardware monitor */
|
92
|
return false;
|
93
|
}
|
94
|
|
95
|
function get_hwtype() {
|
96
|
if(`/sbin/dmesg -a | /usr/bin/grep net4801` <> "") {
|
97
|
return "4801";
|
98
|
}
|
99
|
|
100
|
return;
|
101
|
}
|
102
|
|
103
|
function get_temp() {
|
104
|
switch(get_hwtype()) {
|
105
|
case '4801':
|
106
|
$ret = rtrim(`/usr/local/sbin/env4801 | /usr/bin/grep Temp |/usr/bin/cut -c24-25`);
|
107
|
break;
|
108
|
default:
|
109
|
return;
|
110
|
}
|
111
|
|
112
|
return $ret;
|
113
|
}
|
114
|
|
115
|
function disk_usage()
|
116
|
{
|
117
|
$dfout = "";
|
118
|
exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
119
|
$diskusage = trim($dfout[0]);
|
120
|
|
121
|
return $diskusage;
|
122
|
}
|
123
|
|
124
|
function swap_usage()
|
125
|
{
|
126
|
$swapUsage = `/usr/sbin/swapinfo | /usr/bin/cut -c45-55 | /usr/bin/grep "%"`;
|
127
|
$swapUsage = ereg_replace('%', "", $swapUsage);
|
128
|
$swapUsage = ereg_replace(' ', "", $swapUsage);
|
129
|
$swapUsage = rtrim($swapUsage);
|
130
|
|
131
|
return $swapUsage;
|
132
|
}
|
133
|
|
134
|
function mem_usage()
|
135
|
{
|
136
|
$memory = "";
|
137
|
exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
|
138
|
"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
|
139
|
|
140
|
$totalMem = $memory[0];
|
141
|
$availMem = $memory[1] + $memory[2] + $memory[3];
|
142
|
$usedMem = $totalMem - $availMem;
|
143
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
144
|
|
145
|
return $memUsage;
|
146
|
}
|
147
|
|