1 |
17623ab5
|
Bill Marquette
|
<?php
|
2 |
|
|
/*
|
3 |
cdcea13f
|
Seth Mos
|
Copyright (C) 2008 Bill Marquette, Seth Mos
|
4 |
c1191d5b
|
Ermal
|
Copyright (C) 2010 Ermal Lu?i
|
5 |
17623ab5
|
Bill Marquette
|
All rights reserved.
|
6 |
|
|
|
7 |
|
|
Redistribution and use in source and binary forms, with or without
|
8 |
|
|
modification, are permitted provided that the following conditions are met:
|
9 |
|
|
|
10 |
c1191d5b
|
Ermal
|
1. Redistributions of source code must retain the above copyright notice,
|
11 |
17623ab5
|
Bill Marquette
|
this list of conditions and the following disclaimer.
|
12 |
|
|
|
13 |
|
|
2. Redistributions in binary form must reproduce the above copyright
|
14 |
|
|
notice, this list of conditions and the following disclaimer in the
|
15 |
|
|
documentation and/or other materials provided with the distribution.
|
16 |
|
|
|
17 |
|
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
18 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
19 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
20 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
|
28 |
523855b0
|
Scott Ullrich
|
pfSense_BUILDER_BINARIES: /usr/bin/killall /sbin/route /usr/local/sbin/apinger
|
29 |
|
|
pfSense_MODULE: routing
|
30 |
|
|
|
31 |
c1191d5b
|
Ermal
|
*/
|
32 |
17623ab5
|
Bill Marquette
|
|
33 |
3d471a14
|
Ermal
|
/*
|
34 |
|
|
* Creates monitoring configuration file and
|
35 |
|
|
* adds apropriate static routes.
|
36 |
cdcea13f
|
Seth Mos
|
*/
|
37 |
|
|
function setup_gateways_monitor() {
|
38 |
3d471a14
|
Ermal
|
global $config, $g;
|
39 |
ffe76308
|
Seth Mos
|
|
40 |
3d471a14
|
Ermal
|
$gateways_arr = return_gateways_array();
|
41 |
|
|
if (!is_array($gateways_arr)) {
|
42 |
|
|
log_error("No gateways to monitor. Apinger will not be run.");
|
43 |
|
|
killbypid("{$g['varrun_path']}/apinger.pid");
|
44 |
|
|
@unlink("{$g['tmp_path']}/apinger.status");
|
45 |
|
|
return;
|
46 |
|
|
}
|
47 |
cdcea13f
|
Seth Mos
|
|
48 |
3d471a14
|
Ermal
|
/* Default settings. Probably should move to globals.inc? */
|
49 |
023920e7
|
Ermal
|
$a_settings = array();
|
50 |
|
|
$a_settings['latencylow'] = "200";
|
51 |
|
|
$a_settings['latencyhigh'] = "500";
|
52 |
|
|
$a_settings['losslow'] = "10";
|
53 |
|
|
$a_settings['losshigh'] = "20";
|
54 |
37c98ef9
|
Seth Mos
|
|
55 |
cdcea13f
|
Seth Mos
|
$fd = fopen("{$g['varetc_path']}/apinger.conf", "w");
|
56 |
|
|
$apingerconfig = <<<EOD
|
57 |
|
|
|
58 |
|
|
# pfSense apinger configuration file. Automatically Generated!
|
59 |
|
|
|
60 |
|
|
## User and group the pinger should run as
|
61 |
0534d60a
|
Chris Buechler
|
user "root"
|
62 |
|
|
group "wheel"
|
63 |
cdcea13f
|
Seth Mos
|
|
64 |
|
|
## Mailer to use (default: "/usr/lib/sendmail -t")
|
65 |
|
|
#mailer "/var/qmail/bin/qmail-inject"
|
66 |
|
|
|
67 |
|
|
## Location of the pid-file (default: "/var/run/apinger.pid")
|
68 |
|
|
pid_file "{$g['varrun_path']}/apinger.pid"
|
69 |
|
|
|
70 |
|
|
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
|
71 |
|
|
#timestamp_format "%Y%m%d%H%M%S"
|
72 |
|
|
|
73 |
|
|
status {
|
74 |
|
|
## File where the status information whould be written to
|
75 |
298e5e0a
|
Ermal
|
file "{$g['tmp_path']}/apinger.status"
|
76 |
cdcea13f
|
Seth Mos
|
## Interval between file updates
|
77 |
|
|
## when 0 or not set, file is written only when SIGUSR1 is received
|
78 |
ecb08c6a
|
Ermal
|
interval 5s
|
79 |
cdcea13f
|
Seth Mos
|
}
|
80 |
|
|
|
81 |
|
|
########################################
|
82 |
|
|
# RRDTool status gathering configuration
|
83 |
|
|
# Interval between RRD updates
|
84 |
|
|
rrd interval 60s;
|
85 |
|
|
|
86 |
|
|
## These parameters can be overriden in a specific alarm configuration
|
87 |
|
|
alarm default {
|
88 |
0ae6daf8
|
Ermal
|
command on "/usr/local/sbin/pfSctl -c 'filter reload'"
|
89 |
|
|
command off "/usr/local/sbin/pfSctl -c 'filter reload'"
|
90 |
cdcea13f
|
Seth Mos
|
combine 10s
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
## "Down" alarm definition.
|
94 |
|
|
## This alarm will be fired when target doesn't respond for 30 seconds.
|
95 |
|
|
alarm down "down" {
|
96 |
|
|
time 10s
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
## "Delay" alarm definition.
|
100 |
|
|
## This alarm will be fired when responses are delayed more than 200ms
|
101 |
|
|
## it will be canceled, when the delay drops below 100ms
|
102 |
|
|
alarm delay "delay" {
|
103 |
37c98ef9
|
Seth Mos
|
delay_low {$a_settings['latencylow']}ms
|
104 |
|
|
delay_high {$a_settings['latencyhigh']}ms
|
105 |
cdcea13f
|
Seth Mos
|
}
|
106 |
|
|
|
107 |
|
|
## "Loss" alarm definition.
|
108 |
|
|
## This alarm will be fired when packet loss goes over 20%
|
109 |
|
|
## it will be canceled, when the loss drops below 10%
|
110 |
|
|
alarm loss "loss" {
|
111 |
37c98ef9
|
Seth Mos
|
percent_low {$a_settings['losslow']}
|
112 |
|
|
percent_high {$a_settings['losshigh']}
|
113 |
cdcea13f
|
Seth Mos
|
}
|
114 |
|
|
|
115 |
|
|
target default {
|
116 |
|
|
## How often the probe should be sent
|
117 |
|
|
interval 1s
|
118 |
|
|
|
119 |
|
|
## How many replies should be used to compute average delay
|
120 |
|
|
## for controlling "delay" alarms
|
121 |
|
|
avg_delay_samples 10
|
122 |
|
|
|
123 |
|
|
## How many probes should be used to compute average loss
|
124 |
|
|
avg_loss_samples 50
|
125 |
|
|
|
126 |
|
|
## The delay (in samples) after which loss is computed
|
127 |
|
|
## without this delays larger than interval would be treated as loss
|
128 |
|
|
avg_loss_delay_samples 20
|
129 |
|
|
|
130 |
|
|
## Names of the alarms that may be generated for the target
|
131 |
|
|
alarms "down","delay","loss"
|
132 |
|
|
|
133 |
|
|
## Location of the RRD
|
134 |
1bce2729
|
Seth Mos
|
#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
|
135 |
cdcea13f
|
Seth Mos
|
}
|
136 |
|
|
|
137 |
|
|
EOD;
|
138 |
|
|
|
139 |
3d471a14
|
Ermal
|
foreach($gateways_arr as $name => $gateway) {
|
140 |
|
|
if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
|
141 |
|
|
if (is_ipaddr($gateway['gateway']))
|
142 |
|
|
$gateway['monitor'] = $gateway['gateway'];
|
143 |
|
|
else /* No chance to get an ip to monitor skip target. */
|
144 |
f31ab121
|
Ermal
|
continue;
|
145 |
3d471a14
|
Ermal
|
}
|
146 |
f31ab121
|
Ermal
|
|
147 |
3d471a14
|
Ermal
|
/* Interface ip is needed since apinger will bind a socket to it. */
|
148 |
01f1b601
|
Ermal
|
$gwifip = find_interface_ip($gateway['interface'], true);
|
149 |
3d471a14
|
Ermal
|
if (!is_ipaddr($gwifip))
|
150 |
|
|
continue; //Skip this target
|
151 |
|
|
|
152 |
f44f8eb5
|
Ermal
|
$apingercfg = "target \"{$gateway['monitor']}\" {\n";
|
153 |
68f291ff
|
Ermal
|
$apingercfg .= " description \"{$name}\"\n";
|
154 |
3d471a14
|
Ermal
|
$apingercfg .= " srcip \"{$gwifip}\"\n";
|
155 |
|
|
$alarms = "";
|
156 |
f44f8eb5
|
Ermal
|
$alarmscfg = "";
|
157 |
3d471a14
|
Ermal
|
$override = false;
|
158 |
|
|
if (!empty($gateway['lowloss'])) {
|
159 |
68f291ff
|
Ermal
|
$alarmscfg .= "alarm loss \"{$name}loss\" {\n";
|
160 |
3d471a14
|
Ermal
|
$alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
|
161 |
|
|
$alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
|
162 |
|
|
$alarmscfg .= "}\n";
|
163 |
68f291ff
|
Ermal
|
$alarms .= "\"{$name}loss\"";
|
164 |
3d471a14
|
Ermal
|
$override = true;
|
165 |
|
|
} else {
|
166 |
023920e7
|
Ermal
|
if ($override == true)
|
167 |
3d471a14
|
Ermal
|
$alarms .= ",";
|
168 |
|
|
$alarms .= "\"loss\"";
|
169 |
|
|
$override = true;
|
170 |
|
|
}
|
171 |
|
|
if (!empty($gateway['latencylow'])) {
|
172 |
68f291ff
|
Ermal
|
$alarmscfg .= "alarm delay \"{$name}delay\" {\n";
|
173 |
3d471a14
|
Ermal
|
$alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
|
174 |
|
|
$alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
|
175 |
|
|
$alarmscfg .= "}\n";
|
176 |
|
|
if ($override == true)
|
177 |
|
|
$alarms .= ",";
|
178 |
68f291ff
|
Ermal
|
$alarms .= "\"{$name}delay\"";
|
179 |
3d471a14
|
Ermal
|
$override = true;
|
180 |
|
|
} else {
|
181 |
|
|
if ($override == true)
|
182 |
|
|
$alarms .= ",";
|
183 |
|
|
$alarms .= "\"delay\"";
|
184 |
|
|
$override = true;
|
185 |
|
|
}
|
186 |
|
|
if (!empty($gateway['down'])) {
|
187 |
68f291ff
|
Ermal
|
$alarmscfg .= "alarm down \"{$name}down\" {\n";
|
188 |
3d471a14
|
Ermal
|
$alarmscfg .= "\ttime {$gateway['down']}s\n";
|
189 |
|
|
$alarmscfg .= "}\n";
|
190 |
|
|
if ($override == true)
|
191 |
|
|
$alarms .= ",";
|
192 |
68f291ff
|
Ermal
|
$alarms .= "\"{$name}down\"";
|
193 |
3d471a14
|
Ermal
|
$override = true;
|
194 |
|
|
} else {
|
195 |
|
|
if ($override == true)
|
196 |
|
|
$alarms .= ",";
|
197 |
|
|
$alarms .= "\"down\"";
|
198 |
|
|
$override = true;
|
199 |
cdcea13f
|
Seth Mos
|
}
|
200 |
3d471a14
|
Ermal
|
if ($override == true)
|
201 |
|
|
$apingercfg .= "\talarms override {$alarms};\n";
|
202 |
|
|
|
203 |
|
|
$apingercfg .= " rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
|
204 |
|
|
$apingercfg .= "}\n";
|
205 |
|
|
$apingercfg .= "\n";
|
206 |
|
|
/*
|
207 |
|
|
* If the gateway is the same as the monitor we do not add a
|
208 |
|
|
* route as this will break the routing table.
|
209 |
|
|
* Add static routes for each gateway with their monitor IP
|
210 |
|
|
* not strictly necessary but is a added level of protection.
|
211 |
|
|
*/
|
212 |
|
|
if (is_ipaddr($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
|
213 |
|
|
log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$gateway['gateway']}");
|
214 |
|
|
mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']), true);
|
215 |
|
|
mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
|
216 |
435a418f
|
Ermal
|
" " . escapeshellarg($gateway['gateway']), true);
|
217 |
3d471a14
|
Ermal
|
}
|
218 |
|
|
|
219 |
023920e7
|
Ermal
|
$apingerconfig .= $alarmscfg;
|
220 |
|
|
$apingerconfig .= $apingercfg;
|
221 |
cdcea13f
|
Seth Mos
|
}
|
222 |
|
|
fwrite($fd, $apingerconfig);
|
223 |
|
|
fclose($fd);
|
224 |
|
|
|
225 |
20f26a50
|
Ermal
|
killbypid("{$g['varrun_path']}/apinger.pid");
|
226 |
|
|
if (is_dir("{$g['tmp_path']}"))
|
227 |
|
|
chmod("{$g['tmp_path']}", 01777);
|
228 |
17649c87
|
Ermal
|
if (!is_dir("{$g['vardb_path']}/rrd"))
|
229 |
21934843
|
jim-p
|
mkdir("{$g['vardb_path']}/rrd", 0775);
|
230 |
17649c87
|
Ermal
|
|
231 |
|
|
@chown("{$g['vardb_path']}/rrd", "nobody");
|
232 |
|
|
|
233 |
20f26a50
|
Ermal
|
/* start a new apinger process */
|
234 |
298e5e0a
|
Ermal
|
@unlink("{$g['tmp_path']}/apinger.status");
|
235 |
9132ae35
|
Ermal
|
sleep(1);
|
236 |
20f26a50
|
Ermal
|
mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
|
237 |
4f060616
|
Ermal
|
|
238 |
cdcea13f
|
Seth Mos
|
return 0;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
/* return the status of the apinger targets as a array */
|
242 |
3d471a14
|
Ermal
|
function return_gateways_status($byname = false) {
|
243 |
ae9618af
|
Ermal
|
global $config, $g;
|
244 |
cdcea13f
|
Seth Mos
|
|
245 |
|
|
$apingerstatus = array();
|
246 |
d9dda2a5
|
Ermal
|
if (file_exists("{$g['tmp_path']}/apinger.status")) {
|
247 |
cdcea13f
|
Seth Mos
|
$apingerstatus = file("{$g['tmp_path']}/apinger.status");
|
248 |
2328dcc5
|
Seth Mos
|
}
|
249 |
cdcea13f
|
Seth Mos
|
|
250 |
67ee1ec5
|
Ermal Luçi
|
$status = array();
|
251 |
cdcea13f
|
Seth Mos
|
foreach($apingerstatus as $line) {
|
252 |
75466131
|
Ermal
|
$info = explode("|", $line);
|
253 |
3d471a14
|
Ermal
|
if ($byname == false)
|
254 |
|
|
$target = $info[0];
|
255 |
|
|
else
|
256 |
|
|
$target = $info[2];
|
257 |
|
|
$status[$target]['monitorip'] = $info[0];
|
258 |
79b7f498
|
Ermal
|
$status[$target]['srcip'] = $info[1];
|
259 |
|
|
$status[$target]['name'] = $info[2];
|
260 |
|
|
$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
|
261 |
|
|
$status[$target]['delay'] = empty($info[6]) ? 0 : $info[6];
|
262 |
|
|
$status[$target]['loss'] = empty($info[7]) ? "0.0%" : $info[7] . "";
|
263 |
|
|
$status[$target]['status'] = trim($info[8]);
|
264 |
cdcea13f
|
Seth Mos
|
}
|
265 |
68f291ff
|
Ermal
|
|
266 |
cdcea13f
|
Seth Mos
|
return($status);
|
267 |
17623ab5
|
Bill Marquette
|
}
|
268 |
|
|
|
269 |
2328dcc5
|
Seth Mos
|
/* Return all configured gateways on the system */
|
270 |
c795339e
|
Ermal Lu?i
|
function return_gateways_array($disabled = false) {
|
271 |
8d3556c2
|
gnhb
|
global $config, $g;
|
272 |
7901dff5
|
Ermal Luçi
|
|
273 |
c66b4242
|
Seth Mos
|
$gateways_arr = array();
|
274 |
|
|
|
275 |
01ba0a72
|
Ermal Lu?i
|
$i = 0;
|
276 |
3d471a14
|
Ermal
|
/* Process/add all the configured gateways. */
|
277 |
ef05ae5f
|
Ermal
|
if (is_array($config['gateways']['gateway_item'])) {
|
278 |
2328dcc5
|
Seth Mos
|
foreach($config['gateways']['gateway_item'] as $gateway) {
|
279 |
78ae2b14
|
Ermal
|
if(empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
|
280 |
2328dcc5
|
Seth Mos
|
$gateway['gateway'] = get_interface_gateway($gateway['interface']);
|
281 |
|
|
/* no IP address found, set to dynamic */
|
282 |
3d471a14
|
Ermal
|
if(! is_ipaddr($gateway['gateway']))
|
283 |
2328dcc5
|
Seth Mos
|
$gateway['gateway'] = "dynamic";
|
284 |
a2532739
|
Ermal Lu?i
|
$gateway['dynamic'] = true;
|
285 |
2328dcc5
|
Seth Mos
|
}
|
286 |
3d471a14
|
Ermal
|
if(empty($gateway['monitor']))
|
287 |
2328dcc5
|
Seth Mos
|
$gateway['monitor'] = $gateway['gateway'];
|
288 |
3d471a14
|
Ermal
|
|
289 |
883c53c9
|
Ermal Lu?i
|
$gateway['friendlyiface'] = $gateway['interface'];
|
290 |
3d471a14
|
Ermal
|
$gateway['interface'] = get_real_interface($gateway['interface']);
|
291 |
979c816c
|
Ermal
|
/* FIXME: Should this be enabled.
|
292 |
|
|
* Some interface like wan might be default but have no info recorded
|
293 |
bb8f919b
|
Ermal
|
* the config.
|
294 |
|
|
if ($gateway['friendlyiface'] == "wan" && !isset($gateway['defaultgw'])) {
|
295 |
|
|
if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw"))
|
296 |
|
|
$gateway['defaultgw'] = true;
|
297 |
|
|
}
|
298 |
979c816c
|
Ermal
|
*/
|
299 |
3d471a14
|
Ermal
|
/* include the gateway index as the attribute */
|
300 |
|
|
$gateway['attribute'] = $i;
|
301 |
2328dcc5
|
Seth Mos
|
|
302 |
|
|
$gateways_arr[$gateway['name']] = $gateway;
|
303 |
|
|
$i++;
|
304 |
|
|
}
|
305 |
883c53c9
|
Ermal Lu?i
|
}
|
306 |
|
|
|
307 |
ef05ae5f
|
Ermal
|
/* Loop through all interfaces with a gateway and add it to a array */
|
308 |
|
|
if ($disabled == false)
|
309 |
|
|
$iflist = get_configured_interface_with_descr();
|
310 |
|
|
else
|
311 |
|
|
$iflist = get_configured_interface_with_descr(false, true);
|
312 |
|
|
|
313 |
3d471a14
|
Ermal
|
/* Process/add dynamic gateways. */
|
314 |
883c53c9
|
Ermal Lu?i
|
foreach($iflist as $ifname => $friendly ) {
|
315 |
3d471a14
|
Ermal
|
if(! interface_has_gateway($ifname))
|
316 |
883c53c9
|
Ermal Lu?i
|
continue;
|
317 |
3d471a14
|
Ermal
|
|
318 |
86df2846
|
jim-p
|
if (empty($config['interfaces'][$ifname]))
|
319 |
|
|
continue;
|
320 |
|
|
|
321 |
|
|
$ifcfg = &$config['interfaces'][$ifname];
|
322 |
e62fe748
|
Ermal
|
if (!empty($ifcfg['ipaddr']) && is_ipaddr($ifcfg['ipaddr']))
|
323 |
|
|
continue;
|
324 |
|
|
|
325 |
883c53c9
|
Ermal Lu?i
|
$gateway = array();
|
326 |
|
|
$gateway['dynamic'] = false;
|
327 |
|
|
$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
|
328 |
|
|
$gateway['interface'] = get_real_interface($ifname);
|
329 |
|
|
$gateway['friendlyiface'] = $ifname;
|
330 |
ef05ae5f
|
Ermal
|
$gateway['name'] = $friendly;
|
331 |
883c53c9
|
Ermal Lu?i
|
$gateway['attribute'] = "system";
|
332 |
2328dcc5
|
Seth Mos
|
|
333 |
6adc8e32
|
gnhb
|
if ($gateway['dynamic'] === "default") {
|
334 |
999111cb
|
Ermal
|
$gateway['defaultgw'] = true;
|
335 |
|
|
$gateway['dynamic'] = true;
|
336 |
|
|
}
|
337 |
883c53c9
|
Ermal Lu?i
|
/* Loopback dummy for dynamic interfaces without a IP */
|
338 |
999111cb
|
Ermal
|
if (!is_ipaddr($gateway['gateway']) && $gateway['dynamic'] == true)
|
339 |
|
|
$gateway['gateway'] = "dynamic";
|
340 |
3439b033
|
Seth Mos
|
|
341 |
883c53c9
|
Ermal Lu?i
|
/* automatically skip known static and dynamic gateways we have a array entry for */
|
342 |
|
|
foreach($gateways_arr as $gateway_item) {
|
343 |
1ced293c
|
gnhb
|
if (($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) ||
|
344 |
|
|
($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true))
|
345 |
7bed8222
|
Ermal
|
continue 2;
|
346 |
883c53c9
|
Ermal Lu?i
|
}
|
347 |
c66b4242
|
Seth Mos
|
|
348 |
e0df9716
|
gnhb
|
if (is_ipaddr($gateway['gateway']))
|
349 |
883c53c9
|
Ermal Lu?i
|
$gateway['monitor'] = $gateway['gateway'];
|
350 |
c65e1e0d
|
Ermal
|
|
351 |
|
|
$gateway['descr'] = "Interface {$friendly} Dynamic Gateway";
|
352 |
|
|
$gateways_arr[$friendly] = $gateway;
|
353 |
c66b4242
|
Seth Mos
|
}
|
354 |
883c53c9
|
Ermal Lu?i
|
|
355 |
c66b4242
|
Seth Mos
|
return($gateways_arr);
|
356 |
|
|
}
|
357 |
|
|
|
358 |
3d471a14
|
Ermal
|
/*
|
359 |
|
|
* Return an array with all gateway groups with name as key
|
360 |
fd8eeda5
|
Seth Mos
|
* All gateway groups will be processed before returning the array.
|
361 |
3d471a14
|
Ermal
|
*/
|
362 |
fd8eeda5
|
Seth Mos
|
function return_gateway_groups_array() {
|
363 |
962625aa
|
Ermal
|
global $config, $g;
|
364 |
fd8eeda5
|
Seth Mos
|
|
365 |
|
|
/* fetch the current gateways status */
|
366 |
3d471a14
|
Ermal
|
$gateways_status = return_gateways_status(true);
|
367 |
fd8eeda5
|
Seth Mos
|
$gateways_arr = return_gateways_array();
|
368 |
|
|
$gateway_groups_array = array();
|
369 |
|
|
|
370 |
|
|
if (is_array($config['gateways']['gateway_group'])) {
|
371 |
|
|
foreach($config['gateways']['gateway_group'] as $group) {
|
372 |
|
|
/* create array with group gateways members seperated by tier */
|
373 |
|
|
$tiers = array();
|
374 |
3d471a14
|
Ermal
|
$backupplan = array();
|
375 |
fd8eeda5
|
Seth Mos
|
foreach($group['item'] as $item) {
|
376 |
|
|
$itemsplit = explode("|", $item);
|
377 |
|
|
$tier = $itemsplit[1];
|
378 |
|
|
$gwname = $itemsplit[0];
|
379 |
3d471a14
|
Ermal
|
|
380 |
|
|
/* Do it here rather than reiterating again the group in case no member is up. */
|
381 |
d8bf779b
|
Ermal
|
$backupplan[$tier][] = $gwname;
|
382 |
3d471a14
|
Ermal
|
|
383 |
fd8eeda5
|
Seth Mos
|
/* check if the gateway is available before adding it to the array */
|
384 |
54b78cc1
|
Ermal
|
if (!empty($gateways_status[$gwname])) {
|
385 |
|
|
$status = $gateways_status[$gwname];
|
386 |
3d471a14
|
Ermal
|
$gwdown = false;
|
387 |
9af4b31b
|
Ermal
|
if (stristr($status['status'], "down")) {
|
388 |
3d471a14
|
Ermal
|
$msg = "MONITOR: {$gwname} has high latency, removing from routing group";
|
389 |
|
|
$gwdown = true;
|
390 |
|
|
} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
|
391 |
|
|
/* packet loss */
|
392 |
|
|
$msg = "MONITOR: {$gwname} has packet loss, removing from routing group";
|
393 |
|
|
$gwdown = true;
|
394 |
|
|
} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
|
395 |
|
|
/* high latency */
|
396 |
|
|
$msg = "MONITOR: {$gwname} has high latency, removing from routing group";
|
397 |
|
|
$gwdown = true;
|
398 |
|
|
}
|
399 |
|
|
if ($gwdown == true) {
|
400 |
9f1d61d3
|
Scott Ullrich
|
log_error($msg);
|
401 |
|
|
notify_via_growl($msg);
|
402 |
3d471a14
|
Ermal
|
} else
|
403 |
857ce5f3
|
Seth Mos
|
/* Online add member */
|
404 |
|
|
$tiers[$tier][] = $gwname;
|
405 |
fd8eeda5
|
Seth Mos
|
}
|
406 |
|
|
}
|
407 |
|
|
$tiers_count = count($tiers);
|
408 |
|
|
if($tiers_count == 0) {
|
409 |
|
|
/* Oh dear, we have no members! Engage Plan B */
|
410 |
9132ae35
|
Ermal
|
if (!$g['booting']) {
|
411 |
|
|
$msg = "Gateways status could not be determined, considering all as up/active.";
|
412 |
|
|
log_error($msg);
|
413 |
|
|
notify_via_growl($msg);
|
414 |
|
|
}
|
415 |
3d471a14
|
Ermal
|
$tiers = $backupplan;
|
416 |
fd8eeda5
|
Seth Mos
|
}
|
417 |
09ae0c17
|
Seth Mos
|
/* sort the tiers array by the tier key */
|
418 |
|
|
ksort($tiers);
|
419 |
3d471a14
|
Ermal
|
|
420 |
fd8eeda5
|
Seth Mos
|
/* we do not really foreach the tiers as we stop after the first tier */
|
421 |
fe22a89b
|
Ermal
|
foreach($tiers as $tier) {
|
422 |
fd8eeda5
|
Seth Mos
|
/* process all gateways in this tier */
|
423 |
fe22a89b
|
Ermal
|
foreach($tier as $member) {
|
424 |
fd8eeda5
|
Seth Mos
|
/* determine interface gateway */
|
425 |
5f53260a
|
Ermal
|
if (isset($gateways_arr[$member])) {
|
426 |
|
|
$gateway = $gateways_arr[$member];
|
427 |
|
|
$int = $gateway['interface'];
|
428 |
|
|
$gatewayip = "";
|
429 |
|
|
if(is_ipaddr($gateway['gateway']))
|
430 |
|
|
$gatewayip = $gateway['gateway'];
|
431 |
|
|
else if ($int <> "")
|
432 |
|
|
$gatewayip = get_interface_gateway($gateway['friendlyiface']);
|
433 |
3d471a14
|
Ermal
|
|
434 |
|
|
if (($int <> "") && is_ipaddr($gatewayip)) {
|
435 |
|
|
$groupmember = array();
|
436 |
|
|
$groupmember['int'] = $int;
|
437 |
|
|
$groupmember['gwip'] = $gatewayip;
|
438 |
|
|
$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
|
439 |
|
|
$gateway_groups_array[$group['name']][] = $groupmember;
|
440 |
|
|
}
|
441 |
fd8eeda5
|
Seth Mos
|
}
|
442 |
|
|
}
|
443 |
|
|
/* we should have the 1st available tier now, exit stage left */
|
444 |
|
|
break;
|
445 |
|
|
}
|
446 |
|
|
}
|
447 |
|
|
}
|
448 |
fe22a89b
|
Ermal
|
return ($gateway_groups_array);
|
449 |
fd8eeda5
|
Seth Mos
|
}
|
450 |
|
|
|
451 |
2468ae76
|
Scott Ullrich
|
/* Update DHCP WAN Interface ip address in gateway group item */
|
452 |
|
|
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
|
453 |
|
|
global $config, $g;
|
454 |
|
|
foreach($config['gateways']['gateway_item'] as & $gw) {
|
455 |
|
|
if($gw['interface'] == $interface) {
|
456 |
|
|
$current_gw = get_interface_gateway($interface);
|
457 |
|
|
if($gw['gateway'] <> $current_gw) {
|
458 |
|
|
$gw['gateway'] = $current_gw;
|
459 |
|
|
$changed = true;
|
460 |
|
|
}
|
461 |
|
|
}
|
462 |
|
|
}
|
463 |
|
|
if($changed && $current_gw)
|
464 |
|
|
write_config("Updating gateway group gateway for $interface - new gateway is $current_gw");
|
465 |
|
|
}
|
466 |
fd8eeda5
|
Seth Mos
|
|
467 |
6dc3a5c2
|
Ermal Lu?i
|
function lookup_gateway_ip_by_name($name) {
|
468 |
fe22a89b
|
Ermal
|
|
469 |
66a96e72
|
Ermal
|
$gateways_arr = return_gateways_array();
|
470 |
68f291ff
|
Ermal
|
foreach ($gateways_arr as $gname => $gw) {
|
471 |
|
|
if ($gw['name'] == $name || $gname == $name)
|
472 |
6f77aca5
|
Ermal
|
return $gw['gateway'];
|
473 |
|
|
}
|
474 |
66a96e72
|
Ermal
|
|
475 |
|
|
return false;
|
476 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
477 |
|
|
|
478 |
|
|
function lookup_gateway_monitor_ip_by_name($name) {
|
479 |
fe22a89b
|
Ermal
|
|
480 |
6dc3a5c2
|
Ermal Lu?i
|
$gateways_arr = return_gateways_array();
|
481 |
fe22a89b
|
Ermal
|
if (!empty($gateways_arr[$name])) {
|
482 |
|
|
$gateway = $gateways_arr[$name];
|
483 |
68f291ff
|
Ermal
|
if(!is_ipaddr($gateway['monitor']))
|
484 |
|
|
return $gateway['gateway'];
|
485 |
6dc3a5c2
|
Ermal Lu?i
|
|
486 |
9fd19334
|
gnhb
|
return $gateway['monitor'];
|
487 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
488 |
fe22a89b
|
Ermal
|
|
489 |
|
|
return (false);
|
490 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
491 |
|
|
|
492 |
|
|
function lookup_gateway_interface_by_name($name) {
|
493 |
|
|
|
494 |
fe22a89b
|
Ermal
|
$gateways_arr = return_gateways_array();
|
495 |
|
|
if (!empty($gateways_arr[$name])) {
|
496 |
|
|
$interfacegw = $gateway['interface'];
|
497 |
|
|
return ($interfacegw);
|
498 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
499 |
fe22a89b
|
Ermal
|
|
500 |
|
|
return (false);
|
501 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
502 |
|
|
|
503 |
a2532739
|
Ermal Lu?i
|
function get_interface_gateway($interface, &$dynamic = false) {
|
504 |
6dc3a5c2
|
Ermal Lu?i
|
global $config, $g;
|
505 |
|
|
|
506 |
|
|
$gw = NULL;
|
507 |
|
|
|
508 |
6727f4ef
|
jim-p
|
$gwcfg = $config['interfaces'][$interface];
|
509 |
f5d3a5ce
|
Ermal
|
if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
|
510 |
66a96e72
|
Ermal
|
foreach($config['gateways']['gateway_item'] as $gateway) {
|
511 |
|
|
if ($gateway['name'] == $gwcfg['gateway']) {
|
512 |
|
|
$gw = $gateway['gateway'];
|
513 |
|
|
break;
|
514 |
|
|
}
|
515 |
|
|
}
|
516 |
|
|
}
|
517 |
6dc3a5c2
|
Ermal Lu?i
|
|
518 |
|
|
// for dynamic interfaces we handle them through the $interface_router file.
|
519 |
83f9c8ee
|
Ermal Lu?i
|
if (!is_ipaddr($gw) && !is_ipaddr($gwcfg['ipaddr'])) {
|
520 |
6dc3a5c2
|
Ermal Lu?i
|
$realif = get_real_interface($interface);
|
521 |
|
|
if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
|
522 |
3d471a14
|
Ermal
|
$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
|
523 |
d62b164a
|
gnhb
|
$dynamic = true;
|
524 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
525 |
d62b164a
|
gnhb
|
if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw"))
|
526 |
|
|
$dynamic = "default";
|
527 |
|
|
|
528 |
|
|
|
529 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
530 |
|
|
|
531 |
|
|
/* return gateway */
|
532 |
fe22a89b
|
Ermal
|
return ($gw);
|
533 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
534 |
|
|
|
535 |
673e8095
|
Scott Ullrich
|
?>
|