Project

General

Profile

Download (9.19 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() {
31
	$stats['cpu'] = cpu_usage();
32
	$stats['mem'] = mem_usage();
33
	$stats['uptime'] = get_uptime();
34
	$stats['states'] = get_pfstate();
35
	$stats['temp'] = get_temp();
36
	$stats['datetime'] = update_date_time();
37
	$stats['cpufreq'] = get_cpufreq();
38
	$stats['load_average'] = get_load_average();
39
	get_mbuf($stats['mbuf'], $stats['mbufpercent']);
40
	$stats['statepercent'] = get_pfstate(true);
41
	$stats = join("|", $stats);
42
	return $stats;
43
}
44

    
45
function get_uptime() {
46
	$uptime = get_uptime_sec();
47

    
48
	if (intval($uptime) == 0) {
49
		return;
50
	}
51

    
52
	$updays = (int)($uptime / 86400);
53
	$uptime %= 86400;
54
	$uphours = (int)($uptime / 3600);
55
	$uptime %= 3600;
56
	$upmins = (int)($uptime / 60);
57
	$uptime %= 60;
58
	$upsecs = (int)($uptime);
59

    
60
	$uptimestr = "";
61
	if ($updays > 1) {
62
		$uptimestr .= "$updays Days ";
63
	} else if ($updays > 0) {
64
		$uptimestr .= "1 Day ";
65
	}
66

    
67
	if ($uphours > 1) {
68
		$hours = "s";
69
	}
70

    
71
	if ($upmins > 1) {
72
		$minutes = "s";
73
	}
74

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

    
79
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
80
	return $uptimestr;
81
}
82

    
83
// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that
84
function cpu_usage() {
85

    
86
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
87
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
88

    
89
	return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
90
}
91

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

    
118
function has_temp() {
119
	/* no known temp monitors available at present */
120

    
121
	/* should only reach here if there is no hardware monitor */
122
	return false;
123
}
124

    
125
function get_hwtype() {
126
	return;
127
}
128

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

    
136
function get_temp() {
137
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
138
	if ($temp_out == "") {
139
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
140
	}
141

    
142
	// Remove 'C' from the end and spaces
143
	$temp_out = trim(rtrim($temp_out, 'C'));
144

    
145
	if ($temp_out[0] == '-') {
146
		return '';
147
	}
148

    
149
	return $temp_out;
150
}
151

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

    
158
	/* Get rid of the header */
159
	array_shift($mout);
160
	foreach ($mout as $fs) {
161
		$f = array();
162
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
163

    
164
		/* We dont' want the trailing % sign. */
165
		$f['percent_used'] = trim($f['percent_used'], '%');
166

    
167
		$filesystems[] = $f;
168
	}
169
	return $filesystems;
170
}
171

    
172
function disk_usage($slice = '/') {
173
	$dfout = "";
174
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
175
	$diskusage = trim($dfout[0]);
176

    
177
	return $diskusage;
178
}
179

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

    
190
	return $swap_used;
191
}
192

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

    
205
	return $memUsage;
