1 |
3ad6d3bb
|
Bill Marquette
|
<?php
|
2 |
577c9191
|
Bill Marquette
|
/* $Id$ */
|
3 |
3ad6d3bb
|
Bill Marquette
|
/*
|
4 |
|
|
vslb.inc
|
5 |
65437f22
|
Scott Ullrich
|
Copyright (C) 2005 Bill Marquette
|
6 |
3ad6d3bb
|
Bill Marquette
|
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 |
09894747
|
Scott Ullrich
|
|
39 |
3ad6d3bb
|
Bill Marquette
|
$a_vs = &$config['load_balancer']['virtual_server'];
|
40 |
43ea32f9
|
Scott Ullrich
|
$a_pool = &$config['load_balancer']['lbpool'];
|
41 |
3ad6d3bb
|
Bill Marquette
|
|
42 |
09894747
|
Scott Ullrich
|
$should_start=0;
|
43 |
3ad6d3bb
|
Bill Marquette
|
|
44 |
|
|
$fd = fopen("{$g['varetc_path']}/slbd.conf", "w");
|
45 |
|
|
|
46 |
65437f22
|
Scott Ullrich
|
|
47 |
|
|
/* Virtual server pools */
|
48 |
fbcda26c
|
Scott Ullrich
|
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 |
ee5f031a
|
Scott Ullrich
|
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 |
fbcda26c
|
Scott Ullrich
|
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 |
b5aa9413
|
Scott Ullrich
|
$slbdconf .= "\t:method=round-robin:\\\n";
|
77 |
fbcda26c
|
Scott Ullrich
|
$slbdconf .= "\t:services={$svrcnt}:\\\n";
|
78 |
|
|
$slbdconf .= $svrtxt;
|
79 |
3ad6d3bb
|
Bill Marquette
|
}
|
80 |
|
|
}
|
81 |
fbcda26c
|
Scott Ullrich
|
|
82 |
|
|
$slbdconf .= "\t:tcppoll:send=:expect=:\n";
|
83 |
|
|
|
84 |
|
|
$should_start=1;
|
85 |
3ad6d3bb
|
Bill Marquette
|
}
|
86 |
09894747
|
Scott Ullrich
|
}
|
87 |
65437f22
|
Scott Ullrich
|
|
88 |
|
|
/* Gateway Pools */
|
89 |
43ea32f9
|
Scott Ullrich
|
if(is_array($a_pool)) {
|
90 |
ee5f031a
|
Scott Ullrich
|
foreach ($a_pool as $vspool) {
|
91 |
65437f22
|
Scott Ullrich
|
if ($vspool['type'] != "gateway")
|
92 |
|
|
continue;
|
93 |
|
|
|
94 |
ee5f031a
|
Scott Ullrich
|
if ($vspool['desc'] == "")
|
95 |
|
|
$slbdconf .= "{$vspool['name']}:\\\n";
|
96 |
43ea32f9
|
Scott Ullrich
|
else
|
97 |
ee5f031a
|
Scott Ullrich
|
$slbdconf .= "{$vspool['name']}|{$vspool['desc']}:\\\n";
|
98 |
2de00bff
|
Scott Ullrich
|
|
99 |
ee5f031a
|
Scott Ullrich
|
/* pool name */
|
100 |
|
|
$slbdconf .= "\t:poolname={$vspool['name']}:\\\n";
|
101 |
|
|
/* virtual IP */
|
102 |
1e6f5e9d
|
Scott Ullrich
|
$slbdconf .= "\t:vip=127.0.0.1:\\\n";
|
103 |
65437f22
|
Scott Ullrich
|
$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 |
ee5f031a
|
Scott Ullrich
|
|
109 |
65437f22
|
Scott Ullrich
|
$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 |
ee5f031a
|
Scott Ullrich
|
}
|
121 |
2de00bff
|
Scott Ullrich
|
}
|
122 |
65437f22
|
Scott Ullrich
|
$slbdconf .= "\t:service-port=666:\\\n";
|
123 |
|
|
$slbdconf .= "\t:method=round-robin:\\\n";
|
124 |
|
|
$slbdconf .= "\t:services={$svrcnt}:\\\n";
|
125 |
|
|
$slbdconf .= $svrtxt;
|
126 |
43ea32f9
|
Scott Ullrich
|
|
127 |
65437f22
|
Scott Ullrich
|
$slbdconf .= "\t:ping:\n";
|
128 |
ee5f031a
|
Scott Ullrich
|
|
129 |
|
|
$should_start=1;
|
130 |
43ea32f9
|
Scott Ullrich
|
}
|
131 |
|
|
}
|
132 |
09894747
|
Scott Ullrich
|
|
133 |
|
|
if($should_start == 1) {
|
134 |
|
|
fwrite($fd, $slbdconf);
|
135 |
|
|
fclose($fd);
|
136 |
ee5f031a
|
Scott Ullrich
|
mwexec("/usr/bin/killall -9 slbd");
|
137 |
09894747
|
Scott Ullrich
|
/* 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 |
79a85ab8
|
Scott Ullrich
|
mwexec("/usr/bin/killall -9 slbd");
|
141 |
ee5f031a
|
Scott Ullrich
|
fclose($fd);
|
142 |
3ad6d3bb
|
Bill Marquette
|
}
|
143 |
|
|
}
|
144 |
|
|
|
145 |
79a85ab8
|
Scott Ullrich
|
?>
|