Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Tag: | Revision:
1 e9258698 NewEraCracker
<?php
2 7ac5a4cb Scott Ullrich
/*
3 8acd654a Renato Botelho
 * functions.inc.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2013-2016 Electric Sheep Fencing, LLC
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgment:
22
 *    "This product includes software developed by the pfSense Project
23
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
 *
25
 * 4. The names "pfSense" and "pfSense Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    coreteam@pfsense.org.
29
 *
30
 * 5. Products derived from this software may not be called "pfSense"
31
 *    nor may "pfSense" appear in their names without prior written
32
 *    permission of the Electric Sheep Fencing, LLC.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *
37
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
52
 */
53 d6ad908c Scott Ullrich
54 f3ec0487 Phil Davis
if (Connection_Aborted()) {
55 d6ad908c Scott Ullrich
	exit;
56
}
57 d772ac32 Erik Kristensen
58 2fc0b1c7 Scott Ullrich
require_once("config.inc");
59 806e5979 Phil Davis
require_once("pfsense-utils.inc");
60 65d4de2e Scott Dale
61 2fc0b1c7 Scott Ullrich
function get_stats() {
62
	$stats['cpu'] = cpu_usage();
63
	$stats['mem'] = mem_usage();
64
	$stats['uptime'] = get_uptime();
65
	$stats['states'] = get_pfstate();
66
	$stats['temp'] = get_temp();
67 65d4de2e Scott Dale
	$stats['datetime'] = update_date_time();
68
	$stats['interfacestatistics'] = get_interfacestats();
69
	$stats['interfacestatus'] = get_interfacestatus();
70 4dedd18a Cristian Feldman
	$stats['cpufreq'] = get_cpufreq();
71 25a46a3c Cristian Feldman
	$stats['load_average'] = get_load_average();
72 8ff9cc38 jim-p
	$stats['mbuf'] = get_mbuf();
73 2cb760da jim-p
	$stats['mbufpercent'] = get_mbuf(true);
74 4a83831c jim-p
	$stats['statepercent'] = get_pfstate(true);
75 2fc0b1c7 Scott Ullrich
	$stats = join("|", $stats);
76
	return $stats;
77
}
78 477361c7 Erik Kristensen
79 d772ac32 Erik Kristensen
function get_uptime() {
80 806e5979 Phil Davis
	$uptime = get_uptime_sec();
81 d772ac32 Erik Kristensen
82 f3ec0487 Phil Davis
	if (intval($uptime) == 0) {
83 c7b8b46a Scott Ullrich
		return;
84 f3ec0487 Phil Davis
	}
85 c7b8b46a Scott Ullrich
86 d772ac32 Erik Kristensen
	$updays = (int)($uptime / 86400);
87
	$uptime %= 86400;
88
	$uphours = (int)($uptime / 3600);
89
	$uptime %= 3600;
90
	$upmins = (int)($uptime / 60);
91 af0b57cb Renato Botelho
	$uptime %= 60;
92 54f818b1 Joecowboy
	$upsecs = (int)($uptime);
93 d772ac32 Erik Kristensen
94
	$uptimestr = "";
95 f3ec0487 Phil Davis
	if ($updays > 1) {
96 54f818b1 Joecowboy
		$uptimestr .= "$updays Days ";
97 f3ec0487 Phil Davis
	} else if ($updays > 0) {
98 54f818b1 Joecowboy
		$uptimestr .= "1 Day ";
99 f3ec0487 Phil Davis
	}
100 54f818b1 Joecowboy
101 f3ec0487 Phil Davis
	if ($uphours > 1) {
102 af0b57cb Renato Botelho
		$hours = "s";
103 f3ec0487 Phil Davis
	}
104 af0b57cb Renato Botelho
105 f3ec0487 Phil Davis
	if ($upmins > 1) {
106 af0b57cb Renato Botelho
		$minutes = "s";
107 f3ec0487 Phil Davis
	}
108 af0b57cb Renato Botelho
109 f3ec0487 Phil Davis
	if ($upmins > 1) {
110 af0b57cb Renato Botelho
		$seconds = "s";
111 f3ec0487 Phil Davis
	}
112 af0b57cb Renato Botelho
113 b10268ca Joecowboy
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
114 d772ac32 Erik Kristensen
	return $uptimestr;
115
}
116
117 e637075a Bill Marquette
/* Calculates non-idle CPU time and returns as a percentage */
118 4fe00253 Espen Johansen
function cpu_usage() {
119 4a1cd18f Bill Marquette
	$duration = 1;
120
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
121 971de1f9 Renato Botelho
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
122 4a1cd18f Bill Marquette
	sleep($duration);
123 971de1f9 Renato Botelho
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
124 af0b57cb Renato Botelho
125 4a1cd18f Bill Marquette
	$totalStart = array_sum($cpuTicks);
126
	$totalEnd = array_sum($cpuTicks2);
127
128
	// Something wrapped ?!?!
129 f3ec0487 Phil Davis
	if ($totalEnd <= $totalStart) {
130 4a1cd18f Bill Marquette
		return 0;
131 f3ec0487 Phil Davis
	}
132 4a1cd18f Bill Marquette
133
	// Calculate total cycles used
134
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
135 65d4de2e Scott Dale
136 4a1cd18f Bill Marquette
	// Calculate the percentage used
137
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
138 af0b57cb Renato Botelho
139 d772ac32 Erik Kristensen
	return $cpuUsage;
140
}
141
142 4a83831c jim-p
function get_pfstate($percent=false) {
143 d772ac32 Erik Kristensen
	global $config;
144 767a716e Scott Ullrich
	$matches = "";
145 f3ec0487 Phil Davis
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
146 af0b57cb Renato Botelho
		$maxstates="{$config['system']['maximumstates']}";
147 f3ec0487 Phil Davis
	} else {
148 af0b57cb Renato Botelho
		$maxstates=pfsense_default_state_size();
149 f3ec0487 Phil Davis
	}
