Project

General

Profile

Download (8.98 KB) Statistics
| Branch: | Tag: | Revision:
1
<?
2
/*
3
	pfSense_MODULE:	ajax
4
        Copyright (C) 2013-2014 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['gateways'] = get_gatewaystats();
24
	$stats['cpufreq'] = get_cpufreq();
25
	$stats['load_average'] = get_load_average();
26
	$stats['mbuf'] = get_mbuf();
27
	$stats['mbufpercent'] = get_mbuf(true);
28
	$stats['statepercent'] = get_pfstate(true);
29
	$stats = join("|", $stats);
30
	return $stats;
31
}
32

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

    
81
function get_uptime() {
82
	$uptime = get_uptime_sec();
83

    
84
	if(intval($uptime) == 0)
85
		return;
86

    
87
	$updays = (int)($uptime / 86400);
88
	$uptime %= 86400;
89
	$uphours = (int)($uptime / 3600);
90
	$uptime %= 3600;
91
	$upmins = (int)($uptime / 60);
92
	$uptime %= 60;
93
	$upsecs = (int)($uptime);
94

    
95
	$uptimestr = "";
96
	if ($updays > 1)
97
		$uptimestr .= "$updays Days ";
98
	else if ($updays > 0)
99
		$uptimestr .= "1 Day ";
100

    
101
	if ($uphours > 1)
102
		$hours = "s";
103

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

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

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

    
114
/* Calculates non-idle CPU time and returns as a percentage */
115
function cpu_usage() {
116
	$duration = 1;
117
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
118
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
119
	sleep($duration);
120
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
121

    
122
	$totalStart = array_sum($cpuTicks);
123
	$totalEnd = array_sum($cpuTicks2);
124

    
125
	// Something wrapped ?!?!
126
	if ($totalEnd <= $totalStart)
127
		return 0;
128

    
129
	// Calculate total cycles used
130
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
131

    
132
	// Calculate the percentage used
133
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
134

    
135
	return $cpuUsage;
136
}
137

    
138
function get_pfstate($percent=false) {
139
	global $config;
140
	$matches = "";
141
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
142
		$maxstates="{$config['system']['maximumstates']}";
143
	else
144
		$maxstates=pfsense_default_state_size();
145
	$curentries = `/sbin/pfctl -si |grep current`;
146
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
147
		$curentries = $matches[1];
148
	}
149
	if (!is_numeric($curentries))
150
		$curentries = 0;
151
	if ($percent)
152
		if (intval($maxstates) > 0)
153
			return round(($curentries / $maxstates) * 100, 0);
154
		else
155
			return "NA";
156
	else
157
		return $curentries . "/" . $maxstates;
158
}
159

    
160
function has_temp() {
161
	/* no known temp monitors available at present */
162

    
163
	/* should only reach here if there is no hardware monitor */
164
	return false;
165
}
166

    
167
function get_hwtype() {
168
	return;
169
}
170

    
171
function get_mbuf($percent=false) {
172
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
173
	list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
174
	if ($percent)
175
		if ($mbufs_max > 0)
176
			return round(($mbufs_total / $mbufs_max) * 100, 0);
177
		else
178
			return "NA";
179
	else
180
		return "{$mbufs_total}/{$mbufs_max}";
181
}
182

    
183
function get_temp() {
184
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
185
	if ($temp_out == "")
186
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
187

    
188
	// Remove 'C' from the end
189
	return rtrim($temp_out, 'C');
190
}
191

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

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

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

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

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

    
217
	return $diskusage;
218
}
219

    
220
function swap_usage() {
221
	exec("/usr/sbin/swapinfo", $swap_info);
222
	$swap_used = "";
223
	foreach ($swap_info as $line)
224
		if (preg_match('/(\d+)%$/', $line, $matches)) {
225
			$swap_used = $matches[1];
226
			break;
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
	return $memUsage;
244
}
245

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

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

    
264
function get_cpu_count($show_detail = false) {
265
	$cpucount = get_single_sysctl('kern.smp.cpus');
266

    
267
	if ($show_detail) {
268
		$cpudetail = "";
269
		exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
270
		$cpucount = $cpudetail[0];
271
	}
272
	return $cpucount;
273
}
274

    
275
function get_load_average() {
276
	$load_average = "";
277
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
278
	return $load_average[0];
279
}
280

    
281
function get_interfacestats() {
282
	global $config;
283
	//build interface list for widget use
284
	$ifdescrs = get_configured_interface_list();
285

    
286
	$array_in_packets = array();
287
	$array_out_packets = array();
288
	$array_in_bytes = array();
289
	$array_out_bytes = array();
290
	$array_in_errors = array();
291
	$array_out_errors = array();
292
	$array_collisions = array();
293
	$array_interrupt = array();
294
	$new_data = "";
295

    
296
	//build data arrays
297
	foreach ($ifdescrs as $ifdescr => $ifname){
298
		$ifinfo = get_interface_info($ifdescr);
299
			$new_data .= "{$ifinfo['inpkts']},";
300
			$new_data .= "{$ifinfo['outpkts']},";
301
			$new_data .= format_bytes($ifinfo['inbytes']) . ",";
302
			$new_data .= format_bytes($ifinfo['outbytes']) . ",";
303
			if (isset($ifinfo['inerrs'])){
304
				$new_data .= "{$ifinfo['inerrs']},";
305
				$new_data .= "{$ifinfo['outerrs']},";
306
			}
307
			else{
308
				$new_data .= "0,";
309
				$new_data .= "0,";
310
			}
311
			if (isset($ifinfo['collisions']))
312
				$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
313
			else
314
				$new_data .= "0,";
315
	}//end for
316

    
317
	return $new_data;
318
}
319

    
320
function get_interfacestatus() {
321
	$data = "";
322
	global $config;
323

    
324
	//build interface list for widget use
325
	$ifdescrs = get_configured_interface_with_descr();
326

    
327
	foreach ($ifdescrs as $ifdescr => $ifname){
328
		$ifinfo = get_interface_info($ifdescr);
329
		$data .= $ifname . ",";
330
		if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
331
			$data .= "up";
332
		}else if ($ifinfo['status'] == "no carrier") {
333
			$data .= "down";
334
		}else if ($ifinfo['status'] == "down") {
335
			$data .= "block";
336
		}
337
		$data .= ",";
338
		if ($ifinfo['ipaddr'])
339
			$data .= htmlspecialchars($ifinfo['ipaddr']);
340
		$data .= ",";
341
		if ($ifinfo['status'] != "down")
342
			$data .= htmlspecialchars($ifinfo['media']);
343

    
344
		$data .= "~";
345

    
346
	}
347
	return $data;
348
}
349

    
350
?>
    (1-1/1)