Project

General

Profile

Download (7.68 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 = join("|", $stats);
25
	return $stats;
26
}
27

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

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

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

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

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

    
103
        if ($uphours > 1)
104
           $hours = "s";
105
        
106
        if ($upmins > 1)
107
           $minutes = "s";
108
        
109
        if ($upmins > 1)
110
           $seconds = "s";
111
        
112
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
113
	return $uptimestr;
114
}
115

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

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

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

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

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

    
154
function has_temp() {
155

    
156
	/* no known temp monitors available at present */
157
	
158
	/* should only reach here if there is no hardware monitor */
159
	return false;
160
}
161

    
162
function get_hwtype() {
163

    
164
	return;
165
}
166

    
167
function get_temp() {
168
//	switch(get_hwtype()) {
169
//		default:
170
//			return;
171
//	}
172
//
173
//	return $ret;
174

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

    
183
	 return $temp_out;
184
}
185

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

    
192
	return $diskusage;
193
}
194

    
195
function swap_usage()
196
{
197
	$swapUsage = `/usr/sbin/swapinfo | /usr/bin/awk '{print $5;'}|/usr/bin/grep '%'`;
198
	$swapUsage = ereg_replace('%', "", $swapUsage);
199
	$swapUsage = rtrim($swapUsage);
200

    
201
	return $swapUsage;
202
}
203

    
204
function mem_usage() {
205
	$memory = "";
206
	exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
207
		"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
208
	
209
	$totalMem = $memory[0];
210
	$availMem = $memory[1] + $memory[2] + $memory[3];
211
	$usedMem = $totalMem - $availMem;
212
	$memUsage = round(($usedMem * 100) / $totalMem, 0);
213

    
214
	return $memUsage;
215
}
216

    
217
function update_date_time() {
218
	
219
	$datetime = date("D M j G:i:s T Y");
220
	return $datetime;
221
}
222

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

    
238
function get_load_average() {
239
	$load_average = "";
240
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
241
	return $load_average[0];
242
}
243

    
244
function get_interfacestats() {
245
	
246
	global $config;
247
	//build interface list for widget use
248
	$ifdescrs = get_configured_interface_list();
249

    
250
	$array_in_packets = array();
251
	$array_out_packets = array();
252
	$array_in_bytes = array();
253
	$array_out_bytes = array();
254
	$array_in_errors = array();
255
	$array_out_errors = array();
256
	$array_collisions = array();
257
	$array_interrupt = array();
258
	$new_data = "";
259
	
260
	//build data arrays
261
	foreach ($ifdescrs as $ifdescr => $ifname){
262
		$ifinfo = get_interface_info($ifdescr);	
263
			$new_data .= "{$ifinfo['inpkts']},";
264
			$new_data .= "{$ifinfo['outpkts']},";
265
			$new_data .= format_bytes($ifinfo['inbytes']) . ",";
266
			$new_data .= format_bytes($ifinfo['outbytes']) . ",";
267
			if (isset($ifinfo['inerrs'])){
268
				$new_data .= "{$ifinfo['inerrs']},";
269
				$new_data .= "{$ifinfo['outerrs']},";
270
			}
271
			else{
272
				$new_data .= "0,";
273
				$new_data .= "0,";
274
			}
275
			if (isset($ifinfo['collisions']))
276
				$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
277
			else
278
				$new_data .= "0,";
279
	}//end for
280
	
281
	return $new_data;
282

    
283
}
284

    
285
function get_interfacestatus() {
286
	$data = "";
287
	global $config;
288

    
289
	//build interface list for widget use
290
	$ifdescrs = get_configured_interface_with_descr();
291
	
292
	foreach ($ifdescrs as $ifdescr => $ifname){
293
		$ifinfo = get_interface_info($ifdescr);
294
		$data .= $ifname . ",";
295
		if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
296
			$data .= "up";
297
		}else if ($ifinfo['status'] == "no carrier") {
298
			$data .= "down";
299
		}else if ($ifinfo['status'] == "down") {
300
			$data .= "block";
301
		}
302
		$data .= ",";
303
		if ($ifinfo['ipaddr'])
304
			$data .= htmlspecialchars($ifinfo['ipaddr']);
305
		$data .= ",";
306
		if ($ifinfo['status'] != "down")
307
			$data .= htmlspecialchars($ifinfo['media']);
308
			
309
		$data .= "~";
310
		
311
	}	
312
	return $data;
313
}
314

    
315
?>
    (1-1/1)