Project

General

Profile

Download (6.23 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 = join("|", $stats);
23
	return $stats;
24
}
25

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

    
72
function get_uptime() {
73
	$boottime = "";
74
	$matches = "";
75
	exec("/sbin/sysctl -n kern.boottime", $boottime);
76
	preg_match("/sec = (\d+)/", $boottime[0], $matches);
77
	$boottime = $matches[1];
78
	$uptime = time() - $boottime;
79

    
80
	if(intval($boottime) == 0) 
81
		return;
82
	if(intval($uptime) == 0) 
83
		return;
84

    
85
	$updays = (int)($uptime / 86400);
86
	$uptime %= 86400;
87
	$uphours = (int)($uptime / 3600);
88
	$uptime %= 3600;
89
	$upmins = (int)($uptime / 60);
90

    
91
	$uptimestr = "";
92
	if ($updays > 1)
93
		$uptimestr .= "$updays days, ";
94
	else if ($updays > 0)
95
		$uptimestr .= "1 day, ";
96
	$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
97
	return $uptimestr;
98
}
99

    
100
/* Calculates non-idle CPU time and returns as a percentage */
101
function cpu_usage() {
102
	$duration = 1;
103
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
104
	$cpuTicks = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
105
	sleep($duration);
106
	$cpuTicks2 = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
107
	
108
	$totalStart = array_sum($cpuTicks);
109
	$totalEnd = array_sum($cpuTicks2);
110

    
111
	// Something wrapped ?!?!
112
	if ($totalEnd <= $totalStart)
113
		return 0;
114

    
115
	// Calculate total cycles used
116
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
117

    
118
	// Calculate the percentage used
119
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
120
	
121
	return $cpuUsage;
122
}
123

    
124
function get_pfstate() {
125
	global $config;
126
	$matches = "";
127
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
128
	        $maxstates="{$config['system']['maximumstates']}";
129
	else
130
	        $maxstates=pfsense_default_state_size();
131
	$curentries = `/sbin/pfctl -si |grep current`;
132
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
133
	     $curentries = $matches[1];
134
	}
135
	return $curentries . "/" . $maxstates;
136
}
137

    
138
function has_temp() {
139

    
140
	/* no known temp monitors available at present */
141
	
142
	/* should only reach here if there is no hardware monitor */
143
	return false;
144
}
145

    
146
function get_hwtype() {
147

    
148
	return;
149
}
150

    
151
function get_temp() {
152
	switch(get_hwtype()) {
153
		default:
154
			return;
155
	}
156

    
157
	return $ret;
158
}
159

    
160
function disk_usage()
161
{
162
	$dfout = "";
163
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
164
	$diskusage = trim($dfout[0]);
165

    
166
	return $diskusage;
167
}
168

    
169
function swap_usage()
170
{
171
	$swapUsage = `/usr/sbin/swapinfo | /usr/bin/awk '{print $5;'}|/usr/bin/grep '%'`;
172
	$swapUsage = ereg_replace('%', "", $swapUsage);
173
	$swapUsage = rtrim($swapUsage);
174

    
175
	return $swapUsage;
176
}
177

    
178
function mem_usage() {
179
	$memory = "";
180
	exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
181
		"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
182
	
183
	$totalMem = $memory[0];
184
	$availMem = $memory[1] + $memory[2] + $memory[3];
185
	$usedMem = $totalMem - $availMem;
186
	$memUsage = round(($usedMem * 100) / $totalMem, 0);
187

    
188
	return $memUsage;
189
}
190

    
191
function update_date_time() {
192
	
193
	$datetime = date("D M j G:i:s T Y");
194
	return $datetime;
195
}
196

    
197
function get_interfacestats() {
198
	
199
	global $config;
200
	//build interface list for widget use
201
	$ifdescrs = get_configured_interface_list();
202

    
203
	$array_in_packets = array();
204
	$array_out_packets = array();
205
	$array_in_bytes = array();
206
	$array_out_bytes = array();
207
	$array_in_errors = array();
208
	$array_out_errors = array();
209
	$array_collisions = array();
210
	$array_interrupt = array();
211
	$new_data = "";
212
	
213
	//build data arrays
214
	foreach ($ifdescrs as $ifdescr => $ifname){
215
		$ifinfo = get_interface_info($ifdescr);	
216
			$new_data .= "{$ifinfo['inpkts']},";
217
			$new_data .= "{$ifinfo['outpkts']},";
218
			$new_data .= format_bytes($ifinfo['inbytes']) . ",";
219
			$new_data .= format_bytes($ifinfo['outbytes']) . ",";
220
			if (isset($ifinfo['inerrs'])){
221
				$new_data .= "{$ifinfo['inerrs']},";
222
				$new_data .= "{$ifinfo['outerrs']},";
223
			}
224
			else{
225
				$new_data .= "0,";
226
				$new_data .= "0,";
227
			}
228
			if (isset($ifinfo['collisions']))
229
				$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
230
			else
231
				$new_data .= "0,";
232
	}//end for
233
	
234
	return $new_data;
235

    
236
}
237

    
238
function get_interfacestatus() {
239
	$data = "";
240
	global $config;
241

    
242
	//build interface list for widget use
243
	$ifdescrs = get_configured_interface_with_descr();
244
	
245
	foreach ($ifdescrs as $ifdescr => $ifname){
246
		$ifinfo = get_interface_info($ifdescr);
247
		$data .= $ifname . ",";
248
		if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
249
			$data .= "up";
250
		}else if ($ifinfo['status'] == "no carrier") {
251
			$data .= "down";
252
		}else if ($ifinfo['status'] == "down") {
253
			$data .= "block";
254
		}
255
		$data .= ",";
256
		if ($ifinfo['ipaddr'])
257
			$data .= htmlspecialchars($ifinfo['ipaddr']);
258
		$data .= ",";
259
		if ($ifinfo['status'] != "down")
260
			$data .= htmlspecialchars($ifinfo['media']);
261
			
262
		$data .= "~";
263
		
264
	}	
265
	return $data;
266
}
267

    
268
?>
    (1-1/1)