load_balancer_monitor_edit.php 16.4 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
        Copyright (C) 2008 Bill Marquette <bill.marquette@gmail.com>.
        All rights reserved.

        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:

        1. Redistributions of source code must retain the above copyright notice,
           this list of conditions and the following disclaimer.

        2. Redistributions in binary form must reproduce the above copyright
           notice, this list of conditions and the following disclaimer in the
           documentation and/or other materials provided with the distribution.

        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        POSSIBILITY OF SUCH DAMAGE.
*/

30
require_once("guiconfig.inc");
31
require_once("services.inc");
32
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
$rfc2616 = array(
	100 => "100 Continue",
	101 => "101 Switching Protocols",
	200 => "200 OK",
	201 => "201 Created",
	202 => "202 Accepted",
	203 => "203 Non-Authoritative Information",
	204 => "204 No Content",
	205 => "205 Reset Content",
	206 => "206 Partial Content",
	300 => "300 Multiple Choices",
	301 => "301 Moved Permanently",
	302 => "302 Found",
	303 => "303 See Other",
	304 => "304 Not Modified",
	305 => "305 Use Proxy",
	306 => "306 (Unused)",
	307 => "307 Temporary Redirect",
	400 => "400 Bad Request",
	401 => "401 Unauthorized",
	402 => "402 Payment Required",
	403 => "403 Forbidden",
	404 => "404 Not Found",
	405 => "405 Method Not Allowed",
	406 => "406 Not Acceptable",
	407 => "407 Proxy Authentication Required",
	408 => "408 Request Timeout",
	409 => "409 Conflict",
	410 => "410 Gone",
	411 => "411 Length Required",
	412 => "412 Precondition Failed",
	413 => "413 Request Entity Too Large",
	414 => "414 Request-URI Too Long",
	415 => "415 Unsupported Media Type",
	416 => "416 Requested Range Not Satisfiable",
	417 => "417 Expectation Failed",
	500 => "500 Internal Server Error",
	501 => "501 Not Implemented",
	502 => "502 Bad Gateway",
	503 => "503 Service Unavailable",
	504 => "504 Gateway Timeout",
	505 => "505 HTTP Version Not Supported"
);

function is_rfc2616_code($code) {
	global $rfc2616;
	if (isset($rfc2616[$code]))
		return true;
	else
		return false;
}

function print_rfc2616_select($tag, $current){
	global $rfc2616;

	/* Default to 200 OK if not set */
	if ($current == "")
		$current = 200;

	echo "<select id=\"{$tag}\" name=\"{$tag}\">\n";
	foreach($rfc2616 as $code => $message) {
		if ($code == $current) {
			$sel = " selected=\"selected\"";
		} else {
			$sel = "";
		}
		echo "<option value=\"{$code}\"{$sel}>{$message}</option>\n";
	}
	echo "</select>\n";
}


Ad Schellevis's avatar
Ad Schellevis committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/load_balancer_monitor.php');

if (!is_array($config['load_balancer']['monitor_type'])) {
	$config['load_balancer']['monitor_type'] = array();
}
$a_monitor = &$config['load_balancer']['monitor_type'];

if (is_numericint($_GET['id']))
	$id = $_GET['id'];
if (isset($_POST['id']) && is_numericint($_POST['id']))
	$id = $_POST['id'];

if (isset($id) && $a_monitor[$id]) {
	$pconfig['name'] = $a_monitor[$id]['name'];
	$pconfig['type'] = $a_monitor[$id]['type'];
	$pconfig['descr'] = $a_monitor[$id]['descr'];
	$pconfig['options'] = array();
	$pconfig['options'] = $a_monitor[$id]['options'];
} else {
	/* Some sane page defaults */
	$pconfig['options']['path'] = '/';
	$pconfig['options']['code'] = 200;
}

