Project

General

Profile

Download (9.77 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 Rubicon Communications, LLC (Netgate)
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

    
54
if (Connection_Aborted()) {
55
	exit;
56
}
57

    
58
require_once("config.inc");
59
require_once("pfsense-utils.inc");
60

    
61
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
	$stats['datetime'] = update_date_time();
68
	$stats['cpufreq'] = get_cpufreq();
69
	$stats['load_average'] = get_load_average();
70
	$stats['mbuf'] = get_mbuf();
71
	$stats['mbufpercent'] = get_mbuf(true);
72
	$stats['statepercent'] = get_pfstate(true);
73
	$stats = join("|", $stats);
74
	return $stats;
75
}
76

    
77
function get_uptime() {
78
	$uptime = get_uptime_sec();
79

    
80
	if (intval($uptime) == 0) {
81
		return;
82
	}
83

    
84
	$updays = (int)($uptime / 86400);
85
	$uptime %= 86400;
86
	$uphours = (int)($uptime / 3600);
87
	$uptime %= 3600;
88
	$upmins = (int)($uptime / 60);
89
	$uptime %= 60;
90
	$upsecs = (int)($uptime);
91

    
92
	$uptimestr = "";
93
	if ($updays > 1) {
94
		$uptimestr .= "$updays Days ";
95
	} else if ($updays > 0) {
96
		$uptimestr .= "1 Day ";
97
	}
98

    
99
	if ($uphours > 1) {
100
		$hours = "s";
101
	}
102

    
103
	if ($upmins > 1) {
104
		$minutes = "s";
105
	}
106

    
107
	if ($upmins > 1) {
108
		$seconds = "s";
109
	}
110

    
111
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
112
	return $uptimestr;
113
}
114

    
115
// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that
116
function cpu_usage() {
117

    
118
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
119
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
120

    
121
	return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
122
}
123

    
124
function get_pfstate($percent=false) {
125
	global $config;
126
	$matches = "";
127
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
128
		$maxstates="{$config['system']['maximumstates']}";
129
	} else {
130
		$maxstates=pfsense_default_state_size();
131
	}
132
	$curentries = `/sbin/pfctl -si |grep current`;
133
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
134
		$curentries = $matches[1];
135
	}
136
	if (!is_numeric($curentries)) {
137
		$curentries = 0;
138
	}
139
	if ($percent) {
140
		if (intval($maxstates) > 0) {
141
			return round(($curentries / $maxstates) * 100, 0);
142
		} else {
143
			return "NA";
144
		}
145
	} else {
146
		return $curentries . "/" . $maxstates;
147
	}
148
}
149

    
150
function has_temp() {
151
	/* no known temp monitors available at present */
152

    
153
	/* should only reach here if there is no hardware monitor */
154
	return false;
155
}
156

    
157
function get_hwtype() {
158
	return;
159
}
160

    
161
function get_mbuf($percent=false) {
162
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
163
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
164
	if ($percent) {
165
		if ($mbufs_max > 0) {
166
			return round(($mbufs_total / $mbufs_max) * 100, 0);
167
		} else {
168
			return "NA";
169
		}
170
	} else {
171
		return "{$mbufs_total}/{$mbufs_max}";
172
	}
173
}
174

    
175
function get_temp() {
176
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
177
	if ($temp_out == "") {
178
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
179
	}
180

    
181
	// Remove 'C' from the end and spaces
182
	$temp_out = trim(rtrim($temp_out, 'C'));
183

    
184
	if ($temp_out[0] == '-') {
185
		return '';
186
	}
187

    
188
	return $temp_out;
189
}
190

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

    
197
	/* Get rid of the header */
198
	array_shift($mout);
199
	foreach ($mout as $fs) {
200
		$f = array();
201
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
202

    
203
		/* We dont' want the trailing % sign. */
204
		$f['percent_used'] = trim($f['percent_used'], '%');
205

    
206
		$filesystems[] = $f;
207
	}
208
	return $filesystems;
209
}
210

    
211
function disk_usage($slice = '/') {
212
	$dfout = "";
213
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
214
	$diskusage = trim($dfout[0]);
215

    
216
	return $diskusage;
217
}
218

    
219
function swap_usage() {
220
	exec("/usr/sbin/swapinfo", $swap_info);
221
	$swap_used = "";
222
	foreach ($swap_info as $line) {
223
		if (preg_match('/(\d+)%$/', $line, $matches)) {
224
			$swap_used = $matches[1];
225
			break;
226
		}
227
	}
228

    
229
	return $swap_used;
230
}
231

    
232
function mem_usage() {
233
	$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
234
	if ($totalMem > 0) {
235
		$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
236
		$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
237
		$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
238
		$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
239
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
240
	} else {
241
		$memUsage = "NA";
242
	}
243

    
244
	return $memUsage;
245
}
246

    
247
function update_date_time() {
248
	$datetime = date("D M j G:i:s T Y");
249
	return $datetime;
250
}
251

    
252
function get_cpufreq() {
253
	$cpufreqs = "";
254
	$out = "";
255
	$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
256
	$maxfreq = explode("/", $cpufreqs[0]);
257
	$maxfreq = $maxfreq[0];
258
	$curfreq = "";
259
	$curfreq = get_single_sysctl('dev.cpu.0.freq');
260
	if (($curfreq > 0) && ($curfreq != $maxfreq)) {
261
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
262
	}
263
	return $out;
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 'SMP.*package.*core' /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)