Project

General

Profile

Download (15.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
  Copyright (C) 2008 Bill Marquette, Seth Mos
5
  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
1. Redistributions of source code must retain the above copyright notice,
11
  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
  */
29

    
30
/* add static routes for monitor IP addresse
31
 * creates monitoring configuration file
32
 */
33
function setup_gateways_monitor() {
34
	global $config;
35
	global $g;
36
	$gateways_arr = return_gateways_array();
37

    
38
	if (is_array($config['gateways']['settings'])) {
39
		$a_settings = &$config['gateways']['settings'];
40
	} else {
41
		$a_settings['latencylow'] = "200";
42
		$a_settings['latencyhigh'] = "500";
43
		$a_settings['losslow'] = "10";
44
		$a_settings['losshigh'] = "20";
45
	}
46

    
47
	/* kill apinger process */
48
	if(is_process_running("apinger"))
49
		mwexec("/usr/bin/killall apinger", true);
50
	$fd = fopen("{$g['varetc_path']}/apinger.conf", "w");
51
	$apingerconfig = <<<EOD
52

    
53
# pfSense apinger configuration file. Automatically Generated!
54

    
55
## User and group the pinger should run as
56
user "nobody"
57
group "nobody"
58

    
59
## Mailer to use (default: "/usr/lib/sendmail -t")
60
#mailer "/var/qmail/bin/qmail-inject" 
61

    
62
## Location of the pid-file (default: "/var/run/apinger.pid")
63
pid_file "{$g['varrun_path']}/apinger.pid"
64

    
65
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
66
#timestamp_format "%Y%m%d%H%M%S"
67

    
68
status {
69
	## File where the status information whould be written to
70
	file "/tmp/apinger.status"
71
	## Interval between file updates
72
	## when 0 or not set, file is written only when SIGUSR1 is received
73
	interval 10s
74
}
75

    
76
########################################
77
# RRDTool status gathering configuration
78
# Interval between RRD updates
79
rrd interval 60s;
80

    
81
## These parameters can be overriden in a specific alarm configuration
82
alarm default { 
83
	command on "/usr/bin/touch /tmp/filter_dirty"
84
	command off "/usr/bin/touch /tmp/filter_dirty"
85
	combine 10s
86
}
87

    
88
## "Down" alarm definition. 
89
## This alarm will be fired when target doesn't respond for 30 seconds.
90
alarm down "down" {
91
	time 10s
92
}
93

    
94
## "Delay" alarm definition. 
95
## This alarm will be fired when responses are delayed more than 200ms
96
## it will be canceled, when the delay drops below 100ms
97
alarm delay "delay" {
98
	delay_low {$a_settings['latencylow']}ms
99
	delay_high {$a_settings['latencyhigh']}ms
100
}
101

    
102
## "Loss" alarm definition. 
103
## This alarm will be fired when packet loss goes over 20%
104
## it will be canceled, when the loss drops below 10%
105
alarm loss "loss" {
106
	percent_low {$a_settings['losslow']}
107
	percent_high {$a_settings['losshigh']}
108
}
109

    
110
target default {
111
	## How often the probe should be sent	
112
	interval 1s
113
	
114
	## How many replies should be used to compute average delay 
115
	## for controlling "delay" alarms
116
	avg_delay_samples 10
117
	
118
	## How many probes should be used to compute average loss
119
	avg_loss_samples 50
120

    
121
	## The delay (in samples) after which loss is computed
122
	## without this delays larger than interval would be treated as loss
123
	avg_loss_delay_samples 20
124

    
125
	## Names of the alarms that may be generated for the target
126
	alarms "down","delay","loss"
127

    
128
	## Location of the RRD
129
	#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
130
}
131

    
132
## Targets to probe
133
## Each one defined with:
134
## target <address> { <parameter>... }
135
## The parameters are those described above in the "target default" section
136
## plus the "description" parameter.
137
## the <address> should be IPv4 or IPv6 address (not hostname!)
138

    
139
EOD;
140

    
141
	/* add static routes for each gateway with their monitor IP */
142
	if(is_array($gateways_arr)) {
143
		foreach($gateways_arr as $name => $gateway) {
144
			if($gateway['monitor'] == "") {
145
				$gateway['monitor'] = $gateway['gateway'];
146
			}
147
			$apingerconfig .= "target \"{$gateway['monitor']}\" {\n";
148
			$apingerconfig .= "	description \"{$gateway['name']}\"\n";
149
			$apingerconfig .= "	rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
150
			$apingerconfig .= "}\n";
151
			$apingerconfig .= "\n";
152
			if($gateway['monitor'] == $gateway['gateway']) {
153
				/* if the gateway is the same as the monitor we do not add a
154
				 * route as this will break the routing table */
155
				continue;
156
			} else {
157
				mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']));
158
				if(! stristr("127.0.0", $gateway['gateway'])) {
159
					mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
160
						" " . escapeshellarg($gateway['gateway']));
161
				log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$gateway['gateway']}");
162
				}
163
			}
164
		}
165
	}
