Project

General

Profile

Download (6.47 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
	exec("/usr/bin/killall apinger");
44
	$fd = fopen("{$g['varetc_path']}/apinger.conf", "w");
45
	$apingerconfig = <<<EOD
46

    
47
# pfSense apinger configuration file. Automatically Generated!
48

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
133
EOD;
134

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

    
159
	sleep(3);
160
	if(!is_process_running("apinger")) {
161
		if (is_dir("{$g['vardb_path']}/rrd"))
162
			chown("{$g['vardb_path']}/rrd", "nobody");
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
?>
(12-12/34)