Project

General

Profile

Download (7.98 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	functions.inc.php
4
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
5
*/
6

    
7
if (Connection_Aborted()) {
8
	exit;
9
}
10

    
11
require_once("config.inc");
12
require_once("pfsense-utils.inc");
13

    
14
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
	$stats['datetime'] = update_date_time();
21
	$stats['interfacestatistics'] = get_interfacestats();
22
	$stats['interfacestatus'] = get_interfacestatus();
23
	$stats['cpufreq'] = get_cpufreq();
24
	$stats['load_average'] = get_load_average();
25
	$stats['mbuf'] = get_mbuf();
26
	$stats['mbufpercent'] = get_mbuf(true);
27
	$stats['statepercent'] = get_pfstate(true);
28
	$stats = join("|", $stats);
29
	return $stats;
30
}
31

    
32
function get_uptime() {
33
	$uptime = get_uptime_sec();
34

    
35
	if (intval($uptime) == 0) {
36
		return;
37
	}
38

    
39
	$updays = (int)($uptime / 86400);
40
	$uptime %= 86400;
41
	$uphours = (int)($uptime / 3600);
42
	$uptime %= 3600;
43
	$upmins = (int)($uptime / 60);
44
	$uptime %= 60;
45
	$upsecs = (int)($uptime);
46

    
47
	$uptimestr = "";
48
	if ($updays > 1) {
49
		$uptimestr .= "$updays Days ";
50
	} else if ($updays > 0) {
51
		$uptimestr .= "1 Day ";
52
	}
53

    
54
	if ($uphours > 1) {
55
		$hours = "s";
56
	}
57

    
58
	if ($upmins > 1) {
59
		$minutes = "s";
60
	}
61

    
62
	if ($upmins > 1) {
63
		$seconds = "s";
64
	}
65

    
66
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
67
	return $uptimestr;
68
}
69

    
70
/* Calculates non-idle CPU time and returns as a percentage */
71
function cpu_usage() {
72
	$duration = 1;
73
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
74
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
75
	sleep($duration);
76
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
77

    
78
	$totalStart = array_sum($cpuTicks);
79
	$totalEnd = array_sum($cpuTicks2);
80

    
81
	// Something wrapped ?!?!
82
	if ($totalEnd <= $totalStart) {
83
		return 0;
84
	}
85

    
86
	// Calculate total cycles used
87
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
88

    
89
	// Calculate the percentage used
90
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
91

    
92
	return $cpuUsage;
93
}
94

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

    
121
function has_temp() {
122
	/* no known temp monitors available at present */
123

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

    
128
function get_hwtype() {
129
	return;
130
}
131

    
132
function get_mbuf($percent=false) {
133
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
134
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
135
	if ($percent) {
136
		if ($mbufs_max > 0) {
137
			return round(($mbufs_total / $mbufs_max) * 100, 0);
138
		} else {
139
			return "NA";
140
		}
141
	} else {
142
		return "{$mbufs_total}/{$mbufs_max}";
143
	}
144
}
145

    
146
function get_temp() {
147
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
148
	if ($temp_out == "") {
149
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
150
	}
151

    
152
	// 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
}
161

    
162
/* 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
	$dfout = "";
184
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
185
	$diskusage = trim($dfout[0]);
186

    
187
	return $diskusage;
188
}
189

    
190
function swap_usage() {
191
	exec("/usr/sbin/swapinfo", $swap_info);
192
	$swap_used = "";
193
	foreach ($swap_info as $line) {
194
		if (preg_match('/(\d+)%$/', $line, $matches)) {
195
			$swap_used = $matches[1];
196
			break;
197
		}
198
	}
199

    
200
	return $swap_used;
201
}
202

    
203
function mem_usage() {
204
	$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
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
211
	} else {
212
		$memUsage = "NA";
213
	}
214

    
215
	return $memUsage;
216
}
217

    
218
function update_date_time() {
219
	$datetime = date("D M j G:i:s T Y");
220
	return $datetime;
221
}
222

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

    
237
function get_cpu_count($show_detail = false) {
238
	$cpucount = get_single_sysctl('kern.smp.cpus');
239

    
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
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
function get_interfacestats() {
255
	global $config;
256
	//build interface list for widget use
257
	$ifdescrs = get_configured_interface_list();
258

    
259
	$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

    
269
	//build data arrays
270
	foreach ($ifdescrs as $ifdescr => $ifname) {
271
		$ifinfo = get_interface_info($ifdescr);
272
		$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
	}//end for
289

    
290
	return $new_data;
291
}
292

    
293
function get_interfacestatus() {
294
	$data = "";
295
	global $config;
296

    
297
	//build interface list for widget use
298
	$ifdescrs = get_configured_interface_with_descr();
299

    
300
	foreach ($ifdescrs as $ifdescr => $ifname) {
301
		$ifinfo = get_interface_info($ifdescr);
302
		$data .= $ifname . "^";
303
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
304
			$data .= "up";
305
		} else if ($ifinfo['status'] == "no carrier") {
306
			$data .= "down";
307
		} else if ($ifinfo['status'] == "down") {
308
			$data .= "block";
309
		}
310
		$data .= "^";
311
		if ($ifinfo['ipaddr']) {
312
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
313
		}
314
		$data .= "^";
315
		if ($ifinfo['ipaddrv6']) {
316
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
317
		}
318
		$data .= "^";
319
		if ($ifinfo['status'] != "down") {
320
			$data .= htmlspecialchars($ifinfo['media']);
321
		}
322

    
323
		$data .= "~";
324

    
325
	}
326
	return $data;
327
}
328

    
329
?>
    (1-1/1)