1
|
<?
|
2
|
|
3
|
if(Connection_Aborted()) {
|
4
|
exit;
|
5
|
}
|
6
|
|
7
|
require_once("config.inc");
|
8
|
|
9
|
|
10
|
function get_stats() {
|
11
|
$stats['cpu'] = cpu_usage();
|
12
|
$stats['mem'] = mem_usage();
|
13
|
$stats['uptime'] = get_uptime();
|
14
|
$stats['states'] = get_pfstate();
|
15
|
$stats['temp'] = get_temp();
|
16
|
$stats['datetime'] = update_date_time();
|
17
|
$stats['interfacestatistics'] = get_interfacestats();
|
18
|
$stats['interfacestatus'] = get_interfacestatus();
|
19
|
|
20
|
$stats = join("|", $stats);
|
21
|
|
22
|
return $stats;
|
23
|
}
|
24
|
|
25
|
|
26
|
function get_uptime() {
|
27
|
$boottime = "";
|
28
|
$matches = "";
|
29
|
exec("/sbin/sysctl -n kern.boottime", $boottime);
|
30
|
preg_match("/sec = (\d+)/", $boottime[0], $matches);
|
31
|
$boottime = $matches[1];
|
32
|
$uptime = time() - $boottime;
|
33
|
|
34
|
if(intval($boottime) == 0)
|
35
|
return;
|
36
|
if(intval($uptime) == 0)
|
37
|
return;
|
38
|
|
39
|
$updays = (int)($uptime / 86400);
|
40
|
$uptime %= 86400;
|
41
|
$uphours = (int)($uptime / 3600);
|
42
|
$uptime %= 3600;
|
43
|
$upmins = (int)($uptime / 60);
|
44
|
|
45
|
$uptimestr = "";
|
46
|
if ($updays > 1)
|
47
|
$uptimestr .= "$updays days, ";
|
48
|
else if ($updays > 0)
|
49
|
$uptimestr .= "1 day, ";
|
50
|
$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
|
51
|
return $uptimestr;
|
52
|
}
|
53
|
|
54
|
function cpu_usage() {
|
55
|
exec("sysctl -n kern.cp_time && sleep 1 && sysctl -n kern.cp_time",$cpuTicksEx);
|
56
|
|
57
|
$cpuTicks = explode(" ", $cpuTicksEx[0]);
|
58
|
$cpuTicks2 = explode(" ", $cpuTicksEx[1]);
|
59
|
|
60
|
$diff = array();
|
61
|
$diff['user'] = ($cpuTicks2[0] - $cpuTicks[0]);
|
62
|
$diff['nice'] = ($cpuTicks2[1] - $cpuTicks[1]);
|
63
|
$diff['sys'] = ($cpuTicks2[2] - $cpuTicks[2]);
|
64
|
$diff['intr'] = ($cpuTicks2[3] - $cpuTicks[3]);
|
65
|
$diff['idle'] = ($cpuTicks2[4] - $cpuTicks[4]);
|
66
|
|
67
|
//echo "<!-- user: {$diff['user']} nice {$diff['nice']} sys {$diff['sys']} intr {$diff['intr']} idle {$diff['idle']} -->";
|
68
|
$totalDiff = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'] + $diff['idle'];
|
69
|
$totalused = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'];
|
70
|
if (isset($totalused)&&$totalused <= 0) {
|
71
|
$totalused = 0.001;
|
72
|
}
|
73
|
$cpuUsage = floor(100 * ($totalused / $totalDiff));
|
74
|
return $cpuUsage;
|
75
|
}
|
76
|
|
77
|
function get_pfstate() {
|
78
|
global $config;
|
79
|
$matches = "";
|
80
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
|
81
|
$maxstates="{$config['system']['maximumstates']}";
|
82
|
else
|
83
|
$maxstates=pfsense_default_state_size();
|
84
|
$curentries = `/sbin/pfctl -si |grep current`;
|
85
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
86
|
$curentries = $matches[1];
|
87
|
}
|
88
|
return $curentries . "/" . $maxstates;
|
89
|
}
|
90
|
|
91
|
function has_temp() {
|
92
|
|
93
|
/* no known temp monitors available at present */
|
94
|
|
95
|
/* should only reach here if there is no hardware monitor */
|
96
|
return false;
|
97
|
}
|
98
|
|
99
|
function get_hwtype() {
|
100
|
|
101
|
return;
|
102
|
}
|
103
|
|
104
|
function get_temp() {
|
105
|
switch(get_hwtype()) {
|
106
|
default:
|
107
|
return;
|
108
|
}
|
109
|
|
110
|
return $ret;
|
111
|
}
|
112
|
|
113
|
function disk_usage()
|
114
|
{
|
115
|
$dfout = "";
|
116
|
exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
117
|
$diskusage = trim($dfout[0]);
|
118
|
|
119
|
return $diskusage;
|
120
|
}
|
121
|
|
122
|
function swap_usage()
|
123
|
{
|
124
|
$swapUsage = `/usr/sbin/swapinfo | /usr/bin/awk '{print $5;'}|/usr/bin/grep '%'`;
|
125
|
$swapUsage = ereg_replace('%', "", $swapUsage);
|
126
|
$swapUsage = rtrim($swapUsage);
|
127
|
|
128
|
return $swapUsage;
|
129
|
}
|
130
|
|
131
|
function mem_usage()
|
132
|
{
|
133
|
$memory = "";
|
134
|
exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
|
135
|
"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
|
136
|
|
137
|
$totalMem = $memory[0];
|
138
|
$availMem = $memory[1] + $memory[2] + $memory[3];
|
139
|
$usedMem = $totalMem - $availMem;
|
140
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
141
|
|
142
|
return $memUsage;
|
143
|
}
|
144
|
|
145
|
function update_date_time() {
|
146
|
|
147
|
$datetime = date("D M j G:i:s T Y");
|
148
|
return $datetime;
|
149
|
}
|
150
|
|
151
|
function get_interfacestats(){
|
152
|
|
153
|
global $config;
|
154
|
//build interface list for widget use
|
155
|
$ifdescrs = get_configured_interface_list();
|
156
|
|
157
|
$array_in_packets = array();
|
158
|
$array_out_packets = array();
|
159
|
$array_in_bytes = array();
|
160
|
$array_out_bytes = array();
|
161
|
$array_in_errors = array();
|
162
|
$array_out_errors = array();
|
163
|
$array_collisions = array();
|
164
|
$array_interrupt = array();
|
165
|
$new_data = "";
|
166
|
|
167
|
//build data arrays
|
168
|
foreach ($ifdescrs as $ifdescr => $ifname){
|
169
|
$ifinfo = get_interface_info($ifdescr);
|
170
|
$new_data .= "{$ifinfo['inpkts']},";
|
171
|
$new_data .= "{$ifinfo['outpkts']},";
|
172
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
173
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
174
|
if (isset($ifinfo['inerrs'])){
|
175
|
$new_data .= "{$ifinfo['inerrs']},";
|
176
|
$new_data .= "{$ifinfo['outerrs']},";
|
177
|
}
|
178
|
else{
|
179
|
$new_data .= "0,";
|
180
|
$new_data .= "0,";
|
181
|
}
|
182
|
if (isset($ifinfo['collisions']))
|
183
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
184
|
else
|
185
|
$new_data .= "0,";
|
186
|
|
187
|
|
188
|
|
189
|
}//end for
|
190
|
|
191
|
return $new_data;
|
192
|
|
193
|
}
|
194
|
|
195
|
function get_interfacestatus(){
|
196
|
$data = "";
|
197
|
global $config;
|
198
|
|
199
|
//build interface list for widget use
|
200
|
$ifdescrs = get_configured_interface_with_descr();
|
201
|
|
202
|
foreach ($ifdescrs as $ifdescr => $ifname){
|
203
|
$ifinfo = get_interface_info($ifdescr);
|
204
|
$data .= $ifname . ",";
|
205
|
if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
206
|
$data .= "up";
|
207
|
}else if ($ifinfo['status'] == "no carrier") {
|
208
|
$data .= "down";
|
209
|
}else if ($ifinfo['status'] == "down") {
|
210
|
$data .= "block";
|
211
|
}
|
212
|
$data .= ",";
|
213
|
if ($ifinfo['ipaddr']){
|
214
|
$data .= htmlspecialchars($ifinfo['ipaddr']);
|
215
|
if ($ifinfo['dhcplink']) {
|
216
|
$data .= " (DHCP)";
|
217
|
}
|
218
|
}
|
219
|
$data .= ",";
|
220
|
if ($ifinfo['status'] != "down")
|
221
|
$data .= htmlspecialchars($ifinfo['media']);
|
222
|
|
223
|
$data .= "~";
|
224
|
|
225
|
}
|
226
|
return $data;
|
227
|
}
|
228
|
|
229
|
?>
|