Project

General

Profile

Download (8.06 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

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

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

    
49
# pfSense apinger configuration file. Automatically Generated!
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
135
EOD;
136

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

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

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

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

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

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

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

    
241
			$gateway['interface'] = $ifname;
242
			$descr = convert_friendly_interface_to_friendly_descr($friendly);
243
			/* FIXME: somehow retrieve a proper monitor IP? */
244
			$gateway['monitor'] = $gateway['gateway'];
245
			$gateway['name'] = "{$friendly}";
246
			$gateway['descr'] = "Interface {$descr} Gateway";
247
			$gateway['modifier'] = "readonly";
248
			$gateways_arr[] = $gateway;
249
		}
250
	}
251

    
252
	/* tack on all the hard defined gateways as well */
253
	if(is_array($config['gateways']['gateway_item'])) {
254
		foreach($config['gateways']['gateway_item'] as $gateway) {
255
			if($gateway['monitor'] == "") {
256
				$gateway['monitor'] = $gateway['gateway'];
257
			}
258
			$gateway['modifier'] = "writeable";
259
			$gateways_arr[] = $gateway;
260
		}
261
	}
262
	return($gateways_arr);
263
}
264

    
265
?>
(14-14/37)