Project

General

Profile

Download (7.94 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['statepercent'] = get_pfstate(true);
26
	$stats = join("|", $stats);
27
	return $stats;
28
}
29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
139
	return $cpuUsage;
140
}
141

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

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

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

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

    
170
function get_mbuf() {
171
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
172
	list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
173
	$mbufusage = round(($mbufs_total / $mbufs_max) * 100);
174
	return $mbufusage;
175
}
176

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

    
186
	return $temp_out;
187
}
188

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

    
194
	return $diskusage;
195
}
196

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

    
206
	return $swap_used;
207
}
208

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

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

    
219
	return $memUsage;
220
}
221

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

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

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

    
248
function get_interfacestats() {
249
	global $config;
250
	//build interface list for widget use
251
	$ifdescrs = get_configured_interface_list();
252

    
253
	$array_in_packets = array();
254
	$array_out_packets = array();
255
	$array_in_bytes = array();
256
	$array_out_bytes = array();
257
	$array_in_errors = array();
258
	$array_out_errors = array();
259
	$array_collisions = array();
260
	$array_interrupt = array();
261
	$new_data = "";
262

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

    
284
	return $new_data;
285
}
286

    
287
function get_interfacestatus() {
288
	$data = "";
289
	global $config;
290

    
291
	//build interface list for widget use
292
	$ifdescrs = get_configured_interface_with_descr();
293

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

    
311
		$data .= "~";
312

    
313
	}
314
	return $data;
315
}
316

    
317
?>
    (1-1/1)