if ($_POST) {
	unset($input_errors);
	$pconfig = $_POST;

	/* turn $_POST['http_options_*'] into $pconfig['options'][*] */
	foreach($_POST as $key => $val) {
		if (stristr($key, 'options') !== false) {
			if (stristr($key, $pconfig['type'].'_') !== false) {
				$opt = explode('_',$key);
				$pconfig['options'][$opt[2]] = $val;
			}
			unset($pconfig[$key]);
		}
	}

	/* input validation */
	$reqdfields = explode(" ", "name type descr");
	$reqdfieldsn = array(gettext("Name"),gettext("Type"),gettext("Description"));

	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);

	/* Ensure that our monitor names are unique */
	for ($i=0; isset($config['load_balancer']['monitor_type'][$i]); $i++)
		if (($_POST['name'] == $config['load_balancer']['monitor_type'][$i]['name']) && ($i != $id))
			$input_errors[] = gettext("This monitor name has already been used.  Monitor names must be unique.");

	if (strpos($_POST['name'], " ") !== false)
		$input_errors[] = gettext("You cannot use spaces in the 'name' field.");

	switch($_POST['type']) {
		case 'icmp': {
			break;
		}
		case 'tcp': {
			break;
		}
		case 'http':
		case 'https': {
			if (is_array($pconfig['options'])) {
				if (isset($pconfig['options']['host']) && $pconfig['options']['host'] != "") {
					if (!is_hostname($pconfig['options']['host'])) {
						$input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'.");
					}
				}
				if (isset($pconfig['options']['code']) && $pconfig['options']['code'] != "") {
					// Check code
					if(!is_rfc2616_code($pconfig['options']['code'])) {
						$input_errors[] = gettext("HTTP(s) codes must be from RFC2616.");
					}
				}
				if (!isset($pconfig['options']['path']) || $pconfig['options']['path'] == "") {
					$input_errors[] = gettext("The path to monitor must be set.");
				}
			}
			break;
		}
		case 'send': {
			if (is_array($pconfig['options'])) {
				if (isset($pconfig['options']['send']) && $pconfig['options']['send'] != "") {
					// Check send
				}
				if (isset($pconfig['options']['expect']) && $pconfig['options']['expect'] != "") {
					// Check expect
				}
			}
			break;
		}
	}

	if (!$input_errors) {
		$monent = array();
201 202

		if (isset($id) && $a_monitor[$id]) {
Ad Schellevis's avatar
Ad Schellevis committed
203
			$monent = $a_monitor[$id];
204
		}
205

206 207 208
		$monent['name'] = $pconfig['name'];
		$monent['type'] = $pconfig['type'];
		$monent['descr'] = $pconfig['descr'];
Ad Schellevis's avatar
Ad Schellevis committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		if($pconfig['type'] == "http" || $pconfig['type'] == "https" ) {
			/* log updates, then clear array and reassign - dumb, but easiest way to have a clear array */
			$monent['options'] = array();
			$monent['options']['path'] = $pconfig['options']['path'];
			$monent['options']['host'] = $pconfig['options']['host'];
			$monent['options']['code'] = $pconfig['options']['code'];
		}
		if($pconfig['type'] == "send" ) {
			/* log updates, then clear array and reassign - dumb, but easiest way to have a clear array */
			$monent['options'] = array();
			$monent['options']['send'] = $pconfig['options']['send'];
			$monent['options']['expect'] = $pconfig['options']['expect'];
		}
		if($pconfig['type'] == "tcp" || $pconfig['type'] == "icmp") {
			$monent['options'] = array();
		}

		if (isset($id) && $a_monitor[$id]) {
			/* modify all pools with this name */
			for ($i = 0; isset($config['load_balancer']['lbpool'][$i]); $i++) {
				if ($config['load_balancer']['lbpool'][$i]['monitor'] == $a_monitor[$id]['name'])
					$config['load_balancer']['lbpool'][$i]['monitor'] = $monent['name'];
			}
			$a_monitor[$id] = $monent;
233
		} else {
Ad Schellevis's avatar
Ad Schellevis committed
234 235 236
			$a_monitor[] = $monent;
		}

237
		mark_subsystem_dirty('loadbalancer');
238
		write_config();
Ad Schellevis's avatar
Ad Schellevis committed
239 240 241 242 243
		header("Location: load_balancer_monitor.php");
		exit;
	}
}

244
$service_hook = 'relayd';
Ad Schellevis's avatar
Ad Schellevis committed
245 246

include("head.inc");
247

Ad Schellevis's avatar
Ad Schellevis committed
248 249 250 251
$types = array("icmp" => gettext("ICMP"), "tcp" => gettext("TCP"), "http" => gettext("HTTP"), "https" => gettext("HTTPS"), "send" => gettext("Send/Expect"));

?>

Ad Schellevis's avatar
Ad Schellevis committed
252 253

<body>
Ad Schellevis's avatar
Ad Schellevis committed
254
<?php include("fbegin.inc"); ?>
255

Ad Schellevis's avatar
Ad Schellevis committed
256 257 258 259 260 261 262 263 264 265 266 267 268
	<script type="text/javascript">
	//<![CDATA[
	function updateType(t){
		switch(t) {
	<?php
		/* OK, so this is sick using php to generate javascript, but it needed to be done */
		foreach ($types as $key => $val) {
			echo "		case \"{$key}\": {\n";
			$t = $types;
			foreach ($t as $k => $v) {
				if ($k != $key) {
					echo "			jQuery('#{$k}').hide();\n";
				}
Ad Schellevis's avatar
Ad Schellevis committed
269
			}
Ad Schellevis's avatar
Ad Schellevis committed
270
			echo "		}\n";
Ad Schellevis's avatar
Ad Schellevis committed
271
		}
Ad Schellevis's avatar
Ad Schellevis committed
272 273 274
	?>
		}
		jQuery('#' + t).show();
Ad Schellevis's avatar
Ad Schellevis committed
275
	}
