Project

General

Profile

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

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

    
48
# pfSense apinger configuration file. Automatically Generated!
49

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

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

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

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

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

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

    
76
## These parameters can be overriden in a specific alarm configuration
77
alarm default { 
78
	command on "/etc/rc.filter_configure"
79
	command off "/etc/rc.filter_configure"
80
	combine 10s
81
}
82

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

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

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

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

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

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

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

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

    
134
EOD;
135

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

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

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

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

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

    
215
?>
(14-14/37)