Project

General

Profile

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

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

    
7
require_once("config.inc");
8
require_once('guiconfig.inc');
9

    
10

    
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

    
22
	$stats = join("|", $stats);
23

    
24
	return $stats;
25
}
26

    
27

    
28
function get_uptime() {
29
	$boottime = "";
30
	$matches = "";
31
	exec("/sbin/sysctl -n kern.boottime", $boottime);
32
	preg_match("/sec = (\d+)/", $boottime[0], $matches);
33
	$boottime = $matches[1];
34
	$uptime = time() - $boottime;
35

    
36
	$updays = (int)($uptime / 86400);
37
	$uptime %= 86400;
38
	$uphours = (int)($uptime / 3600);
39
	$uptime %= 3600;
40
	$upmins = (int)($uptime / 60);
41

    
42
	$uptimestr = "";
43
	if ($updays > 1)
44
		$uptimestr .= "$updays days, ";
45
	else if ($updays > 0)
46
		$uptimestr .= "1 day, ";
47
	$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
48
	return $uptimestr;
49
}
50

    
51
function cpu_usage() {
52
	exec("sysctl -n kern.cp_time && sleep 1 && sysctl -n kern.cp_time",$cpuTicksEx);
53

    
54
	$cpuTicks = explode(" ", $cpuTicksEx[0]);
55
	$cpuTicks2 = explode(" ", $cpuTicksEx[1]);
56
	
57
	$diff = array();
58
    $diff['user'] = ($cpuTicks2[0] - $cpuTicks[0]);
59
    $diff['nice'] = ($cpuTicks2[1] - $cpuTicks[1]);
60
    $diff['sys'] = ($cpuTicks2[2] - $cpuTicks[2]);
61
    $diff['intr'] = ($cpuTicks2[3] - $cpuTicks[3]);
62
    $diff['idle'] = ($cpuTicks2[4] - $cpuTicks[4]);
63
    
64
    //echo "<!-- user: {$diff['user']}  nice {$diff['nice']}  sys {$diff['sys']}  intr {$diff['intr']}  idle {$diff['idle']} -->";
65
    $totalDiff = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'] + $diff['idle'];
66
    $totalused = $diff['user'] + $diff['nice'] + $diff['sys'] + $diff['intr'];
67
        if (isset($totalused)&&$totalused <= 0) {
68
            $totalused = 0.001;
69
        }
70
    $cpuUsage = floor(100 * ($totalused / $totalDiff));
71
	return $cpuUsage;
72
}
73

    
74
function get_pfstate() {
75
	global $config;
76
	$matches = "";
77
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
78
	        $maxstates="{$config['system']['maximumstates']}";
79
	else
80
	        $maxstates=pfsense_default_state_size();
81
	$curentries = `/sbin/pfctl -si |grep current`;
82
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
83
	     $curentries = $matches[1];
84
	}
85
	return $curentries . "/" . $maxstates;
86
}
87

    
88
function has_temp() {
89
	if(`/sbin/dmesg -a | /usr/bin/grep net4801` <> "") {
90
		/* Initialize hw monitor */
91
		exec("/usr/local/sbin/env4801 -i");
92
		return true;
93
	}
94

    
95
	/* should only reach here if there is no hardware monitor */
96
	return false;
97
}
98

    
99
function get_hwtype() {
100
        if(`/sbin/dmesg -a | /usr/bin/grep net4801` <> "") {
101
                return "4801";
102
        }
103

    
104
	return;
105
}
106

    
107
function get_temp() {
108
	switch(get_hwtype()) {
109
		case '4801':
110
			$ret = rtrim(`/usr/local/sbin/env4801 | /usr/bin/grep Temp |/usr/bin/cut -c24-25`);
111
			break;
112
		default:
113
			return;
114
	}
115

    
116
	return $ret;
117
}
118

    
119
function disk_usage()
120
{
121
	$dfout = "";
122
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
123
	$diskusage = trim($dfout[0]);
124

    
125
	return $diskusage;
126
}
127

    
128
function swap_usage()
129
{
130
	$swapUsage = `/usr/sbin/swapinfo | /usr/bin/awk '{print $5;'}|/usr/bin/grep '%'`;
131
	$swapUsage = ereg_replace('%', "", $swapUsage);
132
	$swapUsage = rtrim($swapUsage);
133

    
134
	return $swapUsage;
135
}
136

    
137
function mem_usage()
138
{
139
	$memory = "";
140
	exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
141
		"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
142
	
143
	$totalMem = $memory[0];
144
	$availMem = $memory[1] + $memory[2] + $memory[3];
145
	$usedMem = $totalMem - $availMem;
146
	$memUsage = round(($usedMem * 100) / $totalMem, 0);
147

    
148
	return $memUsage;
149
}
150

    
151
function update_date_time() {
152
	
153
	$datetime = date("D M j G:i:s T Y");
154
	return $datetime;
155
}
156

    
157
function get_interfacestats(){
158
	
159
	global $config;
160
	//build interface list for widget use
161
	$i = 0; $ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
162
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
163
		$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
164
	}
165
	$array_in_packets = array();
166
	$array_out_packets = array();
167
	$array_in_bytes = array();
168
	$array_out_bytes = array();
169
	$array_in_errors = array();
170
	$array_out_errors = array();
171
	$array_collisions = array();
172
	$array_interrupt = array();
173
	$new_data = "";
174
	
175
	//build data arrays
176
	foreach ($ifdescrs as $ifdescr => $ifname){
177
		$ifinfo = get_interface_info($ifdescr);	
178
			$new_data .= "{$ifinfo['inpkts']},";
179
			$new_data .= "{$ifinfo['outpkts']},";
180
			$new_data .= format_bytes($ifinfo['inbytes']) . ",";
181
			$new_data .= format_bytes($ifinfo['outbytes']) . ",";
182
			if (isset($ifinfo['inerrs'])){
183
				$new_data .= "{$ifinfo['inerrs']},";
184
				$new_data .= "{$ifinfo['outerrs']},";
185
			}
186
			else{
187
				$new_data .= "0,";
188
				$new_data .= "0,";
189
			}
190
			if (isset($ifinfo['collisions']))
191
				$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
192
			else
193
				$new_data .= "0,";
194
		
195
				
196
		
197
	}//end for
198
	
199
	return $new_data;
200

    
201
}
202

    
203
function get_interfacestatus(){
204
	$data = "";
205
	global $config;
206

    
207
	//build interface list for widget use
208
	$i = 0; $ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
209
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
210
		$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
211
	}
212
	
213
	foreach ($ifdescrs as $ifdescr => $ifname){
214
		$ifinfo = get_interface_info($ifdescr);
215
		$data .= $ifname . ",";
216
		if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
217
			$data .= "up";
218
		}else if ($ifinfo['status'] == "no carrier") {
219
			$data .= "down";
220
		}else if ($ifinfo['status'] == "down") {
221
			$data .= "block";
222
		}
223
		$data .= ",";
224
		if ($ifinfo['ipaddr']){ 
225
			$data .= htmlspecialchars($ifinfo['ipaddr']);
226
			if ($ifinfo['dhcplink']) {
227
				$data .= " (DHCP)";
228
			}
229
		}
230
		$data .= ",";
231
		if ($ifinfo['status'] != "down")
232
			$data .= htmlspecialchars($ifinfo['media']);
233
			
234
		$data .= "~";
235
		
236
	}	
237
	return $data;
238
}
239

    
(1-1/3)