Project

General

Profile

Download (4.58 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
				/* Add static routes to the monitor IPs */
116
				$next_hop = exec_command("/sbin/route -n get {$lbsvr_split[1]} | /usr/bin/grep gateway |/usr/bin/awk '{ print \$2; };'");
117
				if ($next_hop != $lbsvr_split[0]) {
118
					mwexec("/sbin/route delete -host {$lbsvr_split[1]} 1>/dev/null 2>&1");
119
					mwexec("/sbin/route add -host {$lbsvr_split[1]} {$lbsvr_split[0]} 1> /dev/null 2>&1");
120
				}
121
			}
122
			$slbdconf .= "\t:service-port=666:\\\n";
123
			$slbdconf .= "\t:method=round-robin:\\\n";
124
			$slbdconf .= "\t:services={$svrcnt}:\\\n";
125
			$slbdconf .= $svrtxt;
126
			
127
			$slbdconf .= "\t:ping:\n";
128
			
129
			$should_start=1;
130
		}
131
	}
132
	
133
	if($should_start == 1) {
134
		fwrite($fd, $slbdconf);
135
		fclose($fd);
136
		mwexec("/usr/bin/killall -9 slbd");
137
		/* startup slbd pointing it's config at /var/etc/slbd.conf with a polling interval of 5 seconds */
138
		mwexec("/usr/local/sbin/slbd -c{$g['varetc_path']}/slbd.conf -r5000");
139
	} else {
140
		mwexec("/usr/bin/killall -9 slbd");
141
		fclose($fd);
142
	}
143
}
144

    
145
?>
(23-23/27)