Commit 0d920a6e authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) refactor vslb.inc

(cherry picked from commit 6b151510)
parent fc36c539
......@@ -40,138 +40,53 @@ function subnetv4_expand($subnet) {
}
/* include all configuration functions */
class Monitor
{
private $conf = array();
function __construct($config) {
$this->conf = $config;
}
public function p() {
return "check {$this->get('proto')}";
}
private function get($var) {
return isset($this->$var) ? $this->$var : "";
}
protected function config($element) {
return isset($this->conf[$element]) ? $this->conf[$element] : "";
}
}
class TCPMonitor extends Monitor
{
protected $proto = 'tcp';
}
class SSLMonitor extends Monitor
{
protected $proto = 'ssl';
}
class ICMPMonitor extends Monitor
{
protected $proto = 'icmp';
}
class HTTPMonitor extends Monitor
{
protected $proto = 'http';
function __construct($config) {
parent::__construct($config);
}
public function p() {
$method = ($this->code() != "") ? $this->code() : $this->digest();
return "check {$this->proto} {$this->path()} {$this->host()} {$method}";
}
private function path() {
return $this->config('path') != "" ? "'{$this->config('path')}'" : "";
}
private function host() {
return $this->config('host') != "" ? "host {$this->config('host')}" : "";
}
private function code() {
return $this->config('code') != "" ? "code {$this->config('code')}" : "";
}
private function digest() {
return $this->config('digest') != "" ? "digest {$this->config('digest')}" : "";
}
}
class HTTPSMonitor extends HTTPMonitor
{
protected $proto = 'https';
}
class SendMonitor extends Monitor {
private $proto = 'send';
function __construct($config) {
parent::__construct($config);
}
public function p() {
return "check {$this->proto} {$this->data()} expect {$this->pattern()} {$this->ssl()}";
}
private function data() {
return $this->config('send') != "" ? "\"{$this->config('send')}\"" : "\"\"";
}
private function pattern() {
return $this->config('expect') != "" ? "\"{$this->config('expect')}\"" : "\"\"";
}
private function ssl() {
return $this->config('ssl') == true ? "ssl" : "";
}
}
function relayd_configure($kill_first = false)
{
global $config;
if (isset($config['load_balancer']['virtual_server'])) {
if (isset($config['load_balancer']['virtual_server']) && is_array($config['load_balancer']['virtual_server'])) {
$vs_a = $config['load_balancer']['virtual_server'];
} else {
$vs_a = null;
$vs_a = array();
}
if (isset($config['load_balancer']['lbpool'])) {
if (isset($config['load_balancer']['lbpool']) && is_array($config['load_balancer']['lbpool'])) {
$pool_a = $config['load_balancer']['lbpool'];
} else {
$pool_a = null;
$pool_a = array();
}
if (isset($config['load_balancer']['setting'])) {
if (isset($config['load_balancer']['setting']) && is_array($config['load_balancer']['setting'])) {
$setting = $config['load_balancer']['setting'];
} else {
$setting = null;
$setting = array();
}
$check_a = array();
foreach ((array)$config['load_balancer']['monitor_type'] as $type) {
$type['options'] = isset($type['options']) ? $type['options'] : array();
switch($type['type']) {
case 'icmp':
$mon = new ICMPMonitor($type['options']);
break;
case 'tcp':
$mon = new TCPMonitor($type['options']);
$check_a[$type['name']] = 'check ' . $type['type'];
break;
case 'http':
$mon = new HTTPMonitor($type['options']);
break;
case 'https':
$mon = new HTTPSMonitor($type['options']);
$check_a[$type['name']] = 'check ' . $type['type']. " ";
if (!empty($type['options']['path'])) {
$check_a[$type['name']] .= "'".$type['options']['path'] . "' ";
}
if (!empty($type['options']['host'])) {
$check_a[$type['name']] .= "host ".$type['options']['host'] . " ";
}
$check_a[$type['name']] .= "code " . $type['options']['code'];
break;
case 'send':
$mon = new SendMonitor($type['options']);
$check_a[$type['name']] = "send ";
$check_a[$type['name']] .= !empty($type['options']['send']) ? "\"{$type['options']['send']}\"" : "\"\"" ;
$check_a[$type['name']] .= " expect ";
$check_a[$type['name']] .= !empty($type['options']['expect']) ? "\"{$type['options']['expect']}\"" : "\"\"" ;
break;
}
if ($mon) {
$check_a[$type['name']] = $mon->p();
}
}
$fd = fopen('/var/etc/relayd.conf', 'w');
......@@ -199,7 +114,6 @@ function relayd_configure($kill_first = false)
/* reindex pools by name as we loop through the pools array */
$pools = array();
/* Virtual server pools */
if (is_array($pool_a)) {
for ($i = 0; isset($pool_a[$i]); $i++) {
if (is_array($pool_a[$i]['servers'])) {
if (!empty($pool_a[$i]['retry'])) {
......@@ -222,13 +136,18 @@ function relayd_configure($kill_first = false)
$pools[$pool_a[$i]['name']] = $pool_a[$i];
}
}
}
$conf .= "dns protocol \"dnsproto\" {\n";
$conf .= "\t" . "tcp { nodelay, sack, socket buffer 1024, backlog 1000 }\n";
$conf .= "}\n";
// collect used protocols
$used_protocols = array();
foreach ($vs_a as $vs) {
if (isset($vs['relay_protocol']) && !in_array($vs['relay_protocol'], $used_protocols)) {
$used_protocols[] = $vs['relay_protocol'];
if (is_file('/usr/local/etc/inc/vslb/'.basename($vs['relay_protocol']).'.proto')) {
$conf .= file_get_contents('/usr/local/etc/inc/vslb/'.basename($vs['relay_protocol']).'.proto')."\n";
}
}
}
if (is_array($vs_a)) {
for ($i = 0; isset($vs_a[$i]); $i++) {
$append_port_to_name = false;
if (is_alias($pools[$vs_a[$i]['poolname']]['port'])) {
......@@ -279,17 +198,13 @@ function relayd_configure($kill_first = false)
$name .= "_" . $src_port;
}
if (($vs_a[$i]['mode'] == 'relay') || ($vs_a[$i]['relay_protocol'] == 'dns')) {
if ($vs_a[$i]['mode'] == 'relay') {
// relay mode
$conf .= "relay \"{$name}\" {\n";
$conf .= " listen on {$ip} port {$src_port}\n";
if ($vs_a[$i]['relay_protocol'] == "dns") {
$conf .= " protocol \"dnsproto\"\n";
} else {
$conf .= " listen on {$ip} port {$src_port} \n";
$conf .= " protocol \"{$vs_a[$i]['relay_protocol']}\"\n";
}
$lbmode = "";
if ( $pools[$vs_a[$i]['poolname']]['mode'] == "loadbalance" ) {
if ($pools[$vs_a[$i]['poolname']]['mode'] == "loadbalance") {
$lbmode = "mode loadbalance";
}
......@@ -300,6 +215,7 @@ function relayd_configure($kill_first = false)
}
$conf .= "}\n";
} else {
// redirect mode
$conf .= "redirect \"{$name}\" {\n";
$conf .= " listen on {$ip} port {$src_port}\n";
$conf .= " forward to <{$vs_a[$i]['poolname']}> port {$dest_port} {$check_a[$pools[$vs_a[$i]['poolname']]['monitor']]} \n";
......@@ -318,7 +234,6 @@ function relayd_configure($kill_first = false)
}
}
}
}
fwrite($fd, $conf);
fclose($fd);
......
dns protocol "dns" {
tcp { nodelay, sack, socket buffer 1024, backlog 1000 }
}
protocol "tcp" {
tcp { nodelay, socket buffer 65536 }
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment