Project

General

Profile

Download (5.47 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	vslb.inc
5
    Copyright (C) 2005 Bill Marquette
6
	All rights reserved.
7

    
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10

    
11
	1. Redistributions of source code must retain the above copyright notice,
12
	   this list of conditions and the following disclaimer.
13

    
14
	2. Redistributions in binary form must reproduce the above copyright
15
	   notice, this list of conditions and the following disclaimer in the
16
	   documentation and/or other materials provided with the distribution.
17

    
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28

    
29
*/
30

    
31
/* include all configuration functions */
32
require_once("functions.inc");
33
require_once("pkg-utils.inc");
34
require_once("notices.inc");
35

    
36
function slbd_configure() {
37
	global $config, $g;
38
	
39
	$a_vs = &$config['load_balancer']['virtual_server'];
40
	$a_pool = &$config['load_balancer']['lbpool'];
41

    
42
	$should_start=0;
43

    
44
	$fd = fopen("{$g['varetc_path']}/slbd.conf", "w");
45

    
46

    
47
	/* Virtual server pools */
48
	if(is_array($a_vs)) {
49
		foreach ($a_vs as $vsent) {
50
			if ($vsent['desc'] == "")
51
				$slbdconf .= "{$vsent['name']}:\\\n";
52
			else
53
				$slbdconf .= "{$vsent['name']}|{$vsent['desc']}:\\\n";
54
	
55
			/* pool name */
56
			$slbdconf .= "\t:poolname={$vsent['name']}:\\\n";
57
			/* remove pool status files so we don't end up with a mismatch */
58
			if(file_exists("{$g['tmp_path']}/{$vsent['name']}.pool"))
59
				unlink("{$g['tmp_path']}/{$vsent['name']}.pool");
60
			/* virtual IP */
61
			$slbdconf .= "\t:vip={$vsent['ipaddr']}:\\\n";
62
			/* virtual port */
63
			$slbdconf .= "\t:vip-port={$vsent['port']}:\\\n";
64
			if($vsent['port'] <> "" and $vsent['sitedown'] <> "") {
65
				/* fallback IP */
66
				$slbdconf .= "\t:sitedown={$vsent['sitedown']}:\\\n";
67
				/* fallback port */
68
				$slbdconf .= "\t:sitedown-port={$vsent['port']}:\\\n";
69
			}
70
			for ($i = 0; isset($config['load_balancer']['lbpool'][$i]); $i++) {
71
				if ($config['load_balancer']['lbpool'][$i]['name'] == $vsent['pool']) {
72
					$svrcnt = 0;
73
					$svrtxt = "";
74
					$svrtxt = "\t:service-port={$config['load_balancer']['lbpool'][$i]['port']}:\\\n";
75
					if($config['load_balancer']['lbpool'])
76
						if(is_array($config['load_balancer']['lbpool'][$i]['servers']))
77
							foreach ($config['load_balancer']['lbpool'][$i]['servers'] as $lbsvr) {
78
								$svrtxt .= "\t:{$svrcnt}={$lbsvr}:\\\n";
79
								$svrcnt++;
80
							}
81
					$slbdconf .= "\t:method=round-robin:\\\n";
82
					$slbdconf .= "\t:services={$svrcnt}:\\\n";
83
					$slbdconf .= $svrtxt;
84
				}
85
			}
86
			
87
			$slbdconf .= "\t:tcppoll:send=:expect=:\n";
88
			
89
			$should_start=1;
90
		}
91
	}
92

    
93
	/* Gateway Pools */
94
	if(is_array($a_pool)) {
95
		foreach ($a_pool as $vspool) {
96
			if ($vspool['type'] != "gateway")
97
				continue;
98

    
99
			if ($vspool['desc'] == "")
100
				$slbdconf .= "{$vspool['name']}:\\\n";
101
			else
102
				$slbdconf .= "{$vspool['name']}|{$vspool['desc']}:\\\n";
103
	
104
			/* pool name */
105
			$slbdconf .= "\t:poolname={$vspool['name']}:\\\n";
106
			/* remove pool status files so we don't end up with a mismatch */
107
			if(file_exists("{$g['tmp_path']}/{$vspool['name']}.pool"))
108
				unlink("{$g['tmp_path']}/{$vspool['name']}.pool");
109
			/* virtual IP */
110
			$slbdconf .= "\t:vip=127.0.0.1:\\\n";
111
			$slbdconf .= "\t:vip-port=666:\\\n";
112
			/* fallback IP */
113
			$slbdconf .= "\t:sitedown=127.0.0.1:\\\n";
114
			/* fallback port */
115
			$slbdconf .= "\t:sitedown-port=666:\\\n";
116

    
117
			$svrcnt = 0;
118
			$svrtxt = "";
119
			if($vspool['servers'])
120
			foreach ($vspool['servers'] as $lbsvr) {
121
				$lbsvr_split=split("\|", $lbsvr);
122
				$svrtxt .= "\t:{$svrcnt}={$lbsvr_split[1]}:\\\n";
123
				$svrcnt++;
124

    
125
				/* Add static routes to the monitor IPs */
126
				$int = convert_friendly_interface_to_real_interface_name($lbsvr_split[0]);
127
				$gateway = get_interface_gateway($int);
128
				$int_ip = find_interface_ip($int);
129
				if($int_ip == "0.0.0.0") {
130
					/*   DHCP Corner case.  If DHCP is down, we delete the route then
131
					 *   there is a chance the monitor ip gateway will go out the link
132
					 *   that is up.
133
					 */
134
					mwexec("/sbin/route delete -host {$lbsvr_split[1]} 1>/dev/null 2>&1");
135
					mwexec("/sbin/route add -host {$lbsvr_split[1]} 127.0.0.1 1> /dev/null 2>&1");
136
				} else {
137
					mwexec("/sbin/route delete -host {$lbsvr_split[1]} 1>/dev/null 2>&1");
138
					mwexec("/sbin/route add -host {$lbsvr_split[1]} {$gateway} 1> /dev/null 2>&1");					
139
				}
140
			}
141
			$slbdconf .= "\t:service-port=666:\\\n";
142
			$slbdconf .= "\t:method=round-robin:\\\n";
143
			$slbdconf .= "\t:services={$svrcnt}:\\\n";
144
			$slbdconf .= $svrtxt;
145
			
146
			$slbdconf .= "\t:ping:\n";
147
			
148
			$should_start=1;
149
		}
150
	}
151
	
152
	if($should_start == 1) {
153
		fwrite($fd, $slbdconf);
154
		fclose($fd);
155
		mwexec("/usr/bin/killall -9 slbd");
156
		sleep(2);
157
		/* startup slbd pointing it's config at /var/etc/slbd.conf with a polling interval of 5 seconds */
158
		mwexec("/usr/local/sbin/slbd -c{$g['varetc_path']}/slbd.conf -r5000");
159
	} else {
160
		mwexec("/usr/bin/killall -9 slbd");
161
		fclose($fd);
162
	}
163
}
164

    
165
?>
(23-23/27)