166
	fwrite($fd, $apingerconfig);
167
	fclose($fd);
168

    
169
	if(!is_process_running("apinger")) {
170
		if (is_dir("{$g['tmp_path']}"))
171
			chmod("{$g['tmp_path']}", 01777);
172
		if (is_dir("{$g['vardb_path']}/rrd"))
173
			chown("{$g['vardb_path']}/rrd", "nobody");
174
		/* start a new apinger process */
175
		mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
176
	}
177
	return 0;
178
}
179

    
180
/* return the status of the apinger targets as a array */
181
function return_gateways_status() {
182
	global $config;
183
	global $g;
184
	$gateways_arr = return_gateways_array();
185

    
186
	$apingerstatus = array();
187
	if(is_readable("{$g['tmp_path']}/apinger.status"))
188
		$apingerstatus = file("{$g['tmp_path']}/apinger.status");
189

    
190
	$status = array();
191
	foreach($apingerstatus as $line) {
192
		$fields = explode(":", $line);
193
		switch($fields[0]) {
194
			case "Target":
195
				$target = trim($fields[1]);
196
				$status[$target] = array();
197
				$status[$target]['monitor'] = $target;
198
				foreach($gateways_arr as $name => $gateway) {
199
					if($gateway['monitor'] == "$target") {
200
						$status[$target]['gateway'] = $gateway['gateway'];
201
						$status[$target]['interface'] = $gateway['interface'];
202
					}
203
				}
204
				break;
205
			case "Description":
206
	 			$status[$target]['name'] = trim($fields[1]);
207
				break;
208
			case "Last reply received":
209
				$status[$target]['lastcheck'] = trim($fields[1]) .":". trim($fields[2]) .":". trim($fields[3]);
210
				break;
211
			case "Average delay":
212
				$status[$target]['delay'] = trim($fields[1]);
213
				break;
214
			case "Average packet loss":
215
				$status[$target]['loss'] = trim($fields[1]);
216
				break;
217
			case "Active alarms":
218
				$status[$target]['status'] = trim($fields[1]);
219
				break;
220
		}
221
	}
222
	return($status);
223
}
224

    
225
function return_gateways_array() {
226
	global $config;
227

    
228
	$gateways_arr = array();
229

    
230
	/* Loop through all interfaces with a gateway and add it to a array */
231
	$iflist = get_configured_interface_with_descr();
232
	foreach($iflist as $ifname => $friendly ) {
233
		if(interface_has_gateway($ifname)) {
234
			$gateway = array();
235
			$gateway['gateway'] = get_interface_gateway($ifname);
236
			/* Loopback dummy for dynamic interfaces without a IP */
237
			if(!is_ipaddr(trim($gateway['gateway']))) {
238
				 $gateway['gateway'] = "127.0.0.2";
239
			}
240
			/* 
241
			 * do not add dynamic gateways if it is also found 
242
			 * in the gateways array.
243
			 * XXX: NB: Can this ever happen?!
244
			 * smos@ get_interface_gateway() also succeeds for 
245
			 * static gateways, thus they need to be excluded
246
			 */
247
			if(is_array($config['gateways']['gateway_item'])) {
248
				foreach($config['gateways']['gateway_item'] as $gateway_item) {
249
					if($gateway_item['gateway'] == $gateway['gateway']) {
250
						continue 2;
251
					}
252
				}
253
			}
254

    
255
			/* retrieve a proper monitor IP? */
256
			if(is_ipaddr($config['interfaces'][$ifname]['monitorip'])) {
257
				$gateway['monitor'] = $config['interfaces'][$ifname]['monitorip'];
258
			} else {
259
				$gateway['monitor'] = $gateway['gateway'];
260
			}
261
			$gateway['interface'] = get_real_interface($ifname);
262
			$gateway['name'] = "{$ifname}";
263
			$gateway['descr'] = "Interface {$friendly} Gateway";
264
			$gateway['attribute'] = "system";
265
			$gateways_arr[$ifname] = $gateway;
266
		}
267
	}
268

    
269
	/* tack on all the hard defined gateways as well */
270
	if(is_array($config['gateways']['gateway_item'])) {
271
		$i = 0;
272
		foreach($config['gateways']['gateway_item'] as $gateway) {
273
			if($gateway['monitor'] == "") {
274
				$gateway['monitor'] = $gateway['gateway'];
275
			}
276
			/* include the gateway index as the attribute */
277
			$gateway['interface'] = convert_friendly_interface_to_real_interface_name($gateway['interface']);
278
			$gateway['attribute'] = "$i";
279
			$gateways_arr[$gateway['name']] = $gateway;
280
			$i++;
281
		}
282
	}
283
	return($gateways_arr);
284
}
285

    
286
/* return a array with all gateway groups with name as key
287
 * All gateway groups will be processed before returning the array.
288
*/
289
function return_gateway_groups_array() {
290
	global $config, $g;
291

    
292
	/* fetch the current gateways status */
293
	$gateways_status = return_gateways_status();
294
	$gateways_arr = return_gateways_array();
295
	$gateway_groups_array = array();
296

    
297
	if (is_array($config['gateways']['gateway_group'])) {
298
		foreach($config['gateways']['gateway_group'] as $group) {
299
			/* create array with group gateways members seperated by tier */
300
			$tiers = array();
301
			foreach($group['item'] as $item) {
302
				$itemsplit = explode("|", $item);
303
				$tier = $itemsplit[1];
304
				$gwname = $itemsplit[0];
305
				/* check if the gateway is available before adding it to the array */
306
				foreach($gateways_status as $status) {
307
					if(($status['name'] != $gwname)) {
308
						continue;
309
					}
310
					if (preg_match("/down/i", $status['status'])) {
311
						log_error("MONITOR: $gwname has high latency, removing from routing group");
312
					} elseif (preg_match("/loss/i", $status['status'])) {
313
						if (strstr($group['trigger'], "loss")) {
314
							/* packet loss */
315
							log_error("MONITOR: $gwname has packet loss, removing from routing group");
316
						} else {
317
							$tiers[$tier][] = $gwname;
318
						}
319
					} elseif (preg_match("/delay/i", $status['status'])) {
320
						if (strstr($group['trigger'] , "latency")) {
321
							/* high latency */
322
							log_error("MONITOR: $gwname has high latency, removing from routing group");
323
						} else {
324
							$tiers[$tier][] = $gwname;
325
						}
326
					} elseif (preg_match("/none/i", $status['status'])) {
327
						/* Online add member */
328
						$tiers[$tier][] = $gwname;
329
					}
330
				}
331
			}
332
			$tiers_count = count($tiers);
333
			if($tiers_count == 0) {
334
				/* Oh dear, we have no members! Engage Plan B */
335
				log_error("All gateways are unavailable, proceeding with configured XML settings!");
336
				foreach($group['item'] as $item) {
337
					foreach($group['item'] as $item) {
338
						$itemsplit = explode("|", $item);
339
						$tier = $itemsplit[1];
340
						$gwname = $itemsplit[0];
341
						$tiers[$tier][] = $gwname;
342
					}
343
				}
344
			}
345
			/* sort the tiers array by the tier key */
346
			ksort($tiers);
347
			/* we do not really foreach the tiers as we stop after the first tier */
348
			foreach($tiers as $tiernr => $tier) {
349
				/* process all gateways in this tier */
350
				$member_count = count($tier);
351
				foreach($tier as $tiernr => $member) {
352
					/* determine interface gateway */
353
					foreach($gateways_arr as $name => $gateway) {
354
						if($gateway['name'] == $member) {
355
							$int = $gateway['interface'];
356
							if(is_ipaddr($gateway['gateway'])) 
357
								$gatewayip = $gateway['gateway'];
358
							else 
359
								$gatewayip = lookup_gateway_ip_by_name($gateway['gateway']);
360
							break;
361
						}
362
					}
363
					if (($int <> "") && is_ipaddr($gatewayip)) {
364
						$gateway_groups_array[$group['name']][$tiernr]['int']  = "$int";
365
						$gateway_groups_array[$group['name']][$tiernr]['gwip']  = "$gatewayip";
366
					}
367
				}
368
				/* we should have the 1st available tier now, exit stage left */
369
				break;
370
			}
371
		}
372
	}
373
	return($gateway_groups_array);
374
}
375

    
376
/* Update DHCP WAN Interface ip address in gateway group item */
377
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
378
	global $config, $g;
