📄 setup_snmp.php
字号:
<?php/******************************************************************** * Copyright (c) 2006, Graham P Phillips * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "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 * COPYRIGHT OWNER OR CONTRIBUTORS 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. ********************************************************************/include_once "php_params.php";include_once "layout.php";define("DB_HOST", '127.0.0.1'); /* 127.0.0.1 is better than localhost */define("DB_TBL2FILE_PORT", 8885);define("DB_LOGMUXD_PORT", 8887);define("DB_USER", "");define("MENU1", "Setup");define("MENU2", "SNMP"); define("SNMPv1Trap", 1);define("SNMPv2Trap", 2);define("SNMPv2Inform", 3);define("MAX_PORT", 65535);define("ACTION_UNKNOWN", -1); define("ACTION_UPDATE_BASIC", 1); define("ACTION_UPDATE_NOTIFICATIONS", 2); define("ACTION_TEST_TRAP", 3); class BasicSNMPConfig { var $rocommunity; var $rwcommunity; var $syslocation; var $syscontact; var $agentport; function BasicSNMPConfig($rocommunity, $rwcommunity, $syscontact, $syslocation, $agentport) { $this->rocommunity = $rocommunity; $this->rwcommunity = $rwcommunity; $this->syslocation = $syslocation; $this->syscontact = $syscontact; $this->agentport = $agentport; }}class Notification { var $id; var $hidden; var $enable; var $targethost; var $targetport; var $targetcommunity; var $traptype; function Notification($id, $enable, $targethost, $targetport, $targetcommunity, $traptype) { $this->id = $id; $this->hidden = false; $this->enable = $enable; $this->targethost = $targethost; $this->targetport = $targetport; $this->targetcommunity = $targetcommunity; $this->traptype = $traptype; }}/* Main Begin */$error = array();$Tbl2fileConn = daemon_connect(DB_HOST, DB_TBL2FILE_PORT, DB_USER, $error);if (is_null($Tbl2fileConn)) { display_error_msg(MENU1, MENU2, $error); return;}$LogmuxdConn = daemon_connect(DB_HOST, DB_LOGMUXD_PORT, DB_USER, $error);if (is_null($LogmuxdConn)) { pg_close($Tbl2fileConn); display_error_msg(MENU1, MENU2, $error); return;}$currBasic = read_basic_from_snmpd_conf($Tbl2fileConn, $error); if (is_null($currBasic)) { pg_close($Tbl2fileConn); display_error_msg(MENU1, MENU2, $error); return;}$currNotifications = read_notifications_from_snmpd_conf($Tbl2fileConn, $error); $action = ACTION_UNKNOWN; $params = read_params();if (array_key_exists('UpdateBasic', $params)) { $newBasic = read_basic_from_ui(); if (!is_null($newBasic)) $action = ACTION_UPDATE_BASIC;} else if (array_key_exists('UpdateNotification', $params)) { $newNotifications = read_notifications_from_ui(); if (!is_null($newNotifications)) $action = ACTION_UPDATE_NOTIFICATIONS;} else if (array_key_exists('TestTrap', $params)) { $newNotifications = read_notifications_from_ui(); if (!is_null($newNotifications)) $action = ACTION_TEST_TRAP;}switch ($action) {case ACTION_UPDATE_BASIC: update_basic($Tbl2fileConn, $currBasic, $newBasic, $currNotifications, $error); break;case ACTION_UPDATE_NOTIFICATIONS: update_notifications($Tbl2fileConn, $LogmuxdConn, $currBasic, $currNotifications, $newNotifications, $error); break;case ACTION_TEST_TRAP: test_traps($Tbl2fileConn, $currBasic, $currNotifications, $newNotifications, $error); break;default: display_snmp_setup($currBasic, $currNotifications, $error); break; }pg_close($Tbl2fileConn);return;/* Main End */function update_basic($tbl2fileConn, $currBasic, $newBasic, $currNotifications, $error) { // User modified the Basic SNMP config if (!validate_alphanum_name($newBasic->rocommunity)) { $error = array(); $error[] = "The Read-Only Community should include only alpha-numeric characters."; display_snmp_setup($currBasic, $currNotifications, $error); return; } if (!validate_alphanum_name($newBasic->rwcommunity)) { $error = array(); $error[] = "The Read-Write Community should include only alpha-numeric characters."; display_snmp_setup($currBasic, $currNotifications, $error); return; } if (!validate_syslocation($newBasic->syslocation)) { $error = array(); $error[] = "The System Location should not include single quotes and other weird characters. However, it may include double quotes and spaces."; display_snmp_setup($currBasic, $currNotifications, $error); return; } if (!validate_syscontact($newBasic->syscontact)) { $error = array(); $error[] = "The System Contact should not include single quotes and other weird characters. However, it may include double quotes and spaces."; display_snmp_setup($currBasic, $currNotifications, $error); return; } if (!validate_port($newBasic->agentport)) { $error = array(); $error[] = "The Listen Port should be a number between 0 and " . MAX_PORT . "."; display_snmp_setup($currBasic, $currNotifications, $error); return; } write_basic_to_snmpd_conf($tbl2fileConn, $newBasic, $currBasic, $error); // ignore write errors, but re-read info $error = array(); $currBasic = read_basic_from_snmpd_conf($tbl2fileConn, $error); if (is_null($currBasic)) { display_error_msg(MENU1, MENU2, $error); return; } display_snmp_setup($currBasic, $currNotifications); return;}function update_notifications($tbl2fileConn, $logmuxdConn, $currBasic, $currNotifications, $newNotifications, $error) { foreach($newNotifications as $notification) { $id = $notification->id; $nth = ''; switch ($notification->id) { case 1: $nth = 'first'; break; case 2: $nth = 'second'; break; case 3: $nth = 'third'; break; } $allowEmpty = ($notification->enable)? false : true; if (!validate_host($notification->targethost, $allowEmpty)) { $error = array(); $error[] = "The $nth Target Host should be a valid host or an IP address."; display_snmp_setup($currBasic, $currNotifications, NULL, $error); return; } if (!validate_port($notification->targetport, $allowEmpty)) { $error = array(); $error[] = "The $nth Port should be a number between 0 and " . MAX_PORT . "."; display_snmp_setup($currBasic, $currNotifications, NULL, $error); return; } if (!validate_alphanum_name($notification->targetcommunity, $allowEmpty)) { $error = array(); $error[] = "The $nth Community Name should include only alpha-numeric characters."; display_snmp_setup($currBasic, $currNotifications, NULL, $error); return; } if (!validate_traptype($notification->traptype)) { $error = array(); $error[] = "Programming error with Type."; display_snmp_setup($currBasic, $currNotifications, NULL, $error); return; } } write_notifications_to_backend($tbl2fileConn, $logmuxdConn, $newNotifications, $currNotifications, $error); display_snmp_setup($currBasic, $newNotifications); return;}function test_traps($tbl2fileConn, $currBasic, $currNotifications, $newNotifications, $error) { /* call tbl2script.sh to send snmp traps to each of the * newNotification entries */ foreach ($newNotifications as $notification) { if (!$notification->enable) continue; $id = $notification->id; $nth = ''; switch ($notification->id) { case 1: $nth = 'first'; break; case 2: $nth = 'second'; break; case 3: $nth = 'third'; break; } $version = $notification->traptype; $host = $notification->targethost; $community = $notification->targetcommunity; $port = $notification->targetport; $command = "UPDATE tbl2file SET script_parms='snmp testtrap $version $host $port $community', do_script=1 where name='snmpd.conf'"; $result = exec_sql($tbl2fileConn, $command, $error, DB_HOST, DB_TBL2FILE_PORT); if (!$result) $error[] = "Unable to send trap for $nth Notification."; else pg_freeresult($result); } display_snmp_setup($currBasic, $currNotifications, NULL, $error); return;}/* Return BasicSNMPConfig object if user pressed Update * button. Return NULL if user did not press Update * button */function read_basic_from_ui() { $params = read_params(); if (array_key_exists("rocommunity", $params)) { $info = new BasicSnmpConfig(trim($params["rocommunity"]), trim($params["rwcommunity"]), trim($params["syscontact"]), trim($params["syslocation"]), trim($params["agentport"])); return $info; } return NULL;}/* Returns an array of (at most 3) Notifications, NULL if error */function read_notifications_from_ui() { $info = array(); $foundUserInput = false; $params = read_params(); for ($i=1; $i<=3; $i++) { if (array_key_exists("targetHost_$i",$params) && array_key_exists("targetPort_$i", $params) && array_key_exists("targetCommunity_$i",$params)) { $foundUserInput = true; $status = (array_key_exists("targetEnabled_$i",$params))? true: false; $traptype = convert_str_to_trap_type($params["trapType_$i"]); $info[$i] = new Notification($i, $status, $params["targetHost_$i"], $params["targetPort_$i"], $params["targetCommunity_$i"], $traptype); } } if (!$foundUserInput) return NULL; return $info;}function display_snmp_setup($snmpBasic, $currNotifications, $snmpBasicMsgs=array(), $notificationMsgs=array()) { $htmlBasic = gen_snmp_basic($snmpBasic, $snmpBasicMsgs); $currNotifications = build_n_notifications_for_ui($currNotifications, 3); $htmlNotifications = gen_snmp_notifications($currNotifications, $notificationMsgs); $widget = "<h2> Setup SNMP </h2>" . $htmlBasic . "<br>" . $htmlNotifications; display_page(MENU1, MENU2, $widget);}function gen_snmp_basic($snmpBasic, $errorMsgs=array()) { $headerHtml = <<< BASIC_HEADER <fieldset> <legend><b>Basic</b></legend> <form action="setup_snmp.php" method="post">BASIC_HEADER; $footerHtml = <<< BASIC_FOOTER <p> <input type="submit" value="Update" name="UpdateBasic"/> </p> </form> </fieldset>BASIC_FOOTER; $tableHtml = "<table>"; $tableHtml .= gen_basic_table_row("Read-Only Community", $snmpBasic->rocommunity, "rocommunity"); $tableHtml .= gen_basic_table_row("Read-Write Community", $snmpBasic->rwcommunity, "rwcommunity"); $tableHtml .= gen_basic_table_row("System Contact", $snmpBasic->syscontact, "syscontact"); $tableHtml .= gen_basic_table_row("System Location", $snmpBasic->syslocation, "syslocation"); $tableHtml .= gen_basic_table_row("Listen Port", $snmpBasic->agentport, "agentport"); $tableHtml .= "</table>"; $errorHtml = ""; if ($errorMsgs && count($errorMsgs) > 0) { $errorHtml .= "<p id=error>"; foreach ($errorMsgs as $msg) { $errorHtml .= htmlentities($msg) . "<br>"; } $errorHtml .= "</p>"; } return $headerHtml . "\n" . $tableHtml . "\n" . $errorHtml . "\n" . $footerHtml; }function gen_basic_table_row($column1, $column2, $name) { $col2 = htmlentities($column2); return "<tr><th align=\"left\">" . $column1 . "</th><td><input type=\"text\" size=\"30\" value=\"" . $col2 . "\" name=\"" . $name . "\"/></td></tr>\n";}function gen_snmp_notifications($currNotifications,$errorMsgs=array()) { $headerHtml = <<< NOTIFICATION_HEADER <fieldset> <legend><b>Notifications</b></legend> <form action="setup_snmp.php" method="post">NOTIFICATION_HEADER; $footerHtml = <<< NOTIFICATION_FOOTER <p> <input type="submit" value="Update" name="UpdateNotification"/> <input type="submit" value="Test Enabled Notifications" name="TestTrap"/> </p> </form> </fieldset>NOTIFICATION_FOOTER; $tableHtml = "<table><tr><th>Enable</th><th>Target Host</th><th>Port</th><th>Community Name</th><th>Type</th></tr>\n"; $row = 1; foreach ($currNotifications as $notify) { $tableHtml .= gen_notification_table_row($notify,$row) . "\n"; $row++; } $tableHtml .= "</table>"; $errorHtml = ""; if ($errorMsgs && count($errorMsgs) > 0) { $errorHtml .= "<p id=errorn>"; foreach ($errorMsgs as $msg) { $errorHtml .= htmlentities($msg) . "<br>"; } $errorHtml .= "</p>"; } return $headerHtml . "\n" . $tableHtml . "\n" . $errorHtml . "\n" . $footerHtml; }function gen_notification_table_row($notify, $row) { $enabledHtml = gen_enabled_html($notify,$row); $trapTypeHtml = gen_trap_type_html($notify,$row); $htmlStr = '<tr>' . $enabledHtml; $htmlStr .= '<td><input type=text size=13 value="' . $notify->targethost . '" name="targetHost_' . $row . '"/></td>'; $htmlStr .= '<td><input type=text size=3 value="' . $notify->targetport . '" name="targetPort_' . $row . '"/></td>'; $htmlStr .= '<td> <input type=text size=20 value="' . $notify->targetcommunity . '" name="targetCommunity_' . $row . '"/></td>'; $htmlStr .= $trapTypeHtml; return $htmlStr;}function gen_enabled_html($notify,$row) { if ($notify->enable) return '<td><input type=checkbox name=targetEnabled_' . $row . ' CHECKED></td>'; else return '<td><input type=checkbox name=targetEnabled_' . $row . '></td>'; }function gen_trap_type_html($notify,$row) { $htmlStr = '<td><select name=trapType_' . $row . '>'; switch($notify->traptype) { default: case SNMPv1Trap: $htmlStr .= '<option selected>SNMPv1 Trap</option><option>SNMPv2 Trap</option> <option>SNMPv2 Inform</option>'; break; case SNMPv2Trap: $htmlStr .= '<option selected>SNMPv2 Trap</option><option>SNMPv1 Trap</option> <option>SNMPv2 Inform</option>'; break; case SNMPv2Inform: $htmlStr .= '<option selected>SNMPv2 Inform</option><option>SNMPv1 Trap</option> <option>SNMPv2 Trap</option>'; break; } $htmlStr .= '</td></tr>'; return $htmlStr;}/* Returns a BasicSNMPConfig instance or NULL if error */ function read_basic_from_snmpd_conf($conn, &$error) { $info = new BasicSNMPConfig(); $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='rocommunity'"; $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT); if (!$result) return NULL; if (pg_NumRows($result) > 0) { $info->rocommunity = rtrim(pg_result($result, 0, 0)); } pg_freeresult($result); $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='rwcommunity'"; $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT); if (!$result) return NULL; if (pg_NumRows($result) > 0) { $info->rwcommunity = rtrim(pg_result($result, 0, 0)); } pg_freeresult($result); $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='syslocation'"; $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT); if (!$result) return NULL; if (pg_NumRows($result) > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -