Project

General

Profile

Download (8.5 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 .= "}\n";
147
			$apingerconfig .= "\n";
148
			if($gateway['monitor'] == $gateway['gateway']) {
149
				/* if the gateway is the same as the monitor we do not add a
150
				 * route as this will break the routing table */
151
				continue;
152
			} else {
153
				mwexec("/sbin/route delete -host " . escapeshellarg($gateway['monitor']));
154
				mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
155
					" " . escapeshellarg($gateway['gateway']));
156
			}
157
		}
158
	}
159
	fwrite($fd, $apingerconfig);
160
	fclose($fd);
161

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

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

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

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

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

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

    
246
			$gateway['interface'] = $ifname;
247
			$descr = convert_friendly_interface_to_friendly_descr($friendly);
248
			/* retrieve a proper monitor IP? */
249
			if(is_ipaddr($config['interfaces'][$friendly]['monitorip'])) {
250
				$gateway['monitor'] = $config['interfaces'][$friendly]['monitorip'];
251
			} else {
252
				$gateway['monitor'] = $gateway['gateway'];
253
			}
254
			$gateway['name'] = "{$friendly}";
255
			$gateway['descr'] = "Interface {$descr} Gateway";
256
			$gateway['attribute'] = "system";
257
			$gateway['interface'] = "$friendly";
258
			$gateways_arr[] = $gateway;
259
		}
260
	}
261

    
262
	/* tack on all the hard defined gateways as well */
263
	if(is_array($config['gateways']['gateway_item'])) {
264
		$i = 0;
265
		foreach($config['gateways']['gateway_item'] as $gateway) {
266
			if($gateway['monitor'] == "") {
267
				$gateway['monitor'] = $gateway['gateway'];
268
			}
269
			/* include the gateway index as the attribute */
270
			$gateway['attribute'] = "$i";
271
			$gateways_arr[] = $gateway;
272
			$i++;
273
		}
274
	}
275
	return($gateways_arr);
276
}
277

    
278
?>
(14-14/37)