Project

General

Profile

Download (7.59 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
		$data .= lookup_gateway_ip_by_name($gname) . ",";
40
		if ($gateways_status[$gname]) {
41
			$gws = $gateways_status[$gname];
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 = "Gathering data";
65
			$bgcolor = "lightgray";
66
		}
67
		$data .= ($online == "Gathering data") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
68
		$data .= "<table><tr><td bgcolor=\"$bgcolor\" > $online </td></td></tr></table>";
69
	}
70
	return $data;
71
}
72

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

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

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

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

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

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

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

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

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

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

    
151
function has_temp() {
152

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

    
159
function get_hwtype() {
160

    
161
	return;
162
}
163

    
164
function get_temp() {
165
//	switch(get_hwtype()) {
166
//		default:
167
//			return;
168
//	}
169
//
170
//	return $ret;
171

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

    
180
	 return $temp_out;
181
}
182

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

    
189
	return $diskusage;
190
}
191

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

    
198
	return $swapUsage;
199
}
200

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

    
211
	return $memUsage;
212
}
213

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

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

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

    
241
function get_interfacestats() {
242
	
243
	global $config;
244
	//build interface list for widget use
245
	$ifdescrs = get_configured_interface_list();
246

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

    
280
}
281

    
282
function get_interfacestatus() {
283
	$data = "";
284
	global $config;
285

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

    
312
?>
    (1-1/1)