Project

General

Profile

Download (12.8 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-2021 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
	$stats['cpu'] = (!in_array('cpu_usage', $sitems)) ? cpu_usage() : '|';
32
	$stats['mem'] = (!in_array('memory_usage', $sitems)) ? mem_usage() : '';
33
	$stats['uptime'] = (!in_array('uptime', $sitems)) ? get_uptime() : '';
34
	$stats['states'] = (!in_array('state_table_size', $sitems)) ? get_pfstate() : '';
35
	$stats['temp'] = (!in_array('temperature', $sitems)) ? get_temp() : '';
36
	$stats['datetime'] = (!in_array('current_datetime', $sitems)) ? update_date_time() : '';
37
	$stats['cpufreq'] = (!in_array('cpu_type', $sitems)) ? get_cpufreq() : '';
38
	$stats['load_average'] = (!in_array('load_average', $sitems)) ? get_load_average() : '';
39
	if (!in_array('mbuf_usage', $sitems)) {
40
		get_mbuf($stats['mbuf'], $stats['mbufpercent']);
41
	} else {
42
		$stats['mbuf'] = '';
43
		$stats['mbufpercent'] = '';
44
	}
45
	$stats['statepercent'] = (!in_array('state_table_size', $sitems)) ? get_pfstate(true) : '';
46
	$stats = join("|", $stats);
47
	return $stats;
48
}
49

    
50
function get_uptime() {
51
	$uptime = get_uptime_sec();
52

    
53
	if (intval($uptime) == 0) {
54
		return;
55
	}
56

    
57
	$updays = (int)($uptime / 86400);
58
	$uptime %= 86400;
59
	$uphours = (int)($uptime / 3600);
60
	$uptime %= 3600;
61
	$upmins = (int)($uptime / 60);
62
	$uptime %= 60;
63
	$upsecs = (int)($uptime);
64

    
65
	$uptimestr = "";
66
	if ($updays > 1) {
67
		$uptimestr .= "$updays Days ";
68
	} else if ($updays > 0) {
69
		$uptimestr .= "1 Day ";
70
	}
71

    
72
	if ($uphours > 1) {
73
		$hours = "s";
74
	}
75

    
76
	if ($upmins > 1) {
77
		$minutes = "s";
78
	}
79

    
80
	if ($upmins > 1) {
81
		$seconds = "s";
82
	}
83

    
84
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
85
	return $uptimestr;
86
}
87

    
88
// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that
89
function cpu_usage() {
90

    
91
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
92
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
93

    
94
	return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
95
}
96

    
97
function get_pfstate($percent=false) {
98
	global $config;
99
	$matches = "";
100
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
101
		$maxstates="{$config['system']['maximumstates']}";
102
	} else {
103
		$maxstates=pfsense_default_state_size();
104
	}
105
	$curentries = `/sbin/pfctl -si |grep current`;
106
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
107
		$curentries = $matches[1];
108
	}
109
	if (!is_numeric($curentries)) {
110
		$curentries = 0;
111
	}
112
	if ($percent) {
113
		if (intval($maxstates) > 0) {
114
			return round(($curentries / $maxstates) * 100, 0);
115
		} else {
116
			return "NA";
117
		}
118
	} else {
119
		return $curentries . "/" . $maxstates;
120
	}
