Project

General

Profile

Download (7.98 KB) Statistics
| Branch: | Tag: | Revision:
1 e9258698 NewEraCracker
<?php
2 7ac5a4cb Scott Ullrich
/*
3 ce77a9c4 Phil Davis
	functions.inc.php
4
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
5 7ac5a4cb Scott Ullrich
*/
6 d6ad908c Scott Ullrich
7 f3ec0487 Phil Davis
if (Connection_Aborted()) {
8 d6ad908c Scott Ullrich
	exit;
9
}
10 d772ac32 Erik Kristensen
11 2fc0b1c7 Scott Ullrich
require_once("config.inc");
12 806e5979 Phil Davis
require_once("pfsense-utils.inc");
13 65d4de2e Scott Dale
14 2fc0b1c7 Scott Ullrich
function get_stats() {
15
	$stats['cpu'] = cpu_usage();
16
	$stats['mem'] = mem_usage();
17
	$stats['uptime'] = get_uptime();
18
	$stats['states'] = get_pfstate();
19
	$stats['temp'] = get_temp();
20 65d4de2e Scott Dale
	$stats['datetime'] = update_date_time();
21
	$stats['interfacestatistics'] = get_interfacestats();
22
	$stats['interfacestatus'] = get_interfacestatus();
23 4dedd18a Cristian Feldman
	$stats['cpufreq'] = get_cpufreq();
24 25a46a3c Cristian Feldman
	$stats['load_average'] = get_load_average();
25 8ff9cc38 jim-p
	$stats['mbuf'] = get_mbuf();
26 2cb760da jim-p
	$stats['mbufpercent'] = get_mbuf(true);
27 4a83831c jim-p
	$stats['statepercent'] = get_pfstate(true);
28 2fc0b1c7 Scott Ullrich
	$stats = join("|", $stats);
29
	return $stats;
30
}
31 477361c7 Erik Kristensen
32 d772ac32 Erik Kristensen
function get_uptime() {
33 806e5979 Phil Davis
	$uptime = get_uptime_sec();
34 d772ac32 Erik Kristensen
35 f3ec0487 Phil Davis
	if (intval($uptime) == 0) {
36 c7b8b46a Scott Ullrich
		return;
37 f3ec0487 Phil Davis
	}
38 c7b8b46a Scott Ullrich
39 d772ac32 Erik Kristensen
	$updays = (int)($uptime / 86400);
40
	$uptime %= 86400;
41
	$uphours = (int)($uptime / 3600);
42
	$uptime %= 3600;
43
	$upmins = (int)($uptime / 60);
44 af0b57cb Renato Botelho
	$uptime %= 60;
45 54f818b1 Joecowboy
	$upsecs = (int)($uptime);
46 d772ac32 Erik Kristensen
47
	$uptimestr = "";
48 f3ec0487 Phil Davis
	if ($updays > 1) {
49 54f818b1 Joecowboy
		$uptimestr .= "$updays Days ";
50 f3ec0487 Phil Davis
	} else if ($updays > 0) {
51 54f818b1 Joecowboy
		$uptimestr .= "1 Day ";
52 f3ec0487 Phil Davis
	}
53 54f818b1 Joecowboy
54 f3ec0487 Phil Davis
	if ($uphours > 1) {
55 af0b57cb Renato Botelho
		$hours = "s";
56 f3ec0487 Phil Davis
	}
57 af0b57cb Renato Botelho
58 f3ec0487 Phil Davis
	if ($upmins > 1) {
59 af0b57cb Renato Botelho
		$minutes = "s";
60 f3ec0487 Phil Davis
	}
61 af0b57cb Renato Botelho
62 f3ec0487 Phil Davis
	if ($upmins > 1) {
63 af0b57cb Renato Botelho
		$seconds = "s";
64 f3ec0487 Phil Davis
	}
65 af0b57cb Renato Botelho
66 b10268ca Joecowboy
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
67 d772ac32 Erik Kristensen
	return $uptimestr;
68
}
69
70 e637075a Bill Marquette
/* Calculates non-idle CPU time and returns as a percentage */
71 4fe00253 Espen Johansen
function cpu_usage() {
72 4a1cd18f Bill Marquette
	$duration = 1;
73
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
74 971de1f9 Renato Botelho
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
75 4a1cd18f Bill Marquette
	sleep($duration);
76 971de1f9 Renato Botelho
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
77 af0b57cb Renato Botelho
78 4a1cd18f Bill Marquette
	$totalStart = array_sum($cpuTicks);
79
	$totalEnd = array_sum($cpuTicks2);
80
81
	// Something wrapped ?!?!
82 f3ec0487 Phil Davis
	if ($totalEnd <= $totalStart) {
83 4a1cd18f Bill Marquette
		return 0;
84 f3ec0487 Phil Davis
	}
85 4a1cd18f Bill Marquette
86
	// Calculate total cycles used
87
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
88 65d4de2e Scott Dale
89 4a1cd18f Bill Marquette
	// Calculate the percentage used
90
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
91 af0b57cb Renato Botelho
92 d772ac32 Erik Kristensen
	return $cpuUsage;
93
}
94
95 4a83831c jim-p
function get_pfstate($percent=false) {
96 d772ac32 Erik Kristensen
	global $config;
97 767a716e Scott Ullrich
	$matches = "";
98 f3ec0487 Phil Davis
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
99 af0b57cb Renato Botelho
		$maxstates="{$config['system']['maximumstates']}";
100 f3ec0487 Phil Davis
	} else {
101 af0b57cb Renato Botelho
		$maxstates=pfsense_default_state_size();
102 f3ec0487 Phil Davis
	}