Ad Schellevis's avatar
Ad Schellevis committed
276 277
	//]]>
	</script>
Ad Schellevis's avatar
Ad Schellevis committed
278

Ad Schellevis's avatar
Ad Schellevis committed
279
	<section class="page-content-main">
Ad Schellevis's avatar
Ad Schellevis committed
280

Ad Schellevis's avatar
Ad Schellevis committed
281
		<div class="container-fluid">
282 283 284

			<div class="row">

285
				<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
286

Ad Schellevis's avatar
Ad Schellevis committed
287
			    <section class="col-xs-12">
288 289 290 291 292 293 294 295

				<div class="content-box">

                        <form action="load_balancer_monitor_edit.php" method="post" name="iform" id="iform">

				<div class="table-responsive">
					<table class="table table-striped table-sort">
						<tr>
Ad Schellevis's avatar
Ad Schellevis committed
296 297 298 299 300
										<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit Load Balancer - Monitor entry"); ?></td>
							                </tr>
									<tr align="left">
										<td width="22%" valign="top" class="vncellreq"><?=gettext("Name"); ?></td>
										<td width="78%" class="vtable" colspan="2">
301
											<input name="name" type="text" <?php if(isset($pconfig['name'])) echo "value=\"" . htmlspecialchars($pconfig['name']) . "\"";?> size="16" maxlength="16" />
Ad Schellevis's avatar
Ad Schellevis committed
302 303 304 305 306
										</td>
									</tr>
									<tr align="left">
										<td width="22%" valign="top" class="vncellreq"><?=gettext("Description"); ?></td>
										<td width="78%" class="vtable" colspan="2">
307
											<input name="descr" type="text" <?php if(isset($pconfig['descr'])) echo "value=\"" . htmlspecialchars($pconfig['descr']) . "\"";?> size="64" />
Ad Schellevis's avatar
Ad Schellevis committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
										</td>
									</tr>
									<tr align="left">
										<td width="22%" valign="top" class="vncellreq"><?=gettext("Type"); ?></td>
										<td width="78%" class="vtable" colspan="2">
											<select id="type" name="type">
							<?
												foreach ($types as $key => $val) {
													if(isset($pconfig['type']) && $pconfig['type'] == $key) {
														$selected = " selected=\"selected\"";
													} else {
														$selected = "";
													}
													echo "<option value=\"{$key}\" onclick=\"updateType('{$key}');\"{$selected}>{$val}</option>\n";
												}
							?>
											</select>
										</td>
									</tr>
									<tr align="left" id="icmp"<?= $pconfig['type'] == "icmp" ? "" : " style=\"display:none;\""?>><td></td>
									</tr>
									<tr align="left" id="tcp"<?= $pconfig['type'] == "tcp" ? "" : " style=\"display:none;\""?>><td></td>
									</tr>
									<tr align="left" id="http"<?= $pconfig['type'] == "http" ? "" : " style=\"display:none;\""?>>
										<td width="22%" valign="top" class="vncellreq"><?=gettext("HTTP"); ?></td>
										<td width="78%" class="vtable" colspan="2">
											<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="http">
												<tr align="left">
													<td valign="top" align="right" class="vtable"><?=gettext("Path"); ?></td>
													<td class="vtable" colspan="2">
338
														<input name="http_options_path" type="text" <?php if(isset($pconfig['options']['path'])) echo "value=\"" . htmlspecialchars($pconfig['options']['path']) . "\"";?> size="64" />
Ad Schellevis's avatar
Ad Schellevis committed
339 340 341 342 343
													</td>
												</tr>
												<tr align="left">
													<td valign="top"  align="right" class="vtable"><?=gettext("Host"); ?></td>
													<td class="vtable" colspan="2">
344
														<input name="http_options_host" type="text" <?php if(isset($pconfig['options']['host'])) echo "value=\"" . htmlspecialchars($pconfig['options']['host']) . "\"";?> size="64" /><br /><?=gettext("Hostname for Host: header if needed."); ?>
Ad Schellevis's avatar
Ad Schellevis committed
345 346 347 348 349
													</td>
												</tr>
												<tr align="left">
													<td valign="top"  align="right" class="vtable"><?=gettext("HTTP Code"); ?></td>
													<td class="vtable" colspan="2">
350
														<?= print_rfc2616_select("http_options_code", isset($pconfig['options']['code'])?$pconfig['options']['code']:""); ?>
Ad Schellevis's avatar
Ad Schellevis committed
351 352 353 354 355 356
													</td>
												</tr>
							<!-- BILLM: XXX not supported digest checking just yet
												<tr align="left">
													<td width="22%" valign="top" class="vncell">MD5 Page Digest</td>
													<td width="78%" class="vtable" colspan="2">
