Project

General

Profile

Download (8.04 KB) Statistics
| Branch: | Tag: | Revision:
1
<?
2
/*
3
	pfSense_MODULE:	ajax
4
*/
5

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

    
10
require_once("config.inc");
11

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

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

    
79
function get_uptime() {
80
	$boottime = "";
81
	$matches = "";
82
	exec("/sbin/sysctl -n kern.boottime", $boottime);
83
	preg_match("/sec = (\d+)/", $boottime[0], $matches);
84
	$boottime = $matches[1];
85
	$uptime = time() - $boottime;
86

    
87
	if(intval($boottime) == 0)
88
		return;
89
	if(intval($uptime) == 0)
90
		return;
91

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

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

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

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

    
112
	if ($upmins > 1)
113
		$seconds = "s";
114

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

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

    
127
	$totalStart = array_sum($cpuTicks);
128
	$totalEnd = array_sum($cpuTicks2);
129

    
130
	// Something wrapped ?!?!
131
	if ($totalEnd <= $totalStart)
132
		return 0;
133

    
134
	// Calculate total cycles used
135
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
136

    
137
	// Calculate the percentage used
138
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
139

    
140
	return $cpuUsage;
141
}
142

    
143
function get_pfstate($percent=false) {
144
	global $config;
145
	$matches = "";
146
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
147
		$maxstates="{$config['system']['maximumstates']}";
148
	else
149
		$maxstates=pfsense_default_state_size();
150
	$curentries = `/sbin/pfctl -si |grep current`;
151
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
152
		$curentries = $matches[1];
153
	}
154
	if ($percent)
155
		return round(($curentries / $maxstates) * 100, 0);
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
		return round(($mbufs_total / $mbufs_max) * 100, 0);
176
	else
177
		return "{$mbufs_total}/{$mbufs_max}";
178
}
179

    
180
function get_temp() {
181
	$temp_out = "";
182
	exec("/sbin/sysctl dev.cpu.0.temperature | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d 'C' -f 1", $dfout);
183
	$temp_out = trim($dfout[0]);
184
	if ($temp_out == "") {
185
		exec("/sbin/sysctl hw.acpi.thermal.tz0.temperature | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d 'C' -f 1", $dfout);
186
		$temp_out = trim($dfout[0]);
187
	}
188

    
189
	return $temp_out;
190
}
191

    
192
function disk_usage() {
193
	$dfout = "";
194
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
195
	$diskusage = trim($dfout[0]);
196

    
197
	return $diskusage;
198
}
199

    
200
function swap_usage() {
201
	exec("/usr/sbin/swapinfo", $swap_info);
202
	$swap_used = "";
203
	foreach ($swap_info as $line)
204
		if (preg_match('/(\d+)%$/', $line, $matches)) {
205
			$swap_used = $matches[1];
206
			break;
207
		}
208

    
209
	return $swap_used;
210
}
211

    
212
function mem_usage() {
213
	$memory = "";
214
	exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
215
		"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
216

    
217
	$totalMem = $memory[0];
218
	$availMem = $memory[1] + $memory[2] + $memory[3];
219
	$usedMem = $totalMem - $availMem;
220
	$memUsage = round(($usedMem * 100) / $totalMem, 0);
221

    
222
	return $memUsage;
223
}
224

    
225
function update_date_time() {
226
	$datetime = date("D M j G:i:s T Y");
227
	return $datetime;
228
}
229

    
230
function get_cpufreq() {
231
	$cpufreqs = "";
232
	$out = "";
233
	exec("/sbin/sysctl -n dev.cpu.0.freq_levels", $cpufreqs);
234
	$cpufreqs = explode(" ", trim($cpufreqs[0]));
235
	$maxfreq = explode("/", $cpufreqs[0]);
236
	$maxfreq = $maxfreq[0];
237
	$curfreq = "";
238
	exec("/sbin/sysctl -n dev.cpu.0.freq", $curfreq);
239
	$curfreq = trim($curfreq[0]);
240
	if (($curfreq > 0) && ($curfreq != $maxfreq))
241
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
242
	return $out;
243
}
244

    
245
function get_load_average() {
246
	$load_average = "";
247
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
248
	return $load_average[0];
249
}
250

    
251
function get_interfacestats() {
252
	global $config;
253
	//build interface list for widget use
254
	$ifdescrs = get_configured_interface_list();
255

    
256
	$array_in_packets = array();
257
	$array_out_packets = array();
258
	$array_in_bytes = array();
259
	$array_out_bytes = array();
260
	$array_in_errors = array();
261
	$array_out_errors = array();
262
	$array_collisions = array();
263
	$array_interrupt = array();
264
	$new_data = "";
265

    
266
	//build data arrays
267
	foreach ($ifdescrs as $ifdescr => $ifname){
268
		$ifinfo = get_interface_info($ifdescr);
269
			$new_data .= "{$ifinfo['inpkts']},";
270
			$new_data .= "{$ifinfo['outpkts']},";
271
			$new_data .= format_bytes($ifinfo['inbytes']) . ",";
272
			$new_data .= format_bytes($ifinfo['outbytes']) . ",";
273
			if (isset($ifinfo['inerrs'])){
274
				$new_data .= "{$ifinfo['inerrs']},";
275
				$new_data .= "{$ifinfo['outerrs']},";
276
			}
277
			else{
278
				$new_data .= "0,";
279
				$new_data .= "0,";
280
			}
281
			if (isset($ifinfo['collisions']))
282
				$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
283
			else
284
				$new_data .= "0,";
285
	}//end for
286

    
287
	return $new_data;
288
}
289

    
290
function get_interfacestatus() {
291
	$data = "";
292
	global $config;
293

    
294
	//build interface list for widget use
295
	$ifdescrs = get_configured_interface_with_descr();
296

    
297
	foreach ($ifdescrs as $ifdescr => $ifname){
298
		$ifinfo = get_interface_info($ifdescr);
299
		$data .= $ifname . ",";
300
		if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
301
			$data .= "up";
302
		}else if ($ifinfo['status'] == "no carrier") {
303
			$data .= "down";
304
		}else if ($ifinfo['status'] == "down") {
305
			$data .= "block";
306
		}
307
		$data .= ",";
308
		if ($ifinfo['ipaddr'])
309
			$data .= htmlspecialchars($ifinfo['ipaddr']);
310
		$data .= ",";
311
		if ($ifinfo['status'] != "down")
312
			$data .= htmlspecialchars($ifinfo['media']);
313

    
314
		$data .= "~";
315

    
316
	}
317
	return $data;
318
}
319

    
320
?>
    (1-1/1)