⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setup_snmp.php

📁 Linux嵌入式设计配套光盘,学习嵌入式设计可参考
💻 PHP
📖 第 1 页 / 共 2 页
字号:
    $info->syslocation = rtrim(pg_result($result, 0, 0));  }  pg_freeresult($result);  $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='syscontact'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result)    return NULL;  if (pg_NumRows($result) > 0) {    $info->syscontact = rtrim(pg_result($result, 0, 0));  }  pg_freeresult($result);  $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='agentport'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result)    return NULL;  if (pg_NumRows($result) > 0) {    $info->agentport = rtrim(pg_result($result, 0, 0));  }  pg_freeresult($result);  return $info;}/* Given a BasicSNMPConfig instance, write the  * data to the snmp configuration file  * Function does not return anything useful. * Can reduce communication by checking for differences * between current and new data. */ function write_basic_to_snmpd_conf($conn, $newBasic, $currBasic, $error) {  $command = "UPDATE tbl2field SET value='$newBasic->rocommunity' where name='snmpd.conf' and field='rocommunity'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  $command = "UPDATE tbl2field SET value='$newBasic->rwcommunity' where name='snmpd.conf' and field='rwcommunity'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  $command = "UPDATE tbl2field SET value='$newBasic->syslocation' where name='snmpd.conf' and field='syslocation'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);    $command = "UPDATE tbl2field SET value='$newBasic->syscontact' where name='snmpd.conf' and field='syscontact'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  $command = "UPDATE tbl2field SET value='$newBasic->agentport' where name='snmpd.conf' and field='agentport'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  /* modify the snmpd.conf file */  $command = "UPDATE tbl2file SET do_commit=1 WHERE name='snmpd.conf'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  /* call tbl2script.sh to send SIGHUP signal to snmpd daemon */   $command = "UPDATE tbl2file SET script_parms='snmpd restart', do_script=1 where name='snmpd.conf'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  return;}function read_notifications_from_snmpd_conf($conn, &$error) {  $notifications = array();  for ($i=1; $i<=3; $i++) {    $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='trap_" . $i . "_v1'";    $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);    if (!$result)      return NULL;    if (pg_NumRows($result) > 0) {      $value = trim(pg_result($result, 0, 0));      if (strlen($value) > 0) {        $info = create_notification(SNMPv1Trap, $i, $value);        $notifications[] = $info;      }    }    pg_freeresult($result);  }  for ($i=1; $i<=3; $i++) {    $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='trap_" . $i . "_v2'";    $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);    if (!$result)      return NULL;    if (pg_NumRows($result) > 0) {      $value = trim(pg_result($result, 0, 0));      if (strlen($value) > 0) {        $info = create_notification(SNMPv2Trap, $i, $value);        $notifications[] = $info;      }    }    pg_freeresult($result);  }  for ($i=1; $i<=3; $i++) {    $command = "SELECT value FROM tbl2field WHERE name='snmpd.conf' and field='inform_" . $i ."'";    $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);    if (!$result)      return NULL;    if (pg_NumRows($result) > 0) {      $value = trim(pg_result($result, 0, 0));      if (strlen($value) > 0) {        $info = create_notification(SNMPv2Inform, $i, $value);        $notifications[] = $info;      }    }    pg_freeresult($result);  }  return $notifications;}/* writes to snmpd.conf and to SnmpDest table in logmuxd *//* snmpd will send traps when snmpd.conf is re-read */ /* logmuxd will send traps when a zone changes state */ function write_notifications_to_backend($tbl2fileConn, $logmuxdConn, $newNotifications, $currNotifications, $error) {    /* The following logic will check if there are more than one "unhidden"    * notifications in currNotifications with the same id, and if so, it will   * "hide" all those but the first one.  This logic is useful if snmpd.conf    * file is not consistent.   */   $toHideList = array();  for ($i=1; $i<=3; $i++) {    $found = false;  /* have we found the first non-hidden one with id = $i */    foreach ($currNotifications as $notification) {      if (!$notification->hidden && $notification->id == $i) {         if (!$found) {	  $found = true;        } else {          $notification->hidden = true;          $toHideList[] = $notification;         }      }    }  }  /* At this point currNotifications will have at most one "unhidden"    * notification with a particular id.  Furthermore, toHideList[] will    * contain all the notifications that are to be "hidden".   */  /* To the toHideList[], add all those notifications in currNotifications   * that are not in newNotifications.    */  foreach ($currNotifications as $notification) {    if (!$notification->hidden) {       $obj = get_notification_by_id($newNotifications, $notification->id);      if (is_null($obj) || ($notification->traptype != $obj->traptype)) {        $notification->hidden = true;        $toHideList[] = $notification;       }    }  }  foreach($toHideList as $notification) {    write_notification_to_snmpd_conf($tbl2fileConn, $notification);  }  foreach($newNotifications as $notification) {    write_notification_to_snmpd_conf($tbl2fileConn, $notification);  }  foreach($newNotifications as $notification) {    write_notification_to_logmuxd_snmp_dest($logmuxdConn, $notification);  }  /* modify the snmpd.conf file */  $command = "UPDATE tbl2file SET do_commit=1 WHERE name='snmpd.conf'";  $result = exec_sql($tbl2fileConn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);  /* call tbl2script.sh to send SIGHUP signal to snmpd daemon */  $command = "UPDATE tbl2file SET script_parms='snmpd restart', do_script=1 where name='snmpd.conf'";  $result = exec_sql($tbl2fileConn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);}function write_notification_to_snmpd_conf($conn, $notification) {  $id = $notification->id;  if ($id<1 || $id>3)    return;  $status = '';  if ($notification->hidden)    $status = '##';  else if ($notification->enable)    $status = '';  else    $status = '#';  switch ($notification->traptype) {  case SNMPv1Trap:     $field = "trap_$id" . '_v1';    $value = sprintf("%strapsink %s %s %s", $status, $notification->targethost, $notification->targetcommunity, $notification->targetport);     break;  case SNMPv2Trap:     $field = "trap_$id" . '_v2';    $value = sprintf("%strap2sink %s %s %s", $status, $notification->targethost, $notification->targetcommunity, $notification->targetport);     break;  case SNMPv2Inform:    $field = "inform_$id";    $value = sprintf("%sinformsink %s %s %s", $status, $notification->targethost, $notification->targetcommunity, $notification->targetport);     break;  default:    return;  }    $command = "UPDATE tbl2field SET value='$value' where name='snmpd.conf' and field='$field'";  $result = exec_sql($conn, $command, $error, DB_HOST, DB_TBL2FILE_PORT);  if (!$result) return; else pg_freeresult($result);}function write_notification_to_logmuxd_snmp_dest($conn, $notification) {  $id = $notification->id;  if ($id<1 || $id>3)    return;  $offset = $id - 1;  /* Works because the way enumerate traptypes in the GUI is the   * same as the way logmuxd handles snmp versions. */   $version = $notification->traptype;   if ($notification->enable) {    if ($version == 1) {       $dest = 'snmp_monitor_v1';    } else {       $dest = 'snmp_monitor_v2';    }    $command = "UPDATE SnmpDest SET destname = '$dest', dest = '$notification->targethost', community = '$notification->targetcommunity', port = '$notification->targetport', version = $version LIMIT 1 OFFSET $offset";   } else {    $command = "UPDATE SnmpDest SET destname = '', dest = '', community = '', port = 162, version = 2 LIMIT 1 OFFSET $offset";   }  $result = exec_sql($conn, $command, $error, DB_HOST, DB_LOGMUXD_PORT);  if (!$result) return; else pg_freeresult($result);}function build_n_notifications_for_ui($notifications, $n) {  if (is_null($notifications))    return NULL;  /* refactor - simply create a non-hidden list */  $enabled_list = array();  $disabled_list = array();  $hidden_list = array();  foreach($notifications as $info) {    if ($info->hidden)      $hidden_list[] = $info;     else if ($info->enable)      $enabled_list[] = $info;    else      $disabled_list[] = $info;  }  $notifications = array_merge($enabled_list, $disabled_list);  $out = array();  /* There should be one item with id=1, one with id=2 and one with id=3 */  for ($i=1; $i<=$n; $i++) {    $obj = get_notification_by_id($notifications, $i);    if (is_null($obj)) {      $obj = new Notification();       $obj->id = $i;     }    $out[] = $obj;   }  return $out;}function convert_str_to_trap_type($string) {  if (!strcmp("SNMPv1 Trap", $string))    return SNMPv1Trap;   else if (!strcmp("SNMPv2 Trap", $string))    return SNMPv2Trap;   else if (!strcmp("SNMPv2 Inform",$string))    return SNMPv2Inform;   return SNMPv1Trap; }/* Expecting a string like * 'trapsink <targetHost> <targetCommunity> <targetPort>'  * or  * '#trapsink <targetHost> <targetCommunity> <targetPort>'  */function create_notification($type, $id, $string_value) {  $info = new Notification();  $info->enable = false;   $info->hidden = false;   $info->traptype = $type;   $info->id = $id;  $tok = split("[ \t]+", $string_value);   if (count($tok) >= 1) {    if (!strncmp($tok[0],'##',2))      $info->hidden = true;     else if (strncmp($tok[0],'#',1))      $info->enable = true;   }  if (count($tok) >= 2)    $info->targethost = $tok[1];  if (count($tok) >= 3)     $info->targetcommunity = $tok[2];  if (count($tok) >= 4)     $info->targetport = $tok[3];      return $info;}function get_notification_by_type_and_id($notifications, $type, $id) {  foreach($notifications as $obj) {    if ($obj->type == $type && $obj->id == $id)      return $obj;  }  return NULL;}function get_notification_by_id($notifications, $id) {  foreach($notifications as $obj) {    if ($obj->id == $id)      return $obj;  }  return NULL;}function validate_alphanum_name(&$name, $allowEmpty=false) {  if (is_null($name))    return false;  $name = trim($name);  if (strlen($name) <= 0) {    return ($allowEmpty)? true: false;  }  if (!ereg("^[a-zA-Z][a-zA-Z0-9]*$", $name))    return false;  return true;}function validate_dotted_name(&$name, $allowEmpty=false) {  if (is_null($name))    return false;  $name = trim($name);  if (strlen($name) <= 0) {    return ($allowEmpty)? true: false;  }  if (!ereg("^[a-zA-Z][a-zA-Z0-9\.]*$", $name))    return false;  return true;}function validate_host(&$name, $allowEmpty=false) {  if (is_null($name))    return false;  if (strlen($name) <= 0) {    return ($allowEmpty)? true: false;  }  $tmp_name = $name;  if (validate_dotted_name($tmp_name)) {    $name = $tmp_name;    return true;  }  if (validate_ip_address($tmp_name)) {    $name = $tmp_name;    return true;  }  return false;}function validate_syslocation(&$syslocation) {  if (is_null($syslocation))    return false;  $syslocation = trim($syslocation);  if (ereg("'", $syslocation))    return false;  return true;}function validate_syscontact(&$syscontact) {  if (is_null($syscontact))    return false;  $syscontact = trim($syscontact);  if (ereg("'", $syscontact))    return false;  return true;}function validate_ip_address(&$address) {  if (!$address)    return false;  $address = trim($address);  $tok = split("\.", $address);  if (count($tok) != 4)    return false;  $a = array();  $count = 0;  foreach ($tok as $n) {    if (!is_numeric($n))      return false;    $n = 0+$n;     if (!is_int($n))      return false;    if (intval($n) < 0)      return false;    if (intval($n) > 255)      return false;    $a[$count] = $n;    $count++;  }  $address = "" . $a[0] . "." . $a[1] . "." . $a[2] . "." . $a[3];   return true; }function validate_port(&$port, $allowEmpty=false) {  if (is_null($port))    return false;  $port = trim($port);  if (strlen($port) <= 0) {    return ($allowEmpty)? true: false;  }  if (!ereg("^[0-9]*$", $port))    return false;  $port = 0 + $port;  if (!is_int($port))    return false;   if (intval($port) < 0)    return false;  if (intval($port) > MAX_PORT)    return false;  return true;}function validate_traptype(&$traptype) {  if (is_null($traptype))    return false;  if (!is_int($traptype))    return false;   switch ($traptype) {  case SNMPv1Trap:   case SNMPv2Trap:   case SNMPv2Inform:     return true;  default:     return false;  }  return false;}/* Provides a wrapper to pg_exec that includes error message generation. Returns the same result as pg_exec; false on error or a result index if the sql command could be executed. * $error is an array that is appended to if there is an error  * $host and $port just used for error reporting * The caller is expected to free the returned object with pg_freeresult.  */function exec_sql($conn, $cmd, &$error, $host, $port) {  $result = pg_exec($conn, $cmd);  if (!$result) {     $detail = pg_ErrorMessage($conn);     $error[] = "SQL command failed. Connected to \"" . $host . "\" on port " . $port . "." . " " . $detail;      return false;   }  return $result;}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -