1
|
<?php
|
2
|
/*
|
3
|
* functions.inc.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2013-2016 Electric Sheep Fencing, LLC
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* Redistribution and use in source and binary forms, with or without
|
10
|
* modification, are permitted provided that the following conditions are met:
|
11
|
*
|
12
|
* 1. Redistributions of source code must retain the above copyright notice,
|
13
|
* this list of conditions and the following disclaimer.
|
14
|
*
|
15
|
* 2. Redistributions in binary form must reproduce the above copyright
|
16
|
* notice, this list of conditions and the following disclaimer in
|
17
|
* the documentation and/or other materials provided with the
|
18
|
* distribution.
|
19
|
*
|
20
|
* 3. All advertising materials mentioning features or use of this software
|
21
|
* must display the following acknowledgment:
|
22
|
* "This product includes software developed by the pfSense Project
|
23
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
24
|
*
|
25
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
26
|
* endorse or promote products derived from this software without
|
27
|
* prior written permission. For written permission, please contact
|
28
|
* coreteam@pfsense.org.
|
29
|
*
|
30
|
* 5. Products derived from this software may not be called "pfSense"
|
31
|
* nor may "pfSense" appear in their names without prior written
|
32
|
* permission of the Electric Sheep Fencing, LLC.
|
33
|
*
|
34
|
* 6. Redistributions of any form whatsoever must retain the following
|
35
|
* acknowledgment:
|
36
|
*
|
37
|
* "This product includes software developed by the pfSense Project
|
38
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
39
|
*
|
40
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
41
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
42
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
43
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
44
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
45
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
46
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
47
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
48
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
49
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
50
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
51
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
52
|
*/
|
53
|
|
54
|
if (Connection_Aborted()) {
|
55
|
exit;
|
56
|
}
|
57
|
|
58
|
require_once("config.inc");
|
59
|
require_once("pfsense-utils.inc");
|
60
|
|
61
|
function get_stats() {
|
62
|
$stats['cpu'] = cpu_usage();
|
63
|
$stats['mem'] = mem_usage();
|
64
|
$stats['uptime'] = get_uptime();
|
65
|
$stats['states'] = get_pfstate();
|
66
|
$stats['temp'] = get_temp();
|
67
|
$stats['datetime'] = update_date_time();
|
68
|
$stats['interfacestatistics'] = get_interfacestats();
|
69
|
$stats['interfacestatus'] = get_interfacestatus();
|
70
|
$stats['cpufreq'] = get_cpufreq();
|
71
|
$stats['load_average'] = get_load_average();
|
72
|
$stats['mbuf'] = get_mbuf();
|
73
|
$stats['mbufpercent'] = get_mbuf(true);
|
74
|
$stats['statepercent'] = get_pfstate(true);
|
75
|
$stats = join("|", $stats);
|
76
|
return $stats;
|
77
|
}
|
78
|
|
79
|
function get_uptime() {
|
80
|
$uptime = get_uptime_sec();
|
81
|
|
82
|
if (intval($uptime) == 0) {
|
83
|
return;
|
84
|
}
|
85
|
|
86
|
$updays = (int)($uptime / 86400);
|
87
|
$uptime %= 86400;
|
88
|
$uphours = (int)($uptime / 3600);
|
89
|
$uptime %= 3600;
|
90
|
$upmins = (int)($uptime / 60);
|
91
|
$uptime %= 60;
|
92
|
$upsecs = (int)($uptime);
|
93
|
|
94
|
$uptimestr = "";
|
95
|
if ($updays > 1) {
|
96
|
$uptimestr .= "$updays Days ";
|
97
|
} else if ($updays > 0) {
|
98
|
$uptimestr .= "1 Day ";
|
99
|
}
|
100
|
|
101
|
if ($uphours > 1) {
|
102
|
$hours = "s";
|
103
|
}
|
104
|
|
105
|
if ($upmins > 1) {
|
106
|
$minutes = "s";
|
107
|
}
|
108
|
|
109
|
if ($upmins > 1) {
|
110
|
$seconds = "s";
|
111
|
}
|
112
|
|
113
|
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
|
114
|
return $uptimestr;
|
115
|
}
|
116
|
|
117
|
/* Calculates non-idle CPU time and returns as a percentage */
|
118
|
function cpu_usage() {
|
119
|
$duration = 1;
|
120
|
$diff = array('user', 'nice', 'sys', 'intr', 'idle');
|
121
|
$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
|
122
|
sleep($duration);
|
123
|
$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
|
124
|
|
125
|
$totalStart = array_sum($cpuTicks);
|
126
|
$totalEnd = array_sum($cpuTicks2);
|
127
|
|
128
|
// Something wrapped ?!?!
|
129
|
if ($totalEnd <= $totalStart) {
|
130
|
return 0;
|
131
|
}
|
132
|
|
133
|
// Calculate total cycles used
|
134
|
$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
|
135
|
|
136
|
// Calculate the percentage used
|
137
|
$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
|
138
|
|
139
|
return $cpuUsage;
|
140
|
}
|
141
|
|
142
|
function get_pfstate($percent=false) {
|
143
|
global $config;
|
144
|
$matches = "";
|
145
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
|
146
|
$maxstates="{$config['system']['maximumstates']}";
|
147
|
} else {
|
148
|
$maxstates=pfsense_default_state_size();
|
149
|
}
|
150
|
$curentries = `/sbin/pfctl -si |grep current`;
|
151
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
152
|
$curentries = $matches[1];
|
153
|
}
|
154
|
if (!is_numeric($curentries)) {
|
155
|
$curentries = 0;
|
156
|
}
|
157
|
if ($percent) {
|
158
|
if (intval($maxstates) > 0) {
|
159
|
return round(($curentries / $maxstates) * 100, 0);
|
160
|
} else {
|
161
|
return "NA";
|
162
|
}
|
163
|
} else {
|
164
|
return $curentries . "/" . $maxstates;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
function has_temp() {
|
169
|
/* no known temp monitors available at present */
|
170
|
|
171
|
/* should only reach here if there is no hardware monitor */
|
172
|
return false;
|
173
|
}
|
174
|
|
175
|
function get_hwtype() {
|
176
|
return;
|
177
|
}
|
178
|
|
179
|
function get_mbuf($percent=false) {
|
180
|
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
|
181
|
list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
|
182
|
if ($percent) {
|
183
|
if ($mbufs_max > 0) {
|
184
|
return round(($mbufs_total / $mbufs_max) * 100, 0);
|
185
|
} else {
|
186
|
return "NA";
|
187
|
}
|
188
|
} else {
|
189
|
return "{$mbufs_total}/{$mbufs_max}";
|
190
|
}
|
191
|
}
|
192
|
|
193
|
function get_temp() {
|
194
|
$temp_out = get_single_sysctl("dev.cpu.0.temperature");
|
195
|
if ($temp_out == "") {
|
196
|
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
|
197
|
}
|
198
|
|
199
|
// Remove 'C' from the end and spaces
|
200
|
$temp_out = trim(rtrim($temp_out, 'C'));
|
201
|
|
202
|
if ($temp_out[0] == '-') {
|
203
|
return '';
|
204
|
}
|
205
|
|
206
|
return $temp_out;
|
207
|
}
|
208
|
|
209
|
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
|
210
|
function get_mounted_filesystems() {
|
211
|
$mout = "";
|
212
|
$filesystems = array();
|
213
|
exec("/bin/df -Tht ufs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
|
214
|
|
215
|
/* Get rid of the header */
|
216
|
array_shift($mout);
|
217
|
foreach ($mout as $fs) {
|
218
|
$f = array();
|
219
|
list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
|
220
|
|
221
|
/* We dont' want the trailing % sign. */
|
222
|
$f['percent_used'] = trim($f['percent_used'], '%');
|
223
|
|
224
|
$filesystems[] = $f;
|
225
|
}
|
226
|
return $filesystems;
|
227
|
}
|
228
|
|
229
|
function disk_usage($slice = '/') {
|
230
|
$dfout = "";
|
231
|
exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
232
|
$diskusage = trim($dfout[0]);
|
233
|
|
234
|
return $diskusage;
|
235
|
}
|
236
|
|
237
|
function swap_usage() {
|
238
|
exec("/usr/sbin/swapinfo", $swap_info);
|
239
|
$swap_used = "";
|
240
|
foreach ($swap_info as $line) {
|
241
|
if (preg_match('/(\d+)%$/', $line, $matches)) {
|
242
|
$swap_used = $matches[1];
|
243
|
break;
|
244
|
}
|
245
|
}
|
246
|
|
247
|
return $swap_used;
|
248
|
}
|
249
|
|
250
|
function mem_usage() {
|
251
|
$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
|
252
|
if ($totalMem > 0) {
|
253
|
$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
|
254
|
$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
|
255
|
$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
|
256
|
$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
|
257
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
258
|
} else {
|
259
|
$memUsage = "NA";
|
260
|
}
|
261
|
|
262
|
return $memUsage;
|
263
|
}
|
264
|
|
265
|
function update_date_time() {
|
266
|
$datetime = date("D M j G:i:s T Y");
|
267
|
return $datetime;
|
268
|
}
|
269
|
|
270
|
function get_cpufreq() {
|
271
|
$cpufreqs = "";
|
272
|
$out = "";
|
273
|
$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
|
274
|
$maxfreq = explode("/", $cpufreqs[0]);
|
275
|
$maxfreq = $maxfreq[0];
|
276
|
$curfreq = "";
|
277
|
$curfreq = get_single_sysctl('dev.cpu.0.freq');
|
278
|
if (($curfreq > 0) && ($curfreq != $maxfreq)) {
|
279
|
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
|
280
|
}
|
281
|
return $out;
|
282
|
}
|
283
|
|
284
|
function get_cpu_count($show_detail = false) {
|
285
|
$cpucount = get_single_sysctl('kern.smp.cpus');
|
286
|
|
287
|
if ($show_detail) {
|
288
|
$cpudetail = "";
|
289
|
exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
|
290
|
$cpucount = $cpudetail[0];
|
291
|
}
|
292
|
return $cpucount;
|
293
|
}
|
294
|
|
295
|
function get_load_average() {
|
296
|
$load_average = "";
|
297
|
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
|
298
|
return $load_average[0];
|
299
|
}
|
300
|
|
301
|
function get_interfacestats() {
|
302
|
global $config;
|
303
|
//build interface list for widget use
|
304
|
$ifdescrs = get_configured_interface_list();
|
305
|
|
306
|
$array_in_packets = array();
|
307
|
$array_out_packets = array();
|
308
|
$array_in_bytes = array();
|
309
|
$array_out_bytes = array();
|
310
|
$array_in_errors = array();
|
311
|
$array_out_errors = array();
|
312
|
$array_collisions = array();
|
313
|
$array_interrupt = array();
|
314
|
$new_data = "";
|
315
|
|
316
|
//build data arrays
|
317
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
318
|
$ifinfo = get_interface_info($ifdescr);
|
319
|
$new_data .= "{$ifinfo['inpkts']},";
|
320
|
$new_data .= "{$ifinfo['outpkts']},";
|
321
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
322
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
323
|
if (isset($ifinfo['inerrs'])) {
|
324
|
$new_data .= "{$ifinfo['inerrs']},";
|
325
|
$new_data .= "{$ifinfo['outerrs']},";
|
326
|
} else {
|
327
|
$new_data .= "0,";
|
328
|
$new_data .= "0,";
|
329
|
}
|
330
|
if (isset($ifinfo['collisions'])) {
|
331
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
332
|
} else {
|
333
|
$new_data .= "0,";
|
334
|
}
|
335
|
}//end for
|
336
|
|
337
|
return $new_data;
|
338
|
}
|
339
|
|
340
|
function get_interfacestatus() {
|
341
|
$data = "";
|
342
|
global $config;
|
343
|
|
344
|
//build interface list for widget use
|
345
|
$ifdescrs = get_configured_interface_with_descr();
|
346
|
|
347
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
348
|
$ifinfo = get_interface_info($ifdescr);
|
349
|
$data .= $ifname . "^";
|
350
|
if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
351
|
$data .= "up";
|
352
|
} else if ($ifinfo['status'] == "no carrier") {
|
353
|
$data .= "down";
|
354
|
} else if ($ifinfo['status'] == "down") {
|
355
|
$data .= "block";
|
356
|
}
|
357
|
$data .= "^";
|
358
|
if ($ifinfo['ipaddr']) {
|
359
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
|
360
|
}
|
361
|
$data .= "^";
|
362
|
if ($ifinfo['ipaddrv6']) {
|
363
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
|
364
|
}
|
365
|
$data .= "^";
|
366
|
if ($ifinfo['status'] != "down") {
|
367
|
$data .= htmlspecialchars($ifinfo['media']);
|
368
|
}
|
369
|
|
370
|
$data .= "~";
|
371
|
|
372
|
}
|
373
|
return $data;
|
374
|
}
|
375
|
|
376
|
?>
|