Project

General

Profile

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

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

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

    
15
function get_stats() {
16
	$stats['cpu'] = cpu_usage();
17
	$stats['mem'] = mem_usage();
18
	$stats['uptime'] = get_uptime();
19
	$stats['states'] = get_pfstate();
20
	$stats['temp'] = get_temp();
21
	$stats['datetime'] = update_date_time();
22
	$stats['interfacestatistics'] = get_interfacestats();
23
	$stats['interfacestatus'] = get_interfacestatus();
24
	$stats['gateways'] = get_gatewaystats();
25
	$stats['cpufreq'] = get_cpufreq();
26
	$stats['load_average'] = get_load_average();
27
	$stats['mbuf'] = get_mbuf();
28
	$stats['mbufpercent'] = get_mbuf(true);
29
	$stats['statepercent'] = get_pfstate(true);
30
	$stats = join("|", $stats);
31
	return $stats;
32
}
33

    
34
function get_gatewaystats() {
35
	$a_gateways = return_gateways_array();
36
	$gateways_status = array();
37
	$gateways_status = return_gateways_status(true);
38
	$data = "";
39
	$isfirst = true;
40
	foreach ($a_gateways as $gname => $gw) {
41
		if (!$isfirst) {
42
			$data .= ",";
43
		}
44
		$isfirst = false;
45
		$data .= $gw['name'] . ",";
46
		if ($gateways_status[$gname]) {
47
			$data .= "<b>" . lookup_gateway_ip_by_name($gname) . "</b>,";
48
			$gws = $gateways_status[$gname];
49
			switch (strtolower($gws['status'])) {
50
				case "none":
51
					$online = "Online";
52
					$bgcolor = "#90EE90";  // lightgreen
53
					break;
54
				case "down":
55
					$online = "Offline";
56
					$bgcolor = "#F08080";  // lightcoral
57
					break;
58
				case "delay":
59
					$online = "Latency";
60
					$bgcolor = "#F0E68C";  // khaki
61
					break;
62
				case "loss":
63
					$online = "Packetloss";
64
					$bgcolor = "#F0E68C";  // khaki
65
					break;
66
				default:
67
					$online = "Pending";
68
					break;
69
			}
70
		} else {
71
			$data .= "~,";
72
			$gws['delay'] = "~";
73
			$gws['loss'] = "~";
74
			$online = "Unknown";
75
			$bgcolor = "#ADD8E6";  // lightblue
76
		}
77
		$data .= ($online == "Pending") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
78
		$data .= "{$online}^{$bgcolor}";
79
	}
80
	return $data;
81
}
82

    
83
function get_uptime() {
84
	$uptime = get_uptime_sec();
85

    
86
	if (intval($uptime) == 0) {
87
		return;
88
	}
89

    
90
	$updays = (int)($uptime / 86400);
91
	$uptime %= 86400;
92
	$uphours = (int)($uptime / 3600);
93
	$uptime %= 3600;
94
	$upmins = (int)($uptime / 60);
95
	$uptime %= 60;
96
	$upsecs = (int)($uptime);
97

    
98
	$uptimestr = "";
99
	if ($updays > 1) {
100
		$uptimestr .= "$updays Days ";
101
	} else if ($updays > 0) {
102
		$uptimestr .= "1 Day ";
103
	}
104

    
105
	if ($uphours > 1) {
106
		$hours = "s";
107
	}
108

    
109
	if ($upmins > 1) {
110
		$minutes = "s";
111
	}
112

    
113
	if ($upmins > 1) {
114
		$seconds = "s";
115
	}
116

    
117
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
118
	return $uptimestr;
119
}
120

    
121
/* Calculates non-idle CPU time and returns as a percentage */
122
function cpu_usage() {
123
	$duration = 1;
124
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
125
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
126
	sleep($duration);
127
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
128

    
129
	$totalStart = array_sum($cpuTicks);
130
	$totalEnd = array_sum($cpuTicks2);
131

    
132
	// Something wrapped ?!?!
133
	if ($totalEnd <= $totalStart) {
134
		return 0;
135
	}
136

    
137
	// Calculate total cycles used
138
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
139

    
140
	// Calculate the percentage used
141
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
142

    
143
	return $cpuUsage;
144
}
145

    
146
function get_pfstate($percent=false) {
147
	global $config;
148
	$matches = "";
149
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
150
		$maxstates="{$config['system']['maximumstates']}";
151
	} else {
152
		$maxstates=pfsense_default_state_size();
153
	}
154
	$curentries = `/sbin/pfctl -si |grep current`;
155
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
156
		$curentries = $matches[1];
157
	}
158
	if (!is_numeric($curentries)) {
159
		$curentries = 0;
160
	}
161
	if ($percent) {
162
		if (intval($maxstates) > 0) {
163
			return round(($curentries / $maxstates) * 100, 0);
164
		} else {
165
			return "NA";
166
		}
167
	} else {
168
		return $curentries . "/" . $maxstates;
169
	}
