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 |
cdcea13f
|
Seth Mos
|
/* add static routes for monitor IP addresse
|
34 |
|
|
* creates monitoring configuration file
|
35 |
|
|
*/
|
36 |
|
|
function setup_gateways_monitor() {
|
37 |
962625aa
|
Ermal
|
global $config;
|
38 |
cdcea13f
|
Seth Mos
|
global $g;
|
39 |
c66b4242
|
Seth Mos
|
$gateways_arr = return_gateways_array();
|
40 |
315e1253
|
Ermal
|
$gateways_status = return_gateways_status();
|
41 |
ffe76308
|
Seth Mos
|
if (!is_array($config['gateways']['gateway_item']))
|
42 |
|
|
$config['gateways']['gateway_item'] = array();
|
43 |
|
|
|
44 |
|
|
$a_gateway_item = &$config['gateways']['gateway_item'];
|
45 |
cdcea13f
|
Seth Mos
|
|
46 |
023920e7
|
Ermal
|
$a_settings = array();
|
47 |
|
|
$a_settings['latencylow'] = "200";
|
48 |
|
|
$a_settings['latencyhigh'] = "500";
|
49 |
|
|
$a_settings['losslow'] = "10";
|
50 |
|
|
$a_settings['losshigh'] = "20";
|
51 |
37c98ef9
|
Seth Mos
|
|
52 |
cdcea13f
|
Seth Mos
|
$fd = fopen("{$g['varetc_path']}/apinger.conf", "w");
|
53 |
|
|
$apingerconfig = <<<EOD
|
54 |
|
|
|
55 |
|
|
# pfSense apinger configuration file. Automatically Generated!
|
56 |
|
|
|
57 |
|
|
## User and group the pinger should run as
|
58 |
0534d60a
|
Chris Buechler
|
user "root"
|
59 |
|
|
group "wheel"
|
60 |
cdcea13f
|
Seth Mos
|
|
61 |
|
|
## Mailer to use (default: "/usr/lib/sendmail -t")
|
62 |
|
|
#mailer "/var/qmail/bin/qmail-inject"
|
63 |
|
|
|
64 |
|
|
## Location of the pid-file (default: "/var/run/apinger.pid")
|
65 |
|
|
pid_file "{$g['varrun_path']}/apinger.pid"
|
66 |
|
|
|
67 |
|
|
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
|
68 |
|
|
#timestamp_format "%Y%m%d%H%M%S"
|
69 |
|
|
|
70 |
|
|
status {
|
71 |
|
|
## File where the status information whould be written to
|
72 |
|
|
file "/tmp/apinger.status"
|
73 |
|
|
## Interval between file updates
|
74 |
|
|
## when 0 or not set, file is written only when SIGUSR1 is received
|
75 |
|
|
interval 10s
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
########################################
|
79 |
|
|
# RRDTool status gathering configuration
|
80 |
|
|
# Interval between RRD updates
|
81 |
|
|
rrd interval 60s;
|
82 |
|
|
|
83 |
|
|
## These parameters can be overriden in a specific alarm configuration
|
84 |
|
|
alarm default {
|
85 |
bd9f5fca
|
Scott Ullrich
|
command on "/usr/bin/touch /tmp/filter_dirty"
|
86 |
|
|
command off "/usr/bin/touch /tmp/filter_dirty"
|
87 |
cdcea13f
|
Seth Mos
|
combine 10s
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
## "Down" alarm definition.
|
91 |
|
|
## This alarm will be fired when target doesn't respond for 30 seconds.
|
92 |
|
|
alarm down "down" {
|
93 |
|
|
time 10s
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
## "Delay" alarm definition.
|
97 |
|
|
## This alarm will be fired when responses are delayed more than 200ms
|
98 |
|
|
## it will be canceled, when the delay drops below 100ms
|
99 |
|
|
alarm delay "delay" {
|
100 |
37c98ef9
|
Seth Mos
|
delay_low {$a_settings['latencylow']}ms
|
101 |
|
|
delay_high {$a_settings['latencyhigh']}ms
|
102 |
cdcea13f
|
Seth Mos
|
}
|
103 |
|
|
|
104 |
|
|
## "Loss" alarm definition.
|
105 |
|
|
## This alarm will be fired when packet loss goes over 20%
|
106 |
|
|
## it will be canceled, when the loss drops below 10%
|
107 |
|
|
alarm loss "loss" {
|
108 |
37c98ef9
|
Seth Mos
|
percent_low {$a_settings['losslow']}
|
109 |
|
|
percent_high {$a_settings['losshigh']}
|
110 |
cdcea13f
|
Seth Mos
|
}
|
111 |
|
|
|
112 |
|
|
target default {
|
113 |
|
|
## How often the probe should be sent
|
114 |
|
|
interval 1s
|
115 |
|
|
|
116 |
|
|
## How many replies should be used to compute average delay
|
117 |
|
|
## for controlling "delay" alarms
|
118 |
|
|
avg_delay_samples 10
|
119 |
|
|
|
120 |
|
|
## How many probes should be used to compute average loss
|
121 |
|
|
avg_loss_samples 50
|
122 |
|
|
|
123 |
|
|
## The delay (in samples) after which loss is computed
|
124 |
|
|
## without this delays larger than interval would be treated as loss
|
125 |
|
|
avg_loss_delay_samples 20
|
126 |
|
|
|
127 |
|
|
## Names of the alarms that may be generated for the target
|
128 |
|
|
alarms "down","delay","loss"
|
129 |
|
|
|
130 |
|
|
## Location of the RRD
|
131 |
1bce2729
|
Seth Mos
|
#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
|
132 |
cdcea13f
|
Seth Mos
|
}
|
133 |
|
|
|
134 |
|
|
## Targets to probe
|
135 |
|
|
## Each one defined with:
|
136 |
|
|
## target <address> { <parameter>... }
|
137 |
|
|
## The parameters are those described above in the "target default" section
|
138 |
|
|
## plus the "description" parameter.
|
139 |
|
|
## the <address> should be IPv4 or IPv6 address (not hostname!)
|
140 |
|
|
|
141 |
|
|
EOD;
|
142 |
|
|
|
143 |
|
|
/* add static routes for each gateway with their monitor IP */
|
144 |
c66b4242
|
Seth Mos
|
if(is_array($gateways_arr)) {
|
145 |
2328dcc5
|
Seth Mos
|
$i = 2;
|
146 |
fd8eeda5
|
Seth Mos
|
foreach($gateways_arr as $name => $gateway) {
|
147 |
023920e7
|
Ermal
|
$gwref = $a_gateway_item[$gateway['attribute']];
|
148 |
2328dcc5
|
Seth Mos
|
/* for dynamic gateways without an IP address we subtitute a local one */
|
149 |
61630681
|
Ermal
|
if(is_ipaddr($gwref['monitor'])) {
|
150 |
023920e7
|
Ermal
|
$gateway['monitor'] = $gwref['monitor'];
|
151 |
ffe76308
|
Seth Mos
|
} else {
|
152 |
01207fd8
|
Ermal
|
if ($gateway['gateway'] == "dynamic") {
|
153 |
ffe76308
|
Seth Mos
|
$gateway['monitor'] = "127.0.0.{$i}";
|
154 |
|
|
$i++;
|
155 |
|
|
}
|
156 |
01207fd8
|
Ermal
|
if (!is_ipaddr($gateway['monitor']))
|
157 |
ffe76308
|
Seth Mos
|
$gateway['monitor'] = $gateway['gateway'];
|
158 |
cdcea13f
|
Seth Mos
|
}
|
159 |
01207fd8
|
Ermal
|
|
160 |
f31ab121
|
Ermal
|
if (!is_ipaddr($gateway['monitor']))
|
161 |
|
|
continue;
|
162 |
|
|
|
163 |
4473744f
|
smos
|
if($gateway['monitor'] == "127.0.0.{$i}") {
|
164 |
|
|
$gwifip = "127.0.0.1";
|
165 |
|
|
} else {
|
166 |
|
|
$gwifip = find_interface_ip($gateway['interface']);
|
167 |
|
|
}
|
168 |
2f92b34d
|
Ermal
|
if (!is_ipaddr($gwifip))
|
169 |
|
|
continue; //Skip this target
|
170 |
439b0e26
|
Ermal
|
|
171 |
|
|
$apingercfg .= "target \"{$gateway['monitor']}\" {\n";
|
172 |
|
|
$apingercfg .= " description \"{$gateway['name']}\"\n";
|
173 |
2f92b34d
|
Ermal
|
$apingercfg .= " srcip \"{$gwifip}\"\n";
|
174 |
023920e7
|
Ermal
|
$alarms = "";
|
175 |
|
|
$override = false;
|
176 |
|
|
if (!empty($gwref['lowloss'])) {
|
177 |
|
|
$alarmscfg .= "alarm loss \"{$gateway['name']}loss\" {\n";
|
178 |
|
|
$alarmscfg .= "\tpercent_low {$gwref['losslow']}\n";
|
179 |
|
|
$alarmscfg .= "\tpercent_high {$gwref['losshigh']}\n";
|
180 |
|
|
$alarmscfg .= "}\n";
|
181 |
|
|
$alarms .= "\"{$gateway['name']}loss\"";
|
182 |
|
|
$override = true;
|
183 |
7726c308
|
Ermal
|
} else {
|
184 |
|
|
if ($override == true)
|
185 |
|
|
$alarms .= ",";
|
186 |
|
|
$alarms .= "\"loss\"";
|
187 |
|
|
$override = true;
|
188 |
|
|
}
|
189 |
023920e7
|
Ermal
|
if (!empty($gwref['latencylow'])) {
|
190 |
|
|
$alarmscfg .= "alarm delay \"{$gateway['name']}delay\" {\n";
|
191 |
|
|
$alarmscfg .= "\tdelay_low {$gwref['latencylow']}ms\n";
|
192 |
|
|
$alarmscfg .= "\tdelay_high {$gwref['latencyhigh']}ms\n";
|
193 |
|
|
$alarmscfg .= "}\n";
|
194 |
|
|
if ($override == true)
|
195 |
|
|
$alarms .= ",";
|
196 |
|
|
$alarms .= "\"{$gateway['name']}delay\"";
|
197 |
|
|
$override = true;
|
198 |
7726c308
|
Ermal
|
} else {
|
199 |
|
|
if ($override == true)
|
200 |
|
|
$alarms .= ",";
|
201 |
|
|
$alarms .= "\"delay\"";
|
202 |
|
|
$override = true;
|
203 |
023920e7
|
Ermal
|
}
|
204 |
|
|
if (!empty($gwref['down'])) {
|
205 |
|
|
$alarmscfg .= "alarm down \"{$gateway['name']}down\" {\n";
|
206 |
|
|
$alarmscfg .= "\ttime {$gwref['down']}s\n";
|
207 |
|
|
$alarmscfg .= "}\n";
|
208 |
|
|
if ($override == true)
|
209 |
|
|
$alarms .= ",";
|
210 |
|
|
$alarms .= "\"{$gateway['name']}down\"";
|
211 |
|
|
$override = true;
|
212 |
7726c308
|
Ermal
|
} else {
|
213 |
|
|
if ($override == true)
|
214 |
|
|
$alarms .= ",";
|
215 |
|
|
$alarms .= "\"down\"";
|
216 |
|
|
$override = true;
|
217 |
|
|
}
|
218 |
023920e7
|
Ermal
|
if ($override == true)
|
219 |
|
|
$apingercfg .= "\talarms override {$alarms};\n";
|
220 |
|
|
|
221 |
|
|
$apingercfg .= " rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
|
222 |
|
|
$apingercfg .= "}\n";
|
223 |
|
|
$apingercfg .= "\n";
|
224 |
cdcea13f
|
Seth Mos
|
if($gateway['monitor'] == $gateway['gateway']) {
|
225 |
|
|
/* if the gateway is the same as the monitor we do not add a
|
226 |
|
|
* route as this will break the routing table */
|
227 |
|
|
continue;
|
228 |
|
|
} else {
|
229 |
2f92b34d
|
Ermal
|
if ($gateway['gateway'] != "dynamic" && is_ipaddr($gateway['gateway'])) {
|
230 |
9ff9a1c7
|
Seth Mos
|
mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']));
|
231 |
7bc60232
|
Seth Mos
|
mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
|
232 |
|
|
" " . escapeshellarg($gateway['gateway']));
|
233 |
5c7296d2
|
Carlos Eduardo Ramos
|
log_error(sprintf(gettext("Removing static route for monitor %s and adding a new route through %s"), $gateway['monitor'], $gateway['gateway']));
|
234 |
7bc60232
|
Seth Mos
|
}
|
235 |
cdcea13f
|
Seth Mos
|
}
|
236 |
|
|
}
|
237 |
023920e7
|
Ermal
|
$apingerconfig .= $alarmscfg;
|
238 |
|
|
$apingerconfig .= $apingercfg;
|
239 |
cdcea13f
|
Seth Mos
|
}
|
240 |
|
|
fwrite($fd, $apingerconfig);
|
241 |
|
|
fclose($fd);
|
242 |
|
|
|
243 |
20f26a50
|
Ermal
|
killbypid("{$g['varrun_path']}/apinger.pid");
|
244 |
|
|
if (is_dir("{$g['tmp_path']}"))
|
245 |
|
|
chmod("{$g['tmp_path']}", 01777);
|
246 |
21934843
|
jim-p
|
if (!is_dir("{$g['vardb_path']}/rrd")) {
|
247 |
|
|
mkdir("{$g['vardb_path']}/rrd", 0775);
|
248 |
|
|
}
|
249 |
20f26a50
|
Ermal
|
if (is_dir("{$g['vardb_path']}/rrd"))
|
250 |
|
|
chown("{$g['vardb_path']}/rrd", "nobody");
|
251 |
|
|
/* start a new apinger process */
|
252 |
|
|
mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
|
253 |
4f060616
|
Ermal
|
|
254 |
cdcea13f
|
Seth Mos
|
return 0;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
/* return the status of the apinger targets as a array */
|
258 |
|
|
function return_gateways_status() {
|
259 |
ae9618af
|
Ermal
|
global $config, $g;
|
260 |
cdcea13f
|
Seth Mos
|
|
261 |
|
|
$apingerstatus = array();
|
262 |
d9dda2a5
|
Ermal
|
if (file_exists("{$g['tmp_path']}/apinger.status")) {
|
263 |
cdcea13f
|
Seth Mos
|
$apingerstatus = file("{$g['tmp_path']}/apinger.status");
|
264 |
2328dcc5
|
Seth Mos
|
}
|
265 |
cdcea13f
|
Seth Mos
|
|
266 |
67ee1ec5
|
Ermal Luçi
|
$status = array();
|
267 |
cdcea13f
|
Seth Mos
|
foreach($apingerstatus as $line) {
|
268 |
75466131
|
Ermal
|
$info = explode("|", $line);
|
269 |
|
|
$target = $info[0];
|
270 |
79b7f498
|
Ermal
|
$status[$target]['srcip'] = $info[1];
|
271 |
|
|
$status[$target]['name'] = $info[2];
|
272 |
|
|
$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
|
273 |
|
|
$status[$target]['delay'] = empty($info[6]) ? 0 : $info[6];
|
274 |
|
|
$status[$target]['loss'] = empty($info[7]) ? "0.0%" : $info[7] . "";
|
275 |
|
|
$status[$target]['status'] = trim($info[8]);
|
276 |
cdcea13f
|
Seth Mos
|
}
|
277 |
|
|
return($status);
|
278 |
17623ab5
|
Bill Marquette
|
}
|
279 |
|
|
|
280 |
2328dcc5
|
Seth Mos
|
/* Return all configured gateways on the system */
|
281 |
c795339e
|
Ermal Lu?i
|
function return_gateways_array($disabled = false) {
|
282 |
962625aa
|
Ermal
|
global $config;
|
283 |
7901dff5
|
Ermal Luçi
|
|
284 |
c66b4242
|
Seth Mos
|
$gateways_arr = array();
|
285 |
|
|
|
286 |
|
|
/* Loop through all interfaces with a gateway and add it to a array */
|
287 |
2328dcc5
|
Seth Mos
|
if ($disabled == false) {
|
288 |
c795339e
|
Ermal Lu?i
|
$iflist = get_configured_interface_with_descr();
|
289 |
2328dcc5
|
Seth Mos
|
} else {
|
290 |
c795339e
|
Ermal Lu?i
|
$iflist = get_configured_interface_with_descr(false, true);
|
291 |
2328dcc5
|
Seth Mos
|
}
|
292 |
01ba0a72
|
Ermal Lu?i
|
|
293 |
|
|
$i = 0;
|
294 |
2328dcc5
|
Seth Mos
|
/* tack on all the hard defined gateways as well */
|
295 |
|
|
if(is_array($config['gateways']['gateway_item'])) {
|
296 |
|
|
foreach($config['gateways']['gateway_item'] as $gateway) {
|
297 |
|
|
if($gateway['gateway'] == "dynamic") {
|
298 |
|
|
$gateway['gateway'] = get_interface_gateway($gateway['interface']);
|
299 |
|
|
/* no IP address found, set to dynamic */
|
300 |
|
|
if(! is_ipaddr($gateway['gateway'])) {
|
301 |
|
|
$gateway['gateway'] = "dynamic";
|
302 |
|
|
}
|
303 |
a2532739
|
Ermal Lu?i
|
$gateway['dynamic'] = true;
|
304 |
2328dcc5
|
Seth Mos
|
}
|
305 |
|
|
if($gateway['monitor'] == "") {
|
306 |
|
|
$gateway['monitor'] = $gateway['gateway'];
|
307 |
|
|
}
|
308 |
|
|
/* include the gateway index as the attribute */
|
309 |
883c53c9
|
Ermal Lu?i
|
$gateway['friendlyiface'] = $gateway['interface'];
|
310 |
2328dcc5
|
Seth Mos
|
$gateway['interface'] = convert_friendly_interface_to_real_interface_name($gateway['interface']);
|
311 |
|
|
$gateway['attribute'] = "$i";
|
312 |
|
|
|
313 |
|
|
$gateways_arr[$gateway['name']] = $gateway;
|
314 |
|
|
$i++;
|
315 |
|
|
}
|
316 |
883c53c9
|
Ermal Lu?i
|
}
|
317 |
|
|
|
318 |
|
|
foreach($iflist as $ifname => $friendly ) {
|
319 |
|
|
if(! interface_has_gateway($ifname)) {
|
320 |
|
|
continue;
|
321 |
|
|
}
|
322 |
|
|
$gateway = array();
|
323 |
|
|
$gateway['dynamic'] = false;
|
324 |
|
|
$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
|
325 |
|
|
$gateway['interface'] = get_real_interface($ifname);
|
326 |
|
|
$gateway['friendlyiface'] = $ifname;
|
327 |
7bed8222
|
Ermal
|
$gateway['name'] = "{$friendly}";
|
328 |
883c53c9
|
Ermal Lu?i
|
$gateway['attribute'] = "system";
|
329 |
2328dcc5
|
Seth Mos
|
|
330 |
883c53c9
|
Ermal Lu?i
|
/* Loopback dummy for dynamic interfaces without a IP */
|
331 |
83f9c8ee
|
Ermal Lu?i
|
if(!is_ipaddr(trim($gateway['gateway'])) && $gateway['dynamic'] == true) {
|
332 |
883c53c9
|
Ermal Lu?i
|
$gateway['gateway'] = "dynamic";
|
333 |
|
|
}
|
334 |
3439b033
|
Seth Mos
|
|
335 |
883c53c9
|
Ermal Lu?i
|
/* automatically skip known static and dynamic gateways we have a array entry for */
|
336 |
|
|
foreach($gateways_arr as $gateway_item) {
|
337 |
7bed8222
|
Ermal
|
if ($ifname == $gateway_item['friendlyiface'] || $friendly == $gateway_item['name']) {
|
338 |
|
|
if ($gateway_item['gateway'] == $gateway['gateway'])
|
339 |
|
|
continue 2;
|
340 |
|
|
if ($gateway_item['gateway'] == "dynamic")
|
341 |
|
|
continue 2;
|
342 |
|
|
}
|
343 |
883c53c9
|
Ermal Lu?i
|
}
|
344 |
c66b4242
|
Seth Mos
|
|
345 |
883c53c9
|
Ermal Lu?i
|
/* retrieve a proper monitor IP? */
|
346 |
|
|
$interface = $config['interfaces'][$ifname];
|
347 |
|
|
if(is_ipaddr($interface['monitorip'])) {
|
348 |
|
|
$gateway['monitor'] = $interface['monitorip'];
|
349 |
|
|
} else {
|
350 |
|
|
$gateway['monitor'] = $gateway['gateway'];
|
351 |
c66b4242
|
Seth Mos
|
}
|
352 |
883c53c9
|
Ermal Lu?i
|
$gateway['descr'] = "Interface $ifname Dynamic Gateway";
|
353 |
|
|
$gateways_arr[$ifname] = $gateway;
|
354 |
|
|
$i++;
|
355 |
c66b4242
|
Seth Mos
|
}
|
356 |
883c53c9
|
Ermal Lu?i
|
|
357 |
c66b4242
|
Seth Mos
|
return($gateways_arr);
|
358 |
|
|
}
|
359 |
|
|
|
360 |
2328dcc5
|
Seth Mos
|
|
361 |
fd8eeda5
|
Seth Mos
|
/* return a array with all gateway groups with name as key
|
362 |
|
|
* All gateway groups will be processed before returning the array.
|
363 |
|
|
*/
|
364 |
|
|
function return_gateway_groups_array() {
|
365 |
962625aa
|
Ermal
|
global $config, $g;
|
366 |
fd8eeda5
|
Seth Mos
|
|
367 |
|
|
/* fetch the current gateways status */
|
368 |
|
|
$gateways_status = return_gateways_status();
|
369 |
|
|
$gateways_arr = return_gateways_array();
|
370 |
|
|
$gateway_groups_array = array();
|
371 |
|
|
|
372 |
|
|
if (is_array($config['gateways']['gateway_group'])) {
|
373 |
|
|
foreach($config['gateways']['gateway_group'] as $group) {
|
374 |
|
|
/* create array with group gateways members seperated by tier */
|
375 |
|
|
$tiers = array();
|
376 |
|
|
foreach($group['item'] as $item) {
|
377 |
|
|
$itemsplit = explode("|", $item);
|
378 |
|
|
$tier = $itemsplit[1];
|
379 |
|
|
$gwname = $itemsplit[0];
|
380 |
|
|
/* check if the gateway is available before adding it to the array */
|
381 |
|
|
foreach($gateways_status as $status) {
|
382 |
d9dda2a5
|
Ermal
|
if ($status['name'] != $gwname) {
|
383 |
fd8eeda5
|
Seth Mos
|
continue;
|
384 |
|
|
}
|
385 |
9af4b31b
|
Ermal
|
if (stristr($status['status'], "down")) {
|
386 |
5c7296d2
|
Carlos Eduardo Ramos
|
$msg = sprintf(gettext("MONITOR: %s has high latency, removing from routing group"), $gwname);
|
387 |
9f1d61d3
|
Scott Ullrich
|
log_error($msg);
|
388 |
|
|
notify_via_growl($msg);
|
389 |
9af4b31b
|
Ermal
|
} elseif (stristr($status['status'], "loss")) {
|
390 |
857ce5f3
|
Seth Mos
|
if (strstr($group['trigger'], "loss")) {
|
391 |
|
|
/* packet loss */
|
392 |
5c7296d2
|
Carlos Eduardo Ramos
|
$msg = sprintf(gettext("MONITOR: %s has packet loss, removing from routing group"), $gwname);
|
393 |
9f1d61d3
|
Scott Ullrich
|
log_error($msg);
|
394 |
|
|
notify_via_growl($msg);
|
395 |
857ce5f3
|
Seth Mos
|
} else {
|
396 |
fd8eeda5
|
Seth Mos
|
$tiers[$tier][] = $gwname;
|
397 |
857ce5f3
|
Seth Mos
|
}
|
398 |
2557738e
|
Ermal
|
} elseif (stristr($status['status'], "delay")) {
|
399 |
857ce5f3
|
Seth Mos
|
if (strstr($group['trigger'] , "latency")) {
|
400 |
|
|
/* high latency */
|
401 |
5c7296d2
|
Carlos Eduardo Ramos
|
$msg = sprintf(gettext("MONITOR: %s has high latency, removing from routing group"), $gwname);
|
402 |
9f1d61d3
|
Scott Ullrich
|
$msg = "MONITOR: $gwname has high latency, removing from routing group";
|
403 |
|
|
log_error($msg);
|
404 |
|
|
notify_via_growl($msg);
|
405 |
857ce5f3
|
Seth Mos
|
} else {
|
406 |
|
|
$tiers[$tier][] = $gwname;
|
407 |
|
|
}
|
408 |
e52ba50c
|
jim-p
|
} elseif ($status['status'] == "none") {
|
409 |
857ce5f3
|
Seth Mos
|
/* Online add member */
|
410 |
|
|
$tiers[$tier][] = $gwname;
|
411 |
fd8eeda5
|
Seth Mos
|
}
|
412 |
|
|
}
|
413 |
|
|
}
|
414 |
|
|
$tiers_count = count($tiers);
|
415 |
|
|
if($tiers_count == 0) {
|
416 |
|
|
/* Oh dear, we have no members! Engage Plan B */
|
417 |
5c7296d2
|
Carlos Eduardo Ramos
|
$msg = gettext("All gateways are unavailable, proceeding with configured XML settings!");
|
418 |
9f1d61d3
|
Scott Ullrich
|
log_error($msg);
|
419 |
|
|
notify_via_growl($msg);
|
420 |
fd8eeda5
|
Seth Mos
|
foreach($group['item'] as $item) {
|
421 |
7382c59d
|
Ermal
|
$itemsplit = explode("|", $item);
|
422 |
|
|
$tier = $itemsplit[1];
|
423 |
|
|
$gwname = $itemsplit[0];
|
424 |
|
|
$tiers[$tier][] = $gwname;
|
425 |
fd8eeda5
|
Seth Mos
|
}
|
426 |
|
|
}
|
427 |
09ae0c17
|
Seth Mos
|
/* sort the tiers array by the tier key */
|
428 |
|
|
ksort($tiers);
|
429 |
fd8eeda5
|
Seth Mos
|
/* we do not really foreach the tiers as we stop after the first tier */
|
430 |
fe22a89b
|
Ermal
|
foreach($tiers as $tier) {
|
431 |
fd8eeda5
|
Seth Mos
|
/* process all gateways in this tier */
|
432 |
fe22a89b
|
Ermal
|
foreach($tier as $member) {
|
433 |
fd8eeda5
|
Seth Mos
|
/* determine interface gateway */
|
434 |
5f53260a
|
Ermal
|
if (isset($gateways_arr[$member])) {
|
435 |
|
|
$gateway = $gateways_arr[$member];
|
436 |
|
|
$int = $gateway['interface'];
|
437 |
|
|
$gatewayip = "";
|
438 |
|
|
if(is_ipaddr($gateway['gateway']))
|
439 |
|
|
$gatewayip = $gateway['gateway'];
|
440 |
|
|
else if ($int <> "")
|
441 |
|
|
$gatewayip = get_interface_gateway($gateway['friendlyiface']);
|
442 |
fd8eeda5
|
Seth Mos
|
}
|
443 |
|
|
if (($int <> "") && is_ipaddr($gatewayip)) {
|
444 |
d9dda2a5
|
Ermal
|
$groupmember = array();
|
445 |
|
|
$groupmember['int'] = "$int";
|
446 |
|
|
$groupmember['gwip'] = "$gatewayip";
|
447 |
|
|
$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
|
448 |
fe22a89b
|
Ermal
|
$gateway_groups_array[$group['name']][] = $groupmember;
|
449 |
fd8eeda5
|
Seth Mos
|
}
|
450 |
|
|
}
|
451 |
|
|
/* we should have the 1st available tier now, exit stage left */
|
452 |
|
|
break;
|
453 |
|
|
}
|
454 |
|
|
}
|
455 |
|
|
}
|
456 |
fe22a89b
|
Ermal
|
return ($gateway_groups_array);
|
457 |
fd8eeda5
|
Seth Mos
|
}
|
458 |
|
|
|
459 |
2468ae76
|
Scott Ullrich
|
/* Update DHCP WAN Interface ip address in gateway group item */
|
460 |
|
|
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
|
461 |
|
|
global $config, $g;
|
462 |
|
|
foreach($config['gateways']['gateway_item'] as & $gw) {
|
463 |
|
|
if($gw['interface'] == $interface) {
|
464 |
|
|
$current_gw = get_interface_gateway($interface);
|
465 |
|
|
if($gw['gateway'] <> $current_gw) {
|
466 |
|
|
$gw['gateway'] = $current_gw;
|
467 |
|
|
$changed = true;
|
468 |
|
|
}
|
469 |
|
|
}
|
470 |
|
|
}
|
471 |
|
|
if($changed && $current_gw)
|
472 |
5c7296d2
|
Carlos Eduardo Ramos
|
write_config(sprintf(gettext("Updating gateway group gateway for %s - new gateway is %s"), $interfac, $current_gw));
|
473 |
2468ae76
|
Scott Ullrich
|
}
|
474 |
fd8eeda5
|
Seth Mos
|
|
475 |
6dc3a5c2
|
Ermal Lu?i
|
function lookup_gateway_ip_by_name($name) {
|
476 |
fe22a89b
|
Ermal
|
|
477 |
66a96e72
|
Ermal
|
$gateways_arr = return_gateways_array();
|
478 |
6f77aca5
|
Ermal
|
foreach ($gateways_arr as $gw) {
|
479 |
|
|
if ($gw['name'] == $name)
|
480 |
|
|
return $gw['gateway'];
|
481 |
|
|
}
|
482 |
66a96e72
|
Ermal
|
|
483 |
|
|
return false;
|
484 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
485 |
|
|
|
486 |
|
|
function lookup_gateway_monitor_ip_by_name($name) {
|
487 |
fe22a89b
|
Ermal
|
|
488 |
6dc3a5c2
|
Ermal Lu?i
|
$gateways_arr = return_gateways_array();
|
489 |
fe22a89b
|
Ermal
|
if (!empty($gateways_arr[$name])) {
|
490 |
|
|
$gateway = $gateways_arr[$name];
|
491 |
|
|
if ($gateway['gateway'] == "dynamic")
|
492 |
|
|
$gateway['monitor'] = "127.0.0.2";
|
493 |
6dc3a5c2
|
Ermal Lu?i
|
|
494 |
fe22a89b
|
Ermal
|
$monitorip = $gateway['monitor'];
|
495 |
096ee9c2
|
Ermal
|
if(!is_ipaddr($monitorip))
|
496 |
fe22a89b
|
Ermal
|
$monitorip = $gateway['gateway'];
|
497 |
6dc3a5c2
|
Ermal Lu?i
|
|
498 |
fe22a89b
|
Ermal
|
return ($monitorip);
|
499 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
500 |
fe22a89b
|
Ermal
|
|
501 |
|
|
return (false);
|
502 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
503 |
|
|
|
504 |
|
|
function lookup_gateway_interface_by_name($name) {
|
505 |
|
|
|
506 |
fe22a89b
|
Ermal
|
$gateways_arr = return_gateways_array();
|
507 |
|
|
if (!empty($gateways_arr[$name])) {
|
508 |
|
|
//$gatewayip = $gateway['gateway'];
|
509 |
|
|
$interfacegw = $gateway['interface'];
|
510 |
|
|
return ($interfacegw);
|
511 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
512 |
fe22a89b
|
Ermal
|
|
513 |
|
|
return (false);
|
514 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
515 |
|
|
|
516 |
a2532739
|
Ermal Lu?i
|
function get_interface_gateway($interface, &$dynamic = false) {
|
517 |
6dc3a5c2
|
Ermal Lu?i
|
global $config, $g;
|
518 |
|
|
|
519 |
|
|
$gw = NULL;
|
520 |
|
|
|
521 |
|
|
$gwcfg = $config['interfaces'][$interface];
|
522 |
|
|
if (is_ipaddr($gwcfg['gateway']))
|
523 |
|
|
$gw = $gwcfg['gateway'];
|
524 |
66a96e72
|
Ermal
|
else if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
|
525 |
|
|
foreach($config['gateways']['gateway_item'] as $gateway) {
|
526 |
|
|
if ($gateway['name'] == $gwcfg['gateway']) {
|
527 |
|
|
$gw = $gateway['gateway'];
|
528 |
|
|
break;
|
529 |
|
|
}
|
530 |
|
|
}
|
531 |
|
|
}
|
532 |
6dc3a5c2
|
Ermal Lu?i
|
|
533 |
|
|
// for dynamic interfaces we handle them through the $interface_router file.
|
534 |
83f9c8ee
|
Ermal Lu?i
|
if (!is_ipaddr($gw) && !is_ipaddr($gwcfg['ipaddr'])) {
|
535 |
6dc3a5c2
|
Ermal Lu?i
|
$realif = get_real_interface($interface);
|
536 |
|
|
if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
|
537 |
|
|
$gw = file_get_contents("{$g['tmp_path']}/{$realif}_router");
|
538 |
|
|
$gw = rtrim($gw);
|
539 |
a2532739
|
Ermal Lu?i
|
$dynamic = true;
|
540 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
541 |
|
|
}
|
542 |
|
|
|
543 |
|
|
/* return gateway */
|
544 |
fe22a89b
|
Ermal
|
return ($gw);
|
545 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
546 |
|
|
|
547 |
315e1253
|
Ermal
|
?>
|