1
|
<?php
|
2
|
/*
|
3
|
* functions.inc.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
7
|
* Copyright (c) 2013-2023 Rubicon Communications, LLC (Netgate)
|
8
|
* All rights reserved.
|
9
|
*
|
10
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
11
|
* you may not use this file except in compliance with the License.
|
12
|
* You may obtain a copy of the License at
|
13
|
*
|
14
|
* http://www.apache.org/licenses/LICENSE-2.0
|
15
|
*
|
16
|
* Unless required by applicable law or agreed to in writing, software
|
17
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
18
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19
|
* See the License for the specific language governing permissions and
|
20
|
* limitations under the License.
|
21
|
*/
|
22
|
|
23
|
if (Connection_Aborted()) {
|
24
|
exit;
|
25
|
}
|
26
|
|
27
|
require_once("config.inc");
|
28
|
require_once("pfsense-utils.inc");
|
29
|
|
30
|
function get_stats($sitems = array()) {
|
31
|
$sitems = is_array($sitems) ? $sitems : [];
|
32
|
$stats['cpu'] = (!in_array('cpu_usage', $sitems)) ? cpu_usage() : '|';
|
33
|
$stats['mem'] = (!in_array('memory_usage', $sitems)) ? mem_usage() : '';
|
34
|
$stats['uptime'] = (!in_array('uptime', $sitems)) ? get_uptime() : '';
|
35
|
$stats['states'] = (!in_array('state_table_size', $sitems)) ? get_pfstate() : '';
|
36
|
$stats['temp'] = (!in_array('temperature', $sitems)) ? get_temp() : '';
|
37
|
$stats['datetime'] = (!in_array('current_datetime', $sitems)) ? update_date_time() : '';
|
38
|
$stats['cpufreq'] = (!in_array('cpu_type', $sitems)) ? get_cpufreq() : '';
|
39
|
$stats['load_average'] = (!in_array('load_average', $sitems)) ? get_load_average() : '';
|
40
|
if (!in_array('mbuf_usage', $sitems)) {
|
41
|
get_mbuf($stats['mbuf'], $stats['mbufpercent']);
|
42
|
} else {
|
43
|
$stats['mbuf'] = '';
|
44
|
$stats['mbufpercent'] = '';
|
45
|
}
|
46
|
$stats['statepercent'] = (!in_array('state_table_size', $sitems)) ? get_pfstate(true) : '';
|
47
|
$stats = join("|", $stats);
|
48
|
return $stats;
|
49
|
}
|
50
|
|
51
|
function get_uptime() {
|
52
|
$uptime = get_uptime_sec();
|
53
|
|
54
|
if (intval($uptime) == 0) {
|
55
|
return;
|
56
|
}
|
57
|
|
58
|
$updays = (int)($uptime / 86400);
|
59
|
$uptime %= 86400;
|
60
|
$uphours = (int)($uptime / 3600);
|
61
|
$uptime %= 3600;
|
62
|
$upmins = (int)($uptime / 60);
|
63
|
$uptime %= 60;
|
64
|
$upsecs = (int)($uptime);
|
65
|
|
66
|
$uptimestr = "";
|
67
|
if ($updays > 1) {
|
68
|
$uptimestr .= "$updays Days ";
|
69
|
} else if ($updays > 0) {
|
70
|
$uptimestr .= "1 Day ";
|
71
|
}
|
72
|
|
73
|
if ($uphours > 1) {
|
74
|
$hours = "s";
|
75
|
}
|
76
|
|
77
|
if ($upmins > 1) {
|
78
|
$minutes = "s";
|
79
|
}
|
80
|
|
81
|
if ($upsecs > 1) {
|
82
|
$seconds = "s";
|
83
|
}
|
84
|
|
85
|
$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
|
86
|
return $uptimestr;
|
87
|
}
|
88
|
|
89
|
// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that
|
90
|
function cpu_usage() {
|
91
|
|
92
|
$diff = array('user', 'nice', 'sys', 'intr', 'idle');
|
93
|
$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
|
94
|
|
95
|
return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
|
96
|
}
|
97
|
|
98
|
function get_pfstate($percent=false) {
|
99
|
global $config;
|
100
|
$matches = "";
|
101
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
|
102
|
$maxstates="{$config['system']['maximumstates']}";
|
103
|
} else {
|
104
|
$maxstates=pfsense_default_state_size();
|
105
|
}
|
106
|
$curentries = `/sbin/pfctl -si |grep current`;
|
107
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
108
|
$curentries = $matches[1];
|
109
|
}
|
110
|
if (!is_numeric($curentries)) {
|
111
|
$curentries = 0;
|
112
|
}
|
113
|
if ($percent) {
|
114
|
if (intval($maxstates) > 0) {
|
115
|
return round(($curentries / $maxstates) * 100, 0);
|
116
|
} else {
|
117
|
return "NA";
|
118
|
}
|
119
|
} else {
|
120
|
return $curentries . "/" . $maxstates;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
function has_temp() {
|
125
|
/* no known temp monitors available at present */
|
126
|
|
127
|
/* should only reach here if there is no hardware monitor */
|
128
|
return false;
|
129
|
}
|
130
|
|
131
|
function get_hwtype() {
|
132
|
return;
|
133
|
}
|
134
|
|
135
|
function get_mbuf(&$mbuf, &$mbufpercent) {
|
136
|
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
|
137
|
list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
|
138
|
$mbuf = "{$mbufs_total}/{$mbufs_max}";
|
139
|
$mbufpercent = ($mbufs_max > 0) ? round(($mbufs_total / $mbufs_max) * 100, 0) : "NA";
|
140
|
}
|
141
|
|
142
|
function get_temp() {
|
143
|
$temp_out = get_single_sysctl("dev.cpu.0.temperature");
|
144
|
if ($temp_out == "") {
|
145
|
$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
|
146
|
}
|
147
|
|
148
|
// Remove 'C' from the end and spaces
|
149
|
$temp_out = trim(rtrim($temp_out, 'C'));
|
150
|
|
151
|
if ($temp_out[0] == '-') {
|
152
|
return '';
|
153
|
}
|
154
|
|
155
|
return $temp_out;
|
156
|
}
|
157
|
|
158
|
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
|
159
|
function get_mounted_filesystems() {
|
160
|
$mout = "";
|
161
|
$filesystems = array();
|
162
|
exec("/bin/df -Tht ufs,zfs,tmpfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
|
163
|
|
164
|
/* Get rid of the header */
|
165
|
array_shift($mout);
|
166
|
foreach ($mout as $fs) {
|
167
|
$f = array();
|
168
|
list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
|
169
|
|
170
|
/* We dont' want the trailing % sign. */
|
171
|
$f['percent_used'] = trim($f['percent_used'], '%');
|
172
|
|
173
|
$filesystems[] = $f;
|
174
|
}
|
175
|
return $filesystems;
|
176
|
}
|
177
|
|
178
|
function disk_usage($slice = '/') {
|
179
|
$dfout = "";
|
180
|
exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
181
|
$diskusage = trim($dfout[0]);
|
182
|
|
183
|
return $diskusage;
|
184
|
}
|
185
|
|
186
|
function swap_usage() {
|
187
|
exec("/usr/sbin/swapinfo | /usr/bin/tail -1", $swap_info);
|
188
|
$swap_used = "";
|
189
|
foreach ($swap_info as $line) {
|
190
|
if (preg_match('/(\d+)%$/', $line, $matches)) {
|
191
|
$swap_used = $matches[1];
|
192
|
break;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
return $swap_used;
|
197
|
}
|
198
|
|
199
|
function mem_usage() {
|
200
|
$memUsage = "NA";
|
201
|
$totalMem = (int) get_single_sysctl("vm.stats.vm.v_page_count");
|
202
|
if (is_numeric($totalMem)) {
|
203
|
/* Include inactive and laundry with free memory since they
|
204
|
* could be freed under pressure. */
|
205
|
$inactiveMem = (int) get_single_sysctl("vm.stats.vm.v_inactive_count");
|
206
|
$laundryMem = (int) get_single_sysctl("vm.stats.vm.v_laundry_count");
|
207
|
$freeMem = (int) get_single_sysctl("vm.stats.vm.v_free_count");
|
208
|
if (is_numeric($inactiveMem) &&
|
209
|
is_numeric($laundryMem) &&
|
210
|
is_numeric($freeMem)) {
|
211
|
$usedMem = $totalMem - ($inactiveMem + $laundryMem + $freeMem);
|
212
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
213
|
}
|
214
|
}
|
215
|
return $memUsage;
|
216
|
}
|
217
|
|
218
|
function update_date_time() {
|
219
|
$datetime = date("D M j G:i:s T Y");
|
220
|
return $datetime;
|
221
|
}
|
222
|
|
223
|
function get_cpufreq() {
|
224
|
$cpufreqs = "";
|
225
|
$out = "";
|
226
|
$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
|
227
|
$maxfreq = explode("/", $cpufreqs[0]);
|
228
|
$maxfreq = $maxfreq[0];
|
229
|
$curfreq = "";
|
230
|
$curfreq = get_single_sysctl('dev.cpu.0.freq');
|
231
|
if (($curfreq > 0) && ($curfreq != $maxfreq)) {
|
232
|
$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
|
233
|
}
|
234
|
return $out;
|
235
|
}
|
236
|
|
237
|
define("INTEL_C2000_IQIA_PHYS", "0x1f188086");
|
238
|
define("INTEL_C3K_QAT", "0x19e28086");
|
239
|
define("INTEL_C3K_QAT_VF", "0x19e38086");
|
240
|
define("INTEL_C620_QAT", "0x37c88086");
|
241
|
define("INTEL_C620_QAT_VF", "0x37c98086");
|
242
|
define("INTEL_XEOND_QAT", "0x6f548086");
|
243
|
define("INTEL_XEOND_QAT_VF", "0x6f558086");
|
244
|
define("INTEL_DH895XCC_QAT", "0x04358086");
|
245
|
define("INTEL_DH895XCC_QAT_VF", "0x04438086");
|
246
|
define("AESNI_ALGS", "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS");
|
247
|
define("AESNI_ALGS_SHA", "SHA1,SHA256");
|
248
|
define("QAT_ALGS", "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS,SHA1,SHA256,SHA384,SHA512");
|
249
|
|
250
|
function crypto_accel_new($name = "", $algs = "") {
|
251
|
return (array("name" => $name, "present" => false, "enabled" => false, "algs" => explode(",", $algs)));
|
252
|
}
|
253
|
|
254
|
function crypto_accel_init() {
|
255
|
$machine = get_single_sysctl('hw.machine');
|
256
|
|
257
|
/* Defaults */
|
258
|
$crypto = array();
|
259
|
$crypto["accel"] = array();
|
260
|
|
261
|
switch ($machine) {
|
262
|
case 'amd64':
|
263
|
$crypto["accel"][] = crypto_accel_new("AESNI", AESNI_ALGS);
|
264
|
$crypto["accel"][] = crypto_accel_new("QAT", QAT_ALGS);
|
265
|
break;
|
266
|
}
|
267
|
|
268
|
return ($crypto);
|
269
|
}
|
270
|
|
271
|
function crypto_accel_set_flags($crypto, $name, $present = false, $enabled = false) {
|
272
|
|
273
|
foreach ($crypto["accel"] as &$accel) {
|
274
|
if ($accel["name"] != $name) {
|
275
|
continue;
|
276
|
}
|
277
|
$accel["present"] = $present;
|
278
|
$accel["enabled"] = $enabled;
|
279
|
}
|
280
|
|
281
|
return ($crypto);
|
282
|
}
|
283
|
|
284
|
function crypto_accel_get($crypto, $name, $key) {
|
285
|
|
286
|
foreach ($crypto["accel"] as $accel) {
|
287
|
if ($accel["name"] != $name) {
|
288
|
continue;
|
289
|
}
|
290
|
return ($accel[$key]);
|
291
|
}
|
292
|
|
293
|
return ("");
|
294
|
}
|
295
|
|
296
|
function crypto_accel_set_algs($crypto, $name, $algs) {
|
297
|
|
298
|
foreach ($crypto["accel"] as &$accel) {
|
299
|
if ($accel["name"] != $name) {
|
300
|
continue;
|
301
|
}
|
302
|
$a = explode(",", $algs);
|
303
|
$m = array_merge($accel["algs"], $a);
|
304
|
$accel["algs"] = array_unique($m, SORT_STRING);
|
305
|
}
|
306
|
|
307
|
return ($crypto);
|
308
|
}
|
309
|
|
310
|
function crypto_accel_get_algs($crypto) {
|
311
|
$algs = [];
|
312
|
|
313
|
foreach ($crypto['accel'] as $accel) {
|
314
|
/* skip these... */
|
315
|
if (!$accel["present"] || !$accel["enabled"]) {
|
316
|
continue;
|
317
|
}
|
318
|
/* $algs is indexed by number, array_merge will automatically
|
319
|
* append values for entries with duplicate numerical keys so no
|
320
|
* need to manually avoid duplicate index values here. */
|
321
|
$algs = array_unique(array_merge($algs, $accel['algs']), SORT_STRING);
|
322
|
}
|
323
|
|
324
|
/* return early */
|
325
|
if (empty($algs)) {
|
326
|
return (gettext('Inactive'));
|
327
|
}
|
328
|
|
329
|
/* Sort values, ignoring keys */
|
330
|
sort($algs, SORT_STRING);
|
331
|
|
332
|
/* format and return the algs string */
|
333
|
return (implode(', ', $algs));
|
334
|
}
|
335
|
|
336
|
function get_cpu_crypto_support() {
|
337
|
global $g;
|
338
|
$machine = get_single_sysctl('hw.machine');
|
339
|
$QATIDS = array(INTEL_C2000_IQIA_PHYS, INTEL_C3K_QAT, INTEL_C3K_QAT_VF, INTEL_C620_QAT, INTEL_C620_QAT_VF,
|
340
|
INTEL_XEOND_QAT, INTEL_XEOND_QAT_VF, INTEL_DH895XCC_QAT, INTEL_DH895XCC_QAT_VF);
|
341
|
|
342
|
/* Defaults */
|
343
|
$crypto = crypto_accel_init();
|
344
|
|
345
|
switch ($machine) {
|
346
|
case 'amd64':
|
347
|
$fd = @fopen("{$g['varlog_path']}/dmesg.boot", "r");
|
348
|
while ($fd && !feof($fd)) {
|
349
|
$dmesgl = fgets($fd);
|
350
|
if (preg_match("/^ Features2.*AESNI/", $dmesgl, $matches)) {
|
351
|
$crypto = crypto_accel_set_flags($crypto, "AESNI", true, (is_module_loaded('aesni')) ? true : false);
|
352
|
}
|
353
|
if (preg_match("/^ Structured Extended Features.*SHA/", $dmesgl, $matches)) {
|
354
|
$crypto = crypto_accel_set_algs($crypto, "AESNI", AESNI_ALGS_SHA);
|
355
|
}
|
356
|
}
|
357
|
if ($fd) {
|
358
|
fclose($fd);
|
359
|
}
|
360
|
exec("/usr/sbin/pciconf -l -l | /usr/bin/awk '{ printf \"%s|0x%s%s\\n\", $1, $6, $5 }'", $pciids);
|
361
|
if (isset($pciids) && is_array($pciids)) {
|
362
|
foreach ($pciids as $pciid) {
|
363
|
list($devid, $pid) = explode('|', $pciid, 2);
|
364
|
if (in_array($pid, $QATIDS)) {
|
365
|
list($devname, $busloc) = explode('@', $devid);
|
366
|
$active = ((substr($devname, 0, 3) == 'qat') && is_module_loaded('qat'));
|
367
|
$crypto = crypto_accel_set_flags($crypto, "QAT", true, $active);
|
368
|
break;
|
369
|
}
|
370
|
}
|
371
|
}
|
372
|
break;
|
373
|
}
|
374
|
|
375
|
return ($crypto);
|
376
|
}
|
377
|
|
378
|
function get_cpu_crypto_string($crypto) {
|
379
|
$machine = get_single_sysctl('hw.machine');
|
380
|
$string = "";
|
381
|
|
382
|
switch ($machine) {
|
383
|
case 'amd64':
|
384
|
$string = "AES-NI CPU Crypto: ";
|
385
|
if ($crypto && crypto_accel_get($crypto, "AESNI", "present")) {
|
386
|
$string .= "Yes ";
|
387
|
$string .= crypto_accel_get($crypto, "AESNI", "enabled") ? "(active)" : "(inactive)";
|
388
|
} else {
|
389
|
$string .= "No";
|
390
|
}
|
391
|
$string .= "<br>\n";
|
392
|
$string .= "QAT Crypto: ";
|
393
|
if ($crypto && crypto_accel_get($crypto, "QAT", "present")) {
|
394
|
$string .= "Yes ";
|
395
|
$string .= crypto_accel_get($crypto, "QAT", "enabled") ? "(active)" : "(inactive)";
|
396
|
} else {
|
397
|
$string .= "No";
|
398
|
}
|
399
|
break;
|
400
|
}
|
401
|
|
402
|
if (strlen($string) == 0) {
|
403
|
$string = "CPU Crypto: None/Unknown Platform";
|
404
|
}
|
405
|
|
406
|
return ($string);
|
407
|
}
|
408
|
|
409
|
function get_cpu_count($show_detail = false) {
|
410
|
$cpucount = get_single_sysctl('kern.smp.cpus');
|
411
|
|
412
|
if ($show_detail) {
|
413
|
$cpudetail = "";
|
414
|
exec("/usr/bin/grep 'FreeBSD/SMP:.*package' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
|
415
|
$cpucount = $cpudetail[0];
|
416
|
}
|
417
|
return $cpucount;
|
418
|
}
|
419
|
|
420
|
function get_load_average() {
|
421
|
$load_average = "";
|
422
|
exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
|
423
|
return $load_average[0];
|
424
|
}
|
425
|
|
426
|
function get_interfacestats() {
|
427
|
global $config;
|
428
|
//build interface list for widget use
|
429
|
$ifdescrs = get_configured_interface_list();
|
430
|
|
431
|
$array_in_packets = array();
|
432
|
$array_out_packets = array();
|
433
|
$array_in_bytes = array();
|
434
|
$array_out_bytes = array();
|
435
|
$array_in_errors = array();
|
436
|
$array_out_errors = array();
|
437
|
$array_collisions = array();
|
438
|
$array_interrupt = array();
|
439
|
$new_data = "";
|
440
|
|
441
|
//build data arrays
|
442
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
443
|
$ifinfo = get_interface_info($ifdescr);
|
444
|
$new_data .= "{$ifinfo['inpkts']},";
|
445
|
$new_data .= "{$ifinfo['outpkts']},";
|
446
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
447
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
448
|
if (isset($ifinfo['inerrs'])) {
|
449
|
$new_data .= "{$ifinfo['inerrs']},";
|
450
|
$new_data .= "{$ifinfo['outerrs']},";
|
451
|
} else {
|
452
|
$new_data .= "0,";
|
453
|
$new_data .= "0,";
|
454
|
}
|
455
|
if (isset($ifinfo['collisions'])) {
|
456
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
457
|
} else {
|
458
|
$new_data .= "0,";
|
459
|
}
|
460
|
}//end for
|
461
|
|
462
|
return $new_data;
|
463
|
}
|
464
|
|
465
|
function get_interfacestatus() {
|
466
|
$data = "";
|
467
|
global $config;
|
468
|
|
469
|
//build interface list for widget use
|
470
|
$ifdescrs = get_configured_interface_with_descr();
|
471
|
|
472
|
foreach ($ifdescrs as $ifdescr => $ifname) {
|
473
|
$ifinfo = get_interface_info($ifdescr);
|
474
|
$data .= $ifname . "^";
|
475
|
if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
476
|
$data .= "up";
|
477
|
} else if ($ifinfo['status'] == "no carrier") {
|
478
|
$data .= "down";
|
479
|
} else if ($ifinfo['status'] == "down") {
|
480
|
$data .= "block";
|
481
|
}
|
482
|
$data .= "^";
|
483
|
if ($ifinfo['ipaddr']) {
|
484
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
|
485
|
}
|
486
|
$data .= "^";
|
487
|
if ($ifinfo['ipaddrv6']) {
|
488
|
$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
|
489
|
}
|
490
|
$data .= "^";
|
491
|
if ($ifinfo['status'] != "down") {
|
492
|
$data .= htmlspecialchars($ifinfo['media']);
|
493
|
}
|
494
|
|
495
|
$data .= "~";
|
496
|
|
497
|
}
|
498
|
return $data;
|
499
|
}
|
500
|
|
501
|
?>
|