103 767a716e Scott Ullrich
	$curentries = `/sbin/pfctl -si |grep current`;
104
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
105 af0b57cb Renato Botelho
		$curentries = $matches[1];
106 767a716e Scott Ullrich
	}
107 f3ec0487 Phil Davis
	if (!is_numeric($curentries)) {
108 66cc4d43 Ermal
		$curentries = 0;
109 f3ec0487 Phil Davis
	}
110
	if ($percent) {
111
		if (intval($maxstates) > 0) {
112 15183bcb phildd
			return round(($curentries / $maxstates) * 100, 0);
113 f3ec0487 Phil Davis
		} else {
114 15183bcb phildd
			return "NA";
115 f3ec0487 Phil Davis
		}
116
	} else {
117 4a83831c jim-p
		return $curentries . "/" . $maxstates;
118 f3ec0487 Phil Davis
	}
119 d772ac32 Erik Kristensen
}
120
121 7979a809 Bill Marquette
function has_temp() {
122 19604f5d Scott Ullrich
	/* no known temp monitors available at present */
123 af0b57cb Renato Botelho
124 7979a809 Bill Marquette
	/* should only reach here if there is no hardware monitor */
125
	return false;
126
}
127
128
function get_hwtype() {
129
	return;
130
}
131
132 2cb760da jim-p
function get_mbuf($percent=false) {
133 2c7f71d9 jim-p
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
134 f3ec0487 Phil Davis
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
135
	if ($percent) {
136
		if ($mbufs_max > 0) {
137 15183bcb phildd
			return round(($mbufs_total / $mbufs_max) * 100, 0);
138 f3ec0487 Phil Davis
		} else {
139 15183bcb phildd
			return "NA";
140 f3ec0487 Phil Davis
		}
141
	} else {
142 2cb760da jim-p
		return "{$mbufs_total}/{$mbufs_max}";
143 f3ec0487 Phil Davis
	}
144 8ff9cc38 jim-p
}
145
146 7979a809 Bill Marquette
function get_temp() {
147 971de1f9 Renato Botelho
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
148 f3ec0487 Phil Davis
	if ($temp_out == "") {
149 971de1f9 Renato Botelho
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
150 f3ec0487 Phil Davis
	}
151 af0b57cb Renato Botelho
152 8822be67 Renato Botelho
	// Remove 'C' from the end and spaces
153
	$temp_out = trim(rtrim($temp_out, 'C'));
154
155
	if ($temp_out[0] == '-') {
156
		return '';
157
	}
158
159
	return $temp_out;
160 1804023d Bill Marquette
}
161 d772ac32 Erik Kristensen
162 0d4b6b89 jim-p
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
163
function get_mounted_filesystems() {
164
	$mout = "";
165
	$filesystems = array();
166
	exec("/bin/df -Tht ufs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
167
168
	/* Get rid of the header */
169
	array_shift($mout);
170
	foreach ($mout as $fs) {
171
		$f = array();
172
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
173
174
		/* We dont' want the trailing % sign. */
175
		$f['percent_used'] = trim($f['percent_used'], '%');
176
177
		$filesystems[] = $f;
178
	}
179
	return $filesystems;
180
}
181
182
function disk_usage($slice = '/') {
183 767a716e Scott Ullrich
	$dfout = "";
184 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);
185 d772ac32 Erik Kristensen
	$diskusage = trim($dfout[0]);
186
187
	return $diskusage;
188
}
189
190 af0b57cb Renato Botelho
function swap_usage() {
191 dfe2769c Renato Botelho
	exec("/usr/sbin/swapinfo", $swap_info);
192 bc488151 Renato Botelho
	$swap_used = "";
193 f3ec0487 Phil Davis
	foreach ($swap_info as $line) {
194 bc488151 Renato Botelho
		if (preg_match('/(\d+)%$/', $line, $matches)) {
195
			$swap_used = $matches[1];
196
			break;
197
		}
198 f3ec0487 Phil Davis
	}
199 d772ac32 Erik Kristensen
200 bc488151 Renato Botelho
	return $swap_used;
201 d772ac32 Erik Kristensen
}
202
203 880637d2 Scott Ullrich
function mem_usage() {
204 971de1f9 Renato Botelho
	$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
205
	if ($totalMem > 0) {
206
		$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
207
		$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
208
		$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
209
		$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
210 15183bcb phildd
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
211 f3ec0487 Phil Davis
	} else {
212 15183bcb phildd
		$memUsage = "NA";
213 f3ec0487 Phil Davis
	}
214 d772ac32 Erik Kristensen
215
	return $memUsage;
216 7e1b16ca Bill Marquette
}
217 60928664 Scott Ullrich
218 65d4de2e Scott Dale
function update_date_time() {
219
	$datetime = date("D M j G:i:s T Y");
220
	return $datetime;
221
}
222
223 4dedd18a Cristian Feldman
function get_cpufreq() {
224
	$cpufreqs = "";
225
	$out = "";
226 971de1f9 Renato Botelho
	$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
227 4dedd18a Cristian Feldman
	$maxfreq = explode("/", $cpufreqs[0]);
228
	$maxfreq = $maxfreq[0];
229
	$curfreq = "";
230 971de1f9 Renato Botelho
	$curfreq = get_single_sysctl('dev.cpu.0.freq');
231 f3ec0487 Phil Davis
	if (($curfreq > 0) && ($curfreq != $maxfreq)) {
232 4dedd18a Cristian Feldman
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
233 f3ec0487 Phil Davis
	}
234 4dedd18a Cristian Feldman
	return $out;
235
}
236
237 b097a7cf jim-p
function get_cpu_count($show_detail = false) {
238 971de1f9 Renato Botelho
	$cpucount = get_single_sysctl('kern.smp.cpus');
239 b097a7cf jim-p
240
	if ($show_detail) {
241
		$cpudetail = "";
242
		exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
243
		$cpucount = $cpudetail[0];
244
	}
245
	return $cpucount;
246
}
247
248 25a46a3c Cristian Feldman
function get_load_average() {
249
	$load_average = "";
250
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
251
	return $load_average[0];
252
}
253
254 880637d2 Scott Ullrich
function get_interfacestats() {
255 65d4de2e Scott Dale
	global $config;
256
	//build interface list for widget use
257 97e9b109 Chris Buechler
	$ifdescrs = get_configured_interface_list();
258 3e321df2 Ermal Luçi
259 65d4de2e Scott Dale
	$array_in_packets = array();
260
	$array_out_packets = array();
261
	$array_in_bytes = array();
262
	$array_out_bytes = array();
263
	$array_in_errors = array();
264
	$array_out_errors = array();
265
	$array_collisions = array();
266
	$array_interrupt = array();
267
	$new_data = "";
268 af0b57cb Renato Botelho
269 65d4de2e Scott Dale
	//build data arrays
270 f3ec0487 Phil Davis
	foreach ($ifdescrs as $ifdescr => $ifname) {
271 af0b57cb Renato Botelho
		$ifinfo = get_interface_info($ifdescr);
272 f3ec0487 Phil Davis
		$new_data .= "{$ifinfo['inpkts']},";
273
		$new_data .= "{$ifinfo['outpkts']},";
274
		$new_data .= format_bytes($ifinfo['inbytes']) . ",";
275
		$new_data .= format_bytes($ifinfo['outbytes']) . ",";
276
		if (isset($ifinfo['inerrs'])) {
277
			$new_data .= "{$ifinfo['inerrs']},";
278
			$new_data .= "{$ifinfo['outerrs']},";
279
		} else {
280
			$new_data .= "0,";
281
			$new_data .= "0,";
282
		}
283
		if (isset($ifinfo['collisions'])) {
284
			$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
285
		} else {
286
			$new_data .= "0,";
287
		}
288 65d4de2e Scott Dale
	}//end for
289
290 af0b57cb Renato Botelho
	return $new_data;
291 65d4de2e Scott Dale
}
292
293 880637d2 Scott Ullrich
function get_interfacestatus() {
294 65d4de2e Scott Dale
	$data = "";
295
	global $config;
296
297
	//build interface list for widget use
298 3e321df2 Ermal Luçi
	$ifdescrs = get_configured_interface_with_descr();
299 af0b57cb Renato Botelho
300 f3ec0487 Phil Davis
	foreach ($ifdescrs as $ifdescr => $ifname) {
301 65d4de2e Scott Dale
		$ifinfo = get_interface_info($ifdescr);
302 9cbdb6e3 Phil Davis
		$data .= $ifname . "^";
303 f3ec0487 Phil Davis
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
304 65d4de2e Scott Dale
			$data .= "up";
305 f3ec0487 Phil Davis
		} else if ($ifinfo['status'] == "no carrier") {
306 65d4de2e Scott Dale
			$data .= "down";
307 f3ec0487 Phil Davis
		} else if ($ifinfo['status'] == "down") {
308 65d4de2e Scott Dale
			$data .= "block";
309
		}
310 9cbdb6e3 Phil Davis
		$data .= "^";
311 f3ec0487 Phil Davis
		if ($ifinfo['ipaddr']) {
312 d7884992 Phil Davis
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
313 f3ec0487 Phil Davis
		}
314 9cbdb6e3 Phil Davis
		$data .= "^";
315 f3ec0487 Phil Davis
		if ($ifinfo['ipaddrv6']) {
316 d7884992 Phil Davis
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
317 f3ec0487 Phil Davis
		}
318 9cbdb6e3 Phil Davis
		$data .= "^";
319 f3ec0487 Phil Davis
		if ($ifinfo['status'] != "down") {
320 65d4de2e Scott Dale
			$data .= htmlspecialchars($ifinfo['media']);
321 f3ec0487 Phil Davis
		}
322 af0b57cb Renato Botelho
323 65d4de2e Scott Dale
		$data .= "~";
324 af0b57cb Renato Botelho
325
	}
326 65d4de2e Scott Dale
	return $data;
327
}
328
329 af0b57cb Renato Botelho
?>