150 767a716e Scott Ullrich
	$curentries = `/sbin/pfctl -si |grep current`;
151
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
152 af0b57cb Renato Botelho
		$curentries = $matches[1];
153 767a716e Scott Ullrich
	}
154 f3ec0487 Phil Davis
	if (!is_numeric($curentries)) {
155 66cc4d43 Ermal
		$curentries = 0;
156 f3ec0487 Phil Davis
	}
157
	if ($percent) {
158
		if (intval($maxstates) > 0) {
159 15183bcb phildd
			return round(($curentries / $maxstates) * 100, 0);
160 f3ec0487 Phil Davis
		} else {
161 15183bcb phildd
			return "NA";
162 f3ec0487 Phil Davis
		}
163
	} else {
164 4a83831c jim-p
		return $curentries . "/" . $maxstates;
165 f3ec0487 Phil Davis
	}
166 d772ac32 Erik Kristensen
}
167
168 7979a809 Bill Marquette
function has_temp() {
169 19604f5d Scott Ullrich
	/* no known temp monitors available at present */
170 af0b57cb Renato Botelho
171 7979a809 Bill Marquette
	/* should only reach here if there is no hardware monitor */
172
	return false;
173
}
174
175
function get_hwtype() {
176
	return;
177
}
178
179 2cb760da jim-p
function get_mbuf($percent=false) {
180 2c7f71d9 jim-p
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
181 f3ec0487 Phil Davis
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
182
	if ($percent) {
183
		if ($mbufs_max > 0) {
184 15183bcb phildd
			return round(($mbufs_total / $mbufs_max) * 100, 0);
185 f3ec0487 Phil Davis
		} else {
186 15183bcb phildd
			return "NA";
187 f3ec0487 Phil Davis
		}
188
	} else {
189 2cb760da jim-p
		return "{$mbufs_total}/{$mbufs_max}";
190 f3ec0487 Phil Davis
	}
191 8ff9cc38 jim-p
}
192
193 7979a809 Bill Marquette
function get_temp() {
194 971de1f9 Renato Botelho
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
195 f3ec0487 Phil Davis
	if ($temp_out == "") {
196 971de1f9 Renato Botelho
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
197 f3ec0487 Phil Davis
	}
198 af0b57cb Renato Botelho
199 8822be67 Renato Botelho
	// Remove 'C' from the end and spaces
200
	$temp_out = trim(rtrim($temp_out, 'C'));
201
202
	if ($temp_out[0] == '-') {
203
		return '';
204
	}
205
206
	return $temp_out;
207 1804023d Bill Marquette
}
208 d772ac32 Erik Kristensen
209 0d4b6b89 jim-p
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
210
function get_mounted_filesystems() {
211
	$mout = "";
212
	$filesystems = array();
213
	exec("/bin/df -Tht ufs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
214
215
	/* Get rid of the header */
216
	array_shift($mout);
217
	foreach ($mout as $fs) {
218
		$f = array();
219
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
220
221
		/* We dont' want the trailing % sign. */
222
		$f['percent_used'] = trim($f['percent_used'], '%');
223
224
		$filesystems[] = $f;
225
	}
226
	return $filesystems;
227
}
228
229
function disk_usage($slice = '/') {
230 767a716e Scott Ullrich
	$dfout = "";
231 0d4b6b89 jim-p
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
232 d772ac32 Erik Kristensen
	$diskusage = trim($dfout[0]);
233
234
	return $diskusage;
235
}
236
237 af0b57cb Renato Botelho
function swap_usage() {
238 dfe2769c Renato Botelho
	exec("/usr/sbin/swapinfo", $swap_info);
239 bc488151 Renato Botelho
	$swap_used = "";
240 f3ec0487 Phil Davis
	foreach ($swap_info as $line) {
241 bc488151 Renato Botelho
		if (preg_match('/(\d+)%$/', $line, $matches)) {
242
			$swap_used = $matches[1];
243
			break;
244
		}
245 f3ec0487 Phil Davis
	}
246 d772ac32 Erik Kristensen
247 bc488151 Renato Botelho
	return $swap_used;
248 d772ac32 Erik Kristensen
}
249
250 880637d2 Scott Ullrich
function mem_usage() {
251 971de1f9 Renato Botelho
	$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
252
	if ($totalMem > 0) {
253
		$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
254
		$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
255
		$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
256
		$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
257 15183bcb phildd
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
258 f3ec0487 Phil Davis
	} else {
259 15183bcb phildd
		$memUsage = "NA";
260 f3ec0487 Phil Davis
	}
261 d772ac32 Erik Kristensen
262
	return $memUsage;
263 7e1b16ca Bill Marquette
}
264 60928664 Scott Ullrich
265 65d4de2e Scott Dale
function update_date_time() {
266
	$datetime = date("D M j G:i:s T Y");
267
	return $datetime;
268
}
269
270 4dedd18a Cristian Feldman
function get_cpufreq() {
271
	$cpufreqs = "";
272
	$out = "";
273 971de1f9 Renato Botelho
	$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
274 4dedd18a Cristian Feldman
	$maxfreq = explode("/", $cpufreqs[0]);
275
	$maxfreq = $maxfreq[0];
276
	$curfreq = "";
277 971de1f9 Renato Botelho
	$curfreq = get_single_sysctl('dev.cpu.0.freq');
278 f3ec0487 Phil Davis
	if (($curfreq > 0) && ($curfreq != $maxfreq)) {
279 4dedd18a Cristian Feldman
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
280 f3ec0487 Phil Davis
	}
281 4dedd18a Cristian Feldman
	return $out;
282
}
283
284 b097a7cf jim-p
function get_cpu_count($show_detail = false) {
285 971de1f9 Renato Botelho
	$cpucount = get_single_sysctl('kern.smp.cpus');
286 b097a7cf jim-p
287
	if ($show_detail) {
288
		$cpudetail = "";
289
		exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
290
		$cpucount = $cpudetail[0];
291
	}
292
	return $cpucount;
293
}
294
295 25a46a3c Cristian Feldman
function get_load_average() {
296
	$load_average = "";
297
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
298
	return $load_average[0];
299
}
300
301 880637d2 Scott Ullrich
function get_interfacestats() {
302 65d4de2e Scott Dale
	global $config;
303
	//build interface list for widget use
304 97e9b109 Chris Buechler
	$ifdescrs = get_configured_interface_list();
305 3e321df2 Ermal Luçi
306 65d4de2e Scott Dale
	$array_in_packets = array();
307
	$array_out_packets = array();
308
	$array_in_bytes = array();
309
	$array_out_bytes = array();
310
	$array_in_errors = array();
311
	$array_out_errors = array();
312
	$array_collisions = array();
313
	$array_interrupt = array();
314
	$new_data = "";
315 af0b57cb Renato Botelho
316 65d4de2e Scott Dale
	//build data arrays
317 f3ec0487 Phil Davis
	foreach ($ifdescrs as $ifdescr => $ifname) {
318 af0b57cb Renato Botelho
		$ifinfo = get_interface_info($ifdescr);
319 f3ec0487 Phil Davis
		$new_data .= "{$ifinfo['inpkts']},";
320
		$new_data .= "{$ifinfo['outpkts']},";
321
		$new_data .= format_bytes($ifinfo['inbytes']) . ",";
322
		$new_data .= format_bytes($ifinfo['outbytes']) . ",";
323
		if (isset($ifinfo['inerrs'])) {
324
			$new_data .= "{$ifinfo['inerrs']},";
325
			$new_data .= "{$ifinfo['outerrs']},";
326
		} else {
327
			$new_data .= "0,";
328
			$new_data .= "0,";
329
		}
330
		if (isset($ifinfo['collisions'])) {
331
			$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
332
		} else {
333
			$new_data .= "0,";
334
		}
335 65d4de2e Scott Dale
	}//end for
336
337 af0b57cb Renato Botelho
	return $new_data;
338 65d4de2e Scott Dale
}
339
340 880637d2 Scott Ullrich
function get_interfacestatus() {
341 65d4de2e Scott Dale
	$data = "";
342
	global $config;
343
344
	//build interface list for widget use
345 3e321df2 Ermal Luçi
	$ifdescrs = get_configured_interface_with_descr();
346 af0b57cb Renato Botelho
347 f3ec0487 Phil Davis
	foreach ($ifdescrs as $ifdescr => $ifname) {
348 65d4de2e Scott Dale
		$ifinfo = get_interface_info($ifdescr);
349 9cbdb6e3 Phil Davis
		$data .= $ifname . "^";
350 f3ec0487 Phil Davis
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
351 65d4de2e Scott Dale
			$data .= "up";
352 f3ec0487 Phil Davis
		} else if ($ifinfo['status'] == "no carrier") {
353 65d4de2e Scott Dale
			$data .= "down";
354 f3ec0487 Phil Davis
		} else if ($ifinfo['status'] == "down") {
355 65d4de2e Scott Dale
			$data .= "block";
356
		}
357 9cbdb6e3 Phil Davis
		$data .= "^";
358 f3ec0487 Phil Davis
		if ($ifinfo['ipaddr']) {
359 d7884992 Phil Davis
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
360 f3ec0487 Phil Davis
		}
361 9cbdb6e3 Phil Davis
		$data .= "^";
362 f3ec0487 Phil Davis
		if ($ifinfo['ipaddrv6']) {
363 d7884992 Phil Davis
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
364 f3ec0487 Phil Davis
		}
365 9cbdb6e3 Phil Davis
		$data .= "^";
366 f3ec0487 Phil Davis
		if ($ifinfo['status'] != "down") {
367 65d4de2e Scott Dale
			$data .= htmlspecialchars($ifinfo['media']);
368 f3ec0487 Phil Davis
		}
369 af0b57cb Renato Botelho
370 65d4de2e Scott Dale
		$data .= "~";
371 af0b57cb Renato Botelho
372
	}
373 65d4de2e Scott Dale
	return $data;
374
}
375
376 af0b57cb Renato Botelho
?>