1
|
<?php
|
2
|
/*
|
3
|
* functions.inc.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2013-2018 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
* you may not use this file except in compliance with the License.
|
11
|
* You may obtain a copy of the License at
|
12
|
*
|
13
|
* http://www.apache.org/licenses/LICENSE-2.0
|
14
|
*
|
15
|
* Unless required by applicable law or agreed to in writing, software
|
16
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
* See the License for the specific language governing permissions and
|
19
|
* limitations under the License.
|
20
|
*/
|
21
|
|
22
|
if (Connection_Aborted()) {
|
23
|
exit;
|
24
|
}
|
25
|
|
26
|
require_once("config.inc");
|
27
|
require_once("pfsense-utils.inc");
|
28
|
|
29
|
function get_stats() {
|
30
|
$stats['cpu'] = cpu_usage();
|
31
|
$stats['mem'] = mem_usage();
|
32
|
$stats['uptime'] = get_uptime();
|
33
|
$stats['states'] = get_pfstate();
|
34
|
$stats['temp'] = get_temp();
|
35
|
$stats['datetime'] = update_date_time();
|
36
|
$stats['cpufreq'] = get_cpufreq();
|
37
|
$stats['load_average'] = get_load_average();
|
38
|
get_mbuf($stats['mbuf'], $stats['mbufpercent']);
|
39
|
$stats['statepercent'] = get_pfstate(true);
|
40
|
$stats = join("|", $stats);
|
41
|
return $stats;
|
42
|
}
|
43
|
|
44
|
function get_uptime() {
|
45
|
$uptime = get_uptime_sec();
|
46
|
|
47
|
if (intval($uptime) == 0) {
|
48
|
return;
|
49
|
}
|
50
|
|
51
|
$updays = (int)($uptime / 86400);
|
52
|
$uptime %= 86400;
|
53
|
$uphours = (int)($uptime / 3600);
|
54
|
$uptime %= 3600;
|
55
|
$upmins = (int)($uptime / 60);
|
56
|
$uptime %= 60;
|
57
|
$upsecs = (int)($uptime);
|
58
|
|
59
|
$uptimestr = "";
|
60
|
if ($updays > 1) {
|
61
|
$uptimestr .= "$updays Days ";
|
62
|
} else if ($updays > 0) {
|
63
|
$uptimestr .= "1 Day ";
|
64
|
}
|
65
|
|
66
|
if ($uphours > 1) {
|
67
|
$hours = "s";
|
68
|
}
|
69
|
|
70
|
if ($upmins > 1) {
|
71
|
$minutes = "s";
|
72
|
}
|
73
|
|
74
|
if ($upmins > 1) {
|
75
|
$seconds = "s";
|
76
|
}
|
77
|
|
78
|
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
|
79
|
return $uptimestr;
|
80
|
}
|
81
|
|
82
|
// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that
|
83
|
function cpu_usage() {
|
84
|
|
85
|
$diff = array('user', 'nice', 'sys', 'intr', 'idle');
|
86
|
$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
|
87
|
|
88
|
return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
|
89
|
}
|
90
|
|
91
|
function get_pfstate($percent=false) {
|
92
|
global $config;
|
93
|
$matches = "";
|
94
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
|
95
|
$maxstates="{$config['system']['maximumstates']}";
|
96
|
} else {
|
97
|
$maxstates=pfsense_default_state_size();
|
98
|
}
|
99
|
$curentries = `/sbin/pfctl -si |grep current`;
|
100
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
101
|
$curentries = $matches[1];
|
102
|
}
|
103
|
if (!is_numeric($curentries)) {
|
104
|
$curentries = 0;
|
105
|
}
|
106
|
if ($percent) {
|
107
|
if (intval($maxstates) > 0) {
|
108
|
return round(($curentries / $maxstates) * 100, 0);
|
109
|
} else {
|
110
|
return "NA";
|
111
|
}
|
112
|
} else {
|
113
|
return $curentries . "/" . $maxstates;
|
114
|
}
|
115
|
}
|
116
|
|
117
|
function has_temp() {
|
118
|
/* no known temp monitors available at present */
|
119
|
|
120
|
/* should only reach here if there is no hardware monitor */
|
121
|
return false;
|
122
|
}
|
123
|
|
124
|
function get_hwtype() {
|
125
|
return;
|
126
|
}
|
127
|
|
128
|
function get_mbuf(&$mbuf, &$mbufpercent) {
|
129
|
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
|
130
|
list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
|
131
|
$mbuf = "{$mbufs_total}/{$mbufs_max}";
|
132
|
$mbufpercent = ($mbufs_max > 0) ? round(($mbufs_total / $mbufs_max) * 100, 0) : "NA";
|
133
|
}
|
134
|
|
135
|
function get_temp() {
|
136
|
$temp_out = get_single_sysctl("dev.cpu.0.temperature");
|
137
|
if ($temp_out == "") {
|
138
|
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
|
139
|
}
|
140
|
|
141
|
// Remove 'C' from the end and spaces
|
142
|
$temp_out = trim(rtrim($temp_out, 'C'));
|
143
|
|
144
|
if ($temp_out[0] == '-') {
|
145
|
return '';
|
146
|
}
|
147
|
|
148
|
return $temp_out;
|
149
|
}
|
150
|
|
151
|
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
|
152
|
function get_mounted_filesystems() {
|
153
|
$mout = "";
|
154
|
$filesystems = array();
|
155
|
exec("/bin/df -Tht ufs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
|
156
|
|
157
|
/* Get rid of the header */
|
158
|
array_shift($mout);
|
159
|
foreach ($mout as $fs) {
|
160
|
$f = array();
|
161
|
list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
|
162
|
|
163
|
/* We dont' want the trailing % sign. */
|
164
|
$f['percent_used'] = trim($f['percent_used'], '%');
|
165
|
|
166
|
$filesystems[] = $f;
|
167
|
}
|
168
|
return $filesystems;
|
169
|
}
|
170
|
|
171
|
function disk_usage($slice = '/') {
|
172
|
$dfout = "";
|
173
|
exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
174
|
$diskusage = trim($dfout[0]);
|
175
|
|
176
|
return $diskusage;
|
177
|
}
|
178
|
|
179
|
function swap_usage() {
|
180
|
exec("/usr/sbin/swapinfo", $swap_info);
|
181
|
$swap_used = "";
|
182
|
foreach ($swap_info as $line) {
|
183
|
if (preg_match('/(\d+)%$/', $line, $matches)) {
|
184
|
$swap_used = $matches[1];
|
185
|
break;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
return $swap_used;
|
190
|
}
|
191
|
|
192
|
function mem_usage() {
|
193
|
$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
|
194
|
if ($totalMem > 0) {
|
195
|
$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
|
196
|
$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
|
197
|
$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
|
198
|
$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
|
199
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
200
|
} else {
|
201
|
$memUsage = "NA";
|
202
|
}
|
203
|
|
204
|
return $memUsage;
|
205
|
}
|
206
|
|
207
|
function update_date_time() {
|
208
|
$datetime = date("D M j G:i:s T Y");
|
209
|
return $datetime;
|
210
|
}
|
211
|
|
212
|
function get_cpufreq() {
|
213
|
$cpufreqs = "";
|
214
|
$out = "";
|
215
|
$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
|
216
|
$maxfreq = explode("/", $cpufreqs[0]);
|
217
|
$maxfreq = $maxfreq[0];
|
218
|
$curfreq = "";
|
219
|
$curfreq = get_single_sysctl('dev.cpu.0.freq');
|
220
|
if (($curfreq > 0) && ($curfreq != $maxfreq)) {
|
221
|
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
|
222
|
}
|
223
|
return $out;
|
224
|
}
|
225
|
|
226
|
function get_cpu_crypto_support() {
|
227
|
$machine = get_single_sysctl('hw.machine');
|
228
|
$accelerated_arm_platforms = array('am335x');
|
229
|
$cpucrypto_type = "";
|
230
|
|
231
|
switch ($machine) {
|
232
|
case 'amd64':
|
233
|
$cpucrypto_type = "AES-NI CPU Crypto: ";
|
234
|
exec("/usr/bin/grep -c ' Features.*AESNI' /var/log/dmesg.boot", $cpucrypto_present);
|
235
|
if ($cpucrypto_present[0] > 0) {
|
236
|
$cpucrypto_type .= "Yes ";
|
237
|
$cpucrypto_type .= (is_module_loaded('aesni')) ? "(active)" : "(inactive)";
|
238
|
} else {
|
239
|
$cpucrypto_type .= "No";
|
240
|
}
|
241
|
case 'arm':
|
242
|
$armplatform = get_single_sysctl('hw.platform');
|
243
|
if (in_array($armplatform, $accelerated_arm_platforms)) {
|
244
|
/* No drivers yet, so mark inactive! */
|
245
|
$cpucrypto_type = "{$armplatform} built-in CPU Crypto (inactive)";
|
246
|
break;
|
247
|
}
|
248
|
$armmv = get_single_sysctl('hw.mv_soc_model');
|
249
|
if (strpos($armmv, "Marvell 88F682") != 0) {
|
250
|
$cpucrypto_type = "Crypto: ". get_single_sysctl('dev.cesa.0.%desc');
|
251
|
}
|
252
|
default:
|
253
|
/* Unknown/unidentified platform */
|
254
|
}
|
255
|
|
256
|
if (!empty($cpucrypto_type)) {
|
257
|
return $cpucrypto_type;
|
258
|
} else {
|
259
|
return "CPU Crypto: None/Unknown Platform";
|
260
|
}
|
261
|
}
|
262
|
|
263
|
function get_cpu_count($show_detail = false) {
|
264
|
$cpucount = get_single_sysctl('kern.smp.cpus');
|
265
|
|
266
|
if ($show_detail) {
|
267
|
$cpudetail = "";
|
268
|
exec("/usr/bin/grep 'FreeBSD/SMP:.*package' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
|
269
|
$cpucount = $cpudetail[0];
|
270
|
}
|
271
|
return $cpucount;
|
272
|
}
|
273
|
|
274
|
function get_load_average() {
|
275
|
$load_average = "";
|
276
|
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
|
277
|
return $load_average[0];
|
278
|
}
|
279
|
|
280
|
function get_interfacestats() {
|
281
|
global $config;
|
282
|
//build interface list for widget use
|
283
|
$ifdescrs = get_configured_interface_list();
|
284
|
|
285
|
$array_in_packets = array();
|
286
|
$array_out_packets = array();
|
287
|
$array_in_bytes = array();
|
288
|
$array_out_bytes = array();
|
289
|
$array_in_errors = array();
|
290
|
$array_out_errors = array();
|
291
|
$array_collisions = array();
|
292
|
$array_interrupt = array();
|
293
|
$new_data = "";
|
294
|
|
295
|
//build data arrays
|
296
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
297
|
$ifinfo = get_interface_info($ifdescr);
|
298
|
$new_data .= "{$ifinfo['inpkts']},";
|
299
|
$new_data .= "{$ifinfo['outpkts']},";
|
300
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
301
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
302
|
if (isset($ifinfo['inerrs'])) {
|
303
|
$new_data .= "{$ifinfo['inerrs']},";
|
304
|
$new_data .= "{$ifinfo['outerrs']},";
|
305
|
} else {
|
306
|
$new_data .= "0,";
|
307
|
$new_data .= "0,";
|
308
|
}
|
309
|
if (isset($ifinfo['collisions'])) {
|
310
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
311
|
} else {
|
312
|
$new_data .= "0,";
|
313
|
}
|
314
|
}//end for
|
315
|
|
316
|
return $new_data;
|
317
|
}
|
318
|
|
319
|
function get_interfacestatus() {
|
320
|
$data = "";
|
321
|
global $config;
|
322
|
|
323
|
//build interface list for widget use
|
324
|
$ifdescrs = get_configured_interface_with_descr();
|
325
|
|
326
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
327
|
$ifinfo = get_interface_info($ifdescr);
|
328
|
$data .= $ifname . "^";
|
329
|
if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
330
|
$data .= "up";
|
331
|
} else if ($ifinfo['status'] == "no carrier") {
|
332
|
$data .= "down";
|
333
|
} else if ($ifinfo['status'] == "down") {
|
334
|
$data .= "block";
|
335
|
}
|
336
|
$data .= "^";
|
337
|
if ($ifinfo['ipaddr']) {
|
338
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
|
339
|
}
|
340
|
$data .= "^";
|
341
|
if ($ifinfo['ipaddrv6']) {
|
342
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
|
343
|
}
|
344
|
$data .= "^";
|
345
|
if ($ifinfo['status'] != "down") {
|
346
|
$data .= htmlspecialchars($ifinfo['media']);
|
347
|
}
|
348
|
|
349
|
$data .= "~";
|
350
|
|
351
|
}
|
352
|
return $data;
|
353
|
}
|
354
|
|
355
|
?>
|