170
}
171

    
172
function has_temp() {
173
	/* no known temp monitors available at present */
174

    
175
	/* should only reach here if there is no hardware monitor */
176
	return false;
177
}
178

    
179
function get_hwtype() {
180
	return;
181
}
182

    
183
function get_mbuf($percent=false) {
184
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
185
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
186
	if ($percent) {
187
		if ($mbufs_max > 0) {
188
			return round(($mbufs_total / $mbufs_max) * 100, 0);
189
		} else {
190
			return "NA";
191
		}
192
	} else {
193
		return "{$mbufs_total}/{$mbufs_max}";
194
	}
195
}
196

    
197
function get_temp() {
198
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
199
	if ($temp_out == "") {
200
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
201
	}
202

    
203
	// Remove 'C' from the end
204
	return rtrim($temp_out, 'C');
205
}
206

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

    
213
	/* Get rid of the header */
214
	array_shift($mout);
215
	foreach ($mout as $fs) {
216
		$f = array();
217
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
218

    
219
		/* We dont' want the trailing % sign. */
220
		$f['percent_used'] = trim($f['percent_used'], '%');
221

    
222
		$filesystems[] = $f;
223
	}
224
	return $filesystems;
225
}
226

    
227
function disk_usage($slice = '/') {
228
	$dfout = "";
229
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
230
	$diskusage = trim($dfout[0]);
231

    
232
	return $diskusage;
233
}
234

    
235
function swap_usage() {
236
	exec("/usr/sbin/swapinfo", $swap_info);
237
	$swap_used = "";
238
	foreach ($swap_info as $line) {
239
		if (preg_match('/(\d+)%$/', $line, $matches)) {
240
			$swap_used = $matches[1];
241
			break;
242
		}
243
	}
244

    
245
	return $swap_used;
246
}
247

    
248
function mem_usage() {
249
	$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
250
	if ($totalMem > 0) {
251
		$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
252
		$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
253
		$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
254
		$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
255
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
256
	} else {
257
		$memUsage = "NA";
258
	}
259

    
260
	return $memUsage;
261
}
262

    
263
function update_date_time() {
264
	$datetime = date("D M j G:i:s T Y");
265
	return $datetime;
266
}
267

    
268
function get_cpufreq() {
269
	$cpufreqs = "";
270
	$out = "";
271
	$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
272
	$maxfreq = explode("/", $cpufreqs[0]);
273
	$maxfreq = $maxfreq[0];
274
	$curfreq = "";
275
	$curfreq = get_single_sysctl('dev.cpu.0.freq');
276
	if (($curfreq > 0) && ($curfreq != $maxfreq)) {
277
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
278
	}
279
	return $out;
280
}
281

    
282
function get_cpu_count($show_detail = false) {
283
	$cpucount = get_single_sysctl('kern.smp.cpus');
284

    
285
	if ($show_detail) {
286
		$cpudetail = "";
287
		exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
288
		$cpucount = $cpudetail[0];
289
	}
290
	return $cpucount;
291
}
292

    
293
function get_load_average() {
294
	$load_average = "";
295
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
296
	return $load_average[0];
297
}
298

    
299
function get_interfacestats() {
300
	global $config;
301
	//build interface list for widget use
302
	$ifdescrs = get_configured_interface_list();
303

    
304
	$array_in_packets = array();
305
	$array_out_packets = array();
306
	$array_in_bytes = array();
307
	$array_out_bytes = array();
308
	$array_in_errors = array();
309
	$array_out_errors = array();
310
	$array_collisions = array();
311
	$array_interrupt = array();
312
	$new_data = "";
313

    
314
	//build data arrays
315
	foreach ($ifdescrs as $ifdescr => $ifname) {
316
		$ifinfo = get_interface_info($ifdescr);
317
		$new_data .= "{$ifinfo['inpkts']},";
318
		$new_data .= "{$ifinfo['outpkts']},";
319
		$new_data .= format_bytes($ifinfo['inbytes']) . ",";
320
		$new_data .= format_bytes($ifinfo['outbytes']) . ",";
321
		if (isset($ifinfo['inerrs'])) {
322
			$new_data .= "{$ifinfo['inerrs']},";
323
			$new_data .= "{$ifinfo['outerrs']},";
324
		} else {
325
			$new_data .= "0,";
326
			$new_data .= "0,";
327
		}
328
		if (isset($ifinfo['collisions'])) {
329
			$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
330
		} else {
331
			$new_data .= "0,";
332
		}
333
	}//end for
334

    
335
	return $new_data;
336
}
337

    
338
function get_interfacestatus() {
339
	$data = "";
340
	global $config;
341

    
342
	//build interface list for widget use
343
	$ifdescrs = get_configured_interface_with_descr();
344

    
345
	foreach ($ifdescrs as $ifdescr => $ifname) {
346
		$ifinfo = get_interface_info($ifdescr);
347
		$data .= $ifname . ",";
348
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
349
			$data .= "up";
350
		} else if ($ifinfo['status'] == "no carrier") {
351
			$data .= "down";
352
		} else if ($ifinfo['status'] == "down") {
353
			$data .= "block";
354
		}
355
		$data .= ",";
356
		if ($ifinfo['ipaddr']) {
357
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
358
		}
359
		$data .= ",";
360
		if ($ifinfo['ipaddrv6']) {
361
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
362
		}
363
		$data .= ",";
364
		if ($ifinfo['status'] != "down") {
365
			$data .= htmlspecialchars($ifinfo['media']);
366
		}
367

    
368
		$data .= "~";
369

    
370
	}
371
	return $data;
372
}
373

    
374
?>
    (1-1/1)