Project

General

Profile

Download (13.2 KB) Statistics
| Branch: | Tag: | Revision:
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 = array();
312
	$algs_str = "";
313

    
314
	foreach ($crypto["accel"] as $accel) {
315
		if (!$accel["present"] || !$accel["enabled"]) {
316
			continue;
317
		}
318
		$algs = array_merge($accel["algs"], $algs);
319
	}
320
	foreach (array_unique($algs, SORT_STRING) as $alg) {
321
		if (strlen($algs_str) > 0) {
322
			$algs_str .= ",";
323
		}
324
		$algs_str .= $alg;
325
	}
326

    
327
	return (!empty($algs_str) ? $algs_str : gettext("Inactive"));
328
}
329

    
330

    
331
function get_cpu_crypto_support() {
332
	global $g;
333
	$machine = get_single_sysctl('hw.machine');
334
	$QATIDS = array(INTEL_C2000_IQIA_PHYS, INTEL_C3K_QAT, INTEL_C3K_QAT_VF, INTEL_C620_QAT, INTEL_C620_QAT_VF,
335
			INTEL_XEOND_QAT, INTEL_XEOND_QAT_VF, INTEL_DH895XCC_QAT, INTEL_DH895XCC_QAT_VF);
336

    
337
	/* Defaults */
338
	$crypto = crypto_accel_init();
339

    
340
	switch ($machine) {
341
	case 'amd64':
342
		$fd = @fopen("{$g['varlog_path']}/dmesg.boot", "r");
343
		while ($fd && !feof($fd)) {
344
			$dmesgl = fgets($fd);
345
			if (preg_match("/^  Features2.*AESNI/", $dmesgl, $matches)) {
346
				$crypto = crypto_accel_set_flags($crypto, "AESNI", true, (is_module_loaded('aesni')) ? true : false);
347
			}
348
			if (preg_match("/^  Structured Extended Features.*SHA/", $dmesgl, $matches)) {
349
				$crypto = crypto_accel_set_algs($crypto, "AESNI", AESNI_ALGS_SHA);
350
			}
351
		}
352
		if ($fd) {
353
			fclose($fd);
354
		}
355
		exec("/usr/sbin/pciconf -l -l | /usr/bin/awk '{ printf \"%s|0x%s%s\\n\", $1, $6, $5 }'", $pciids);
356
		if (isset($pciids) && is_array($pciids)) {
357
			foreach ($pciids as $pciid) {
358
				list($devid, $pid) = explode('|', $pciid, 2);
359
				if (in_array($pid, $QATIDS)) {
360
					list($devname, $busloc) = explode('@', $devid);
361
					$active = ((substr($devname, 0, 3) == 'qat') && is_module_loaded('qat'));
362
					$crypto = crypto_accel_set_flags($crypto, "QAT", true, $active);
363
					break;
364
				}
365
			}
366
		}
367
		break;
368
	}
369

    
370
	return ($crypto);
371
}
372

    
373
function get_cpu_crypto_string($crypto) {
374
	$machine = get_single_sysctl('hw.machine');
375
	$string = "";
376

    
377
	switch ($machine) {
378
	case 'amd64':
379
		$string = "AES-NI CPU Crypto: ";
380
		if ($crypto && crypto_accel_get($crypto, "AESNI", "present")) {
381
			$string .= "Yes ";
382
			$string .= crypto_accel_get($crypto, "AESNI", "enabled") ? "(active)" : "(inactive)";
383
		} else {
384
			$string .= "No";
385
		}
386
		$string .= "<br>\n";
387
		$string .= "QAT Crypto: ";
388
		if ($crypto && crypto_accel_get($crypto, "QAT", "present")) {
389
			$string .= "Yes ";
390
			$string .= crypto_accel_get($crypto, "QAT", "enabled") ? "(active)" : "(inactive)";
391
		} else {
392
			$string .= "No";
393
		}
394
		break;
395
	}
396

    
397
	if (strlen($string) == 0) {
398
		$string = "CPU Crypto: None/Unknown Platform";
399
	}
400

    
401
	return ($string);
402
}
403

    
404
function get_cpu_count($show_detail = false) {
405
	$cpucount = get_single_sysctl('kern.smp.cpus');
406

    
407
	if ($show_detail) {
408
		$cpudetail = "";
409
		exec("/usr/bin/grep 'FreeBSD/SMP:.*package' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
410
		$cpucount = $cpudetail[0];
411
	}
412
	return $cpucount;
413
}
414

    
415
function get_load_average() {
416
	$load_average = "";
417
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
418
	return $load_average[0];
419
}
420

    
421
function get_interfacestats() {
422
	global $config;
423
	//build interface list for widget use
424
	$ifdescrs = get_configured_interface_list();
425

    
426
	$array_in_packets = array();
427
	$array_out_packets = array();
428
	$array_in_bytes = array();
429
	$array_out_bytes = array();
430
	$array_in_errors = array();
431
	$array_out_errors = array();
432
	$array_collisions = array();
433
	$array_interrupt = array();
434
	$new_data = "";
435

    
436
	//build data arrays
437
	foreach ($ifdescrs as $ifdescr => $ifname) {
438
		$ifinfo = get_interface_info($ifdescr);
439
		$new_data .= "{$ifinfo['inpkts']},";
440
		$new_data .= "{$ifinfo['outpkts']},";
441
		$new_data .= format_bytes($ifinfo['inbytes']) . ",";
442
		$new_data .= format_bytes($ifinfo['outbytes']) . ",";
443
		if (isset($ifinfo['inerrs'])) {
444
			$new_data .= "{$ifinfo['inerrs']},";
445
			$new_data .= "{$ifinfo['outerrs']},";
446
		} else {
447
			$new_data .= "0,";
448
			$new_data .= "0,";
449
		}
450
		if (isset($ifinfo['collisions'])) {
451
			$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
452
		} else {
453
			$new_data .= "0,";
454
		}
455
	}//end for
456

    
457
	return $new_data;
458
}
459

    
460
function get_interfacestatus() {
461
	$data = "";
462
	global $config;
463

    
464
	//build interface list for widget use
465
	$ifdescrs = get_configured_interface_with_descr();
466

    
467
	foreach ($ifdescrs as $ifdescr => $ifname) {
468
		$ifinfo = get_interface_info($ifdescr);
469
		$data .= $ifname . "^";
470
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
471
			$data .= "up";
472
		} else if ($ifinfo['status'] == "no carrier") {
473
			$data .= "down";
474
		} else if ($ifinfo['status'] == "down") {
475
			$data .= "block";
476
		}
477
		$data .= "^";
478
		if ($ifinfo['ipaddr']) {
479
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
480
		}
481
		$data .= "^";
482
		if ($ifinfo['ipaddrv6']) {
483
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
484
		}
485
		$data .= "^";
486
		if ($ifinfo['status'] != "down") {
487
			$data .= htmlspecialchars($ifinfo['media']);
488
		}
489

    
490
		$data .= "~";
491

    
492
	}
493
	return $data;
494
}
495

    
496
?>
    (1-1/1)