1 |
d772ac32
|
Erik Kristensen
|
<?
|
2 |
7ac5a4cb
|
Scott Ullrich
|
/*
|
3 |
|
|
pfSense_MODULE: ajax
|
4 |
|
|
*/
|
5 |
d6ad908c
|
Scott Ullrich
|
|
6 |
|
|
if(Connection_Aborted()) {
|
7 |
|
|
exit;
|
8 |
|
|
}
|
9 |
d772ac32
|
Erik Kristensen
|
|
10 |
2fc0b1c7
|
Scott Ullrich
|
require_once("config.inc");
|
11 |
65d4de2e
|
Scott Dale
|
|
12 |
2fc0b1c7
|
Scott Ullrich
|
function get_stats() {
|
13 |
|
|
$stats['cpu'] = cpu_usage();
|
14 |
|
|
$stats['mem'] = mem_usage();
|
15 |
|
|
$stats['uptime'] = get_uptime();
|
16 |
|
|
$stats['states'] = get_pfstate();
|
17 |
|
|
$stats['temp'] = get_temp();
|
18 |
65d4de2e
|
Scott Dale
|
$stats['datetime'] = update_date_time();
|
19 |
|
|
$stats['interfacestatistics'] = get_interfacestats();
|
20 |
|
|
$stats['interfacestatus'] = get_interfacestatus();
|
21 |
3dfa6a52
|
Scott Ullrich
|
$stats['gateways'] = get_gatewaystats();
|
22 |
4dedd18a
|
Cristian Feldman
|
$stats['cpufreq'] = get_cpufreq();
|
23 |
25a46a3c
|
Cristian Feldman
|
$stats['load_average'] = get_load_average();
|
24 |
8ff9cc38
|
jim-p
|
$stats['mbuf'] = get_mbuf();
|
25 |
4a83831c
|
jim-p
|
$stats['statepercent'] = get_pfstate(true);
|
26 |
2fc0b1c7
|
Scott Ullrich
|
$stats = join("|", $stats);
|
27 |
|
|
return $stats;
|
28 |
|
|
}
|
29 |
477361c7
|
Erik Kristensen
|
|
30 |
3dfa6a52
|
Scott Ullrich
|
function get_gatewaystats() {
|
31 |
68f291ff
|
Ermal
|
$a_gateways = return_gateways_array();
|
32 |
3dfa6a52
|
Scott Ullrich
|
$gateways_status = array();
|
33 |
68f291ff
|
Ermal
|
$gateways_status = return_gateways_status(true);
|
34 |
3dfa6a52
|
Scott Ullrich
|
$data = "";
|
35 |
|
|
$isfirst = true;
|
36 |
68f291ff
|
Ermal
|
foreach($a_gateways as $gname => $gw) {
|
37 |
af0b57cb
|
Renato Botelho
|
if(!$isfirst)
|
38 |
3dfa6a52
|
Scott Ullrich
|
$data .= ",";
|
39 |
|
|
$isfirst = false;
|
40 |
|
|
$data .= $gw['name'] . ",";
|
41 |
68f291ff
|
Ermal
|
if ($gateways_status[$gname]) {
|
42 |
603f19f0
|
smos
|
$data .= lookup_gateway_ip_by_name($gname) . ",";
|
43 |
68f291ff
|
Ermal
|
$gws = $gateways_status[$gname];
|
44 |
5af7eba3
|
jim-p
|
switch(strtolower($gws['status'])) {
|
45 |
a87c7901
|
jim-p
|
case "none":
|
46 |
3dfa6a52
|
Scott Ullrich
|
$online = "Online";
|
47 |
a0c0e8ae
|
Colin Fleming
|
$bgcolor = "#90EE90"; // lightgreen
|
48 |
3dfa6a52
|
Scott Ullrich
|
break;
|
49 |
a87c7901
|
jim-p
|
case "down":
|
50 |
3dfa6a52
|
Scott Ullrich
|
$online = "Offline";
|
51 |
a0c0e8ae
|
Colin Fleming
|
$bgcolor = "#F08080"; // lightcoral
|
52 |
3dfa6a52
|
Scott Ullrich
|
break;
|
53 |
a87c7901
|
jim-p
|
case "delay":
|
54 |
603f19f0
|
smos
|
$online = "Latency";
|
55 |
a0c0e8ae
|
Colin Fleming
|
$bgcolor = "#F0E68C"; // khaki
|
56 |
3dfa6a52
|
Scott Ullrich
|
break;
|
57 |
a87c7901
|
jim-p
|
case "loss":
|
58 |
603f19f0
|
smos
|
$online = "Packetloss";
|
59 |
a0c0e8ae
|
Colin Fleming
|
$bgcolor = "#F0E68C"; // khaki
|
60 |
3dfa6a52
|
Scott Ullrich
|
break;
|
61 |
|
|
default:
|
62 |
603f19f0
|
smos
|
$online = "Pending";
|
63 |
68f291ff
|
Ermal
|
break;
|
64 |
|
|
}
|
65 |
|
|
} else {
|
66 |
603f19f0
|
smos
|
$data .= "~,";
|
67 |
|
|
$gws['delay'] = "~";
|
68 |
|
|
$gws['loss'] = "~";
|
69 |
|
|
$online = "Unknown";
|
70 |
a0c0e8ae
|
Colin Fleming
|
$bgcolor = "#ADD8E6"; // lightblue
|
71 |
3dfa6a52
|
Scott Ullrich
|
}
|
72 |
603f19f0
|
smos
|
$data .= ($online == "Pending") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
|
73 |
a0c0e8ae
|
Colin Fleming
|
$data .= "<table><tr><td bgcolor=\"$bgcolor\"> $online </td></td></tr></table>";
|
74 |
3dfa6a52
|
Scott Ullrich
|
}
|
75 |
|
|
return $data;
|
76 |
|
|
}
|
77 |
477361c7
|
Erik Kristensen
|
|
78 |
d772ac32
|
Erik Kristensen
|
function get_uptime() {
|
79 |
767a716e
|
Scott Ullrich
|
$boottime = "";
|
80 |
|
|
$matches = "";
|
81 |
d772ac32
|
Erik Kristensen
|
exec("/sbin/sysctl -n kern.boottime", $boottime);
|
82 |
|
|
preg_match("/sec = (\d+)/", $boottime[0], $matches);
|
83 |
|
|
$boottime = $matches[1];
|
84 |
|
|
$uptime = time() - $boottime;
|
85 |
|
|
|
86 |
af0b57cb
|
Renato Botelho
|
if(intval($boottime) == 0)
|
87 |
c7b8b46a
|
Scott Ullrich
|
return;
|
88 |
af0b57cb
|
Renato Botelho
|
if(intval($uptime) == 0)
|
89 |
c7b8b46a
|
Scott Ullrich
|
return;
|
90 |
|
|
|
91 |
d772ac32
|
Erik Kristensen
|
$updays = (int)($uptime / 86400);
|
92 |
|
|
$uptime %= 86400;
|
93 |
|
|
$uphours = (int)($uptime / 3600);
|
94 |
|
|
$uptime %= 3600;
|
95 |
|
|
$upmins = (int)($uptime / 60);
|
96 |
af0b57cb
|
Renato Botelho
|
$uptime %= 60;
|
97 |
54f818b1
|
Joecowboy
|
$upsecs = (int)($uptime);
|
98 |
d772ac32
|
Erik Kristensen
|
|
99 |
|
|
$uptimestr = "";
|
100 |
|
|
if ($updays > 1)
|
101 |
54f818b1
|
Joecowboy
|
$uptimestr .= "$updays Days ";
|
102 |
d772ac32
|
Erik Kristensen
|
else if ($updays > 0)
|
103 |
54f818b1
|
Joecowboy
|
$uptimestr .= "1 Day ";
|
104 |
|
|
|
105 |
af0b57cb
|
Renato Botelho
|
if ($uphours > 1)
|
106 |
|
|
$hours = "s";
|
107 |
|
|
|
108 |
|
|
if ($upmins > 1)
|
109 |
|
|
$minutes = "s";
|
110 |
|
|
|
111 |
|
|
if ($upmins > 1)
|
112 |
|
|
$seconds = "s";
|
113 |
|
|
|
114 |
b10268ca
|
Joecowboy
|
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
|
115 |
d772ac32
|
Erik Kristensen
|
return $uptimestr;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
e637075a
|
Bill Marquette
|
/* Calculates non-idle CPU time and returns as a percentage */
|
119 |
4fe00253
|
Espen Johansen
|
function cpu_usage() {
|
120 |
4a1cd18f
|
Bill Marquette
|
$duration = 1;
|
121 |
|
|
$diff = array('user', 'nice', 'sys', 'intr', 'idle');
|
122 |
|
|
$cpuTicks = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
|
123 |
|
|
sleep($duration);
|
124 |
|
|
$cpuTicks2 = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
|
125 |
af0b57cb
|
Renato Botelho
|
|
126 |
4a1cd18f
|
Bill Marquette
|
$totalStart = array_sum($cpuTicks);
|
127 |
|
|
$totalEnd = array_sum($cpuTicks2);
|
128 |
|
|
|
129 |
|
|
// Something wrapped ?!?!
|
130 |
|
|
if ($totalEnd <= $totalStart)
|
131 |
|
|
return 0;
|
132 |
|
|
|
133 |
|
|
// Calculate total cycles used
|
134 |
|
|
$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
|
135 |
65d4de2e
|
Scott Dale
|
|
136 |
4a1cd18f
|
Bill Marquette
|
// Calculate the percentage used
|
137 |
|
|
$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
|
138 |
af0b57cb
|
Renato Botelho
|
|
139 |
d772ac32
|
Erik Kristensen
|
return $cpuUsage;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
4a83831c
|
jim-p
|
function get_pfstate($percent=false) {
|
143 |
d772ac32
|
Erik Kristensen
|
global $config;
|
144 |
767a716e
|
Scott Ullrich
|
$matches = "";
|
145 |
|
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
|
146 |
af0b57cb
|
Renato Botelho
|
$maxstates="{$config['system']['maximumstates']}";
|
147 |
767a716e
|
Scott Ullrich
|
else
|
148 |
af0b57cb
|
Renato Botelho
|
$maxstates=pfsense_default_state_size();
|
149 |
767a716e
|
Scott Ullrich
|
$curentries = `/sbin/pfctl -si |grep current`;
|
150 |
|
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
151 |
af0b57cb
|
Renato Botelho
|
$curentries = $matches[1];
|
152 |
767a716e
|
Scott Ullrich
|
}
|
153 |
4a83831c
|
jim-p
|
if ($percent)
|
154 |
|
|
return ($curentries / $maxstates) * 100;
|
155 |
|
|
else
|
156 |
|
|
return $curentries . "/" . $maxstates;
|
157 |
d772ac32
|
Erik Kristensen
|
}
|
158 |
|
|
|
159 |
7979a809
|
Bill Marquette
|
function has_temp() {
|
160 |
19604f5d
|
Scott Ullrich
|
/* no known temp monitors available at present */
|
161 |
af0b57cb
|
Renato Botelho
|
|
162 |
7979a809
|
Bill Marquette
|
/* should only reach here if there is no hardware monitor */
|
163 |
|
|
return false;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
function get_hwtype() {
|
167 |
|
|
return;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
8ff9cc38
|
jim-p
|
function get_mbuf() {
|
171 |
2c7f71d9
|
jim-p
|
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
|
172 |
8ff9cc38
|
jim-p
|
list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
|
173 |
|
|
$mbufusage = round(($mbufs_total / $mbufs_max) * 100);
|
174 |
2c7f71d9
|
jim-p
|
return $mbufusage;
|
175 |
8ff9cc38
|
jim-p
|
}
|
176 |
|
|
|
177 |
7979a809
|
Bill Marquette
|
function get_temp() {
|
178 |
af0b57cb
|
Renato Botelho
|
$temp_out = "";
|
179 |
|
|
exec("/sbin/sysctl dev.cpu.0.temperature | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d 'C' -f 1", $dfout);
|
180 |
|
|
$temp_out = trim($dfout[0]);
|
181 |
|
|
if ($temp_out == "") {
|
182 |
|
|
exec("/sbin/sysctl hw.acpi.thermal.tz0.temperature | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d 'C' -f 1", $dfout);
|
183 |
|
|
$temp_out = trim($dfout[0]);
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
return $temp_out;
|
187 |
1804023d
|
Bill Marquette
|
}
|
188 |
d772ac32
|
Erik Kristensen
|
|
189 |
af0b57cb
|
Renato Botelho
|
function disk_usage() {
|
190 |
767a716e
|
Scott Ullrich
|
$dfout = "";
|
191 |
7e1b16ca
|
Bill Marquette
|
exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
192 |
d772ac32
|
Erik Kristensen
|
$diskusage = trim($dfout[0]);
|
193 |
|
|
|
194 |
|
|
return $diskusage;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
af0b57cb
|
Renato Botelho
|
function swap_usage() {
|
198 |
dfe2769c
|
Renato Botelho
|
exec("/usr/sbin/swapinfo", $swap_info);
|
199 |
bc488151
|
Renato Botelho
|
$swap_used = "";
|
200 |
dfe2769c
|
Renato Botelho
|
foreach ($swap_info as $line)
|
201 |
bc488151
|
Renato Botelho
|
if (preg_match('/(\d+)%$/', $line, $matches)) {
|
202 |
|
|
$swap_used = $matches[1];
|
203 |
|
|
break;
|
204 |
|
|
}
|
205 |
d772ac32
|
Erik Kristensen
|
|
206 |
bc488151
|
Renato Botelho
|
return $swap_used;
|
207 |
d772ac32
|
Erik Kristensen
|
}
|
208 |
|
|
|
209 |
880637d2
|
Scott Ullrich
|
function mem_usage() {
|
210 |
767a716e
|
Scott Ullrich
|
$memory = "";
|
211 |
60928664
|
Scott Ullrich
|
exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
|
212 |
|
|
"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
|
213 |
af0b57cb
|
Renato Botelho
|
|
214 |
60928664
|
Scott Ullrich
|
$totalMem = $memory[0];
|
215 |
|
|
$availMem = $memory[1] + $memory[2] + $memory[3];
|
216 |
|
|
$usedMem = $totalMem - $availMem;
|
217 |
d772ac32
|
Erik Kristensen
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
218 |
|
|
|
219 |
|
|
return $memUsage;
|
220 |
7e1b16ca
|
Bill Marquette
|
}
|
221 |
60928664
|
Scott Ullrich
|
|
222 |
65d4de2e
|
Scott Dale
|
function update_date_time() {
|
223 |
|
|
$datetime = date("D M j G:i:s T Y");
|
224 |
|
|
return $datetime;
|
225 |
|
|
}
|
226 |
|
|
|
227 |
4dedd18a
|
Cristian Feldman
|
function get_cpufreq() {
|
228 |
|
|
$cpufreqs = "";
|
229 |
|
|
$out = "";
|
230 |
|
|
exec("/sbin/sysctl -n dev.cpu.0.freq_levels", $cpufreqs);
|
231 |
|
|
$cpufreqs = explode(" ", trim($cpufreqs[0]));
|
232 |
|
|
$maxfreq = explode("/", $cpufreqs[0]);
|
233 |
|
|
$maxfreq = $maxfreq[0];
|
234 |
|
|
$curfreq = "";
|
235 |
|
|
exec("/sbin/sysctl -n dev.cpu.0.freq", $curfreq);
|
236 |
|
|
$curfreq = trim($curfreq[0]);
|
237 |
f5c47a7c
|
jim-p
|
if (($curfreq > 0) && ($curfreq != $maxfreq))
|
238 |
4dedd18a
|
Cristian Feldman
|
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
|
239 |
|
|
return $out;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
25a46a3c
|
Cristian Feldman
|
function get_load_average() {
|
243 |
|
|
$load_average = "";
|
244 |
|
|
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
|
245 |
|
|
return $load_average[0];
|
246 |
|
|
}
|
247 |
|
|
|
248 |
880637d2
|
Scott Ullrich
|
function get_interfacestats() {
|
249 |
65d4de2e
|
Scott Dale
|
global $config;
|
250 |
|
|
//build interface list for widget use
|
251 |
97e9b109
|
Chris Buechler
|
$ifdescrs = get_configured_interface_list();
|
252 |
3e321df2
|
Ermal Luçi
|
|
253 |
65d4de2e
|
Scott Dale
|
$array_in_packets = array();
|
254 |
|
|
$array_out_packets = array();
|
255 |
|
|
$array_in_bytes = array();
|
256 |
|
|
$array_out_bytes = array();
|
257 |
|
|
$array_in_errors = array();
|
258 |
|
|
$array_out_errors = array();
|
259 |
|
|
$array_collisions = array();
|
260 |
|
|
$array_interrupt = array();
|
261 |
|
|
$new_data = "";
|
262 |
af0b57cb
|
Renato Botelho
|
|
263 |
65d4de2e
|
Scott Dale
|
//build data arrays
|
264 |
|
|
foreach ($ifdescrs as $ifdescr => $ifname){
|
265 |
af0b57cb
|
Renato Botelho
|
$ifinfo = get_interface_info($ifdescr);
|
266 |
65d4de2e
|
Scott Dale
|
$new_data .= "{$ifinfo['inpkts']},";
|
267 |
|
|
$new_data .= "{$ifinfo['outpkts']},";
|
268 |
|
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
269 |
|
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
270 |
|
|
if (isset($ifinfo['inerrs'])){
|
271 |
|
|
$new_data .= "{$ifinfo['inerrs']},";
|
272 |
|
|
$new_data .= "{$ifinfo['outerrs']},";
|
273 |
|
|
}
|
274 |
|
|
else{
|
275 |
|
|
$new_data .= "0,";
|
276 |
|
|
$new_data .= "0,";
|
277 |
|
|
}
|
278 |
|
|
if (isset($ifinfo['collisions']))
|
279 |
|
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
280 |
|
|
else
|
281 |
|
|
$new_data .= "0,";
|
282 |
|
|
}//end for
|
283 |
|
|
|
284 |
af0b57cb
|
Renato Botelho
|
return $new_data;
|
285 |
65d4de2e
|
Scott Dale
|
}
|
286 |
|
|
|
287 |
880637d2
|
Scott Ullrich
|
function get_interfacestatus() {
|
288 |
65d4de2e
|
Scott Dale
|
$data = "";
|
289 |
|
|
global $config;
|
290 |
|
|
|
291 |
|
|
//build interface list for widget use
|
292 |
3e321df2
|
Ermal Luçi
|
$ifdescrs = get_configured_interface_with_descr();
|
293 |
af0b57cb
|
Renato Botelho
|
|
294 |
65d4de2e
|
Scott Dale
|
foreach ($ifdescrs as $ifdescr => $ifname){
|
295 |
|
|
$ifinfo = get_interface_info($ifdescr);
|
296 |
|
|
$data .= $ifname . ",";
|
297 |
|
|
if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
298 |
|
|
$data .= "up";
|
299 |
|
|
}else if ($ifinfo['status'] == "no carrier") {
|
300 |
|
|
$data .= "down";
|
301 |
|
|
}else if ($ifinfo['status'] == "down") {
|
302 |
|
|
$data .= "block";
|
303 |
|
|
}
|
304 |
|
|
$data .= ",";
|
305 |
030f0cb7
|
Scott Ullrich
|
if ($ifinfo['ipaddr'])
|
306 |
65d4de2e
|
Scott Dale
|
$data .= htmlspecialchars($ifinfo['ipaddr']);
|
307 |
|
|
$data .= ",";
|
308 |
|
|
if ($ifinfo['status'] != "down")
|
309 |
|
|
$data .= htmlspecialchars($ifinfo['media']);
|
310 |
af0b57cb
|
Renato Botelho
|
|
311 |
65d4de2e
|
Scott Dale
|
$data .= "~";
|
312 |
af0b57cb
|
Renato Botelho
|
|
313 |
|
|
}
|
314 |
65d4de2e
|
Scott Dale
|
return $data;
|
315 |
|
|
}
|
316 |
|
|
|
317 |
af0b57cb
|
Renato Botelho
|
?>
|