Project

General

Profile

Download (4.25 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.carpbackup
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
require_once("functions.inc");
24
require_once("config.inc");
25
require_once("notices.inc");
26
require_once("openvpn.inc");
27
require_once("interfaces.inc");
28

    
29
if (isset($_GET['interface'])) {
30
	$argument = $_GET['interface'];
31
} else {
32
	$argument = str_replace("\n", "", $argv[1]);
33
}
34
if (!strstr($argument, "@")) {
35
	log_error("CARP master event triggered from wrong source {$argument}");
36
	exit;
37
}
38

    
39
list($vhid, $iface) = explode("@", $argument);
40

    
41
$friendly = convert_real_interface_to_friendly_interface_name($iface);
42
$friendly_descr = convert_friendly_interface_to_friendly_descr($friendly);
43
$vips = link_interface_to_vips($friendly, '', $vhid);
44
if (!is_array($vips)) {
45
	log_error("CARP master event triggered from wrong source {$argument} - no associated VIPs");
46
	exit;
47
}
48
foreach ($vips as $vip) {
49
	$notificationmsg = sprintf('HA cluster member "(%1$s@%2$s): (%3$s)" has resumed CARP state "BACKUP" for vhid %4$s',
50
		$vip['subnet'], $iface, $friendly_descr, $vhid);
51

    
52
	notify_via_smtp($notificationmsg);
53
	notify_via_growl($notificationmsg);
54
	log_error($notificationmsg);
55
}
56
restart_ppp_interfaces_using_interfaces($vips);
57

    
58
/* Stop OpenVPN clients running on this VIP, since multiple active OpenVPN clients on a CARP cluster can be problematic. */
59
global $config;
60
$a_groups = return_gateway_groups_array(true);
61
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-client'])) {
62
	foreach ($config['openvpn']['openvpn-client'] as $settings) {
63
		if (substr($settings['interface'], 0, 4) == '_vip') {
64
			$openvpn_vip = $settings['interface'];
65
		} else if (is_array($a_groups[$settings['interface']])) {
66
			// interface is a gateway group, check CARP VIP
67
			if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
68
				$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
69
			}
70
		} else {
71
			// this OpenVPN instance not on a CARP IP
72
			continue;
73
		}
74
		foreach ($vips as $vip) {
75
			if ($openvpn_vip == "_vip{$vip['uniqid']}") {
76
				log_error("Stopping OpenVPN client instance on {$friendly_descr} because of transition to CARP backup.");
77
				openvpn_restart('client', $settings);
78
			}
79
		}
80
	}
81
}
82

    
83
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
84
	foreach ($config['openvpn']['openvpn-server'] as $settings) {
85
		if (substr($settings['interface'], 0, 4) == '_vip') {
86
			$openvpn_vip = $settings['interface'];
87
		} else if (is_array($a_groups[$settings['interface']])) {
88
			// interface is a gateway group, check CARP VIP
89
			if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
90
				$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
91
			}
92
		} else {
93
			// this OpenVPN instance not on a CARP IP
94
			continue;
95
		}
96
		foreach ($vips as $vip) {
97
			if ($openvpn_vip == "_vip{$vip['uniqid']}") {
98
				log_error("Stopping OpenVPN server instance on {$friendly_descr} because of transition to CARP backup.");
99
				openvpn_restart('server', $settings);
100
			}
101
		}
102
	}
103
}
104

    
105
/* Reconfigure radvd when necessary */
106
if (isset($config['dhcpdv6']) && is_array($config['dhcpdv6'])) {
107
	$rafound = false;
108
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
109
		foreach ($vips as $vip) {
110
			if ($dhcpv6ifconf['rainterface'] == "_vip{$vip['uniqid']}") {
111
				log_error("Stopping radvd instance on {$friendly_descr} because of transition to CARP master.");
112
				$rafound = true;
113
			}
114
		}
115
	}
116
	if ($rafound) {
117
		services_radvd_configure();
118
	}
119
}
120

    
121
$pluginparams = array();
122
$pluginparams['type'] = 'carp';
123
$pluginparams['event'] = 'rc.carpbackup';
124
$pluginparams['interface'] = $argument;
125
pkg_call_plugins('plugin_carp', $pluginparams);
126

    
127
?>
(25-25/83)