121
}
122

    
123
function has_temp() {
124
	/* no known temp monitors available at present */
125

    
126
	/* should only reach here if there is no hardware monitor */
127
	return false;
128
}
129

    
130
function get_hwtype() {
131
	return;
132
}
133

    
134
function get_mbuf(&$mbuf, &$mbufpercent) {
135
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
136
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
137
	$mbuf = "{$mbufs_total}/{$mbufs_max}";
138
	$mbufpercent = ($mbufs_max > 0) ? round(($mbufs_total / $mbufs_max) * 100, 0) : "NA";
139
}
140

    
141
function get_temp() {
142
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
143
	if ($temp_out == "") {
144
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
145
	}
146

    
147
	// Remove 'C' from the end and spaces
148
	$temp_out = trim(rtrim($temp_out, 'C'));
149

    
150
	if ($temp_out[0] == '-') {
151
		return '';
152
	}
153

    
154
	return $temp_out;
155
}
156

    
157
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
158
function get_mounted_filesystems() {
159
	$mout = "";
160
	$filesystems = array();
161
	exec("/bin/df -Tht ufs,zfs,tmpfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
162

    
163
	/* Get rid of the header */
164
	array_shift($mout);
165
	foreach ($mout as $fs) {
166
		$f = array();
167
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
168

    
169
		/* We dont' want the trailing % sign. */
170
		$f['percent_used'] = trim($f['percent_used'], '%');
171

    
172
		$filesystems[] = $f;
173
	}
174
	return $filesystems;
175
}
176

    
177
function disk_usage($slice = '/') {
178
	$dfout = "";
179
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
180
	$diskusage = trim($dfout[0]);
181

    
182
	return $diskusage;
183
}
184

    
185
function swap_usage() {
186
	exec("/usr/sbin/swapinfo | /usr/bin/tail -1", $swap_info);
187
	$swap_used = "";
188
	foreach ($swap_info as $line) {
189
		if (preg_match('/(\d+)%$/', $line, $matches)) {
190
			$swap_used = $matches[1];
191
			break;
192
		}
193
	}
194

    
195
	return $swap_used;
196
}
197

    
198
function mem_usage() {
199
	$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
200
	if ($totalMem > 0) {
201
		$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
202
		$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
203
		$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
204
		$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
205
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
206
	} else {
207
		$memUsage = "NA";
208
	}
209

    
210
	return $memUsage;
211
}
212

    
213
function update_date_time() {
214
	$datetime = date("D M j G:i:s T Y");
215
	return $datetime;
216
}
217

    
218
function get_cpufreq() {
219
	$cpufreqs = "";
220
	$out = "";
221
	$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
222
	$maxfreq = explode("/", $cpufreqs[0]);
223
	$maxfreq = $maxfreq[0];
224
	$curfreq = "";
225
	$curfreq = get_single_sysctl('dev.cpu.0.freq');
226
	if (($curfreq > 0) && ($curfreq != $maxfreq)) {
227
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
228
	}
229
	return $out;
230
}
231

    
232
define("INTEL_C2000_IQIA_PHYS", "0x1f188086");
233
define("INTEL_C3K_QAT", "0x19e28086");
234
define("INTEL_C3K_QAT_VF", "0x19e38086");
235
define("INTEL_C620_QAT", "0x37c88086");
236
define("INTEL_C620_QAT_VF", "0x37c98086");
237
define("INTEL_XEOND_QAT", "0x6f548086");
238
define("INTEL_XEOND_QAT_VF", "0x6f558086");
239
define("INTEL_DH895XCC_QAT", "0x04358086");
240
define("INTEL_DH895XCC_QAT_VF", "0x04438086");
241
define("AESNI_ALGS", "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS");
242
define("AESNI_ALGS_SHA", "SHA1,SHA256");
243
define("QAT_ALGS", "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS,SHA1,SHA256,SHA384,SHA512");
244

    
245
function crypto_accel_new($name = "", $algs = "") {
246
	return (array("name" => $name, "present" => false, "enabled" => false, "algs" => explode(",", $algs)));
247
}
248

    
249
function crypto_accel_init() {
250
	$machine = get_single_sysctl('hw.machine');
251

    
252
	/* Defaults */
253
	$crypto = array();
254
	$crypto["accel"] = array();
255

    
256
	switch ($machine) {
257
	case 'amd64':
258
		$crypto["accel"][] = crypto_accel_new("AESNI", AESNI_ALGS);
259
		$crypto["accel"][] = crypto_accel_new("QAT", QAT_ALGS);
260
		break;
261
	}
262

    
263
	return ($crypto);
264
}
265

    
266
function crypto_accel_set_flags($crypto, $name, $present = false, $enabled = false) {
267

    
268
	foreach ($crypto["accel"] as $id => &$accel) {
269
		if ($accel["name"] != $name) {
270
			continue;
271
		}
272
		$accel["present"] = $present;
273
		$accel["enabled"] = $enabled;
274
	}
275

    
276
	return ($crypto);
277
}
278

    
279
function crypto_accel_get($crypto, $name, $key) {
280

    
281
	foreach ($crypto["accel"] as $id => $accel) {
282
		if ($accel["name"] != $name) {
283
			continue;
284
		}
285
                return ($accel[$key]);
286
	}
287

    
288
	return ("");
289
}
290

    
291
function crypto_accel_set_algs($crypto, $name, $algs) {
292

    
293
	foreach ($crypto["accel"] as $id => &$accel) {
294
		if ($accel["name"] != $name) {
295
			continue;
296
		}
297
		$a = explode(",", $algs);
298
		$m = array_merge($accel["algs"], $a);
299
		$accel["algs"] = array_unique($m, SORT_STRING);
300
	}
301

    
302
	return ($crypto);
303
}
304

    
305
function crypto_accel_get_algs($crypto) {
306
	$algs = array();
307
	$algs_str = "";
308

    
309
	foreach ($crypto["accel"] as $id => $accel) {
310
		if (!$accel["present"] || !$accel["enabled"]) {
311
			continue;
312
		}
313
		$algs = array_merge($accel["algs"], $algs);
314
	}
315
	foreach (array_unique($algs, SORT_STRING) as $id => $alg) {
316
		if (strlen($algs_str) > 0) {
317
			$algs_str .= ",";
318
		}
319
		$algs_str .= $alg;
320
	}
321

    
322
	return ($algs_str);
323
}
324

    
325

    
326
function get_cpu_crypto_support() {
327
	global $g;
328
	$machine = get_single_sysctl('hw.machine');
329
	$QATIDS = array(INTEL_C2000_IQIA_PHYS, INTEL_C3K_QAT, INTEL_C3K_QAT_VF, INTEL_C620_QAT, INTEL_C620_QAT_VF,
330
			INTEL_XEOND_QAT, INTEL_XEOND_QAT_VF, INTEL_DH895XCC_QAT, INTEL_DH895XCC_QAT_VF);
331

    
332
	/* Defaults */
333
	$crypto = crypto_accel_init();
334

    
335
	switch ($machine) {
336
	case 'amd64':
337
		$fd = @fopen("{$g['varlog_path']}/dmesg.boot", "r");
338
		while ($fd && !feof($fd)) {
339
			$dmesgl = fgets($fd);
340
			if (preg_match("/^  Features2.*AESNI/", $dmesgl, $matches)) {
341
				$crypto = crypto_accel_set_flags($crypto, "AESNI", true, (is_module_loaded('aesni')) ? true : false);
342
			}
343
			if (preg_match("/^  Structured Extended Features.*SHA/", $dmesgl, $matches)) {
344
				$crypto = crypto_accel_set_algs($crypto, "AESNI", AESNI_ALGS_SHA);
345
			}
346
		}
347
		if ($fd) {
348
			fclose($fd);
349
		}
350
		exec("/usr/sbin/pciconf -l | /usr/bin/awk '{ printf \"%s\\n\", $4 }' | /usr/bin/cut -f2 -d=", $pciids);
351
		if (isset($pciids) && is_array($pciids)) {
352
			foreach ($pciids as $id => $pciid) {
353
				if (in_array($pciid, $QATIDS)) {
354
					$crypto = crypto_accel_set_flags($crypto, "QAT", true, (is_module_loaded('qat')) ? true : false);
355
					break;
356
				}
357
			}
358
		}
359
		break;
360
	}
361

    
362
	return ($crypto);
363
}
364

    
365
function get_cpu_crypto_string($crypto) {
366
	$machine = get_single_sysctl('hw.machine');
367
	$string = "";
368

    
369
	switch ($machine) {
370
	case 'amd64':
371
		$string = "AES-NI CPU Crypto: ";
372
		if ($crypto && crypto_accel_get($crypto, "AESNI", "present")) {
373
			$string .= "Yes ";
374
			$string .= crypto_accel_get($crypto, "AESNI", "enabled") ? "(active)" : "(inactive)";
375
		} else {
376
			$string .= "No";
377
		}
378
		$string .= "<br>\n";
379
		$string .= "QAT Crypto: ";
380
		if ($crypto && crypto_accel_get($crypto, "QAT", "present")) {
381
			$string .= "Yes ";
382
			$string .= crypto_accel_get($crypto, "QAT", "enabled") ? "(active)" : "(inactive)";
383
		} else {
384
			$string .= "No";
385
		}
386
		break;
387
	}
388

    
389
	if (strlen($string) == 0) {
390
		$string = "CPU Crypto: None/Unknown Platform";
391
	}
392

    
393
	return ($string);
394
}
395

    
396
function get_cpu_count($show_detail = false) {
397
	$cpucount = get_single_sysctl('kern.smp.cpus');
398

    
399
	if ($show_detail) {
400
		$cpudetail = "";
401
		exec("/usr/bin/grep 'FreeBSD/SMP:.*package' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
402
		$cpucount = $cpudetail[0];
403
	}
404
	return $cpucount;
405
}
406

    
407
function get_load_average() {
408
	$load_average = "";
409
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
410
	return $load_average[0];
411
}
412

    
413
function get_interfacestats() {
414
	global $config;
415
	//build interface list for widget use
416
	$ifdescrs = get_configured_interface_list();
417

    
418
	$array_in_packets = array();
419
	$array_out_packets = array();
420
	$array_in_bytes = array();
421
	$array_out_bytes = array();
422
	$array_in_errors = array();
423
	$array_out_errors = array();
424
	$array_collisions = array();
425
	$array_interrupt = array();
426
	$new_data = "";
427

    
428
	//build data arrays
429
	foreach ($ifdescrs as $ifdescr => $ifname) {
430
		$ifinfo = get_interface_info($ifdescr);
431
		$new_data .= "{$ifinfo['inpkts']},";
432
		$new_data .= "{$ifinfo['outpkts']},";
433
		$new_data .= format_bytes($ifinfo['inbytes']) . ",";
434
		$new_data .= format_bytes($ifinfo['outbytes']) . ",";
435
		if (isset($ifinfo['inerrs'])) {
436
			$new_data .= "{$ifinfo['inerrs']},";
437
			$new_data .= "{$ifinfo['outerrs']},";
438
		} else {
439
			$new_data .= "0,";
440
			$new_data .= "0,";
441
		}
442
		if (isset($ifinfo['collisions'])) {
443
			$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
444
		} else {
445
			$new_data .= "0,";
446
		}
447
	}//end for
448

    
449
	return $new_data;
450
}
451

    
452
function get_interfacestatus() {
453
	$data = "";
454
	global $config;
455

    
456
	//build interface list for widget use
457
	$ifdescrs = get_configured_interface_with_descr();
458

    
459
	foreach ($ifdescrs as $ifdescr => $ifname) {
460
		$ifinfo = get_interface_info($ifdescr);
461
		$data .= $ifname . "^";
462
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
463
			$data .= "up";
464
		} else if ($ifinfo['status'] == "no carrier") {
465
			$data .= "down";
466
		} else if ($ifinfo['status'] == "down") {
467
			$data .= "block";
468
		}
469
		$data .= "^";
470
		if ($ifinfo['ipaddr']) {
471
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
472
		}
473
		$data .= "^";
474
		if ($ifinfo['ipaddrv6']) {
475
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
476
		}
477
		$data .= "^";
478
		if ($ifinfo['status'] != "down") {
479
			$data .= htmlspecialchars($ifinfo['media']);
480
		}
481

    
482
		$data .= "~";
483

    
484
	}
485
	return $data;
486
}
487

    
488
?>
    (1-1/1)