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
|
$a_gateways = return_gateways_array();
|
28
|
$gateways_status = array();
|
29
|
$gateways_status = return_gateways_status(true);
|
30
|
$data = "";
|
31
|
$isfirst = true;
|
32
|
foreach($a_gateways as $gname => $gw) {
|
33
|
if(!$isfirst)
|
34
|
$data .= ",";
|
35
|
$isfirst = false;
|
36
|
$data .= $gw['name'] . ",";
|
37
|
$data .= lookup_gateway_ip_by_name($gname) . ",";
|
38
|
if ($gateways_status[$gname]) {
|
39
|
$gws = $gateways_status[$gname];
|
40
|
switch(strtolower($gws['status'])) {
|
41
|
case "none":
|
42
|
$online = "Online";
|
43
|
$bgcolor = "lightgreen";
|
44
|
break;
|
45
|
case "down":
|
46
|
$online = "Offline";
|
47
|
$bgcolor = "lightcoral";
|
48
|
break;
|
49
|
case "delay":
|
50
|
$online = "Warning: Latency";
|
51
|
$bgcolor = "khaki";
|
52
|
break;
|
53
|
case "loss":
|
54
|
$online = "Warning: Packetloss";
|
55
|
$bgcolor = "khaki";
|
56
|
break;
|
57
|
default:
|
58
|
$online = "Gathering data";
|
59
|
break;
|
60
|
}
|
61
|
} else {
|
62
|
$online = "Gathering data";
|
63
|
$bgcolor = "lightgray";
|
64
|
}
|
65
|
$data .= ($online == "Gathering data") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
|
66
|
$data .= "<table><tr><td bgcolor=\"$bgcolor\" > $online </td></td></tr></table>";
|
67
|
}
|
68
|
return $data;
|
69
|
}
|
70
|
|
71
|
function get_uptime() {
|
72
|
$boottime = "";
|
73
|
$matches = "";
|
74
|
exec("/sbin/sysctl -n kern.boottime", $boottime);
|
75
|
preg_match("/sec = (\d+)/", $boottime[0], $matches);
|
76
|
$boottime = $matches[1];
|
77
|
$uptime = time() - $boottime;
|
78
|
|
79
|
if(intval($boottime) == 0)
|
80
|
return;
|
81
|
if(intval($uptime) == 0)
|
82
|
return;
|
83
|
|
84
|
$updays = (int)($uptime / 86400);
|
85
|
$uptime %= 86400;
|
86
|
$uphours = (int)($uptime / 3600);
|
87
|
$uptime %= 3600;
|
88
|
$upmins = (int)($uptime / 60);
|
89
|
|
90
|
$uptimestr = "";
|
91
|
if ($updays > 1)
|
92
|
$uptimestr .= "$updays days, ";
|
93
|
else if ($updays > 0)
|
94
|
$uptimestr .= "1 day, ";
|
95
|
$uptimestr .= sprintf("%02d:%02d", $uphours, $upmins);
|
96
|
return $uptimestr;
|
97
|
}
|
98
|
|
99
|
/* Calculates non-idle CPU time and returns as a percentage */
|
100
|
function cpu_usage() {
|
101
|
$duration = 1;
|
102
|
$diff = array('user', 'nice', 'sys', 'intr', 'idle');
|
103
|
$cpuTicks = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
|
104
|
sleep($duration);
|
105
|
$cpuTicks2 = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
|
106
|
|
107
|
$totalStart = array_sum($cpuTicks);
|
108
|
$totalEnd = array_sum($cpuTicks2);
|
109
|
|
110
|
// Something wrapped ?!?!
|
111
|
if ($totalEnd <= $totalStart)
|
112
|
return 0;
|
113
|
|
114
|
// Calculate total cycles used
|
115
|
$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
|
116
|
|
117
|
// Calculate the percentage used
|
118
|
$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
|
119
|
|
120
|
return $cpuUsage;
|
121
|
}
|
122
|
|
123
|
function get_pfstate() {
|
124
|
global $config;
|
125
|
$matches = "";
|
126
|
if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
|
127
|
$maxstates="{$config['system']['maximumstates']}";
|
128
|
else
|
129
|
$maxstates=pfsense_default_state_size();
|
130
|
$curentries = `/sbin/pfctl -si |grep current`;
|
131
|
if (preg_match("/([0-9]+)/", $curentries, $matches)) {
|
132
|
$curentries = $matches[1];
|
133
|
}
|
134
|
return $curentries . "/" . $maxstates;
|
135
|
}
|
136
|
|
137
|
function has_temp() {
|
138
|
|
139
|
/* no known temp monitors available at present */
|
140
|
|
141
|
/* should only reach here if there is no hardware monitor */
|
142
|
return false;
|
143
|
}
|
144
|
|
145
|
function get_hwtype() {
|
146
|
|
147
|
return;
|
148
|
}
|
149
|
|
150
|
function get_temp() {
|
151
|
switch(get_hwtype()) {
|
152
|
default:
|
153
|
return;
|
154
|
}
|
155
|
|
156
|
return $ret;
|
157
|
}
|
158
|
|
159
|
function disk_usage()
|
160
|
{
|
161
|
$dfout = "";
|
162
|
exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
|
163
|
$diskusage = trim($dfout[0]);
|
164
|
|
165
|
return $diskusage;
|
166
|
}
|
167
|
|
168
|
function swap_usage()
|
169
|
{
|
170
|
$swapUsage = `/usr/sbin/swapinfo | /usr/bin/awk '{print $5;'}|/usr/bin/grep '%'`;
|
171
|
$swapUsage = ereg_replace('%', "", $swapUsage);
|
172
|
$swapUsage = rtrim($swapUsage);
|
173
|
|
174
|
return $swapUsage;
|
175
|
}
|
176
|
|
177
|
function mem_usage() {
|
178
|
$memory = "";
|
179
|
exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
|
180
|
"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
|
181
|
|
182
|
$totalMem = $memory[0];
|
183
|
$availMem = $memory[1] + $memory[2] + $memory[3];
|
184
|
$usedMem = $totalMem - $availMem;
|
185
|
$memUsage = round(($usedMem * 100) / $totalMem, 0);
|
186
|
|
187
|
return $memUsage;
|
188
|
}
|
189
|
|
190
|
function update_date_time() {
|
191
|
|
192
|
$datetime = date("D M j G:i:s T Y");
|
193
|
return $datetime;
|
194
|
}
|
195
|
|
196
|
function get_interfacestats() {
|
197
|
|
198
|
global $config;
|
199
|
//build interface list for widget use
|
200
|
$ifdescrs = get_configured_interface_list();
|
201
|
|
202
|
$array_in_packets = array();
|
203
|
$array_out_packets = array();
|
204
|
$array_in_bytes = array();
|
205
|
$array_out_bytes = array();
|
206
|
$array_in_errors = array();
|
207
|
$array_out_errors = array();
|
208
|
$array_collisions = array();
|
209
|
$array_interrupt = array();
|
210
|
$new_data = "";
|
211
|
|
212
|
//build data arrays
|
213
|
foreach ($ifdescrs as $ifdescr => $ifname){
|
214
|
$ifinfo = get_interface_info($ifdescr);
|
215
|
$new_data .= "{$ifinfo['inpkts']},";
|
216
|
$new_data .= "{$ifinfo['outpkts']},";
|
217
|
$new_data .= format_bytes($ifinfo['inbytes']) . ",";
|
218
|
$new_data .= format_bytes($ifinfo['outbytes']) . ",";
|
219
|
if (isset($ifinfo['inerrs'])){
|
220
|
$new_data .= "{$ifinfo['inerrs']},";
|
221
|
$new_data .= "{$ifinfo['outerrs']},";
|
222
|
}
|
223
|
else{
|
224
|
$new_data .= "0,";
|
225
|
$new_data .= "0,";
|
226
|
}
|
227
|
if (isset($ifinfo['collisions']))
|
228
|
$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
|
229
|
else
|
230
|
$new_data .= "0,";
|
231
|
}//end for
|
232
|
|
233
|
return $new_data;
|
234
|
|
235
|
}
|
236
|
|
237
|
function get_interfacestatus() {
|
238
|
$data = "";
|
239
|
global $config;
|
240
|
|
241
|
//build interface list for widget use
|
242
|
$ifdescrs = get_configured_interface_with_descr();
|
243
|
|
244
|
foreach ($ifdescrs as $ifdescr => $ifname){
|
245
|
$ifinfo = get_interface_info($ifdescr);
|
246
|
$data .= $ifname . ",";
|
247
|
if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
|
248
|
$data .= "up";
|
249
|
}else if ($ifinfo['status'] == "no carrier") {
|
250
|
$data .= "down";
|
251
|
}else if ($ifinfo['status'] == "down") {
|
252
|
$data .= "block";
|
253
|
}
|
254
|
$data .= ",";
|
255
|
if ($ifinfo['ipaddr'])
|
256
|
$data .= htmlspecialchars($ifinfo['ipaddr']);
|
257
|
$data .= ",";
|
258
|
if ($ifinfo['status'] != "down")
|
259
|
$data .= htmlspecialchars($ifinfo['media']);
|
260
|
|
261
|
$data .= "~";
|
262
|
|
263
|
}
|
264
|
return $data;
|
265
|
}
|
266
|
|
267
|
?>
|