Project

General

Profile

Download (17.4 KB) Statistics
| Branch: | Tag: | Revision:
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 9ff9a1c7 Seth Mos
					log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$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 9f1d61d3 Scott Ullrich
						$msg = "MONITOR: $gwname has high latency, removing from routing group";
387
						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 9f1d61d3 Scott Ullrich
							$msg = "MONITOR: $gwname has packet loss, removing from routing group";
393
							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 9f1d61d3 Scott Ullrich
							$msg = "MONITOR: $gwname has high latency, removing from routing group";
402
							log_error($msg);
403
							notify_via_growl($msg);
404 857ce5f3 Seth Mos
						} else {
405
							$tiers[$tier][] = $gwname;
406
						}
407 e52ba50c jim-p
					} elseif ($status['status'] == "none") {
408 857ce5f3 Seth Mos
						/* Online add member */
409
						$tiers[$tier][] = $gwname;
410 fd8eeda5 Seth Mos
					}
411
				}
412
			}
413
			$tiers_count = count($tiers);
414
			if($tiers_count == 0) {
415
				/* Oh dear, we have no members! Engage Plan B */
416 9f1d61d3 Scott Ullrich
				$msg = "All gateways are unavailable, proceeding with configured XML settings!";
417
				log_error($msg);
418
				notify_via_growl($msg);
419 fd8eeda5 Seth Mos
				foreach($group['item'] as $item) {
420 7382c59d Ermal
					$itemsplit = explode("|", $item);
421
					$tier = $itemsplit[1];
422
					$gwname = $itemsplit[0];
423
					$tiers[$tier][] = $gwname;
424 fd8eeda5 Seth Mos
				}
425
			}
426 09ae0c17 Seth Mos
			/* sort the tiers array by the tier key */
427
			ksort($tiers);
428 fd8eeda5 Seth Mos
			/* we do not really foreach the tiers as we stop after the first tier */
429 fe22a89b Ermal
			foreach($tiers as $tier) {
430 fd8eeda5 Seth Mos
				/* process all gateways in this tier */
431 fe22a89b Ermal
				foreach($tier as $member) {
432 fd8eeda5 Seth Mos
					/* determine interface gateway */
433 5f53260a Ermal
					if (isset($gateways_arr[$member])) {
434
						$gateway = $gateways_arr[$member];
435
						$int = $gateway['interface'];
436
						$gatewayip = "";
437
						if(is_ipaddr($gateway['gateway'])) 
438
							$gatewayip = $gateway['gateway'];
439
						else if ($int <> "")
440
							$gatewayip = get_interface_gateway($gateway['friendlyiface']);
441 fd8eeda5 Seth Mos
					}
442
					if (($int <> "") && is_ipaddr($gatewayip)) {
443 d9dda2a5 Ermal
						$groupmember = array();
444
						$groupmember['int']  = "$int";
445
						$groupmember['gwip']  = "$gatewayip";
446
						$groupmember['weight']  = isset($gateway['weight']) ? $gateway['weight'] : 1;
447 fe22a89b Ermal
						$gateway_groups_array[$group['name']][] = $groupmember;
448 fd8eeda5 Seth Mos
					}
449
				}
450
				/* we should have the 1st available tier now, exit stage left */
451
				break;
452
			}
453
		}
454
	}
455 fe22a89b Ermal
	return ($gateway_groups_array);
456 fd8eeda5 Seth Mos
}
457
458 2468ae76 Scott Ullrich
/* Update DHCP WAN Interface ip address in gateway group item */
459
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
460
	global $config, $g;
461
	foreach($config['gateways']['gateway_item'] as & $gw) {	
462
		if($gw['interface'] == $interface) {
463
			$current_gw = get_interface_gateway($interface);
464
			if($gw['gateway'] <> $current_gw) {
465
				$gw['gateway'] = $current_gw;
466
				$changed = true;
467
			}
468
		}
469
	}
470
	if($changed && $current_gw)
471
		write_config("Updating gateway group gateway for $interface - new gateway is $current_gw");
472
}
473 fd8eeda5 Seth Mos
474 6dc3a5c2 Ermal Lu?i
function lookup_gateway_ip_by_name($name) {
475 fe22a89b Ermal
476 66a96e72 Ermal
	$gateways_arr = return_gateways_array();
477 6f77aca5 Ermal
        foreach ($gateways_arr as $gw) {
478
                if ($gw['name'] == $name)
479
                        return $gw['gateway'];
480
        }
481 66a96e72 Ermal
482
	return false;
483 6dc3a5c2 Ermal Lu?i
}
484
485
function lookup_gateway_monitor_ip_by_name($name) {
486 fe22a89b Ermal
487 6dc3a5c2 Ermal Lu?i
        $gateways_arr = return_gateways_array();
488 fe22a89b Ermal
	if (!empty($gateways_arr[$name])) {
489
		$gateway = $gateways_arr[$name];
490
		if ($gateway['gateway'] == "dynamic")
491
			$gateway['monitor'] = "127.0.0.2";
492 6dc3a5c2 Ermal Lu?i
493 fe22a89b Ermal
		$monitorip = $gateway['monitor'];
494 096ee9c2 Ermal
		if(!is_ipaddr($monitorip))
495 fe22a89b Ermal
			$monitorip = $gateway['gateway'];
496 6dc3a5c2 Ermal Lu?i
497 fe22a89b Ermal
		return ($monitorip);
498 6dc3a5c2 Ermal Lu?i
        }
499 fe22a89b Ermal
500
        return (false);
501 6dc3a5c2 Ermal Lu?i
}
502
503
function lookup_gateway_interface_by_name($name) {
504
505 fe22a89b Ermal
        $gateways_arr = return_gateways_array();
506
	if (!empty($gateways_arr[$name])) {
507
		//$gatewayip = $gateway['gateway'];
508
		$interfacegw = $gateway['interface'];
509
		return ($interfacegw);
510 6dc3a5c2 Ermal Lu?i
        }
511 fe22a89b Ermal
512
        return (false);
513 6dc3a5c2 Ermal Lu?i
}
514
515 a2532739 Ermal Lu?i
function get_interface_gateway($interface, &$dynamic = false) {
516 6dc3a5c2 Ermal Lu?i
        global $config, $g;
517
518
        $gw = NULL;
519
520
        $gwcfg = $config['interfaces'][$interface];
521
        if (is_ipaddr($gwcfg['gateway']))
522
                $gw = $gwcfg['gateway'];
523 66a96e72 Ermal
        else if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
524
               	foreach($config['gateways']['gateway_item'] as $gateway) {
525
                        if ($gateway['name'] == $gwcfg['gateway']) {
526
                                $gw = $gateway['gateway'];
527
				break;
528
			}
529
                }
530
	}
531 6dc3a5c2 Ermal Lu?i
532
        // for dynamic interfaces we handle them through the $interface_router file.
533 83f9c8ee Ermal Lu?i
        if (!is_ipaddr($gw) && !is_ipaddr($gwcfg['ipaddr'])) {
534 6dc3a5c2 Ermal Lu?i
                $realif = get_real_interface($interface);
535
                if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
536
                        $gw = file_get_contents("{$g['tmp_path']}/{$realif}_router");
537
                        $gw = rtrim($gw);
538 a2532739 Ermal Lu?i
			$dynamic = true;
539 6dc3a5c2 Ermal Lu?i
                }
540
        }
541
542
        /* return gateway */
543 fe22a89b Ermal
        return ($gw);
544 6dc3a5c2 Ermal Lu?i
}
545
546 315e1253 Ermal
?>