1
|
<?php
|
2
|
/*
|
3
|
functions.inc.php
|
4
|
Copyright (C) 2013-2015 Electric Sheep Fencing, LP
|
5
|
*/
|
6
|
|
7
|
if (Connection_Aborted()) {
|
8
|
exit;
|
9
|
}
|
10
|
|
11
|
require_once("config.inc");
|
12
|
require_once("pfsense-utils.inc");
|
13
|
|
14
|
function get_stats() {
|
15
|
$stats['cpu'] = cpu_usage();
|
16
|
$stats['mem'] = mem_usage();
|
17
|
$stats['uptime'] = get_uptime();
|
18
|
$stats['states'] = get_pfstate();
|
19
|
$stats['temp'] = get_temp();
|
20
|
$stats['datetime'] = update_date_time();
|
21
|
$stats['interfacestatistics'] = get_interfacestats();
|
22
|
$stats['interfacestatus'] = get_interfacestatus();
|
23
|
$stats['gateways'] = get_gatewaystats();
|
24
|
$stats['cpufreq'] = get_cpufreq();
|
25
|
$stats['load_average'] = get_load_average();
|
26
|
$stats['mbuf'] = get_mbuf();
|
27
|
$stats['mbufpercent'] = get_mbuf(true);
|
28
|
$stats['statepercent'] = get_pfstate(true);
|
29
|
$stats = join("|", $stats);
|
30
|
return $stats;
|
31
|
}
|
32
|
|
33
|
function get_gatewaystats() {
|
34
|
global $config;
|
35
|
if (isset($config["widgets"]["gateways_widget"]["display_type"])) {
|
36
|
$display_type = $config["widgets"]["gateways_widget"]["display_type"];
|
37
|
} else {
|
38
|
$display_type = "gw_ip";
|
39
|
}
|
40
|
|
41
|
$a_gateways = return_gateways_array();
|
42
|
$gateways_status = array();
|
43
|
$gateways_status = return_gateways_status(true);
|
44
|
$data = "";
|
45
|
$isfirst = true;
|
46
|
foreach ($a_gateways as $gname => $gw) {
|
47
|
if (!$isfirst) {
|
48
|
$data .= ",";
|
49
|
}
|
50
|
$isfirst = false;
|
51
|
$data .= $gw['name'] . ",";
|
52
|
|
53
|
$monitor_address = "";
|
54
|
$monitor_address_disp = "";
|
55
|
if ($display_type == "monitor_ip" || $display_type == "both_ip") {
|
56
|
$monitor_address = $gw['monitor'];
|
57
|
if ($monitor_address != "" && $display_type == "both_ip") {
|
58
|
$monitor_address_disp = " (" . $monitor_address . ")";
|
59
|
} else {
|
60
|
$monitor_address_disp = $monitor_address;
|
61
|
}
|
62
|
}
|
63
|
|
64
|
if ($gateways_status[$gname]) {
|
65
|
if ($display_type == "gw_ip" || $display_type == "both_ip" || ($display_type == "monitor_ip" && $monitor_address == "")) {
|
66
|
$if_gw = lookup_gateway_ip_by_name($gname);
|
67
|
} else {
|
68
|
$if_gw = "";
|
69
|
}
|
70
|
if ($monitor_address == $if_gw) {
|
71
|
$monitor_address_disp = "";
|
72
|
}
|
73
|
|
74
|
$data .= "<b>" . $if_gw . $monitor_address_disp . "</b>,";
|
75
|
$gws = $gateways_status[$gname];
|
76
|
switch (strtolower($gws['status'])) {
|
77
|
case "none":
|
78
|
$online = "Online";
|
79
|
$bgcolor = "#90EE90"; // lightgreen
|
80
|
break;
|
81
|
case "down":
|
82
|
$online = "Offline";
|
83
|
$bgcolor = "#F08080"; // lightcoral
|
84
|
break;
|
85
|
case "delay":
|
86
|
$online = "Latency";
|
87
|
$bgcolor = "#F0E68C"; // khaki
|
88
|
break;
|
89
|
case "loss":
|
90
|
$online = "Packetloss";
|
91
|
$bgcolor = "#F0E68C"; // khaki
|
92
|
break;
|
93
|
default:
|
94
|
$online = "Pending";
|
95
|
break;
|
96
|
}
|
97
|
} else {
|
98
|
if ($display_type == "gw_ip" || $display_type == "both_ip" || ($display_type == "monitor_ip" && $monitor_address == "")) {
|
99
|
$if_gw = "~";
|
100
|
} else {
|
101
|
$if_gw = "";
|
102
|
}
|
103
|
$data .= $if_gw . $monitor_address_disp . ",";
|
104
|
$gws['delay'] = "~";
|
105
|
$gws['loss'] = "~";
|
106
|
$online = "Unknown";
|
107
|
$bgcolor = "#ADD8E6"; // lightblue
|
108
|
}
|
109
|
$data .= ($online == "Pending") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
|
110
|
$data .= "{$online}^{$bgcolor}";
|
111
|
}
|
112
|
return $data;
|
113
|
}
|
114
|
|
115
|
function get_uptime() {
|
116
|
$uptime = get_uptime_sec();
|
117
|
|
118
|
if (intval($uptime) == 0) {
|
119
|
return;
|
120
|
}
|
121
|
|
122
|
$updays = (int)($uptime / 86400);
|
123
|
$uptime %= 86400;
|
124
|
$uphours = (int)($uptime / 3600);
|
125
|
$uptime %= 3600;
|
126
|
$upmins = (int)($uptime / 60);
|
127
|
$uptime %= 60;
|
128
|
$upsecs = (int)($uptime);
|
129
|
|
130
|
$uptimestr = "";
|
131
|
if ($updays > 1) {
|
132
|
$uptimestr .= "$updays Days ";
|
133
|
} else if ($updays > 0) {
|
134
|
$uptimestr .= "1 Day ";
|
135
|
}
|
136
|
|
137
|
if ($uphours > 1) {
|
138
|
$hours = "s";
|
139
|
}
|
140
|
|
141
|
if ($upmins > 1) {
|
142
|
$minutes = "s";
|
143
|
}
|
144
|
|
145
|
if ($upmins > 1) {
|
146
|
$seconds = "s";
|
147
|
}
|
148
|
|
149
|
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
|
150
|
return $uptimestr;
|
151
|
}
|
152
|
|
153
|
/* Calculates non-idle CPU time and returns as a percentage */
|
154
|
function cpu_usage() {
|
155
|
$duration = 1;
|
156
|
$diff = array('user', 'nice', 'sys', 'intr', 'idle');
|
157
|
$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
|
158
|
sleep($duration);
|
159
|
$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
|
160
|
|
161
|
$totalStart = array_sum($cpuTicks);
|
162
|
$totalEnd = array_sum($cpuTicks2);
|
163
|
|
164
|
// Something wrapped ?!?!
|
165
|
if ($totalEnd <= $totalStart) {
|
166
|
return 0;
|
167
|
}
|
168
|
|
169
|
// Calculate total cycles used
|
170
|
$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
|
171
|
|
172
|
// Calculate the percentage used
|
173
|
$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
|
174
|
|
175
|
return $cpuUsage;
|
176
|
}
|
177
|
|
178
|
function get_pfstate($percent=false) {
|
179
|
global $config;
|
180
|
$matches = "";
|
181
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
|
182
|
$maxstates="{$config['system']['maximumstates']}";
|
183
|
} else {
|
184
|
$maxstates=pfsense_default_state_size();
|
185
|
}
|
186
|
$curentries = `/sbin/pfctl -si |grep current`;
|
187
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
188
|
$curentries = $matches[1];
|
189
|
}
|
190
|
if (!is_numeric($curentries)) {
|
191
|
$curentries = 0;
|
192
|
}
|
193
|
if ($percent) {
|
194
|
if (intval($maxstates) > 0) {
|
195
|
return round(($curentries / $maxstates) * 100, 0);
|
196
|
} else {
|
197
|
return "NA";
|
198
|
}
|
199
|
} else {
|
200
|
return $curentries . "/" . $maxstates;
|
201
|
}
|
202
|
}
|
203
|
|
204
|
function has_temp() {
|
205
|
/* no known temp monitors available at present */
|
206
|
|
207
|
/* should only reach here if there is no hardware monitor */
|
208
|
return false;
|
209
|
}
|
210
|
|
211
|
function get_hwtype() {
|
212
|
return;
|
213
|
}
|
214
|
|
215
|
function get_mbuf($percent=false) {
|
216
|
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
|
217
|
list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
|
218
|
if ($percent) {
|
219
|
if ($mbufs_max > 0) {
|
220
|
return round(($mbufs_total / $mbufs_max) * 100, 0);
|
221
|
} else {
|
222
|
return "NA";
|
223
|
}
|
224
|
} else {
|
225
|
return "{$mbufs_total}/{$mbufs_max}";
|
226
|
}
|
227
|
}
|
228
|
|
229
|
function get_temp() {
|
230
|
$temp_out = get_single_sysctl("dev.cpu.0.temperature");
|
231
|
if ($temp_out == "") {
|
232
|
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
|
233
|
}
|
234
|
|
235
|
// Remove 'C' from the end and spaces
|
236
|
$temp_out = trim(rtrim($temp_out, 'C'));
|
237
|
|
238
|
if ($temp_out[0] == '-') {
|
239
|
return '';
|
240
|
}
|
241
|
|
242
|
return $temp_out;
|
243
|
}
|
244
|
|
245
|
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
|
246
|
function get_mounted_filesystems() {
|
247
|
$mout = "";
|
248
|
$filesystems = array();
|
249
|
exec("/bin/df -Tht ufs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
|
250
|
|
251
|
/* Get rid of the header */
|
252
|
array_shift($mout);
|
253
|
foreach ($mout as $fs) {
|
254
|
$f = array();
|
255
|
list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
|
256
|
|
257
|
/* We dont' want the trailing % sign. */
|
258
|
$f['percent_used'] = trim($f['percent_used'], '%');
|
259
|
|
260
|
$filesystems[] = $f;
|
261
|
}
|
262
|
return $filesystems;
|
263
|
}
|
264
|
|
265
|
function disk_usage($slice = '/') {
|
266
|
$dfout = "";
|
267
|
exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
268
|
$diskusage = trim($dfout[0]);
|
269
|
|
270
|
return $diskusage;
|
271
|
}
|
272
|
|
273
|
function swap_usage() {
|
274
|
exec("/usr/sbin/swapinfo", $swap_info);
|
275
|
$swap_used = "";
|
276
|
foreach ($swap_info as $line) {
|
277
|
if (preg_match('/(\d+)%$/', $line, $matches)) {
|
278
|
$swap_used = $matches[1];
|
279
|
break;
|
280
|
}
|
281
|
}
|
282
|
|
283
|
return $swap_used;
|
284
|
}
|
285
|
|
286
|
function mem_usage() {
|
287
|
$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
|
288
|
if ($totalMem > 0) {
|
289
|
$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
|
290
|
$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
|
291
|
$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
|
292
|
$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
|
293
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
294
|
} else {
|
295
|
$memUsage = "NA";
|
296
|
}
|
297
|
|
298
|
return $memUsage;
|
299
|
}
|
300
|
|
301
|
function update_date_time() {
|
302
|
$datetime = date("D M j G:i:s T Y");
|
303
|
return $datetime;
|
304
|
}
|
305
|
|
306
|
function get_cpufreq() {
|
307
|
$cpufreqs = "";
|
308
|
$out = "";
|
309
|
$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
|
310
|
$maxfreq = explode("/", $cpufreqs[0]);
|
311
|
$maxfreq = $maxfreq[0];
|
312
|
$curfreq = "";
|
313
|
$curfreq = get_single_sysctl('dev.cpu.0.freq');
|
314
|
if (($curfreq > 0) && ($curfreq != $maxfreq)) {
|
315
|
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
|
316
|
}
|
317
|
return $out;
|
318
|
}
|
319
|
|
320
|
function get_cpu_count($show_detail = false) {
|
321
|
$cpucount = get_single_sysctl('kern.smp.cpus');
|
322
|
|
323
|
if ($show_detail) {
|
324
|
$cpudetail = "";
|
325
|
exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
|
326
|
$cpucount = $cpudetail[0];
|
327
|
}
|
328
|
return $cpucount;
|
329
|
}
|
330
|
|
331
|
function get_load_average() {
|
332
|
$load_average = "";
|
333
|
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
|
334
|
return $load_average[0];
|
335
|
}
|
336
|
|
337
|
function get_interfacestats() {
|
338
|
global $config;
|
339
|
//build interface list for widget use
|
340
|
$ifdescrs = get_configured_interface_list();
|
341
|
|
342
|
$array_in_packets = array();
|
343
|
$array_out_packets = array();
|
344
|
$array_in_bytes = array();
|
345
|
$array_out_bytes = array();
|
346
|
$array_in_errors = array();
|
347
|
$array_out_errors = array();
|
348
|
$array_collisions = array();
|
349
|
$array_interrupt = array();
|
350
|
$new_data = "";
|
351
|
|
352
|
//build data arrays
|
353
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
354
|
$ifinfo = get_interface_info($ifdescr);
|
355
|
$new_data .= "{$ifinfo['inpkts']},";
|
356
|
$new_data .= "{$ifinfo['outpkts']},";
|
357
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
358
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
359
|
if (isset($ifinfo['inerrs'])) {
|
360
|
$new_data .= "{$ifinfo['inerrs']},";
|
361
|
$new_data .= "{$ifinfo['outerrs']},";
|
362
|
} else {
|
363
|
$new_data .= "0,";
|
364
|
$new_data .= "0,";
|
365
|
}
|
366
|
if (isset($ifinfo['collisions'])) {
|
367
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
368
|
} else {
|
369
|
$new_data .= "0,";
|
370
|
}
|
371
|
}//end for
|
372
|
|
373
|
return $new_data;
|
374
|
}
|
375
|
|
376
|
function get_interfacestatus() {
|
377
|
$data = "";
|
378
|
global $config;
|
379
|
|
380
|
//build interface list for widget use
|
381
|
$ifdescrs = get_configured_interface_with_descr();
|
382
|
|
383
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
384
|
$ifinfo = get_interface_info($ifdescr);
|
385
|
$data .= $ifname . "^";
|
386
|
if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
387
|
$data .= "up";
|
388
|
} else if ($ifinfo['status'] == "no carrier") {
|
389
|
$data .= "down";
|
390
|
} else if ($ifinfo['status'] == "down") {
|
391
|
$data .= "block";
|
392
|
}
|
393
|
$data .= "^";
|
394
|
if ($ifinfo['ipaddr']) {
|
395
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
|
396
|
}
|
397
|
$data .= "^";
|
398
|
if ($ifinfo['ipaddrv6']) {
|
399
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
|
400
|
}
|
401
|
$data .= "^";
|
402
|
if ($ifinfo['status'] != "down") {
|
403
|
$data .= htmlspecialchars($ifinfo['media']);
|
404
|
}
|
405
|
|
406
|
$data .= "~";
|
407
|
|
408
|
}
|
409
|
return $data;
|
410
|
}
|
411
|
|
412
|
?>
|