TOP ▲ itcore TOPTIPSmonit_gen.php  タグ:監視 monit 生成

監視ツール monitの設定データ生成 | itcore 2019年

設定データ生成

ホスト名
IP
監視先 ホスト名 IP
タイムアウト
監視項目 icmp(ping)
53(dns-udp)
80(www)
443(https)
port(tcp)


設定データ生成

check host 61.213.223.40-www.itcore.jp with address www.itcore.jp
  if failed icmp type echo with timeout 30 seconds then alert

ソース

<?php
$title = "監視ツール monitの設定データ生成 | itcore 2019年";
include_once "header.inc"; // ヘッダー
tag("監視 monit 生成");
?>
<h2> <?php echo $title; ?></h2>
<h3>設定データ生成</h3>
<?php
$self = uSelf();
$host = uForm2("host", "www.itcore.jp");
$ip = uForm2("ip", "61.213.223.40");
$target = uForm2("target", "host");
$checked_host = uChecked($target, "host");
$checked_ip = uChecked($target, "ip");
$timeout = uForm2("timeout", "30");
$timeout_disp = uHtmlentities($timeout);
$sp = "&nbsp;&nbsp;";

// 監視項目
$a_item = array("icmp(ping)", "53(dns-udp)", "80(www)", "443(https)", "port(tcp)");
$html_checkbox = "";
$html_text = "";
$cmd = "";
foreach ($a_item as $item) {
  $value = uForm($item);
  $value_disp = uHtmlentities($value);
  if ("" == uForm("sub_gen")) {
    if ($item == $a_item[0]) $value = "1"; // icmp 初期オン
  }
  if ("port(tcp)" != $item) { // チェックボックスの項目
    $checked = ""; if ("" != $value) $checked = "checked";
    $html_checkbox .= "<input type=checkbox $checked name=$item value=1>$item<br>\n";
  } elseif ($item == $a_item[4]) { // port(tcp)
    $html_text .= "$item <input type=text name=$item value='$value_disp' size=5><br>\n";
  }
  if ("" != $value) {
    if ($item == $a_item[0]) { // icmp
      $cmd .= $sp."if failed icmp type echo with timeout $timeout_disp seconds then alert<br>\n";
    } elseif ($item == $a_item[1]) { // dns
      $cmd .= $sp."if failed port 53 type udp proto dns with timeout $timeout_disp seconds then alert<br>\n";
    } elseif ($item == $a_item[2]) { // www
      $cmd .= $sp."if failed port 80 proto http with timeout $timeout_disp seconds then alert<br>\n";
    } elseif ($item == $a_item[3]) { // https
      $cmd .= $sp."if failed port 443 type TCPSSL proto http with timeout $timeout_disp seconds then alert<br>\n";
    } elseif ($item == $a_item[4]) { // port(tcp)
      $cmd .= $sp."if failed port $value_disp type tcp with timeout $timeout_disp seconds then alert<br>\n";
    }
  }
}

// HTML出力
$host_disp = uHtmlentities($host);
$ip_disp = uHtmlentities($ip);
echo <<<EOT
<form action=$self method=post>
<table border=1>
<tr><th>ホスト名<td><input type=text name=host value="$host_disp" size=20>
<tr><th>IP<td><input type=text name=ip value="$ip_disp" size=20>
<tr><th>監視先<td>
  <input type=radio name=target $checked_host value="host_disp" size=20>ホスト名
  <input type=radio name=target $checked_ip value="ip" size=20>IP
<tr><th>タイムアウト<td><input type=text name=timeout value="$timeout_disp" size=5>秒
<tr><th>監視項目<td>
  $html_checkbox
  $html_text
</table>
<br>
<input type=submit value="生成" name=sub_gen><br>
</form>
EOT;

// 生成データ出力
$address = $host_disp;
if ("ip" == $target) $address = $ip_disp;
echo "<h4>設定データ生成</h4>\n";
echo "check host $ip_disp-$host_disp with address $address<br>\n";
echo $cmd;

?>

<h3>ソース</h3>
<?php echo nl2br(uHtmlspecialchars2(file_get_contents(basename("$self")))); ?>

<br>
<hr>

<?php
//--------------------
// 共通関数
//--------------------
function uSelf() {
    return $_SERVER['PHP_SELF'];
}
function uForm($var) {
  if (isset($_POST[$var])) return $_POST[$var];
  if (isset($_GET[$var])) return $_GET[$var];
  return "";
}
function uForm2($var, $default) {
  $value = "";
  if (isset($_POST[$var])) $value = $_POST[$var];
  if (isset($_GET[$var])) $value = $_GET[$var];
  if ("" == $value) $value = $default;
  return $value;
}
// 変数が値に等しければチェックボックスのcheckedを返す。
function uChecked($rdo, $value) {
  if ($rdo == $value) return "checked";
  return "";
}
// uHtmlspecialchars2 空白を&nbsp;に変換する。
function uHtmlspecialchars2($text) {
  $text = uHtmlentities($text);
  $text = str_replace("\t", "    ", $text);
  $text = str_replace(" ", "&nbsp", $text);
  return $text;
}

// クオートもエンコードする。
function uHtmlentities($data) {
  return htmlentities($data, ENT_QUOTES);
}
?>