206
}
207

    
208
function update_date_time() {
209
	$datetime = date("D M j G:i:s T Y");
210
	return $datetime;
211
}
212

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

    
227
function get_cpu_crypto_support() {
228
	$machine = get_single_sysctl('hw.machine');
229
	$accelerated_arm_platforms = array('am335x');
230
	$cpucrypto_type = "";
231

    
232
	switch ($machine) {
233
	case 'amd64':
234
		$cpucrypto_type = "AES-NI CPU Crypto: ";
235
		exec("/usr/bin/grep -c '  Features.*AESNI' /var/log/dmesg.boot", $cpucrypto_present);
236
		if ($cpucrypto_present[0] > 0) {
237
			$cpucrypto_type .= "Yes ";
238
			$cpucrypto_type .= (is_module_loaded('aesni')) ? "(active)" : "(inactive)";
239
		} else {
240
			$cpucrypto_type .= "No";
241
		}
242
		break;
243
	case 'arm':
244
		$armplatform = get_single_sysctl('hw.platform');
245
		if (in_array($armplatform, $accelerated_arm_platforms)) {
246
		/* No drivers yet, so mark inactive! */
247
			$cpucrypto_type = "{$armplatform} built-in CPU Crypto (inactive)";
248
			break;
249
		}
250
		$armmv = get_single_sysctl('hw.mv_soc_model');
251
		if (strpos($armmv, "Marvell 88F682") != 0) {
252
			$cpucrypto_type = "Crypto: ". get_single_sysctl('dev.cesa.0.%desc');
253
		}
254
		break;
255
	default:
256
		/* Unknown/unidentified platform */
257
	}
258

    
259
	if (!empty($cpucrypto_type)) {
260
		return $cpucrypto_type;
261
	}
262

    
263
	return "CPU Crypto: None/Unknown Platform";
264
}
265

    
266
function get_cpu_count($show_detail = false) {
267
	$cpucount = get_single_sysctl('kern.smp.cpus');
268

    
269
	if ($show_detail) {
270
		$cpudetail = "";
271
		exec("/usr/bin/grep 'FreeBSD/SMP:.*package' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
272
		$cpucount = $cpudetail[0];
273
	}
274
	return $cpucount;
275
}
276

    
277
function get_load_average() {
278
	$load_average = "";
279
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
280
	return $load_average[0];
281
}
282

    
283
function get_interfacestats() {
284
	global $config;
285
	//build interface list for widget use
286
	$ifdescrs = get_configured_interface_list();
287

    
288
	$array_in_packets = array();
289
	$array_out_packets = array();
290
	$array_in_bytes = array();
291
	$array_out_bytes = array();
292
	$array_in_errors = array();
293
	$array_out_errors = array();
294
	$array_collisions = array();
295
	$array_interrupt = array();
296
	$new_data = "";
297

    
298
	//build data arrays
299
	foreach ($ifdescrs as $ifdescr => $ifname) {
300
		$ifinfo = get_interface_info($ifdescr);
301
		$new_data .= "{$ifinfo['inpkts']},";
302
		$new_data .= "{$ifinfo['outpkts']},";
303
		$new_data .= format_bytes($ifinfo['inbytes']) . ",";
304
		$new_data .= format_bytes($ifinfo['outbytes']) . ",";
305
		if (isset($ifinfo['inerrs'])) {
306
			$new_data .= "{$ifinfo['inerrs']},";
307
			$new_data .= "{$ifinfo['outerrs']},";
308
		} else {
309
			$new_data .= "0,";
310
			$new_data .= "0,";
311
		}
312
		if (isset($ifinfo['collisions'])) {
313
			$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
314
		} else {
315
			$new_data .= "0,";
316
		}
317
	}//end for
318

    
319
	return $new_data;
320
}
321

    
322
function get_interfacestatus() {
323
	$data = "";
324
	global $config;
325

    
326
	//build interface list for widget use
327
	$ifdescrs = get_configured_interface_with_descr();
328

    
329
	foreach ($ifdescrs as $ifdescr => $ifname) {
330
		$ifinfo = get_interface_info($ifdescr);
331
		$data .= $ifname . "^";
332
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
333
			$data .= "up";
334
		} else if ($ifinfo['status'] == "no carrier") {
335
			$data .= "down";
336
		} else if ($ifinfo['status'] == "down") {
337
			$data .= "block";
338
		}
339
		$data .= "^";
340
		if ($ifinfo['ipaddr']) {
341
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
342
		}
343
		$data .= "^";
344
		if ($ifinfo['ipaddrv6']) {
345
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
346
		}
347
		$data .= "^";
348
		if ($ifinfo['status'] != "down") {
349
			$data .= htmlspecialchars($ifinfo['media']);
350
		}
351

    
352
		$data .= "~";
353

    
354
	}
355
	return $data;
356
}
357

    
358
?>
    (1-1/1)