Project

General

Profile

Download (4.96 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
			/* virtual IP */
58
			$slbdconf .= "\t:vip={$vsent['ipaddr']}:\\\n";
59
			/* virtual port */
60
			$slbdconf .= "\t:vip-port={$vsent['port']}:\\\n";
61
			if($vsent['port'] <> "" and $vsent['sitedown'] <> "") {
62
				/* fallback IP */
63
				$slbdconf .= "\t:sitedown={$vsent['sitedown']}:\\\n";
64
				/* fallback port */
65
				$slbdconf .= "\t:sitedown-port={$vsent['port']}:\\\n";
66
			}
67
			for ($i = 0; isset($config['load_balancer']['lbpool'][$i]); $i++) {
68
				if ($config['load_balancer']['lbpool'][$i]['name'] == $vsent['pool']) {
69
					$svrcnt = 0;
70
					$svrtxt = "";
71
					$svrtxt = "\t:service-port={$config['load_balancer']['lbpool'][$i]['port']}:\\\n";
72
					foreach ($config['load_balancer']['lbpool'][$i]['servers'] as $lbsvr) {
73
						$svrtxt .= "\t:{$svrcnt}={$lbsvr}:\\\n";
74
						$svrcnt++;
75
					}
76
					$slbdconf .= "\t:method=round-robin:\\\n";
77
					$slbdconf .= "\t:services={$svrcnt}:\\\n";
78
					$slbdconf .= $svrtxt;
79
				}
80
			}
81
			
82
			$slbdconf .= "\t:tcppoll:send=:expect=:\n";
83
			
84
			$should_start=1;
85
		}
86
	}
87

    
88
	/* Gateway Pools */
89
	if(is_array($a_pool)) {
90
		foreach ($a_pool as $vspool) {
91
			if ($vspool['type'] != "gateway")
92
				continue;
93

    
94
			if ($vspool['desc'] == "")
95
				$slbdconf .= "{$vspool['name']}:\\\n";
96
			else
97
				$slbdconf .= "{$vspool['name']}|{$vspool['desc']}:\\\n";
98
	
99
			/* pool name */
100
			$slbdconf .= "\t:poolname={$vspool['name']}:\\\n";
101
			/* virtual IP */
102
			$slbdconf .= "\t:vip=127.0.0.1:\\\n";
103
			$slbdconf .= "\t:vip-port=666:\\\n";
104
			/* fallback IP */
105
			$slbdconf .= "\t:sitedown=127.0.0.1:\\\n";
106
			/* fallback port */
107
			$slbdconf .= "\t:sitedown-port=666:\\\n";
108

    
109
			$svrcnt = 0;
110
			$svrtxt = "";
111
			foreach ($vspool['servers'] as $lbsvr) {
112
				$lbsvr_split=split("\|", $lbsvr);
113
				$svrtxt .= "\t:{$svrcnt}={$lbsvr_split[1]}:\\\n";
114
				$svrcnt++;
115

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

    
156
?>
(23-23/27)