379
	foreach($config['gateways']['gateway_item'] as & $gw) {	
380
		if($gw['interface'] == $interface) {
381
			$current_gw = get_interface_gateway($interface);
382
			if($gw['gateway'] <> $current_gw) {
383
				$gw['gateway'] = $current_gw;
384
				$changed = true;
385
			}
386
		}
387
	}
388
	if($changed && $current_gw)
389
		write_config("Updating gateway group gateway for $interface - new gateway is $current_gw");
390
}
391

    
392
function lookup_gateway_ip_by_name($name) {
393
        global $config;
394
        if(is_array($config['gateways']['gateway_item'])) {
395
                foreach($config['gateways']['gateway_item'] as $gateway) {
396
                        if($gateway['name'] == $name) {
397
                                $gatewayip = $gateway['gateway'];
398
                                //$interfacegw = $gateway['interface'];
399
                                return($gatewayip);
400
                        }
401
                }
402
        } else {
403
                return(false);
404
        }
405
}
406

    
407
function lookup_gateway_monitor_ip_by_name($name) {
408
        global $config;
409
        $gateways_arr = return_gateways_array();
410

    
411
        foreach($gateways_arr as $gateway) {
412
                if($gateway['name'] == "$name") {
413
                        $monitorip = $gateway['monitor'];
414
                        if($monitorip == "")
415
                                $monitorip = $gateway['gateway'];
416

    
417
                        return($monitorip);
418
                }
419
        }
420
        return(false);
421
}
422

    
423
function lookup_gateway_interface_by_name($name) {
424
        global $config;
425
        $gateways_arr = return_gateways_array();
426

    
427
        foreach($gateways_arr as $gateway) {
428
                if($gateway['name'] == "$name") {
429
                        $gatewayip = $gateway['gateway'];
430
                        $interfacegw = $gateway['interface'];
431
                        return($interfacegw);
432
                }
433
        }
434
        return(false);
435
}
436

    
437
function get_interface_gateway($interface) {
438
        global $config, $g;
439

    
440
        $iflist = get_configured_interface_with_descr();
441
        /*
442
         * XXX: BUG: This is silly at first, but we may be called with the interface
443
         *      descr for no apparent reason!!!
444
         *      Probably one of those silly strtoupper() legacy stuff!
445
         */
446
        foreach ($iflist as $ifent => $ifdesc) {
447
                if ($ifent == $interface || $ifdesc == $interface) {
448
                        $interface = $ifent;
449
                        break;
450
                }
451
        }
452

    
453
        $gw = NULL;
454

    
455
        $gwcfg = $config['interfaces'][$interface];
456
        if (is_ipaddr($gwcfg['gateway']))
457
                $gw = $gwcfg['gateway'];
458
        else if (!empty($gwcfg['gateway']))
459
                $gw = lookup_gateway_ip_by_name($gwcfg['gateway']);
460

    
461
        // for dynamic interfaces we handle them through the $interface_router file.
462
        if (!is_ipaddr($gw)) {
463
                $realif = get_real_interface($interface);
464
                if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
465
                        $gw = file_get_contents("{$g['tmp_path']}/{$realif}_router");
466
                        $gw = rtrim($gw);
467
                }
468
        }
469

    
470
        /* return gateway */
471
        return $gw;
472
}
473

    
474
?>
(15-15/41)