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 $name => $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
|
if(! stristr("127.0.0", $gateway['gateway'])) {
|
156
|
mwexec("/sbin/route add -host " . escapeshellarg($gateway['monitor']) .
|
157
|
" " . escapeshellarg($gateway['gateway']));
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
fwrite($fd, $apingerconfig);
|
163
|
fclose($fd);
|
164
|
|
165
|
if(!is_process_running("apinger")) {
|
166
|
if (is_dir("{$g['tmp_path']}"))
|
167
|
chmod("{$g['tmp_path']}", 01777);
|
168
|
if (is_dir("{$g['vardb_path']}/rrd"))
|
169
|
chown("{$g['vardb_path']}/rrd", "nobody");
|
170
|
/* start a new apinger process */
|
171
|
mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
|
172
|
}
|
173
|
return 0;
|
174
|
}
|
175
|
|
176
|
/* return the status of the apinger targets as a array */
|
177
|
function return_gateways_status() {
|
178
|
global $config;
|
179
|
global $g;
|
180
|
$gateways_arr = return_gateways_array();
|
181
|
|
182
|
$apingerstatus = array();
|
183
|
if(is_readable("{$g['tmp_path']}/apinger.status"))
|
184
|
$apingerstatus = file("{$g['tmp_path']}/apinger.status");
|
185
|
|
186
|
$status = array();
|
187
|
foreach($apingerstatus as $line) {
|
188
|
$fields = explode(":", $line);
|
189
|
switch($fields[0]) {
|
190
|
case "Target":
|
191
|
$target = trim($fields[1]);
|
192
|
$status[$target] = array();
|
193
|
$status[$target]['monitor'] = $target;
|
194
|
foreach($gateways_arr as $name => $gateway) {
|
195
|
if($gateway['monitor'] == "$target") {
|
196
|
$status[$target]['gateway'] = $gateway['gateway'];
|
197
|
$status[$target]['interface'] = $gateway['interface'];
|
198
|
}
|
199
|
}
|
200
|
break;
|
201
|
case "Description":
|
202
|
$status[$target]['name'] = trim($fields[1]);
|
203
|
break;
|
204
|
case "Last reply received":
|
205
|
$status[$target]['lastcheck'] = trim($fields[1]) .":". trim($fields[2]) .":". trim($fields[3]);
|
206
|
break;
|
207
|
case "Average delay":
|
208
|
$status[$target]['delay'] = trim($fields[1]);
|
209
|
break;
|
210
|
case "Average packet loss":
|
211
|
$status[$target]['loss'] = trim($fields[1]);
|
212
|
break;
|
213
|
case "Active alarms":
|
214
|
$status[$target]['status'] = trim($fields[1]);
|
215
|
break;
|
216
|
}
|
217
|
}
|
218
|
return($status);
|
219
|
}
|
220
|
|
221
|
function return_gateways_array() {
|
222
|
global $config;
|
223
|
|
224
|
$gateways_arr = array();
|
225
|
|
226
|
/* Loop through all interfaces with a gateway and add it to a array */
|
227
|
$iflist = get_configured_interface_with_descr();
|
228
|
foreach($iflist as $ifname => $friendly ) {
|
229
|
if(interface_has_gateway($ifname)) {
|
230
|
$gateway = array();
|
231
|
$gateway['gateway'] = get_interface_gateway($ifname);
|
232
|
/* Loopback dummy for dynamic interfaces without a IP */
|
233
|
if(!is_ipaddr(trim($gateway['gateway']))) {
|
234
|
$gateway['gateway'] = "127.0.0.2";
|
235
|
}
|
236
|
/*
|
237
|
* do not add dynamic gateways if it is also found
|
238
|
* in the gateways array.
|
239
|
* XXX: NB: Can this ever happen?!
|
240
|
* smos@ get_interface_gateway() also succeeds for
|
241
|
* static gateways, thus they need to be excluded
|
242
|
*/
|
243
|
if(is_array($config['gateways']['gateway_item'])) {
|
244
|
foreach($config['gateways']['gateway_item'] as $gateway_item) {
|
245
|
if($gateway_item['gateway'] == $gateway['gateway']) {
|
246
|
continue 2;
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
|
251
|
/* retrieve a proper monitor IP? */
|
252
|
if(is_ipaddr($config['interfaces'][$ifname]['monitorip'])) {
|
253
|
$gateway['monitor'] = $config['interfaces'][$ifname]['monitorip'];
|
254
|
} else {
|
255
|
$gateway['monitor'] = $gateway['gateway'];
|
256
|
}
|
257
|
$gateway['interface'] = get_real_interface($ifname);
|
258
|
$gateway['name'] = "{$ifname}";
|
259
|
$gateway['descr'] = "Interface {$friendly} Gateway";
|
260
|
$gateway['attribute'] = "system";
|
261
|
$gateways_arr[$ifname] = $gateway;
|
262
|
}
|
263
|
}
|
264
|
|
265
|
/* tack on all the hard defined gateways as well */
|
266
|
if(is_array($config['gateways']['gateway_item'])) {
|
267
|
$i = 0;
|
268
|
foreach($config['gateways']['gateway_item'] as $gateway) {
|
269
|
if($gateway['monitor'] == "") {
|
270
|
$gateway['monitor'] = $gateway['gateway'];
|
271
|
}
|
272
|
/* include the gateway index as the attribute */
|
273
|
$gateway['interface'] = convert_friendly_interface_to_real_interface_name($gateway['interface']);
|
274
|
$gateway['attribute'] = "$i";
|
275
|
$gateways_arr[$gateway['name']] = $gateway;
|
276
|
$i++;
|
277
|
}
|
278
|
}
|
279
|
return($gateways_arr);
|
280
|
}
|
281
|
|
282
|
/* return a array with all gateway groups with name as key
|
283
|
* All gateway groups will be processed before returning the array.
|
284
|
*/
|
285
|
function return_gateway_groups_array() {
|
286
|
global $config, $g;
|
287
|
|
288
|
/* fetch the current gateways status */
|
289
|
$gateways_status = return_gateways_status();
|
290
|
$gateways_arr = return_gateways_array();
|
291
|
$gateway_groups_array = array();
|
292
|
|
293
|
if (is_array($config['gateways']['gateway_group'])) {
|
294
|
foreach($config['gateways']['gateway_group'] as $group) {
|
295
|
/* create array with group gateways members seperated by tier */
|
296
|
$tiers = array();
|
297
|
foreach($group['item'] as $item) {
|
298
|
$itemsplit = explode("|", $item);
|
299
|
$tier = $itemsplit[1];
|
300
|
$gwname = $itemsplit[0];
|
301
|
/* check if the gateway is available before adding it to the array */
|
302
|
foreach($gateways_status as $status) {
|
303
|
if(($status['name'] != $gwname)) {
|
304
|
continue;
|
305
|
}
|
306
|
switch($status['status']) {
|
307
|
case "None":
|
308
|
/* Online add member */
|
309
|
$tiers[$tier][] = $gwname;
|
310
|
break;
|
311
|
case "delay":
|
312
|
if(strstr($group['trigger'] , "latency")) {
|
313
|
/* high latency */
|
314
|
log_error("MONITOR: $gwname has high latency, removing from routing group");
|
315
|
} else {
|
316
|
$tiers[$tier][] = $gwname;
|
317
|
}
|
318
|
break;
|
319
|
case "loss":
|
320
|
if(strstr($group['trigger'], "loss")) {
|
321
|
/* packet loss */
|
322
|
log_error("MONITOR: $gwname has packet loss, removing from routing group");
|
323
|
} else {
|
324
|
$tiers[$tier][] = $gwname;
|
325
|
}
|
326
|
break;
|
327
|
}
|
328
|
}
|
329
|
}
|
330
|
$tiers_count = count($tiers);
|
331
|
if($tiers_count == 0) {
|
332
|
/* Oh dear, we have no members! Engage Plan B */
|
333
|
log_error("All gateways are unavailable, proceeding with configured XML settings!");
|
334
|
foreach($group['item'] as $item) {
|
335
|
foreach($group['item'] as $item) {
|
336
|
$itemsplit = explode("|", $item);
|
337
|
$tier = $itemsplit[1];
|
338
|
$gwname = $itemsplit[0];
|
339
|
$tiers[$tier][] = $gwname;
|
340
|
}
|
341
|
}
|
342
|
}
|
343
|
/* sort the tiers array by the tier key */
|
344
|
ksort($tiers);
|
345
|
/* we do not really foreach the tiers as we stop after the first tier */
|
346
|
foreach($tiers as $tiernr => $tier) {
|
347
|
/* process all gateways in this tier */
|
348
|
$member_count = count($tier);
|
349
|
foreach($tier as $tiernr => $member) {
|
350
|
/* determine interface gateway */
|
351
|
foreach($gateways_arr as $name => $gateway) {
|
352
|
if($gateway['name'] == $member) {
|
353
|
$int = $gateway['interface'];
|
354
|
if(is_ipaddr($gateway['gateway']))
|
355
|
$gatewayip = $gateway['gateway'];
|
356
|
else
|
357
|
$gatewayip = lookup_gateway_ip_by_name($gateway['gateway']);
|
358
|
break;
|
359
|
}
|
360
|
}
|
361
|
if (($int <> "") && is_ipaddr($gatewayip)) {
|
362
|
$gateway_groups_array[$group['name']][$tiernr]['int'] = "$int";
|
363
|
$gateway_groups_array[$group['name']][$tiernr]['gwip'] = "$gatewayip";
|
364
|
}
|
365
|
}
|
366
|
/* we should have the 1st available tier now, exit stage left */
|
367
|
break;
|
368
|
}
|
369
|
}
|
370
|
}
|
371
|
return($gateway_groups_array);
|
372
|
}
|
373
|
|
374
|
|
375
|
?>
|