Project

General

Profile

Download (8.9 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
/* include all configuration functions */
31
require_once("functions.inc");
32
require_once("pkg-utils.inc");
33
require_once("notices.inc");
34
require_once("globals.inc");
35

    
36
/* add static routes for monitor IP addresse
37
 * creates monitoring configuration file
38
 */
39
function setup_gateways_monitor() {
40
	global $config;
41
	global $g;
42
	$gateways_arr = return_gateways_array();
43

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

    
50
# pfSense apinger configuration file. Automatically Generated!
51

    
52
## User and group the pinger should run as
53
user "nobody"
54
group "nobody"
55

    
56
## Mailer to use (default: "/usr/lib/sendmail -t")
57
#mailer "/var/qmail/bin/qmail-inject" 
58

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

    
62
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
63
#timestamp_format "%Y%m%d%H%M%S"
64

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

    
73
########################################
74
# RRDTool status gathering configuration
75
# Interval between RRD updates
76
rrd interval 60s;
77

    
78
## These parameters can be overriden in a specific alarm configuration
79
alarm default { 
80
	command on "touch /tmp/filter_dirty"
81
	command off "touch /tmp/filter_dirty"
82
	combine 10s
83
}
84

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

    
91
## "Delay" alarm definition. 
92
## This alarm will be fired when responses are delayed more than 200ms
93
## it will be canceled, when the delay drops below 100ms
94
alarm delay "delay" {
95
	delay_low 200ms
96
	delay_high 500ms
97
}
98

    
99
## "Loss" alarm definition. 
100
## This alarm will be fired when packet loss goes over 20%
101
## it will be canceled, when the loss drops below 10%
102
alarm loss "loss" {
103
	percent_low 10
104
	percent_high 20
105
}
106

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

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

    
122
	## Names of the alarms that may be generated for the target
123
	alarms "down","delay","loss"
124

    
125
	## Location of the RRD
126
	#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
127
}
128

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

    
136
EOD;
137

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

    
163
	if(!is_process_running("apinger")) {
164
		if (is_dir("{$g['tmp_path']}"))
165
			chmod("{$g['tmp_path']}", 01777);
166
		if (is_dir("{$g['vardb_path']}/rrd"))
167
			chown("{$g['vardb_path']}/rrd", "nobody");
168
		/* start a new apinger process */
169
		mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
170
	}
171
	return 0;
172
}
173

    
174
/* return the status of the apinger targets as a array */
175
function return_gateways_status() {
176
	global $config;
177
	global $g;
178
	$gateways_arr = return_gateways_array();
179

    
180
	$apingerstatus = array();
181
	if(is_readable("{$g['tmp_path']}/apinger.status"))
182
		$apingerstatus = file("{$g['tmp_path']}/apinger.status");
183

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

    
219
function return_gateways_array() {
220
	global $config;
221
	global $g;
222
	$gateways_arr = array();
223

    
224
	/* Loop through all interfaces with a gateway and add it to a array */
225
	$iflist = get_configured_interface_list();
226
	foreach($iflist as $ifname => $if ) {
227
		if(interface_has_gateway($ifname)) {
228
			$gateway = array();
229
			$friendly = convert_real_interface_to_friendly_interface_name($ifname);
230
			if(isset($config['interfaces'][$friendly]['gateway'])) {
231
				$gateway['gateway'] = lookup_gateway_ip_by_name($config['interfaces'][$friendly]['gateway']);
232
			} else {
233
				$gateway['gateway'] = get_interface_gateway($ifname);
234
			}
235
			log_error("Interface $friendly has a gateway, found gateway '{$gateway['gateway']}'");
236
			/* Loopback for dynamic interfaces without a IP */
237
			if(!is_ipaddr(trim($gateway['gateway']))) {
238
				 $gateway['gateway'] = "127.0.0.2";
239
			}
240
			/* do not add dynamic gateways if it is also found in the gateways array */
241
			if(is_array($config['gateways']['gateway_item'])) {
242
				foreach($config['gateways']['gateway_item'] as $gateway_item) {
243
					if($gateway_item['gateway'] == $gateway['gateway']) {
244
						log_error("Found a match for {$gateway['gateway']} in the config array, skipping $friendly");
245
						continue 2;
246
					}
247
				}
248
			}
249

    
250
			$gateway['interface'] = $ifname;
251
			$descr = convert_friendly_interface_to_friendly_descr($friendly);
252
			/* retrieve a proper monitor IP? */
253
			if(is_ipaddr($config['interfaces'][$friendly]['monitorip'])) {
254
				$gateway['monitor'] = $config['interfaces'][$friendly]['monitorip'];
255
			} else {
256
				$gateway['monitor'] = $gateway['gateway'];
257
			}
258
			$gateway['name'] = "{$friendly}";
259
			$gateway['descr'] = "Interface {$descr} Gateway";
260
			$gateway['attribute'] = "system";
261
			$gateway['interface'] = "$friendly";
262
			$gateways_arr[] = $gateway;
263
			$debugstr = print_r($gateway, true);
264
			log_error("proceeding with gateway config $debugstr");
265
		}
266
	}
267

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

    
284
?>
(14-14/37)