Project

General

Profile

Download (15.1 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'] = "10";
42
		$a_settings['latencyhigh'] = "20";
43
		$a_settings['losslow'] = "100";
44
		$a_settings['losshigh'] = "500";
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
					switch($status['status']) {
311
						case "None":
312
							/* Online add member */
313
							$tiers[$tier][] = $gwname;
314
							break;
315
						case "delay":
316
							if(strstr($group['trigger'] , "latency")) {
317
								/* high latency */
318
								log_error("MONITOR: $gwname has high latency, removing from routing group");
319
							} else {
320
								$tiers[$tier][] = $gwname;
321
							}
322
							break;
323
						case "loss":
324
							if(strstr($group['trigger'], "loss")) {
325
								/* packet loss */
326
								log_error("MONITOR: $gwname has packet loss, removing from routing group");
327
							} else {
328
								$tiers[$tier][] = $gwname;
329
							}
330
							break;
331
					}
332
				}
333
			}
334
			$tiers_count = count($tiers);
335
			if($tiers_count == 0) {
336
				/* Oh dear, we have no members! Engage Plan B */
337
				log_error("All gateways are unavailable, proceeding with configured XML settings!");
338
				foreach($group['item'] as $item) {
339
					foreach($group['item'] as $item) {
340
						$itemsplit = explode("|", $item);
341
						$tier = $itemsplit[1];
342
						$gwname = $itemsplit[0];
343
						$tiers[$tier][] = $gwname;
344
					}
345
				}
346
			}
347
			/* sort the tiers array by the tier key */
348
			ksort($tiers);
349
			/* we do not really foreach the tiers as we stop after the first tier */
350
			foreach($tiers as $tiernr => $tier) {
351
				/* process all gateways in this tier */
352
				$member_count = count($tier);
353
				foreach($tier as $tiernr => $member) {
354
					/* determine interface gateway */
355
					foreach($gateways_arr as $name => $gateway) {
356
						if($gateway['name'] == $member) {
357
							$int = $gateway['interface'];
358
							if(is_ipaddr($gateway['gateway'])) 
359
								$gatewayip = $gateway['gateway'];
360
							else 
361
								$gatewayip = lookup_gateway_ip_by_name($gateway['gateway']);
362
							break;
363
						}
364
					}
365
					if (($int <> "") && is_ipaddr($gatewayip)) {
366
						$gateway_groups_array[$group['name']][$tiernr]['int']  = "$int";
367
						$gateway_groups_array[$group['name']][$tiernr]['gwip']  = "$gatewayip";
368
					}
369
				}
370
				/* we should have the 1st available tier now, exit stage left */
371
				break;
372
			}
373
		}
374
	}
375
	return($gateway_groups_array);
376
}
377

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

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

    
409
function lookup_gateway_monitor_ip_by_name($name) {
410
        global $config;
411
        $gateways_arr = return_gateways_array();
412

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

    
419
                        return($monitorip);
420
                }
421
        }
422
        return(false);
423
}
424

    
425
function lookup_gateway_interface_by_name($name) {
426
        global $config;
427
        $gateways_arr = return_gateways_array();
428

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

    
439
function get_interface_gateway($interface) {
440
        global $config, $g;
441

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

    
455
        $gw = NULL;
456

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

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

    
472
        /* return gateway */
473
        return $gw;
474
}
475

    
476
?>
(15-15/40)