Project

General

Profile

Download (6.01 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
	$gateways_status = array();
28
	$gateways_status = return_gateways_status();
29
	$data = "";
30
	$isfirst = true;
31
	foreach($gateways_status as $gw) {
32
		if(!$isfirst) 
33
			$data .= ",";
34
		$isfirst = false;
35
		$data .= $gw['name'] . ",";
36
		$data .= $gw['gateway'] . ",";
37
		$data .= $gw['delay'] . ",";
38
		$data .= $gw['loss'] . ",";
39
        switch($gw['status']) {
40
			case "None":
41
				$online = "Online";
42
				$bgcolor = "lightgreen";
43
				break;
44
			case "\"down\"":
45
				$online = "Offline";
46
				$bgcolor = "lightcoral";
47
				break;
48
			case "\"delay\"":
49
				$online = "Warning, Latency";
50
				$bgcolor = "khaki";
51
				break;
52
			case "\"loss\"":
53
				$online = "Warning, Packetloss";
54
				$bgcolor = "khaki";
55
				break;
56
			default:
57
				$online = "Gathering data";
58
		}
59
		$data .= "<table><tr><td bgcolor=\"$bgcolor\" > $online </td></td></tr></table>";
60
	}
61
	return $data;
62
}
63

    
64
function get_uptime() {
65
	$boottime = "";
66
	$matches = "";
67
	exec("/sbin/sysctl -n kern.boottime", $boottime);
68
	preg_match("/sec = (\d+)/", $boottime[0], $matches);
69
	$boottime = $matches[1];
70
	$uptime = time() - $boottime;
71

    
72
	if(intval($boottime) == 0) 
73
		return;
74
	if(intval($uptime) == 0) 
75
		return;
76

    
77
	$updays = (int)($uptime / 86400);
78
	$uptime %= 86400;
79
	$uphours = (int)($uptime / 3600);
80
	$uptime %= 3600;
81
	$upmins = (int)($uptime / 60);
82

    
83
	$uptimestr = "";
84
	if ($updays > 1)
85
		$uptimestr .= "$updays days, ";
86
	else if ($updays > 0)
87
		$uptimestr .= "1 day, ";
88
	$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
89
	return $uptimestr;
90
}
91

    
92
/* Calculates non-idle CPU time and returns as a percentage */
93
function cpu_usage() {
94
	$duration = 1;
95
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
96
	$cpuTicks = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
97
	sleep($duration);
98
	$cpuTicks2 = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
99
	
100
	$totalStart = array_sum($cpuTicks);
101
	$totalEnd = array_sum($cpuTicks2);
102

    
103
	// Something wrapped ?!?!
104
	if ($totalEnd <= $totalStart)
105
		return 0;
106

    
107
	// Calculate total cycles used
108
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
109

    
110
	// Calculate the percentage used
111
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
112
	
113
	return $cpuUsage;
114
}
115

    
116
function get_pfstate() {
117
	global $config;
118
	$matches = "";
119
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
120
	        $maxstates="{$config['system']['maximumstates']}";
121
	else
122
	        $maxstates=pfsense_default_state_size();
123
	$curentries = `/sbin/pfctl -si |grep current`;
124
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
125
	     $curentries = $matches[1];
126
	}
127
	return $curentries . "/" . $maxstates;
128
}
129

    
130
function has_temp() {
131

    
132
	/* no known temp monitors available at present */
133
	
134
	/* should only reach here if there is no hardware monitor */
135
	return false;
136
}
137

    
138
function get_hwtype() {
139

    
140
	return;
141
}
142

    
143
function get_temp() {
144
	switch(get_hwtype()) {
145
		default:
146
			return;
147
	}
148

    
149
	return $ret;
150
}
151

    
152
function disk_usage()
153
{
154
	$dfout = "";
155
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
156
	$diskusage = trim($dfout[0]);
157

    
158
	return $diskusage;
159
}
160

    
161
function swap_usage()
162
{
163
	$swapUsage = `/usr/sbin/swapinfo | /usr/bin/awk '{print $5;'}|/usr/bin/grep '%'`;
164
	$swapUsage = ereg_replace('%', "", $swapUsage);
165
	$swapUsage = rtrim($swapUsage);
166

    
167
	return $swapUsage;
168
}
169

    
170
function mem_usage() {
171
	$memory = "";
172
	exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
173
		"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
174
	
175
	$totalMem = $memory[0];
176
	$availMem = $memory[1] + $memory[2] + $memory[3];
177
	$usedMem = $totalMem - $availMem;
178
	$memUsage = round(($usedMem * 100) / $totalMem, 0);
179

    
180
	return $memUsage;
181
}
182

    
183
function update_date_time() {
184
	
185
	$datetime = date("D M j G:i:s T Y");
186
	return $datetime;
187
}
188

    
189
function get_interfacestats() {
190
	
191
	global $config;
192
	//build interface list for widget use
193
	$ifdescrs = get_configured_interface_list();
194

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

    
228
}
229

    
230
function get_interfacestatus() {
231
	$data = "";
232
	global $config;
233

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

    
260
?>
    (1-1/1)