357
														<input name="digest" type="text" <?php if(isset($pconfig['digest'])) echo "value=\"" . htmlspecialchars($pconfig['digest']) . "\"";?>size="32"><br /><b>TODO: add fetch functionality here</b>
Ad Schellevis's avatar
Ad Schellevis committed
358 359 360 361 362 363 364 365 366 367 368 369 370
													</td>
												</tr>
							-->
											</table>
										</td>
									</tr>
									<tr align="left" id="https"<?= $pconfig['type'] == "https" ? "" : " style=\"display:none;\""?>>
										<td width="22%" valign="top" class="vncellreq"><?=gettext("HTTPS"); ?></td>
										<td width="78%" class="vtable" colspan="2">
											<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="https">
												<tr align="left">
													<td valign="top"  align="right" class="vtable"><?=gettext("Path"); ?></td>
													<td class="vtable" colspan="2">
371
														<input name="https_options_path" type="text" <?php if(isset($pconfig['options']['path'])) echo "value=\"" . htmlspecialchars($pconfig['options']['path']) ."\"";?> size="64" />
Ad Schellevis's avatar
Ad Schellevis committed
372 373 374 375 376
													</td>
												</tr>
												<tr align="left">
													<td valign="top"  align="right" class="vtable"><?=gettext("Host"); ?></td>
													<td class="vtable" colspan="2">
377
														<input name="https_options_host" type="text" <?php if(isset($pconfig['options']['host'])) echo "value=\"" . htmlspecialchars($pconfig['options']['host']) . "\"";?> size="64" /><br /><?=gettext("Hostname for Host: header if needed."); ?>
Ad Schellevis's avatar
Ad Schellevis committed
378 379 380 381 382
													</td>
												</tr>
												<tr align="left">
													<td valign="top"  align="right" class="vtable"><?=gettext("HTTP Code"); ?></td>
													<td class="vtable" colspan="2">
383
														<?= print_rfc2616_select("https_options_code", isset($pconfig['options']['code'])?$pconfig['options']['code']:""); ?>
Ad Schellevis's avatar
Ad Schellevis committed
384 385 386
													</td>
												</tr>
							<!-- BILLM: XXX not supported digest checking just yet
387

Ad Schellevis's avatar
Ad Schellevis committed
388 389 390
												<tr align="left">
													<td width="22%" valign="top" class="vncellreq">MD5 Page Digest</td>
													<td width="78%" class="vtable" colspan="2">
391
														<input name="digest" type="text" <?php if(isset($pconfig['digest'])) echo "value=\"" . htmlspecialchars($pconfig['digest']) . "\"";?>size="32"><br /><b>TODO: add fetch functionality here</b>
Ad Schellevis's avatar
Ad Schellevis committed
392 393 394 395 396 397 398 399 400 401 402 403 404
													</td>
												</tr>
							-->
											</table>
										</td>
									</tr>
									<tr align="left" id="send"<?= $pconfig['type'] == "send" ? "" : " style=\"display:none;\""?>>
										<td width="22%" valign="top" class="vncellreq"><?=gettext("Send/Expect"); ?></td>
										<td width="78%" class="vtable" colspan="2">
											<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="send expect">
												<tr align="left">
													<td valign="top"  align="right" class="vtable"><?=gettext("Send string"); ?></td>
													<td class="vtable" colspan="2">
405
														<input name="send_options_send" type="text" <?php if(isset($pconfig['options']['send'])) echo "value=\"" . htmlspecialchars($pconfig['options']['send']) . "\"";?> size="64" />
Ad Schellevis's avatar
Ad Schellevis committed
406 407 408 409 410
													</td>
												</tr>
												<tr align="left">
													<td valign="top" align="right"  class="vtable"><?=gettext("Expect string"); ?></td>
													<td class="vtable" colspan="2">
411
														<input name="send_options_expect" type="text" <?php if(isset($pconfig['options']['expect'])) echo "value=\"" . htmlspecialchars($pconfig['options']['expect']) . "\"";?> size="64" />
Ad Schellevis's avatar
Ad Schellevis committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr align="left">
										<td width="22%" valign="top">&nbsp;</td>
										<td width="78%">
											<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
							<input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
											<?php if (isset($id) && $a_monitor[$id]): ?>
											<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
											<?php endif; ?>
										</td>
									</tr>
								</table>
428
				</div>
Ad Schellevis's avatar
Ad Schellevis committed
429
                        </form>
430
				</div>
Ad Schellevis's avatar
Ad Schellevis committed
431 432 433 434
			    </section>
			</div>
		</div>
	</section>
435 436

<?php include("foot.inc"); ?>