Project

General

Profile

Download (6.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?
2

    
3
if(Connection_Aborted()) {
4
	exit;
5
}
6

    
7
require_once("config.inc");
8

    
9
function get_stats() {
10
	$stats['cpu'] = cpu_usage();
11
	$stats['mem'] = mem_usage();
12
	$stats['uptime'] = get_uptime();
13
	$stats['states'] = get_pfstate();
14
	$stats['temp'] = get_temp();
15
	$stats['datetime'] = update_date_time();
16
	$stats['interfacestatistics'] = get_interfacestats();
17
	$stats['interfacestatus'] = get_interfacestatus();
18
	$stats['gateways'] = get_gatewaystats();
19
	$stats = join("|", $stats);
20
	return $stats;
21
}
22

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

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

    
69
	if(intval($boottime) == 0) 
70
		return;
71
	if(intval($uptime) == 0) 
72
		return;
73

    
74
	$updays = (int)($uptime / 86400);
75
	$uptime %= 86400;
76
	$uphours = (int)($uptime / 3600);
77
	$uptime %= 3600;
78
	$upmins = (int)($uptime / 60);
79

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

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

    
100
	// Something wrapped ?!?!
101
	if ($totalEnd <= $totalStart)
102
		return 0;
103

    
104
	// Calculate total cycles used
105
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
106

    
107
	// Calculate the percentage used
108
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
109
	
110
	return $cpuUsage;
111
}
112

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

    
127
function has_temp() {
128

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

    
135
function get_hwtype() {
136

    
137
	return;
138
}
139

    
140
function get_temp() {
141
	switch(get_hwtype()) {
142
		default:
143
			return;
144
	}
145

    
146
	return $ret;
147
}
148

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

    
155
	return $diskusage;
156
}
157

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

    
164
	return $swapUsage;
165
}
166

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

    
178
	return $memUsage;
179
}
180

    
181
<<<<<<< HEAD:usr/local/www/includes/functions.inc.php
182
function update_date_time() {
183
	
184
	$datetime = date("D M j G:i:s T Y");
185
	return $datetime;
186
}
187

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

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

    
230
}
231

    
232
function get_interfacestatus(){
233
	$data = "";
234
	global $config;
235

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

    
266
?>
267
=======
268
?>
269
>>>>>>> 9d75714... cleaner and more proper cpu graph:usr/local/www/includes/functions.inc.php
(1-1/3)