Project

General

Profile

Download (11 KB) Statistics
| Branch: | Tag: | Revision:
1 3ad6d3bb Bill Marquette
<?php
2 577c9191 Bill Marquette
/* $Id$ */
3 3ad6d3bb Bill Marquette
/*
4 17623ab5 Bill Marquette
  vslb.inc
5
  Copyright (C) 2005-2008 Bill Marquette
6
  All rights reserved.
7 3ad6d3bb Bill Marquette
8 17623ab5 Bill Marquette
  Redistribution and use in source and binary forms, with or without
9
  modification, are permitted provided that the following conditions are met:
10 3ad6d3bb Bill Marquette
11 17623ab5 Bill Marquette
  1. Redistributions of source code must retain the above copyright notice,
12
  this list of conditions and the following disclaimer.
13 3ad6d3bb Bill Marquette
14 17623ab5 Bill Marquette
  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 3ad6d3bb Bill Marquette
18 17623ab5 Bill Marquette
  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 3ad6d3bb Bill Marquette
29 17623ab5 Bill Marquette
 */
30 3ad6d3bb Bill Marquette
31 523855b0 Scott Ullrich
/*
32
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/relayd
33
	pfSense_MODULE:	routing
34
*/
35
36 50d86c13 Bill Marquette
/* DISABLE_PHP_LINT_CHECKING */
37
38 3ad6d3bb Bill Marquette
/* include all configuration functions */
39
40 50d86c13 Bill Marquette
class Monitor {
41
	private $conf = array();
42
	function __construct($config) {
43
		$this->conf = $config;
44
	}
45
46
	public function p() {
47
		return "check {$this->get('proto')}";
48
	}
49
	private function get($var) {
50
		return isset($this->$var) ? $this->$var : "";
51
	}
52
	protected function config($element) {
53
		return isset($this->conf[$element]) ? $this->conf[$element] : "";
54
	}
55
}
56
57
class TCPMonitor extends Monitor {
58
	protected $proto = 'tcp';
59
}
60
61
class SSLMonitor extends Monitor {
62
	protected $proto = 'ssl';
63
}
64
65
class ICMPMonitor extends Monitor {
66
	protected $proto = 'icmp';
67
}
68
69
class HTTPMonitor extends Monitor {
70
	protected $proto = 'http';
71
	function __construct($config) {
72
		parent::__construct($config);
73
	}
74
	public function p() {
75
		$method = ($this->code() != "") ? $this->code() : $this->digest();
76
		return "check {$this->proto} {$this->path()} {$this->host()} {$method}";
77
	}
78
79
	private function path() {
80
		return $this->config('path') != "" ? "'{$this->config('path')}'" : "";
81
	}
82
83
	private function host() {
84
		return $this->config('host') != "" ? "host {$this->config('host')}" : "";
85
	}
86
87
	private function code() {
88
		return $this->config('code') != "" ? "code {$this->config('code')}" : "";
89
	}
90
91
	private function digest() {
92
		return $this->config('digest') != "" ? "digest {$this->config('digest')}" : "";
93
	}
94
}
95
96
class HTTPSMonitor extends HTTPMonitor {
97
	protected $proto = 'https';
98
}
99
100
class SendMonitor extends Monitor {
101
	private $proto = 'send';
102
	function __construct($config) {
103
		parent::__construct($config);
104
	}
105
	public function p() {
106
		return "check {$this->proto} {$this->data()} expect {$this->pattern()} {$this->ssl()}";
107
	}
108
109
110
	private function data() {
111
		return $this->config('send') != "" ? "{$this->config('send')}" : "";
112
	}
113
114
	private function pattern() {
115
		return $this->config('expect') != "" ? "{$this->config('expect')}" : "";
116
	}
117
118
	private function ssl() {
119
		return $this->config('ssl') == true ? "ssl" : "";
120
	}
121
}
122
123 0919224f Bill Marquette
function echo_lbaction($action) {
124
  global $config;
125
  
126
  // Index actions by name
127
  $actions_a = array();
128
  for ($i=0; isset($config['load_balancer']['lbaction'][$i]); $i++)
129
		$actions_a[$config['load_balancer']['lbaction'][$i]['name']] = $config['load_balancer']['lbaction'][$i];
130
131
  $ret = "";
132
  $ret .=  "{$actions_a[$action]['direction']} {$actions_a[$action]['type']} {$actions_a[$action]['action']}";
133
  switch($actions_a[$action]['action']) {
134
    case 'append': {
135
      $ret .= " \"{$actions_a[$action]['options']['value']}\" to \"{$actions_a[$action]['options']['akey']}\"";
136
      break;
137
    }
138
    case 'change': {
139
      $ret .= " \"{$actions_a[$action]['options']['akey']}\" to \"{$actions_a[$action]['options']['value']}\"";
140
      break;
141
    }
142
    case 'expect': {
143
      $ret .= " \"{$actions_a[$action]['options']['value']}\" from \"{$actions_a[$action]['options']['akey']}\"";
144
      break;
145
    }
146
    case 'filter': {
147
      $ret .= " \"{$actions_a[$action]['options']['value']}\" from \"{$actions_a[$action]['options']['akey']}\"";
148
      break;
149
    }
150
    case 'hash': {
151
      $ret .= " \"{$actions_a[$action]['options']['akey']}\"";
152
      break;
153
    }
154
    case 'log': {
155
      $ret .= " \"{$actions_a[$action]['options']['akey']}\"";
156
      break;
157
    }
158
  }
159
  return $ret;
160
}
161 50d86c13 Bill Marquette
162 17623ab5 Bill Marquette
function relayd_configure() {
163
  global $config, $g;
164
165 a825c6f7 Bill Marquette
  $vs_a = $config['load_balancer']['virtual_server'];
166
  $pool_a = $config['load_balancer']['lbpool'];
167
  $protocol_a = $config['load_balancer']['lbprotocol'];
168 17623ab5 Bill Marquette
169 50d86c13 Bill Marquette
  $check_a = array();
170
171 52bd375c Bill Marquette
  foreach ((array)$config['load_balancer']['monitor_type'] as $type) {
172 50d86c13 Bill Marquette
    switch($type['type']) {
173
      case 'icmp': {
174
        $mon = new ICMPMonitor($type['options']);
175
	break;
176
      }
177
      case 'tcp': {
178
        $mon = new TCPMonitor($type['options']);
179
	break;
180
      }
181
      case 'http': {
182
        $mon = new HTTPMonitor($type['options']);
183
	break;
184
      }
185
      case 'https': {
186
        $mon = new HTTPSMonitor($type['options']);
187
	break;
188
      }
189
      case 'send': {
190
        $mon = new SendMonitor($type['options']);
191
	break;
192
      }
193
    }
194 596a3aba Seth Mos
    if($mon) {
195
      $check_a[$type['name']] = $mon->p();
196
    }
197 50d86c13 Bill Marquette
  }
198
199
200 17623ab5 Bill Marquette
  $fd = fopen("{$g['varetc_path']}/relayd.conf", "w");
201
202
  /* reindex pools by name as we loop through the pools array */
203
  $pools = array();
204 087a89f8 Chris Buechler
  $conf .= "log updates \n";
205 17623ab5 Bill Marquette
  /* Virtual server pools */
206
  if(is_array($pool_a)) {
207
    for ($i = 0; isset($pool_a[$i]); $i++) {
208
      if(is_array($pool_a[$i]['servers'])) {
209 74b7361f jim-p
	if (!empty($pool_a[$i]['retry'])) {
210
		$retrytext = " retry {$pool_a[$i]['retry']}";
211
		$srvtxt = implode("{$retrytext}, ", $pool_a[$i]['servers']) . "{$retrytext}";
212
	} else {
213
		$srvtxt = implode(", ", $pool_a[$i]['servers']);
214
	}
215 17623ab5 Bill Marquette
        $conf .= "table <{$pool_a[$i]['name']}> { $srvtxt }\n";
216
        /* Index by name for easier fetching when we loop through the virtual servers */
217
        $pools[$pool_a[$i]['name']] = $pool_a[$i];
218
      }
219
    }
220
  }
221 0919224f Bill Marquette
  if(is_array($protocol_a)) {
222
    for ($i = 0; isset($protocol_a[$i]); $i++) {
223 ab9c7767 Bill Marquette
      $proto = "{$protocol_a[$i]['type']} protocol \"{$protocol_a[$i]['name']}\" {\n";
224 0919224f Bill Marquette
      if(is_array($protocol_a[$i]['lbaction'])) {
225 ab9c7767 Bill Marquette
        if($protocol_a[$i]['lbaction'][0] == "") {
226
          continue;
227
        }
228 0919224f Bill Marquette
        for ($a = 0; isset($protocol_a[$i]['lbaction'][$a]); $a++) {
229 ab9c7767 Bill Marquette
          $proto .= "  " . echo_lbaction($protocol_a[$i]['lbaction'][$a]) . "\n";
230 0919224f Bill Marquette
        }
231
      }
232 ab9c7767 Bill Marquette
      $proto .= "}\n";
233
      $conf .= $proto;
234 0919224f Bill Marquette
    }
235
  }
236 17623ab5 Bill Marquette
  if(is_array($vs_a)) {
237
    for ($i = 0; isset($vs_a[$i]); $i++) {
238 0919224f Bill Marquette
      switch($vs_a[$i]['mode']) {
239 f8cb57db Chris Buechler
        case 'redirect_mode': {
240 0919224f Bill Marquette
          $conf .= "redirect \"{$vs_a[$i]['name']}\" {\n";
241
          $conf .= "  listen on {$vs_a[$i]['ipaddr']} port {$vs_a[$i]['port']}\n";
242
          $conf .= "  forward to <{$vs_a[$i]['pool']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";
243
244 43b01df4 Bill Marquette
          # sitedown MUST use the same port as the primary pool - sucks, but it's a relayd thing
245 9c426e33 Scott Ullrich
          if (isset($vs_a[$i]['sitedown']) && strlen($vs_a[$i]['sitedown']) > 0)
246 aeb90d90 Bill Marquette
            $conf .= "  forward to <{$vs_a[$i]['sitedown']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";
247 9c426e33 Scott Ullrich
248 0919224f Bill Marquette
          $conf .= "}\n";
249
          break;
250
        }
251
        case 'relay': {
252
          $conf .= "relay \"{$vs_a[$i]['name']}\" {\n";
253
          $conf .= "  listen on {$vs_a[$i]['ipaddr']} port {$vs_a[$i]['port']}\n";
254
          $conf .= "  protocol \"{$vs_a[$i]['relay_protocol']}\"\n";
255
          $conf .= "  forward to <{$vs_a[$i]['pool']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";
256
257 9c426e33 Scott Ullrich
          if (isset($vs_a[$i]['sitedown']) &&  strlen($vs_a[$i]['sitedown']) > 0)
258 0919224f Bill Marquette
            $conf .= "  forward to <{$vs_a[$i]['sitedown']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";
259
          $conf .= "}\n";
260
          break;
261
        }
262
      }
263 17623ab5 Bill Marquette
    }
264
  }
265
  fwrite($fd, $conf);
266
  fclose($fd);
267
268 f8cb57db Chris Buechler
  if (is_process_running('relayd')) {
269 b1bd2119 Chris Buechler
	if (! empty($vs_a)) {
270
		// it's running and there is a config, just reload
271 780e7dc5 Erik Fonnesbeck
		mwexec("/usr/local/sbin/relayctl reload");
272 b1bd2119 Chris Buechler
	} else {
273
		/*
274
		* XXX: Something breaks our control connection with relayd
275
		* and makes 'relayctl stop' not work
276
		* rule reloads are the current suspect
277 780e7dc5 Erik Fonnesbeck
		* mwexec('/usr/local/sbin/relayctl stop');
278 b1bd2119 Chris Buechler
		*  returns "command failed"
279
		*/
280
		mwexec('pkill relayd');
281
	}
282
  } else {
283
	if (! empty($vs_a)) {
284
		// not running and there is a config, start it
285
		mwexec("/usr/local/sbin/relayd -f {$g['varetc_path']}/relayd.conf");
286
	}	
287 3eb583e6 Seth Mos
  }
288 b1bd2119 Chris Buechler
289 3ad6d3bb Bill Marquette
}
290
291 a776c720 jim-p
function get_lb_redirects() {
292
/*
293
# relayctl show summary
294
Id   Type      Name                      Avlblty Status
295
1    redirect  testvs2                           active
296
5    table     test2:80                          active (3 hosts up)
297
11   host      192.168.1.2               91.55%  up
298
10   host      192.168.1.3               100.00% up
299
9    host      192.168.1.4               88.73%  up
300
3    table     test:80                           active (1 hosts up)
301
7    host      192.168.1.2               66.20%  down
302
6    host      192.168.1.3               97.18%  up
303
0    redirect  testvs                            active
304
3    table     test:80                           active (1 hosts up)
305
7    host      192.168.1.2               66.20%  down
306
6    host      192.168.1.3               97.18%  up
307
4    table     testvs-sitedown:80                active (1 hosts up)
308
8    host      192.168.1.4               84.51%  up
309
# relayctl show redirects
310
Id   Type      Name                      Avlblty Status
311
1    redirect  testvs2                           active
312
0    redirect  testvs                            active
313
# relayctl show redirects
314
Id   Type      Name                      Avlblty Status
315
1    redirect  testvs2                           active
316
           total: 2 sessions
317
           last: 2/60s 2/h 2/d sessions
318
           average: 1/60s 0/h 0/d sessions
319
0    redirect  testvs                            active
320
*/
321
	$rdr_a = array();
322
	exec('/usr/local/sbin/relayctl show redirects 2>&1', $rdr_a);
323
	$vs = array();
324
	for ($i = 0; isset($rdr_a[$i]); $i++) {
325
		$line = $rdr_a[$i];
326
		if (preg_match("/^[0-9]+/", $line)) {
327
			$regs = array();
328
			if($x = preg_match("/^[0-9]+\s+redirect\s+([^\s]+)\s+([^\s]+)/", $line, $regs)) {
329
				$vs[trim($regs[1])] = array();
330
				$vs[trim($regs[1])]['status'] = trim($regs[2]);
331
			}
332
		}
333
	}
334
	return $vs;
335
}
336
337
function get_lb_summary() {
338
	$relayctl = array();
339
	exec('/usr/local/sbin/relayctl show summary 2>&1', $relayctl);
340
	$relay_hosts=Array();
341
	foreach( (array) $relayctl as $line) {
342
		$t=split("\t", $line);
343
		switch (trim($t[1])) {
344
			case "table":
345
				$curpool=trim($t[2]);
346
			break;
347
			case "host":
348
				$curhost=trim($t[2]);
349
				$relay_hosts[$curpool][$curhost]['avail']=trim($t[3]);
350
				$relay_hosts[$curpool][$curhost]['state']=trim($t[4]);
351
			break;
352
		}
353
	}
354
	return $relay_hosts;
355
}
356
357 087a89f8 Chris Buechler
?>