function ss_host_disk($hostname, $host_id, $snmp_auth, $cmd, $arg1 = "", $arg2 = "") { $snmp = explode(":", $snmp_auth); $snmp_version = $snmp[0]; $snmp_port = $snmp[1]; $snmp_timeout = $snmp[2]; $ping_retries = $snmp[3]; $max_oids = $snmp[4]; $snmp_auth_username = ""; $snmp_auth_password = ""; $snmp_auth_protocol = ""; $snmp_priv_passphrase = ""; $snmp_priv_protocol = ""; $snmp_context = ""; $snmp_community = ""; if ($snmp_version == 3) { $snmp_auth_username = $snmp[6]; $snmp_auth_password = $snmp[7]; $snmp_auth_protocol = $snmp[8]; $snmp_priv_passphrase = $snmp[9]; $snmp_priv_protocol = $snmp[10]; $snmp_context = $snmp[11]; } else { $snmp_community = $snmp[5]; } $oids = array("total" => ".1.3.6.1.2.1.25.2.3.1.5", "used" => ".1.3.6.1.2.1.25.2.3.1.6", "failures" => ".1.3.6.1.2.1.25.2.3.1.7", "index" => ".1.3.6.1.2.1.25.2.3.1.1", "description" => ".1.3.6.1.2.1.25.2.3.1.3", "sau" => ".1.3.6.1.2.1.25.2.3.1.4"); if ($cmd == "index") { $return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER)); for ($i = 0; $i < sizeof($return_arr); $i++) { print $return_arr[$i] . "\n"; } } elseif ($cmd == "num_indexes") { $return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER)); print sizeof($return_arr); } elseif ($cmd == "query") { $arg = $arg1; $arr_index = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER)); $arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids[$arg], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, $max_oids, SNMP_POLLER)); for ($i = 0; $i < sizeof($arr_index); $i++) { print $arr_index[$i] . "!" . $arr[$i] . "\n"; } } elseif ($cmd == "get") { $arg = $arg1; $index = $arg2; if ($arg == "total" || $arg == "used") { $sau = preg_replace("/[^0-9]/i", "", db_fetch_cell("select field_value from host_snmp_cache where host_id={$host_id} and field_name='hrStorageAllocationUnits' and snmp_index='{$index}'")); $snmp_data = cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".{$index}", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER); if ($snmp_data < 0) { return ($snmp_data + 4294967296) * $sau; } else { return $snmp_data * $sau; } } else { return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".{$index}", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER); } } }
function ss_host_disk($hostname, $host_id, $snmp_auth, $cmd, $arg1 = "", $arg2 = "") { $snmp = explode(":", $snmp_auth); $snmp_version = $snmp[0]; $snmp_port = $snmp[1]; $snmp_timeout = $snmp[2]; $snmp_auth_username = ""; $snmp_auth_password = ""; $snmp_auth_protocol = ""; $snmp_priv_passphrase = ""; $snmp_priv_protocol = ""; $snmp_context = ""; $snmp_community = ""; if ($snmp_version == 3) { $snmp_auth_username = $snmp[4]; $snmp_auth_password = $snmp[5]; $snmp_auth_protocol = $snmp[6]; $snmp_priv_passphrase = $snmp[7]; $snmp_priv_protocol = $snmp[8]; $snmp_context = $snmp[9]; } else { $snmp_community = $snmp[3]; } $oids = array("total" => ".1.3.6.1.2.1.25.2.3.1.5", "totalin" => ".1.3.6.1.2.1.25.2.3.1.5", "used" => ".1.3.6.1.2.1.25.2.3.1.6", "failures" => ".1.3.6.1.2.1.25.2.3.1.7", "index" => ".1.3.6.1.2.1.25.2.3.1.1", "description" => ".1.3.6.1.2.1.25.2.3.1.3", "sau" => ".1.3.6.1.2.1.25.2.3.1.4"); if ($cmd == "index") { $return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, read_config_option("snmp_retries"), SNMP_POLLER)); for ($i = 0; $i < sizeof($return_arr); $i++) { print $return_arr[$i] . "\n"; } } elseif ($cmd == "query") { $arg = $arg1; $arr_index = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, read_config_option("snmp_retries"), SNMP_POLLER)); $arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids[$arg], $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, read_config_option("snmp_retries"), SNMP_POLLER)); for ($i = 0; $i < sizeof($arr_index); $i++) { print $arr_index[$i] . "!" . $arr[$i] . "\n"; } } elseif ($cmd == "get") { $arg = $arg1; $index = $arg2; $value = api_plugin_hook_function('hmib_get_disk', array("host_id" => $host_id, "arg" => $arg, "index" => $index)); if (is_array($value)) { if ($arg == "total" || $arg == "used") { $sau = eregi_replace("[^0-9]", "", db_fetch_cell("select field_value from host_snmp_cache where host_id={$host_id} and field_name='hrStorageAllocationUnits' and snmp_index='{$index}'")); return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".{$index}", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, read_config_option("snmp_retries"), SNMP_POLLER) * $sau; } else { return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".{$index}", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, read_config_option("snmp_retries"), SNMP_POLLER); } } else { return $value; } } }
function ss_mikrotik_snmpget($hostid = '', $oid = '') { global $config; include_once $config['base_path'] . '/lib/snmp.php'; if ($hostid > 0) { $host = db_fetch_row("SELECT hostname, snmp_community, snmp_version, snmp_username, snmp_password, \n\t\t\tsnmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, \n\t\t\tsnmp_port, snmp_timeout, max_oids \n\t\t\tFROM host \n\t\t\tWHERE id={$hostid}"); if (sizeof($host)) { $get = cacti_snmp_get($host['hostname'], $host['snmp_community'], $oid, $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI); if (!empty($get)) { return $get; } } } return '0'; }
function ss_host_disk($hostname, $snmp_community, $snmp_version, $host_id, $cmd, $arg1 = "", $arg2 = "", $snmp_port = 161, $snmp_timeout = 500) { $oids = array( "total" => ".1.3.6.1.2.1.25.2.3.1.5", "used" => ".1.3.6.1.2.1.25.2.3.1.6", "failures" => ".1.3.6.1.2.1.25.2.3.1.7", "index" => ".1.3.6.1.2.1.25.2.3.1.1", "description" => ".1.3.6.1.2.1.25.2.3.1.3", "sau" => ".1.3.6.1.2.1.25.2.3.1.4" ); if ((func_num_args() == "9") || (func_num_args() == "7") || (func_num_args() == "6") || (func_num_args() == "5")) { if ($cmd == "index") { $return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, "", "", $snmp_port, $snmp_timeout, SNMP_POLLER)); for ($i=0;($i<sizeof($return_arr));$i++) { print $return_arr[$i] . "\n"; } }elseif ($cmd == "query") { $arg = $arg1; $arr_index = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, "", "", $snmp_port, $snmp_timeout, SNMP_POLLER)); $arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids[$arg], $snmp_version, "", "", $snmp_port, $snmp_timeout, SNMP_POLLER)); for ($i=0;($i<sizeof($arr_index));$i++) { print $arr_index[$i] . "!" . $arr[$i] . "\n"; } }elseif ($cmd == "get") { $arg = $arg1; $index = $arg2; if (($arg == "total") || ($arg == "used")) { /* get hrStorageAllocationUnits from the snmp cache since it is faster */ $sau = eregi_replace("[^0-9]", "", db_fetch_cell("select field_value from host_snmp_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'")); return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, "", "", $snmp_port, $snmp_timeout, SNMP_POLLER) * $sau; }else{ return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, "", "", $snmp_port, $snmp_timeout, SNMP_POLLER); } } } else { return "ERROR: Invalid Parameters\n"; } }
function ping_snmp() { /* initialize variables */ $this->snmp_status = "down"; $this->snmp_response = "Host did not respond to SNMP"; $output = ""; /* get start time */ $this->start_time(); /* poll sysUptime for status */ $retry_count = 0; /* getnext does not work in php versions less than 5 */ if (version_compare("5", phpversion(), "<")) { $oid = ".1.3"; }else{ $oid = ".1.3.6.1.2.1.1.3.0"; } while (1) { if ($retry_count >= $this->retries) { $this->snmp_status = "down"; $this->snmp_response = "Host did not respond to SNMP"; return false; } /* getnext does not work in php versions less than 5 */ if (version_compare("5", phpversion(), "<")) { $output = cacti_snmp_getnext($this->host["hostname"], $this->host["snmp_community"], $oid, $this->host["snmp_version"], $this->host["snmp_username"], $this->host["snmp_password"], $this->host["snmp_auth_protocol"], $this->host["snmp_priv_passphrase"], $this->host["snmp_priv_protocol"], $this->host["snmp_context"], $this->host["snmp_port"], $this->host["snmp_timeout"], SNMP_CMDPHP); }else{ $output = cacti_snmp_get($this->host["hostname"], $this->host["snmp_community"], $oid, $this->host["snmp_version"], $this->host["snmp_username"], $this->host["snmp_password"], $this->host["snmp_auth_protocol"], $this->host["snmp_priv_passphrase"], $this->host["snmp_priv_protocol"], $this->host["snmp_context"], $this->host["snmp_port"], $this->host["snmp_timeout"], SNMP_CMDPHP); } /* determine total time +- ~10% */ $this->time = $this->get_time($this->precision); /* check result for uptime */ if (strlen($output)) { /* calculte total time */ $this->snmp_status = $this->time*1000; $this->snmp_response = "Host responded to SNMP"; return true; } if ($retry_count == 0) { $oid = ".1.3.6.1.2.1.1.3.0"; }else{ $oid = ".1.3.6.1.2.1.1.1.0"; } $retry_count++; } } /* ping_snmp */
function ping_host() { input_validate_input_number($_REQUEST['id']); if (empty($_REQUEST['id'])) { return ""; } $host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($_REQUEST['id'])); $am = $host['availability_method']; $anym = false; if ($am == AVAIL_SNMP || $am == AVAIL_SNMP_GET_NEXT || $am == AVAIL_SNMP_GET_SYSDESC || $am == AVAIL_SNMP_AND_PING || $am == AVAIL_SNMP_OR_PING) { $anym = true; print "SNMP Information<br>\n"; print "<span style='font-size: 10px; font-weight: normal; font-family: monospace;'>\n"; if ($host['snmp_community'] == '' && $host['snmp_username'] == '' || $host['snmp_version'] == 0) { print "<span style='color: #ab3f1e; font-weight: bold;'>SNMP not in use</span>\n"; } else { $snmp_system = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.1.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI); /* modify for some system descriptions */ /* 0000937: System output in host.php poor for Alcatel */ if (substr_count($snmp_system, '00:')) { $snmp_system = str_replace('00:', '', $snmp_system); $snmp_system = str_replace(':', ' ', $snmp_system); } if ($snmp_system == '') { print "<span class='hostDown'>SNMP error</span>\n"; } else { $snmp_uptime = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.3.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI); $snmp_hostname = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.5.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI); $snmp_location = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.6.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI); $snmp_contact = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.4.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI); print '<strong>System:</strong>' . html_split_string($snmp_system) . "<br>\n"; $days = intval($snmp_uptime / (60 * 60 * 24 * 100)); $remainder = $snmp_uptime % (60 * 60 * 24 * 100); $hours = intval($remainder / (60 * 60 * 100)); $remainder = $remainder % (60 * 60 * 100); $minutes = intval($remainder / (60 * 100)); print "<strong>Uptime:</strong> {$snmp_uptime}"; print " ({$days} days, {$hours} hours, {$minutes} minutes)<br>\n"; print "<strong>Hostname:</strong> {$snmp_hostname}<br>\n"; print "<strong>Location:</strong> {$snmp_location}<br>\n"; print "<strong>Contact:</strong> {$snmp_contact}<br>\n"; } } print "</span>\n"; } if ($am == AVAIL_PING || $am == AVAIL_SNMP_AND_PING || $am == AVAIL_SNMP_OR_PING) { $anym = true; /* create new ping socket for host pinging */ $ping = new Net_Ping(); $ping->host = $host; $ping->port = $host['ping_port']; /* perform the appropriate ping check of the host */ $ping_results = $ping->ping(AVAIL_PING, $host['ping_method'], $host['ping_timeout'], $host['ping_retries']); if ($ping_results == true) { $host_down = false; $class = 'hostUp'; } else { $host_down = true; $class = 'hostDown'; } print "Ping Results<br>\n"; print "<span class='" . $class . "'>" . $ping->ping_response . "</span>\n"; } if ($anym == false) { print "No Ping or SNMP Availability Check In Use<br><br>\n"; } }
function ss_host_disk($hostname, $host_id, $snmp_auth, $cmd, $arg1 = "", $arg2 = "") { $snmp = explode(":", $snmp_auth); $snmp_version = $snmp[0]; $snmp_port = $snmp[1]; $snmp_timeout = $snmp[2]; $snmpv3_auth_username = ""; $snmpv3_auth_password = ""; $snmpv3_auth_protocol = ""; $snmpv3_priv_passphrase = ""; $snmpv3_priv_protocol = ""; $snmp_community = ""; if ($snmp_version == 3) { $snmpv3_auth_username = $snmp[4]; $snmpv3_auth_password = $snmp[5]; $snmpv3_auth_protocol = $snmp[6]; $snmpv3_priv_passphrase = $snmp[7]; $snmpv3_priv_protocol = $snmp[8]; }else{ $snmp_community = $snmp[3]; } $oids = array( "total" => ".1.3.6.1.2.1.25.2.3.1.5", "used" => ".1.3.6.1.2.1.25.2.3.1.6", "failures" => ".1.3.6.1.2.1.25.2.3.1.7", "index" => ".1.3.6.1.2.1.25.2.3.1.1", "description" => ".1.3.6.1.2.1.25.2.3.1.3", "sau" => ".1.3.6.1.2.1.25.2.3.1.4" ); if ($cmd == "index") { $return_arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmp_port, $snmp_timeout, SNMP_POLLER)); for ($i=0;($i<sizeof($return_arr));$i++) { print $return_arr[$i] . "\n"; } }elseif ($cmd == "query") { $arg = $arg1; $arr_index = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmp_port, $snmp_timeout, SNMP_POLLER)); $arr = ss_host_disk_reindex(cacti_snmp_walk($hostname, $snmp_community, $oids[$arg], $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmp_port, $snmp_timeout, SNMP_POLLER)); for ($i=0;($i<sizeof($arr_index));$i++) { print $arr_index[$i] . "!" . $arr[$i] . "\n"; } }elseif ($cmd == "get") { $arg = $arg1; $index = $arg2; if (($arg == "total") || ($arg == "used")) { /* get hrStorageAllocationUnits from the snmp cache since it is faster */ $sau = eregi_replace("[^0-9]", "", db_fetch_cell("select field_value from host_snmp_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'")); return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmp_port, $snmp_timeout, SNMP_POLLER) * $sau; }else{ return cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmp_port, $snmp_timeout, SNMP_POLLER); } } }
print $arr_index[$i] . "!" . $arr[$i] . "\n"; } /* * process GET requests */ }elseif ($cmd == "get") { $arg = $_SERVER["argv"][5]; $index = $_SERVER["argv"][6]; if (($arg == "total") || ($arg == "used")) { /* get hrStorageAllocationUnits from the snmp cache since it is faster */ $sau = db_fetch_cell("select field_value from device_snmp_cache where device_id=$device_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'"); print (cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol,$snmp_priv_passphrase,$snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER)* $sau); }else{ print (cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmp_auth_username, $snmp_auth_password, $snmp_auth_protocol,$snmp_priv_passphrase,$snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $ping_retries, SNMP_POLLER)); } } function reindex($arr) { $return_arr = array(); for ($i=0;($i<sizeof($arr));$i++) { $return_arr[$i] = $arr[$i]["value"]; } return $return_arr; } ?>
function host_edit() { global $colors, $fields_host_edit, $reindex_types; /* ================= input validation ================= */ input_validate_input_number(get_request_var("id")); /* ==================================================== */ display_output_messages(); if (!empty($_GET["id"])) { $host = db_fetch_row("select * from host where id=" . $_GET["id"]); $header_label = "[edit: " . $host["description"] . "]"; } else { $header_label = "[new]"; } if (!empty($host["id"])) { ?> <table width="100%" align="center"> <tr> <td class="textInfo" colspan="2"> <?php print $host["description"]; ?> (<?php print $host["hostname"]; ?> ) </td> </tr> <tr> <td class="textHeader"> <?php if ($host["availability_method"] == AVAIL_SNMP || $host["availability_method"] == AVAIL_SNMP_AND_PING || $host["availability_method"] == AVAIL_SNMP_OR_PING) { ?> SNMP Information<br> <span style="font-size: 10px; font-weight: normal; font-family: monospace;"> <?php if ($host["snmp_community"] == "" && $host["snmp_username"] == "" || $host["snmp_version"] == 0) { print "<span style='color: #ab3f1e; font-weight: bold;'>SNMP not in use</span>\n"; } else { $snmp_system = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.1.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); /* modify for some system descriptions */ /* 0000937: System output in hosts.php poor for Alcatel */ if (substr_count($snmp_system, "00:")) { $snmp_system = str_replace("00:", "", $snmp_system); $snmp_system = str_replace(":", " ", $snmp_system); } if ($snmp_system == "") { print "<span style='color: #ff0000; font-weight: bold;'>SNMP error</span>\n"; } else { $snmp_uptime = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.3.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); $snmp_hostname = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.5.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); $snmp_location = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.6.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); $snmp_contact = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.4.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); print "<strong>System:</strong>" . html_split_string($snmp_system) . "<br>\n"; $days = intval($snmp_uptime / (60 * 60 * 24 * 100)); $remainder = $snmp_uptime % (60 * 60 * 24 * 100); $hours = intval($remainder / (60 * 60 * 100)); $remainder = $remainder % (60 * 60 * 100); $minutes = intval($remainder / (60 * 100)); print "<strong>Uptime:</strong> {$snmp_uptime}"; print " ({$days} days, {$hours} hours, {$minutes} minutes)<br>\n"; print "<strong>Hostname:</strong> {$snmp_hostname}<br>\n"; print "<strong>Location:</strong> {$snmp_location}<br>\n"; print "<strong>Contact:</strong> {$snmp_contact}<br>\n"; } } ?> </span> <?php } if ($host["availability_method"] == AVAIL_PING || $host["availability_method"] == AVAIL_SNMP_AND_PING || $host["availability_method"] == AVAIL_SNMP_OR_PING) { /* create new ping socket for host pinging */ $ping = new Net_Ping(); $ping->host = $host; $ping->port = $host["ping_port"]; /* perform the appropriate ping check of the host */ if ($ping->ping($host["availability_method"], $host["ping_method"], $host["ping_timeout"], $host["ping_retries"])) { $host_down = false; $color = "#000000"; } else { $host_down = true; $color = "#ff0000"; } ?> <br>Ping Results<br> <span style="font-size: 10px; font-weight: normal; color: <?php print $color; ?> ; font-family: monospace;"> <?php print $ping->ping_response; ?> </span> <?php } else { if ($host["availability_method"] == AVAIL_NONE) { ?> No Availability Check In Use<br> <?php } } ?> </td> <td class="textInfo" valign="top"> <span style="color: #c16921;">*</span><a href="graphs_new.php?host_id=<?php print $host["id"]; ?> ">Create Graphs for this Host</a><br> <span style="color: #c16921;">*</span><a href="data_sources.php?host_id=<?php print $host["id"]; ?> &ds_rows=30&filter=&template_id=-1&method_id=-1&page=1">Data Source List</a><br> <span style="color: #c16921;">*</span><a href="graphs.php?host_id=<?php print $host["id"]; ?> &graph_rows=30&filter=&template_id=-1&page=1">Graph List</a> </td> </tr> </table> <?php } html_start_box("<strong>Devices</strong> {$header_label}", "100%", $colors["header"], "3", "center", ""); /* preserve the host template id if passed in via a GET variable */ if (!empty($_GET["host_template_id"])) { $fields_host_edit["host_template_id"]["value"] = $_GET["host_template_id"]; } draw_edit_form(array("config" => array("form_name" => "chk"), "fields" => inject_form_variables($fields_host_edit, isset($host) ? $host : array()))); html_end_box(); ?> <script type="text/javascript"> <!-- // default snmp information var snmp_community = document.getElementById('snmp_community').value; var snmp_username = document.getElementById('snmp_username').value; var snmp_password = document.getElementById('snmp_password').value; var snmp_auth_protocol = document.getElementById('snmp_auth_protocol').value; var snmp_priv_passphrase = document.getElementById('snmp_priv_passphrase').value; var snmp_priv_protocol = document.getElementById('snmp_priv_protocol').value; var snmp_context = document.getElementById('snmp_context').value; var snmp_port = document.getElementById('snmp_port').value; var snmp_timeout = document.getElementById('snmp_timeout').value; var max_oids = document.getElementById('max_oids').value; // default ping methods var ping_method = document.getElementById('ping_method').value; var ping_port = document.getElementById('ping_port').value; var ping_timeout = document.getElementById('ping_timeout').value; var ping_retries = document.getElementById('ping_retries').value; var availability_methods = document.getElementById('availability_method').options; var num_methods = document.getElementById('availability_method').length; var selectedIndex = document.getElementById('availability_method').selectedIndex; var agent = navigator.userAgent; agent = agent.match("MSIE"); function setPingVisibility() { availability_method = document.getElementById('availability_method').value; ping_method = document.getElementById('ping_method').value; /* debugging, uncomment as required */ //alert("The availability method is '" + availability_method + "'"); //alert("The ping method is '" + ping_method + "'"); switch(availability_method) { case "0": // none document.getElementById('row_ping_method').style.display = "none"; document.getElementById('row_ping_port').style.display = "none"; document.getElementById('row_ping_timeout').style.display = "none"; document.getElementById('row_ping_retries').style.display = "none"; break; case "2": // snmp document.getElementById('row_ping_method').style.display = "none"; document.getElementById('row_ping_port').style.display = "none"; document.getElementById('row_ping_timeout').style.display = ""; document.getElementById('row_ping_retries').style.display = ""; break; default: // ping ok switch(ping_method) { case "1": // ping icmp document.getElementById('row_ping_method').style.display = ""; document.getElementById('row_ping_port').style.display = "none"; document.getElementById('row_ping_timeout').style.display = ""; document.getElementById('row_ping_retries').style.display = ""; break; case "2": // ping udp case "3": // ping tcp document.getElementById('row_ping_method').style.display = ""; document.getElementById('row_ping_port').style.display = ""; document.getElementById('row_ping_timeout').style.display = ""; document.getElementById('row_ping_retries').style.display = ""; break; } break; } } function addSelectItem(item, formObj) { if (agent != "MSIE") { formObj.add(item,null); // standards compliant }else{ formObj.add(item); // IE only } } function setAvailability(type) { /* get the availability structure */ var am=document.getElementById('availability_method'); /* get current selectedIndex */ selectedIndex = document.getElementById('availability_method').selectedIndex; /* debugging uncomment as required */ //alert("The selectedIndex is '" + selectedIndex + "'"); //alert("The array length is '" + am.length + "'"); switch(type) { case "NoSNMP": /* remove snmp options */ if (am.length == 5) { am.remove(1); am.remove(1); am.remove(1); } /* set the index to something valid, like "ping" */ if (selectedIndex > 1) { am.selectedIndex=1; } break; case "All": /* restore all options */ if (am.length == 2) { am.remove(0); am.remove(0); var a=document.createElement('option'); var b=document.createElement('option'); var c=document.createElement('option'); var d=document.createElement('option'); var e=document.createElement('option'); a.value="0"; a.text="None"; addSelectItem(a,am); b.value="1"; b.text="Ping and SNMP"; addSelectItem(b,am); e.value="4"; e.text="Ping or SNMP"; addSelectItem(e,am); c.value="2"; c.text="SNMP"; addSelectItem(c,am); d.value="3"; d.text="Ping"; addSelectItem(d,am); /* restore the correct index number */ if (selectedIndex == 0) { am.selectedIndex = 0; }else{ am.selectedIndex = 3; } } break; } setAvailabilityVisibility(type, am.selectedIndex); setPingVisibility(); } function setAvailabilityVisibility(type, selectedIndex) { switch(type) { case "NoSNMP": switch(selectedIndex) { case "0": // availability none document.getElementById('row_ping_method').style.display="none"; document.getElementById('ping_method').value=0; break; case "1": // ping document.getElementById('row_ping_method').style.display=""; document.getElementById('ping_method').value=ping_method; break; } case "All": switch(selectedIndex) { case "0": // availability none document.getElementById('row_ping_method').style.display="none"; document.getElementById('ping_method').value=0; break; case "1": // ping and snmp case "3": // ping case "4": // ping or snmp if ((document.getElementById('row_ping_method').style.display == "none") || (document.getElementById('row_ping_method').style.display == undefined)) { document.getElementById('ping_method').value=ping_method; document.getElementById('row_ping_method').style.display=""; } break; case "2": // snmp document.getElementById('row_ping_method').style.display="none"; document.getElementById('ping_method').value="0"; break; } } } function changeHostForm() { snmp_version = document.getElementById('snmp_version').value; switch(snmp_version) { case "0": setAvailability("NoSNMP"); setSNMP("None"); break; case "1": case "2": setAvailability("All"); setSNMP("v1v2"); break; case "3": setAvailability("All"); setSNMP("v3"); break; } } function setSNMP(snmp_type) { switch(snmp_type) { case "None": document.getElementById('row_snmp_username').style.display = "none"; document.getElementById('row_snmp_password').style.display = "none"; document.getElementById('row_snmp_community').style.display = "none"; document.getElementById('row_snmp_auth_protocol').style.display = "none"; document.getElementById('row_snmp_priv_passphrase').style.display = "none"; document.getElementById('row_snmp_priv_protocol').style.display = "none"; document.getElementById('row_snmp_context').style.display = "none"; document.getElementById('row_snmp_port').style.display = "none"; document.getElementById('row_snmp_timeout').style.display = "none"; document.getElementById('row_max_oids').style.display = "none"; break; case "v1v2": document.getElementById('row_snmp_username').style.display = "none"; document.getElementById('row_snmp_password').style.display = "none"; document.getElementById('row_snmp_community').style.display = ""; document.getElementById('row_snmp_auth_protocol').style.display = "none"; document.getElementById('row_snmp_priv_passphrase').style.display = "none"; document.getElementById('row_snmp_priv_protocol').style.display = "none"; document.getElementById('row_snmp_context').style.display = "none"; document.getElementById('row_snmp_port').style.display = ""; document.getElementById('row_snmp_timeout').style.display = ""; document.getElementById('row_max_oids').style.display = ""; break; case "v3": document.getElementById('row_snmp_username').style.display = ""; document.getElementById('row_snmp_password').style.display = ""; document.getElementById('row_snmp_community').style.display = "none"; document.getElementById('row_snmp_auth_protocol').style.display = ""; document.getElementById('row_snmp_priv_passphrase').style.display = ""; document.getElementById('row_snmp_priv_protocol').style.display = ""; document.getElementById('row_snmp_context').style.display = ""; document.getElementById('row_snmp_port').style.display = ""; document.getElementById('row_snmp_timeout').style.display = ""; document.getElementById('row_max_oids').style.display = ""; break; } } function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } addLoadEvent(changeHostForm); --> </script> <?php if (isset($_GET["display_dq_details"]) && isset($_SESSION["debug_log"]["data_query"])) { html_start_box("<strong>Data Query Debug Information</strong>", "100%", $colors["header"], "3", "center", ""); print "<tr><td><span style='font-family: monospace;'>" . debug_log_return("data_query") . "</span></td></tr>"; html_end_box(); } if (!empty($host["id"])) { html_start_box("<strong>Associated Graph Templates</strong>", "100%", $colors["header"], "3", "center", ""); html_header(array("Graph Template Name", "Status"), 2); $selected_graph_templates = db_fetch_assoc("select\n\t\t\tgraph_templates.id,\n\t\t\tgraph_templates.name\n\t\t\tfrom (graph_templates,host_graph)\n\t\t\twhere graph_templates.id=host_graph.graph_template_id\n\t\t\tand host_graph.host_id=" . $_GET["id"] . "\n\t\t\torder by graph_templates.name"); $available_graph_templates = db_fetch_assoc("SELECT\n\t\t\tgraph_templates.id, graph_templates.name\n\t\t\tFROM snmp_query_graph RIGHT JOIN graph_templates\n\t\t\tON (snmp_query_graph.graph_template_id = graph_templates.id)\n\t\t\tWHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name"); $i = 0; if (sizeof($selected_graph_templates) > 0) { foreach ($selected_graph_templates as $item) { $i++; /* get status information for this graph template */ $is_being_graphed = sizeof(db_fetch_assoc("select id from graph_local where graph_template_id=" . $item["id"] . " and host_id=" . $_GET["id"])) > 0 ? true : false; ?> <tr> <td style="padding: 4px;"> <strong><?php print $i; ?> )</strong> <?php print $item["name"]; ?> </td> <td> <?php print $is_being_graphed == true ? "<span style='color: green;'>Is Being Graphed</span> (<a href='graphs.php?action=graph_edit&id=" . db_fetch_cell("select id from graph_local where graph_template_id=" . $item["id"] . " and host_id=" . $_GET["id"] . " limit 0,1") . "'>Edit</a>)" : "<span style='color: #484848;'>Not Being Graphed</span>"; ?> </td> <td align='right' nowrap> <a href='host.php?action=gt_remove&id=<?php print $item["id"]; ?> &host_id=<?php print $_GET["id"]; ?> '><img src='images/delete_icon_large.gif' title='Delete Graph Template Association' alt='Delete Graph Template Association' border='0' align='absmiddle'></a> </td> </tr> <?php } } else { print "<tr><td><em>No associated graph templates.</em></td></tr>"; } ?> <tr bgcolor="#<?php print $colors["form_alternate1"]; ?> "> <td colspan="4"> <table cellspacing="0" cellpadding="1" width="100%"> <td nowrap>Add Graph Template: <?php form_dropdown("graph_template_id", $available_graph_templates, "name", "id", "", "", ""); ?> </td> <td align="right"> <input type="image" src="images/button_add.gif" alt="Add" name="add_gt" align="absmiddle"> </td> </table> </td> </tr> <?php html_end_box(); html_start_box("<strong>Associated Data Queries</strong>", "100%", $colors["header"], "3", "center", ""); html_header(array("Data Query Name", "Debugging", "Re-Index Method", "Status"), 2); $selected_data_queries = db_fetch_assoc("select\n\t\t\tsnmp_query.id,\n\t\t\tsnmp_query.name,\n\t\t\thost_snmp_query.reindex_method\n\t\t\tfrom (snmp_query,host_snmp_query)\n\t\t\twhere snmp_query.id=host_snmp_query.snmp_query_id\n\t\t\tand host_snmp_query.host_id=" . $_GET["id"] . "\n\t\t\torder by snmp_query.name"); $available_data_queries = db_fetch_assoc("select\n\t\t\tsnmp_query.id,\n\t\t\tsnmp_query.name\n\t\t\tfrom snmp_query\n\t\t\torder by snmp_query.name"); $keeper = array(); foreach ($available_data_queries as $item) { if (sizeof(db_fetch_assoc("SELECT snmp_query_id FROM host_snmp_query " . " WHERE ((host_id=" . $_GET["id"] . ")" . " and (snmp_query_id=" . $item["id"] . "))")) > 0) { /* do nothing */ } else { array_push($keeper, $item); } } $available_data_queries = $keeper; $i = 0; if (sizeof($selected_data_queries) > 0) { foreach ($selected_data_queries as $item) { $i++; /* get status information for this data query */ $num_dq_items = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"])); $num_dq_rows = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"] . " group by snmp_index")); $status = "success"; ?> <tr> <td style="padding: 4px;"> <strong><?php print $i; ?> )</strong> <?php print $item["name"]; ?> </td> <td> (<a href="host.php?action=query_verbose&id=<?php print $item["id"]; ?> &host_id=<?php print $_GET["id"]; ?> ">Verbose Query</a>) </td> <td> <?php print $reindex_types[$item["reindex_method"]]; ?> </td> <td> <?php print $status == "success" ? "<span style='color: green;'>Success</span>" : "<span style='color: green;'>Fail</span>"; ?> [<?php print $num_dq_items; ?> Item<?php print $num_dq_items == 1 ? "" : "s"; ?> , <?php print $num_dq_rows; ?> Row<?php print $num_dq_rows == 1 ? "" : "s"; ?> ] </td> <td align='right' nowrap> <a href='host.php?action=query_reload&id=<?php print $item["id"]; ?> &host_id=<?php print $_GET["id"]; ?> '><img src='images/reload_icon_small.gif' title='Reload Data Query' alt='Reload Data Query' border='0' align='absmiddle'></a> <a href='host.php?action=query_remove&id=<?php print $item["id"]; ?> &host_id=<?php print $_GET["id"]; ?> '><img src='images/delete_icon_large.gif' title='Delete Data Query Association' alt='Delete Data Query Association' border='0' align='absmiddle'></a> </td> </tr> <?php } } else { print "<tr><td><em>No associated data queries.</em></td></tr>"; } ?> <tr bgcolor="#<?php print $colors["form_alternate1"]; ?> "> <td colspan="5"> <table cellspacing="0" cellpadding="1" width="100%"> <td nowrap>Add Data Query: <?php form_dropdown("snmp_query_id", $available_data_queries, "name", "id", "", "", ""); ?> </td> <td nowrap>Re-Index Method: <?php form_dropdown("reindex_method", $reindex_types, "", "", read_config_option("reindex_method"), "", ""); ?> </td> <td align="right"> <input type="image" src="images/button_add.gif" alt="Add" name="add_dq" align="absmiddle"> </td> </table> </td> </tr> <?php html_end_box(); } form_save_button("host.php"); }
function update_reindex_cache($host_id, $data_query_id) { global $config; include_once $config["library_path"] . "/data_query.php"; include_once $config["library_path"] . "/snmp.php"; /* will be used to keep track of sql statements to execute later on */ $recache_stack = array(); $host = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from host where id={$host_id}"); $data_query = db_fetch_row("select reindex_method, sort_field from host_snmp_query where host_id={$host_id} and snmp_query_id={$data_query_id}"); $data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id={$data_query_id}"); $data_query_xml = get_data_query_array($data_query_id); switch ($data_query["reindex_method"]) { case DATA_QUERY_AUTOINDEX_NONE: break; case DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME: /* the uptime backwards method requires snmp, so make sure snmp is actually enabled * on this device first */ if ($host["snmp_version"] > 0) { if (isset($data_query_xml["oid_uptime"])) { $oid_uptime = $data_query_xml["oid_uptime"]; } elseif (isset($data_query_xml["uptime_oid"])) { $oid_uptime = $data_query_xml["uptime_oid"]; } else { $oid_uptime = ".1.3.6.1.2.1.1.3.0"; } $assert_value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid_uptime, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_POLLER); $recache_stack[] = "('{$host_id}', '{$data_query_id}'," . POLLER_ACTION_SNMP . ", '<', '{$assert_value}', '{$oid_uptime}', '1')"; } break; case DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE: /* this method requires that some command/oid can be used to determine the * current number of indexes in the data query * pay ATTENTION to quoting! * the script parameters are usually enclosed in single tics: ' * so we have to enclose the whole list of parameters in double tics: " * */ /* the assert_value counts the number of distinct indexes currently available in host_snmp_cache * we do NOT make use of <oid_num_indexes> or the like! * this works, even if no <oid_num_indexes> was given */ $assert_value = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id={$host_id} and snmp_query_id={$data_query_id} group by snmp_index")); /* now, we have to build the (list of) commands that are later used on a recache event * the result of those commands will be compared to the assert_value we have just computed * on a comparison failure, a reindex event will be generated */ switch ($data_query_type) { case DATA_INPUT_TYPE_SNMP_QUERY: if (isset($data_query_xml["oid_num_indexes"])) { /* we have a specific OID for counting indexes */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP . ", '=', '{$assert_value}', '" . $data_query_xml["oid_num_indexes"] . "', '1')"; } else { /* count all indexes found */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP_COUNT . ", '=', '{$assert_value}', '" . $data_query_xml["oid_index"] . "', '1')"; } break; case DATA_INPUT_TYPE_SCRIPT_QUERY: if (isset($data_query_xml["arg_num_indexes"])) { /* we have a specific request for counting indexes */ /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id)) . "', '1')"; } else { /* count all indexes found */ /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT_COUNT . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_index"], $data_query_xml["script_path"], $host_id)) . "', '1')"; } break; case DATA_INPUT_TYPE_QUERY_SCRIPT_SERVER: if (isset($data_query_xml["arg_num_indexes"])) { /* we have a specific request for counting indexes */ /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SCRIPT_PHP . ", '=', '{$assert_value}', '" . addslashes(get_script_query_path($data_query_xml["script_function"] . " " . (isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id)) . "', '1')"; } else { /* count all indexes found */ # TODO: push the correct assert value /* escape path (windows!) and parameters for use with database sql; TODO: replace by db specific escape function like mysql_real_escape_string? */ #$recache_stack[] = "($host_id, $data_query_id," . POLLER_ACTION_SCRIPT_PHP_COUNT . ", '=', '$assert_value', '" . addslashes(get_script_query_path($data_query_xml["script_function"] . " " . (isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_index"], $data_query_xml["script_path"], $host_id)) . "', '1')"; # omit the assert value until we are able to run an 'index' command through script server } break; } break; case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION: $primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from host_snmp_cache where host_id={$host_id} and snmp_query_id={$data_query_id} and field_name='" . $data_query["sort_field"] . "'"); if (sizeof($primary_indexes) > 0) { foreach ($primary_indexes as $index) { $assert_value = $index["field_value"]; if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { $recache_stack[] = "({$host_id}, {$data_query_id}," . POLLER_ACTION_SNMP . ", '=', '{$assert_value}', '" . $data_query_xml["fields"][$data_query["sort_field"]]["oid"] . "." . $index["snmp_index"] . "', '1')"; } else { if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { $recache_stack[] = "('{$host_id}', '{$data_query_id}'," . POLLER_ACTION_SCRIPT . ", '=', '{$assert_value}', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " " : "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"][$data_query["sort_field"]]["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $host_id) . "', '1')"; } } } } break; } if (sizeof($recache_stack)) { poller_update_poller_reindex_from_buffer($host_id, $data_query_id, $recache_stack); } }
function collectHostIndexedOid(&$host, $tree, $table, $name) { global $cnn_id; static $types; debug("Beginning Processing for '" . $host["description"] . "[" . $host["hostname"] . "]', Table '{$name}'"); if (!sizeof($types)) { $types = array_rekey(db_fetch_assoc("SELECT id, oid, description FROM plugin_hmib_types"), "oid", array("id", "description")); } if (sizeof($host)) { /* mark for deletion */ db_execute("UPDATE {$table} SET present=0 WHERE host_id=" . $host["id"]); debug("Polling {$name} from '" . $host["description"] . "[" . $host["hostname"] . "]'"); $hostMib = array(); foreach ($tree as $mname => $oid) { if ($name == "hrProcessor") { $retrieval = SNMP_VALUE_PLAIN; } elseif ($mname == "date") { $retrieval = SNMP_VALUE_LIBRARY; } elseif ($mname != "baseOID") { $retrieval = SNMP_VALUE_PLAIN; } else { continue; } $walk = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $oid, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), $host["max_oids"], $retrieval, SNMP_WEBUI); $hostMib = array_merge($hostMib, $walk); } $set_string = ""; $values = ""; $sql_suffix = ""; $sql_prefix = "INSERT INTO {$table}"; if (sizeof($tree)) { foreach ($tree as $bname => $oid) { if ($bname != "baseOID" && $bname != "index") { $values .= (strlen($values) ? "`, `" : "`") . $bname; $sql_suffix .= (!strlen($sql_suffix) ? " ON DUPLICATE KEY UPDATE `index`=VALUES(`index`), `" : ", `") . $bname . "`=VALUES(`" . $bname . "`)"; } } } $sql_prefix .= " (`host_id`, `index`, " . $values . "`) VALUES "; $sql_suffix .= ", present=1"; // Locate the values names $prevIndex = ""; $new_array = array(); $wonky = false; $hrProcValid = false; $effective = 0; if (sizeof($hostMib)) { foreach ($hostMib as $mib) { /* do some cleanup */ if (substr($mib["oid"], 0, 1) != ".") { $mib["oid"] = "." . $mib["oid"]; } if (substr($mib["value"], 0, 4) == "OID:") { $mib["value"] = trim(str_replace("OID:", "", $mib["value"])); } $splitIndex = hmib_splitBaseIndex($mib["oid"]); if (sizeof($splitIndex)) { $index = $splitIndex[1]; $oid = $splitIndex[0]; $key = array_search($oid, $tree); /* issue workaround for snmp issues */ if ($name == "hrProcessor" && $mib["value"] == ".0.0") { if ($wonky) { $key = "load"; $mib["value"] = $effective; } elseif (!$hrProcValid) { if (db_fetch_cell("SELECT count(*) FROM plugin_hmib_hrSystem WHERE sysDescr LIKE '%Linux%' AND host_id=" . $host["id"])) { /* look for the hrProcessorLoad value */ $temp_mib = $hostMib; foreach ($temp_mib as $kk => $vv) { if (substr_count($kk, ".1.3.6.1.2.1.25.3.3.1.2")) { $hrProcValid = true; } } if (!$hrProcValid) { $user = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.4.1.2021.11.9.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), $host["max_oids"], SNMP_VALUE_LIBRARY, SNMP_WEBUI); $system = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.4.1.2021.11.10.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), $host["max_oids"], SNMP_VALUE_LIBRARY, SNMP_WEBUI); $effective = ($user + $system) * 2 / sizeof($mib); $key = "load"; $mib["value"] = $effective; $wonky = true; } } else { $effective = 0; } } } if (!empty($key)) { if ($key == "type") { $value = explode("(", $mib["value"]); if (sizeof($value) > 1) { $value = trim($value[1], " \n\r)"); if ($table != "plugin_hmib_hrSWInstalled" && $table != "plugin_hmib_hrSWRun") { $new_array[$index][$key] = isset($types[$value]) ? $types[$value]["id"] : 0; } else { $new_array[$index][$key] = $value; } } else { if ($table != "plugin_hmib_hrSWInstalled" && $table != "plugin_hmib_hrSWRun") { $new_array[$index][$key] = isset($types[$value[0]]) ? $types[$value[0]]["id"] : 0; } else { $new_array[$index][$key] = $value[0]; } } } elseif ($key == "date") { $new_array[$index][$key] = hmib_dateParse($mib["value"]); } elseif ($key != "index") { $new_array[$index][$key] = $mib["value"]; } } if (!empty($key) && $key != "index") { debug("Key:'" . $key . "', Orig:'" . $mib["oid"] . "', Val:'" . $new_array[$index][$key] . "', Index:'" . $index . "', Base:'" . $oid . "'"); } } else { echo "WARNING: Error parsing OID value\n"; } } } /* dump the output to the database */ $sql_insert = ""; $count = 0; if (sizeof($new_array)) { foreach ($new_array as $index => $item) { $sql_insert .= (strlen($sql_insert) ? "), (" : "(") . $host["id"] . ", " . $index . ", "; $i = 0; foreach ($tree as $mname => $oid) { if ($mname != "baseOID" && $mname != "index") { $sql_insert .= ($i > 0 ? ", " : "") . (isset($item[$mname]) && strlen(strlen($item[$mname])) ? $cnn_id->qstr($item[$mname]) : "''"); $i++; } } } $sql_insert .= ")"; $count++; if ($count % 200 == 0) { db_execute($sql_prefix . $sql_insert . $sql_suffix); $sql_insert = ""; } } if ($sql_insert != '') { db_execute($sql_prefix . $sql_insert . $sql_suffix); } /* remove old records */ db_execute("DELETE FROM {$table} WHERE present=0 AND host_id=" . $host["id"]); } }
function update_system_mibs($host_id) { $system_mibs = array('snmp_sysDescr' => '.1.3.6.1.2.1.1.1.0', 'snmp_sysObjectID' => '.1.3.6.1.2.1.1.2.0', 'snmp_sysUpTimeInstance' => '.1.3.6.1.2.1.1.3.0', 'snmp_sysContact' => '.1.3.6.1.2.1.1.4.0', 'snmp_sysName' => '.1.3.6.1.2.1.1.5.0', 'snmp_sysLocation' => '.1.3.6.1.2.1.1.6.0'); $h = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($host_id)); if (sizeof($h)) { foreach ($system_mibs as $name => $oid) { $value = cacti_snmp_get($h['hostname'], $h['snmp_community'], $oid, $h['snmp_version'], $h['snmp_username'], $h['snmp_password'], $h['snmp_auth_protocol'], $h['snmp_priv_passphrase'], $h['snmp_priv_protocol'], $h['snmp_context'], $h['snmp_port'], $h['snmp_timeout'], read_config_option('snmp_retries'), SNMP_CMDPHP); if (!empty($value)) { db_execute_prepared("UPDATE host SET {$name} = ? WHERE id = ?", array($value, $host_id)); } } } }
function ping_snmp() { /* initialize variables */ $this->snmp_status = "down"; $this->snmp_response = "Host did not respond to SNMP"; $output = ""; /* get start time */ $this->start_time(); /* poll sysUptime for status */ $retry_count = 0; while (1) { if ($retry_count >= $this->retries) { $this->snmp_status = "down"; $this->snmp_response = "Host did not respond to SNMP"; return false; } $output = cacti_snmp_get($this->host["hostname"], $this->host["snmp_community"], ".1.3.6.1.2.1.1.3.0" , $this->host["snmp_version"], $this->host["snmp_username"], $this->host["snmp_password"], $this->host["snmp_port"], $this->host["snmp_timeout"], SNMP_CMDPHP); /* determine total time +- ~10% */ $this->time = $this->get_time($this->precision); /* check result for uptime */ if (!empty($output)) { /* calculte total time */ $this->snmp_status = $this->time*1000; $this->snmp_response = "Host responded to SNMP"; return true; } $retry_count++; } } /* ping_snmp */
function xform_enterasys_N7_vlan_associations(&$device, $snmp_readstring = "") { /* get raw index data */ if ($snmp_readstring == "") { $snmp_readstring = $device["snmp_readstring"]; } /* initialize the output array */ $output_array = array(); /* obtain vlan associations: dot1qTpFdbStatus from dot1qTpFdbTable */ $xformArray = cacti_snmp_walk($device["hostname"], $snmp_readstring, ".1.3.6.1.2.1.17.7.1.2.2.1.2", $device["snmp_version"], "", "", "", "", "", "", $device["snmp_port"], $device["snmp_timeout"]); $i = 0; foreach ($xformArray as $xformItem) { //print_r($xformItem); /* peel off the beginning of the OID */ $key = $xformItem["oid"]; //print ("========= key=[$key]"); $key = str_replace("iso", "1", $key); $key = str_replace("1.3.6.1.2.1.17.7.1.2.2.1.2.", "", $key); //print ("========= key=[$key]\n"); /* now grab the VLAN Id */ $perPos = strpos($key, ".", 1); //print ("========= perPos=[$perPos]\n"); $output_array[$i]["vlan_id"] = substr($key, 1, $perPos - 1); //print ("========= i=[$i] [" . $output_array[$i]["vlan_id"] . "]\n"); /* save the key=MAC Address for association with the dot1d table */ $output_array[$i]["key"] = substr($key, $perPos); /* get VLAN name, if any: dot1qVlanStaticName from dot1qVlanStaticTable */ $vlan_name = @cacti_snmp_get($device["hostname"], $snmp_readstring, ".1.3.6.1.2.1.17.7.1.4.3.1.1." . $output_array[$i]["vlan_id"], $device["snmp_version"], "", "", "", "", "", "", $device["snmp_port"], $device["snmp_timeout"]); $output_array[$i]["vlan_name"] = $vlan_name; //print ("========= i=[$i] [" . $output_array[$i]["vlan_id"] . "] name=[" . $output_array[$i]["vlan_name"] . "]\n"); $i++; } return array_rekey($output_array, "key", "vlan_id"); #return $output_array; }
for ($i=0;($i<sizeof($arr_index));$i++) { print $arr_index[$i] . "!" . $arr[$i] . "\n"; } }elseif ($cmd == "get") { $arg = $_SERVER["argv"][10]; $index = $_SERVER["argv"][11]; if (($arg == "total") || ($arg == "used")) { /* get hrStorageAllocationUnits from the snmp cache since it is faster */ $host_id = db_fetch_cell("select id from host where hostname='$hostname' and snmp_community='$snmp_community'"); $sau = db_fetch_cell("select field_value from host_data_query_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and index_value='$index'"); print (cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmpv3_auth_protocol, $snmpv3_priv_passphrase, $snmpv3_priv_protocol, 161, 1000) * $sau); }else{ print (cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, $snmpv3_auth_username, $snmpv3_auth_password, $snmpv3_auth_protocol, $snmpv3_priv_passphrase, $snmpv3_priv_protocol, 161, 1000)); } } function reindex($arr) { $return_arr = array(); for ($i=0;($i<sizeof($arr));$i++) { $return_arr[$i] = $arr[$i]["value"]; } return $return_arr; } ?>
} } } } $new_host = false; $last_host = $current_host; } if (!$host_down) { switch ($item["action"]) { case POLLER_ACTION_SNMP: /* snmp */ if ($item["snmp_version"] == 0 || $item["snmp_community"] == "" && $item["snmp_version"] != 3) { cacti_log("Host[{$host_id}] DS[{$data_source}] ERROR: Invalid SNMP Data Source. Please either delete it from the database, or correct it.", $print_data_to_stdout); $output = "U"; } else { $output = cacti_snmp_get($item["hostname"], $item["snmp_community"], $item["arg1"], $item["snmp_version"], $item["snmp_username"], $item["snmp_password"], $item["snmp_auth_protocol"], $item["snmp_priv_passphrase"], $item["snmp_priv_protocol"], $item["snmp_context"], $item["snmp_port"], $item["snmp_timeout"], read_config_option("snmp_retries"), SNMP_CMDPHP); /* remove any quotes from string */ $output = strip_quotes($output); if (!validate_result($output)) { if (strlen($output) > 20) { $strout = 20; } else { $strout = strlen($output); } cacti_log("Host[{$host_id}] DS[{$data_source}] WARNING: Result from SNMP not valid. Partial Result: " . substr($output, 0, $strout), $print_data_to_stdout); $output = "U"; } } if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_MEDIUM) { cacti_log("Host[{$host_id}] DS[{$data_source}] SNMP: v" . $item["snmp_version"] . ": " . $item["hostname"] . ", dsname: " . $item["rrd_name"] . ", oid: " . $item["arg1"] . ", output: {$output}", $print_data_to_stdout); }
function query_snmp_host($host_id, $snmp_query_id) { global $config; include_once $config["library_path"] . "/snmp.php"; $host = db_fetch_row("SELECT\n\t\thostname,\n\t\tsnmp_community,\n\t\tsnmp_version,\n\t\tsnmp_username,\n\t\tsnmp_password,\n\t\tsnmp_auth_protocol,\n\t\tsnmp_priv_passphrase,\n\t\tsnmp_priv_protocol,\n\t\tsnmp_context,\n\t\tsnmp_port,\n\t\tsnmp_timeout,\n\t\tping_retries,\n\t\tmax_oids\n\t\tFROM host\n\t\tWHERE id='{$host_id}'"); $snmp_queries = get_data_query_array($snmp_query_id); if ($host["hostname"] == "") { debug_log_insert("data_query", "Invalid host_id: {$host_id}"); return false; } /* invalid xml check */ if (!is_array($snmp_queries) || sizeof($snmp_queries) == 0) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } debug_log_insert("data_query", "XML file parsed ok."); if (!verify_index_order($snmp_queries)) { debug_log_insert("data_query", "Invalid field <index_order>" . $snmp_queries["index_order"] . "</index_order>"); debug_log_insert("data_query", "Must contain <direction>input</direction> fields only"); return false; } /* provide data for oid_num_indexes, if given */ if (isset($snmp_queries["oid_num_indexes"])) { $snmp_num_indexes = cacti_snmp_get($host["hostname"], $host["snmp_community"], $snmp_queries["oid_num_indexes"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], $host["ping_retries"], $host["max_oids"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP get for num of indexes @ '" . $snmp_queries["oid_num_indexes"] . "' Index Count: " . $snmp_num_indexes); } else { debug_log_insert("data_query", "<oid_num_indexes> missing in XML file, 'Index Count Changed' emulated by counting oid_index entries"); } /* fetch specified index at specified OID */ $snmp_indexes = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $snmp_queries["oid_index"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], $host["ping_retries"], $host["max_oids"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for list of indexes @ '" . $snmp_queries["oid_index"] . "' Index Count: " . sizeof($snmp_indexes)); /* no data found; get out */ if (!sizeof($snmp_indexes)) { debug_log_insert("data_query", "No SNMP data returned"); return false; } else { /* show list of indices found */ for ($i = 0; $i < sizeof($snmp_indexes); $i++) { debug_log_insert("data_query", "Index found at OID: '" . $snmp_indexes[$i]["oid"] . "' value: '" . $snmp_indexes[$i]["value"] . "'"); } } /* the last octet of the oid is the index by default */ $index_parse_regexp = '.*\\.([0-9]+)$'; /* parse the index if required */ if (isset($snmp_queries["oid_index_parse"])) { $index_parse_regexp = str_replace("OID/REGEXP:", "", $snmp_queries["oid_index_parse"]); for ($i = 0; $i < sizeof($snmp_indexes); $i++) { $snmp_indexes[$i]["value"] = preg_replace('/' . $index_parse_regexp . '/', "\\1", $snmp_indexes[$i]["oid"]); debug_log_insert("data_query", "index_parse at OID: '" . $snmp_indexes[$i]["oid"] . "' results: '" . $snmp_indexes[$i]["value"] . "'"); } } /* set an array to host all updates */ $output_array = array(); while (list($field_name, $field_array) = each($snmp_queries["fields"])) { if (!isset($field_array["oid"]) && $field_array["source"] == "index") { for ($i = 0; $i < sizeof($snmp_indexes); $i++) { debug_log_insert("data_query", "Inserting index data for field '" . $field_name . "' [value='" . $snmp_indexes[$i]["value"] . "']"); $output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $snmp_indexes[$i]["value"], $snmp_indexes[$i]["value"], ''); } } else { if ($field_array["method"] == "get" && $field_array["direction"] == "input") { debug_log_insert("data_query", "Located input field '{$field_name}' [get]"); if ($field_array["source"] == "value") { for ($i = 0; $i < sizeof($snmp_indexes); $i++) { $oid = $field_array["oid"] . "." . $snmp_indexes[$i]["value"]; $oid .= isset($field_array["oid_suffix"]) ? "." . $field_array["oid_suffix"] : ""; $value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP get for data @ '{$oid}' [value='{$value}']"); $output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $value, $snmp_indexes[$i]["value"], $oid); } } } else { if ($field_array["method"] == "walk" && $field_array["direction"] == "input") { debug_log_insert("data_query", "Located input field '{$field_name}' [walk]"); $snmp_data = array(); $snmp_data = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $field_array["oid"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], $host["ping_retries"], $host["max_oids"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for data @ '" . $field_array["oid"] . "'"); if ($field_array["source"] == "value") { for ($i = 0; $i < sizeof($snmp_data); $i++) { $snmp_index = preg_replace('/' . (isset($field_array["oid_index_parse"]) ? $field_array["oid_index_parse"] : $index_parse_regexp) . '/', "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . ".{$snmp_index}"; if ($field_name == "ifOperStatus") { if (substr_count(strtolower($snmp_data[$i]["value"]), "down") || $snmp_data[$i]["value"] == "2") { $snmp_data[$i]["value"] = "Down"; } else { if (substr_count(strtolower($snmp_data[$i]["value"]), "up") || $snmp_data[$i]["value"] == "1") { $snmp_data[$i]["value"] = "Up"; } else { if (substr_count(strtolower($snmp_data[$i]["value"]), "notpresent") || $snmp_data[$i]["value"] == "6") { $snmp_data[$i]["value"] = "notPresent"; } else { $snmp_data[$i]["value"] = "Testing"; } } } } debug_log_insert("data_query", "Found item [{$field_name}='" . $snmp_data[$i]["value"] . "'] index: {$snmp_index} [from value]"); $output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $snmp_data[$i]["value"], $snmp_index, $oid); } } elseif (substr($field_array["source"], 0, 11) == "OID/REGEXP:") { for ($i = 0; $i < sizeof($snmp_data); $i++) { $value = preg_replace('/' . str_replace("OID/REGEXP:", "", $field_array["source"]) . '/', "\\1", $snmp_data[$i]["oid"]); if (isset($snmp_queries["oid_index_parse"])) { $snmp_index = preg_replace('/' . $index_parse_regexp . '/', "\\1", $snmp_data[$i]["oid"]); } else { if (isset($snmp_data[$i]["value"]) && $snmp_data[$i]["value"] != "") { $snmp_index = $snmp_data[$i]["value"]; } } /* correct bogus index value */ /* found in some devices such as an EMC Cellera */ if ($snmp_index == 0) { $snmp_index = 1; } $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [{$field_name}='{$value}'] index: {$snmp_index} [from regexp oid parse]"); $output_array[] = data_query_format_record($host_id, $snmp_query_id, $field_name, $value, $snmp_index, $oid); } } elseif (substr($field_array["source"], 0, 13) == "VALUE/REGEXP:") { for ($i = 0; $i < sizeof($snmp_data); $i++) { $value = preg_replace('/' . str_replace("VALUE/REGEXP:", "", $field_array["source"]) . '/', "\\1", $snmp_data[$i]["value"]); $snmp_index = preg_replace('/' . (isset($field_array["oid_index_parse"]) ? $field_array["oid_index_parse"] : $index_parse_regexp) . '/', "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . "." . $snmp_index; debug_log_insert("data_query", "Found item [{$field_name}='{$value}'] index: {$snmp_index} [from regexp value parse]"); $output_array[] = "('{$host_id}', '{$snmp_query_id}', '{$field_name}', '{$value}', '{$snmp_index}', '{$oid}', '1')"; } } } } } } data_query_update_host_cache_from_buffer($host_id, $snmp_query_id, $output_array); return true; }
} } } } $new_host = false; $last_host = $current_host; } if (!$host_down) { switch ($item['action']) { case POLLER_ACTION_SNMP: /* snmp */ if ($item['snmp_version'] == 0 || $item['snmp_community'] == '' && $item['snmp_version'] != 3) { cacti_log("Device[{$host_id}] DS[{$data_source}] ERROR: Invalid SNMP Data Source. Please either delete it from the database, or correct it.", $print_data_to_stdout); $output = 'U'; } else { $output = cacti_snmp_get($item['hostname'], $item['snmp_community'], $item['arg1'], $item['snmp_version'], $item['snmp_username'], $item['snmp_password'], $item['snmp_auth_protocol'], $item['snmp_priv_passphrase'], $item['snmp_priv_protocol'], $item['snmp_context'], $item['snmp_port'], $item['snmp_timeout'], read_config_option('snmp_retries'), SNMP_CMDPHP); /* remove any quotes from string */ $output = strip_quotes($output); if (!validate_result($output)) { if (strlen($output) > 20) { $strout = 20; } else { $strout = strlen($output); } cacti_log("Device[{$host_id}] DS[{$data_source}] WARNING: Result from SNMP not valid. Partial Result: " . substr($output, 0, $strout), $print_data_to_stdout); $output = 'U'; } } if (read_config_option('log_verbosity') >= POLLER_VERBOSITY_MEDIUM) { cacti_log("Device[{$host_id}] DS[{$data_source}] SNMP: v" . $item['snmp_version'] . ': ' . $item['hostname'] . ', dsname: ' . $item['rrd_name'] . ', oid: ' . $item['arg1'] . ", output: {$output}", $print_data_to_stdout); }
for ($i=0;($i<sizeof($arr_index));$i++) { print $arr_index[$i] . "!" . $arr[$i] . "\n"; } }elseif ($cmd == "get") { $arg = $_SERVER["argv"][5]; $index = $_SERVER["argv"][6]; if (($arg == "total") || ($arg == "used")) { /* get hrStorageAllocationUnits from the snmp cache since it is faster */ $host_id = db_fetch_cell("select id from host where hostname='$hostname' and snmp_community='$snmp_community'"); $sau = db_fetch_cell("select field_value from host_snmp_cache where host_id=$host_id and field_name='hrStorageAllocationUnits' and snmp_index='$index'"); print (cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, "", "", "", 161, 1000) * $sau); }else{ print (cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version, "", "", "", 161, 1000)); } } function reindex($arr) { $return_arr = array(); for ($i=0;($i<sizeof($arr));$i++) { $return_arr[$i] = $arr[$i]["value"]; } return $return_arr; } ?>
/** * execute an SNMP query for a given device * @param int $device_id * @param int $snmp_query_id */ function query_snmp_device($device_id, $snmp_query_id) { global $config; include_once(CACTI_BASE_PATH . "/lib/snmp.php"); $device = db_fetch_row("SELECT hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout, ping_retries, max_oids FROM device WHERE id='$device_id'"); $snmp_queries = get_data_query_array($snmp_query_id); if ($device["hostname"] == "") { debug_log_insert("data_query", __("Invalid device_id:") . " $device_id"); return false; } /* invalid xml check */ if ((!is_array($snmp_queries)) || (sizeof($snmp_queries) == 0)) { debug_log_insert("data_query", __("Error parsing XML file into an array.")); return false; } debug_log_insert("data_query", __("XML file parsed ok.")); /* fetch specified index at specified OID */ $snmp_indexes = cacti_snmp_walk($device["hostname"], $device["snmp_community"], $snmp_queries["oid_index"], $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["ping_retries"], $device["max_oids"], SNMP_WEBUI); debug_log_insert("data_query", __("Executing SNMP walk for list of indexes @ '%s'", $snmp_queries["oid_index"])); /* no data found; get out */ if (!$snmp_indexes) { debug_log_insert("data_query", __("No SNMP data returned")); return false; } else { /* show list of indices found */ for ($i=0; $i<sizeof($snmp_indexes); $i++) { debug_log_insert("data_query", __("Index found at OID: '%s' value: '%s'", $snmp_indexes[$i]["oid"], $snmp_indexes[$i]["value"])); } } /* the last octet of the oid is the index by default */ $index_parse_regexp = '/.*\.([0-9]+)$/'; /* parse the index if required */ if (isset($snmp_queries["oid_index_parse"])) { $index_parse_regexp = str_replace("OID/REGEXP:", "", $snmp_queries["oid_index_parse"]); for ($i=0; $i<sizeof($snmp_indexes); $i++) { $snmp_indexes[$i]["value"] = preg_replace($index_parse_regexp, "\\1", $snmp_indexes[$i]["oid"]); debug_log_insert("data_query", __("index_parse at OID: '%s' results: '%s'", $snmp_indexes[$i]["oid"], $snmp_indexes[$i]["value"])); } } /* set an array to host all updates */ $output_array = array(); $fields_processed = array(); rewrite_snmp_enum_value(NULL); while (list($field_name, $field_array) = each($snmp_queries["fields"])) { if ($field_array["direction"] == "input" && $field_array["method"] != "get" && (isset($field_array["rewrite_index"]) || isset($field_array["oid_suffix"]) )){ #$field_array["method"] = "get"; # do NOT change method, only create error message debug_log_insert("data_query", __("FIXME: wrong 'method' defined for '%s' while using 'rewrite_index' or 'oid_suffix'. Please change XML to use 'get' instead.", $field_name)); } $values = array(); if ((!isset($field_array["oid"])) && ($field_array["source"] == "index")) { for ($i=0; $i<sizeof($snmp_indexes); $i++) { debug_log_insert("data_query", __("Inserting index data for field '%s' [value='%s']", $field_name, $snmp_indexes[$i]["value"])); $values[] = array( "value" => $snmp_indexes[$i]["value"], "index" => $snmp_indexes[$i]["value"], "oid" => $oid); } }elseif (($field_array["method"] == "get") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", __("Located input field '%s' [get]", $field_name)); $rewritten_indexes = array(); if (isset($field_array["rewrite_index"])) { $rewritten_indexes = data_query_rewrite_indexes($errmsg, $device_id, $snmp_query_id, $field_array["rewrite_index"], $snmp_indexes, $fields_processed); if(sizeof($errmsg)){ foreach($errmsg as $message){ debug_log_insert("data_query", "Field '$field_name'" . $message); } } } for ($i=0; $i<sizeof($snmp_indexes); $i++) { $oid = $field_array["oid"]; if (isset($field_array["rewrite_index"])) { if (isset($rewritten_indexes[$snmp_indexes[$i]["value"]])) { $oid_suffix = $rewritten_indexes[$snmp_indexes[$i]["value"]]; }else{ // we failed to build rewritten index. warnings are sent already, just skip this index silently continue; } $oid .= "." . $oid_suffix; } else { $oid .= "." . $snmp_indexes[$i]["value"]; } $oid .= isset($field_array["oid_suffix"]) ? ("." . $field_array["oid_suffix"]) : ""; $value = NULL; if (substr($field_array["source"], 0, 13) == "VALUE/REGEXP:" || $field_array["source"] == "value") { $value = cacti_snmp_get($device["hostname"], $device["snmp_community"], $oid, $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], SNMP_WEBUI); if (substr($field_array["source"], 0, 13) == "VALUE/REGEXP:") { $value = preg_replace("/" . str_replace("VALUE/REGEXP:", "", $field_array["source"]) . "/", "\\1", $value); } } else if(substr($field_array["source"], 0, 11) == "OID/REGEXP:") { $value = preg_replace("/" . str_replace("OID/REGEXP:", "", $field_array["source"]) . "/", "\\1", $oid); } debug_log_insert("data_query", __("Executing SNMP get for data") . " @ '$oid' [value='$value']"); $values[] = array( "value" => $value, "index" => $snmp_indexes[$i]["value"], "oid" => $oid); } } elseif (($field_array["method"] == "walk") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", __("Located input field '%s' [walk]", $field_name)); $snmp_data = array(); $snmp_data = cacti_snmp_walk($device["hostname"], $device["snmp_community"], $field_array["oid"], $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["ping_retries"], $device["max_oids"], SNMP_WEBUI); debug_log_insert("data_query", __("Executing SNMP walk for data @ '%s'", $field_array["oid"])); if ($field_array["source"] == "value") { for ($i=0; $i<sizeof($snmp_data); $i++) { # Is this preg_replace functional? - JPP $snmp_index = preg_replace(isset($field_array["oid_index_parse"]) ? $field_array["oid_index_parse"] : $index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . ".$snmp_index"; # deprecated in favour of "<rewrite_value>", bug#0001225 #if ($field_name == "ifOperStatus") { # if ((substr_count(strtolower($snmp_data[$i]["value"]), "down")) || # ($snmp_data[$i]["value"] == "2")) { # $snmp_data[$i]["value"] = __("Down"); # }else if ((substr_count(strtolower($snmp_data[$i]["value"]), "up")) || # ($snmp_data[$i]["value"] == "1")) { # $snmp_data[$i]["value"] = __("Up"); # }else if ((substr_count(strtolower($snmp_data[$i]["value"]), "notpresent")) || # ($snmp_data[$i]["value"] == "6")) { # $snmp_data[$i]["value"] = __("notPresent"); # }else{ # $snmp_data[$i]["value"] = __("Testing"); # } #} debug_log_insert("data_query", __("Found item [$field_name='%s'] index: %s [from value]", $snmp_data[$i]["value"], $snmp_index)); $values[] = array( "value" => $snmp_data[$i]["value"], "index" => $snmp_index, "oid" => $oid); } } elseif (substr($field_array["source"], 0, 11) == "OID/REGEXP:") { for ($i=0; $i<sizeof($snmp_data); $i++) { $value = preg_replace("/" . str_replace("OID/REGEXP:", "", $field_array["source"]) . "/", "\\1", $snmp_data[$i]["oid"]); if (isset($snmp_queries["oid_index_parse"])) { $snmp_index = preg_replace("/" . $index_parse_regexp . "/", "\\1", $snmp_data[$i]["oid"]); }else if ((isset($snmp_data[$i]["value"])) && ($snmp_data[$i]["value"] != "")) { $snmp_index = $snmp_data[$i]["value"]; } $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", __("Found item [%s='%s'] index: %s [from regexp oid parse]", $field_name, $value, $snmp_index)); $values[] = array( "value" => $value, "index" => $snmp_index, "oid" => $oid); } }elseif (substr($field_array["source"], 0, 13) == "VALUE/REGEXP:") { for ($i=0; $i<sizeof($snmp_data); $i++) { $value = preg_replace("/" . str_replace("VALUE/REGEXP:", "", $field_array["source"]) . "/", "\\1", $snmp_data[$i]["value"]); $snmp_index = preg_replace("/" . $index_parse_regexp . "/", "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", __("Found item [%s='%s'] index: %s [from regexp value parse]", $field_name, $value, $snmp_index)); $values[] = array( "value" => $value, "index" => $snmp_index, "oid" => $oid); } } } if (sizeof($values)) { foreach($values as $item){ if(isset($field_array["rewrite_value"])) { $item["value"] = rewrite_snmp_enum_value($field_name, $item["value"], $field_array["rewrite_value"]); } $output_array[] = data_query_format_record($device_id, $snmp_query_id, $field_name, $item["value"], $item["index"], $item["oid"]); } $fields_processed[] = $field_name; } } if (sizeof($output_array)) { data_query_update_device_cache_from_buffer($device_id, $snmp_query_id, $output_array); } return true; }
function thold_update_host_status () { global $config; // Return if we aren't set to notify $deadnotify = (read_config_option('alert_deadnotify') == 'on'); if (!$deadnotify) return 0; include_once($config['base_path'] . '/plugins/thold/thold_functions.php'); if (api_plugin_is_enabled('maint')) { include_once($config["base_path"] . '/plugins/maint/functions.php'); } $alert_email = read_config_option('alert_email'); $ping_failure_count = read_config_option('ping_failure_count'); // Lets find hosts that were down, but are now back up $failed = db_fetch_assoc("SELECT * FROM plugin_thold_host_failed"); if (sizeof($failed)) { foreach($failed as $fh) { if (!empty($fh['host_id'])) { if (api_plugin_is_enabled('maint')) { if (plugin_maint_check_cacti_host ($fh['host_id'])) { continue; } } $host = db_fetch_row('SELECT * FROM host WHERE id = ' . $fh['host_id']); if ($host['status'] == HOST_UP) { $snmp_system = ''; $snmp_hostname = ''; $snmp_location = ''; $snmp_contact = ''; $snmp_uptime = ''; $uptimelong = ''; $downtimemsg = ''; if (($host["snmp_community"] == "" && $host["snmp_username"] == "") || $host["snmp_version"] == 0) { // SNMP not in use $snmp_system = 'SNMP not in use'; } else { $snmp_system = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.1.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); if (substr_count($snmp_system, "00:")) { $snmp_system = str_replace("00:", "", $snmp_system); $snmp_system = str_replace(":", " ", $snmp_system); } if ($snmp_system != "") { $snmp_uptime = cacti_snmp_get($host['hostname'], $host['snmp_community'], ".1.3.6.1.2.1.1.3.0", $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option("snmp_retries"), SNMP_WEBUI); $snmp_hostname = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.5.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); $snmp_location = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.6.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); $snmp_contact = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.4.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); $days = intval($snmp_uptime / (60*60*24*100)); $remainder = $snmp_uptime % (60*60*24*100); $hours = intval($remainder / (60*60*100)); $remainder = $remainder % (60*60*100); $minutes = intval($remainder / (60*100)); $uptimelong = $days . "d " . $hours . "h " . $minutes . "m"; } $downtime = time() - strtotime($host['status_fail_date']); $downtime_days = floor($downtime/86400); $downtime_hours = floor(($downtime - ($downtime_days * 86400))/3600); $downtime_minutes = floor(($downtime - ($downtime_days * 86400) - ($downtime_hours * 3600))/60); $downtime_seconds = $downtime - ($downtime_days * 86400) - ($downtime_hours * 3600) - ($downtime_minutes * 60); if ($downtime_days > 0 ) { $downtimemsg = $downtime_days . "d " . $downtime_hours . "h " . $downtime_minutes . "m " . $downtime_seconds . "s "; } elseif ($downtime_hours > 0 ) { $downtimemsg = $downtime_hours . "h " . $downtime_minutes . "m " . $downtime_seconds . "s"; } elseif ($downtime_minutes > 0 ) { $downtimemsg = $downtime_minutes . "m " . $downtime_seconds . "s"; } else { $downtimemsg = $downtime_seconds . "s "; } } $subject = read_config_option('thold_up_subject'); if ($subject == '') { $subject = 'Host Notice: <DESCRIPTION> (<HOSTNAME>) returned from DOWN state'; } $subject = str_replace('<HOSTNAME>', !$snmp_hostname ? $host['hostname'] : $snmp_hostname, $subject); $subject = str_replace('<DESCRIPTION>', $host['description'], $subject); $subject = str_replace('<DOWN/UP>', 'UP', $subject); $subject = strip_tags($subject); $msg = read_config_option('thold_up_text'); if ($msg == '') { $msg = '<br>System <DESCRIPTION> (<HOSTNAME>) status: <DOWN/UP><br><br>Current ping response: <CUR_TIME> ms<br>Average system response : <AVG_TIME> ms<br>System availability: <AVAILABILITY><br>Total Checks Since Clear: <TOT_POLL><br>Total Failed Checks: <FAIL_POLL><br>Last Date Checked UP: <LAST_FAIL><br>Host Previously DOWN for: <DOWNTIME><br><br>Snmp Info:<br>Name - <SNMP_HOSTNAME><br>Location - <SNMP_LOCATION><br>Uptime - <UPTIMETEXT> (<UPTIME> ms)<br>System - <SNMP_SYSTEM><br><br>NOTE: <NOTES>'; } $msg = str_replace('<SUBJECT>', $subject, $msg); $msg = str_replace('<HOSTNAME>', $host['hostname'], $msg); $msg = str_replace('<DESCRIPTION>', $host['description'], $msg); $msg = str_replace('<UPTIME>', $snmp_uptime, $msg); $msg = str_replace('<UPTIMETEXT>', $uptimelong, $msg); $msg = str_replace('<DOWNTIME>', $downtimemsg, $msg); $msg = str_replace('<MESSAGE>', '', $msg); $msg = str_replace('<DOWN/UP>', 'UP', $msg); $msg = str_replace('<SNMP_HOSTNAME>', $snmp_hostname, $msg); $msg = str_replace('<SNMP_LOCATION>', $snmp_location, $msg); $msg = str_replace('<SNMP_CONTACT>', $snmp_contact, $msg); $msg = str_replace('<SNMP_SYSTEM>', html_split_string($snmp_system), $msg); $msg = str_replace('<LAST_FAIL>', $host["status_fail_date"], $msg); $msg = str_replace('<AVAILABILITY>', round(($host["availability"]), 2) . ' %', $msg); $msg = str_replace('<TOT_POLL>', $host["total_polls"], $msg); $msg = str_replace('<FAIL_POLL>', $host["failed_polls"], $msg); $msg = str_replace('<CUR_TIME>', round(($host["cur_time"]), 2), $msg); $msg = str_replace('<AVG_TIME>', round(($host["avg_time"]), 2), $msg); $msg = str_replace('<NOTES>', $host["notes"], $msg); $msg = str_replace("\n", '<br>', $msg); switch($host['thold_send_email']) { case '0': // Disabled $alert_email = ''; break; case '1': // Global List break; case '2': // Host List Only $alert_email = get_thold_notification_emails($host['thold_host_email']); break; case '3': // Global and Host List $alert_email = $alert_email . ',' . get_thold_notification_emails($host['thold_host_email']); break; } if ($alert_email == '' && $host['thold_send_email'] > 0) { cacti_log('THOLD: Can not send a Host Recovering Email since the \'Alert Email\' setting is not set !', true, 'POLLER'); } elseif ($host['thold_send_email'] == '0') { cacti_log('THOLD: Did not send a Host Recovering Email, disabled per host setting : ' . $host['description'] . ' !', true, 'POLLER'); } elseif ($alert_email != '') { thold_mail($alert_email, '', $subject, $msg, ''); } thold_log(array( "type" => 3, "time" => time(), "host_id" => $host['id'], "graph_id" => 0, "threshold_id" => 0, "threshold_value" => 'Up/Down', "current" => 'Up', "status" => 5, "description" => 'Host Notice: '.$host['hostname'].' returned from DOWN state', "emails" => $alert_email ) ); } } } } // Lets find hosts that are down $hosts = db_fetch_assoc('SELECT * FROM host WHERE disabled="" AND status=' . HOST_DOWN . ' AND status_event_count=' . $ping_failure_count); $total_hosts = sizeof($hosts); if (count($hosts)) { foreach($hosts as $host) { if (api_plugin_is_enabled('maint')) { if (plugin_maint_check_cacti_host ($host['id'])) { continue; } } $downtime = time() - strtotime($host['status_rec_date']); $downtime_days = floor($downtime/86400); $downtime_hours = floor(($downtime - ($downtime_days * 86400))/3600); $downtime_minutes = floor(($downtime - ($downtime_days * 86400) - ($downtime_hours * 3600))/60); $downtime_seconds = $downtime - ($downtime_days * 86400) - ($downtime_hours * 3600) - ($downtime_minutes * 60); if ($downtime_days > 0 ) { $downtimemsg = $downtime_days . "d " . $downtime_hours . "h " . $downtime_minutes . "m " . $downtime_seconds . "s "; } elseif ($downtime_hours > 0 ) { $downtimemsg = $downtime_hours . "h " . $downtime_minutes . "m " . $downtime_seconds . "s"; } elseif ($downtime_minutes > 0 ) { $downtimemsg = $downtime_minutes . "m " . $downtime_seconds . "s"; } else { $downtimemsg = $downtime_seconds . "s "; } /*$subject = read_config_option('thold_down_subject'); if ($subject == '') { $subject = 'Host Error: <DESCRIPTION> (<HOSTNAME>) is DOWN'; } $subject = str_replace('<HOSTNAME>', $host['hostname'], $subject); */ $subject = read_config_option('thold_down_subject'); if ($subject == '') { $subject = 'Host Error: <DESCRIPTION> (<HOSTNAME>) is DOWN'; } $snmpsys = db_fetch_assoc('SELECT snmp_sysName, snmp_sysLocation, snmp_sysDescr FROM mac_track_devices WHERE hostname = \''.$host['hostname'].'\''); $subject = str_replace('<HOSTNAME>', !$snmpsys ? $host['hostname'] : $snmpsys[0]['snmp_sysName'], $subject); $subject = str_replace('<DESCRIPTION>', $host['description'], $subject); $subject = str_replace('<DOWN/UP>', 'DOWN', $subject); $subject = strip_tags($subject); $msg = read_config_option('thold_down_text'); if ($msg == '') { $msg = 'System Error : <DESCRIPTION> (<HOSTNAME>) is <DOWN/UP><br>Reason: <MESSAGE><SNMP_HOSTNAME><SNMP_LOCATION><SNMP_SYSTEM><br><br>Average system response : <AVG_TIME> ms<br>System availability: <AVAILABILITY><br>Total Checks Since Clear: <TOT_POLL><br>Total Failed Checks: <FAIL_POLL><br>Last Date Checked DOWN : <LAST_FAIL><br>Host Previously UP for: <DOWNTIME><br>NOTE: <NOTES>'; } $msg = str_replace('<SUBJECT>', $subject, $msg); $msg = str_replace('<HOSTNAME>', $host['hostname'], $msg); $msg = str_replace('<DESCRIPTION>', $host['description'], $msg); $msg = str_replace('<UPTIME>', '', $msg); $msg = str_replace('<DOWNTIME>', $downtimemsg, $msg); $msg = str_replace('<MESSAGE>', $host['status_last_error'], $msg); $msg = str_replace('<DOWN/UP>', 'DOWN', $msg); $msg = str_replace('<SNMP_HOSTNAME>', !$snmpsys ? '' : '<br><br>Device name: '.$snmpsys[0]['snmp_sysName'], $msg); $msg = str_replace('<SNMP_LOCATION>', !$snmpsys ? '' : '<br>Location: '.$snmpsys[0]['snmp_sysLocation'], $msg); $msg = str_replace('<SNMP_CONTACT>', '', $msg); $msg = str_replace('<SNMP_SYSTEM>', !$snmpsys ? '' : '<br>Description: '.$snmpsys[0]['snmp_sysDescr'], $msg); $msg = str_replace('<LAST_FAIL>', $host["status_fail_date"], $msg); $msg = str_replace('<AVAILABILITY>', round(($host["availability"]), 2) . ' %', $msg); $msg = str_replace('<CUR_TIME>', round(($host["cur_time"]), 2), $msg); $msg = str_replace('<TOT_POLL>', $host["total_polls"], $msg); $msg = str_replace('<FAIL_POLL>', $host["failed_polls"], $msg); $msg = str_replace('<AVG_TIME>', round(($host["avg_time"]), 2), $msg); $msg = str_replace('<NOTES>', $host["notes"], $msg); $msg = str_replace("\n", '<br>', $msg); switch($host['thold_send_email']) { case '0': // Disabled $alert_email = ''; break; case '1': // Global List break; case '2': // Host List Only $alert_email = get_thold_notification_emails($host['thold_host_email']); break; case '3': // Global and Host List $alert_email = $alert_email . ',' . get_thold_notification_emails($host['thold_host_email']); break; } if ($alert_email == '' && $host['thold_send_email'] > 0) { cacti_log('THOLD: Can not send a Host Down Email since the \'Alert Email\' setting is not set !', true, 'POLLER'); } elseif ($host['thold_send_email'] == '0') { cacti_log('THOLD: Did not send a Host Down Email, disabled per host setting : ' . $host['description'] . ' !', true, 'POLLER'); } elseif ($alert_email != '') { thold_mail($alert_email, '', $subject, $msg, ''); } thold_log(array( "type" => 3, "time" => time(), "host_id" => $host['id'], "graph_id" => 0, "threshold_id" => 0, "threshold_value" => 'Up/Down', "current" => 'Down', "status" => 4, "description" => 'Host Error: '.$host['hostname'].' is DOWN', "emails" => $alert_email ) ); } } // Now lets record all failed hosts db_execute("TRUNCATE TABLE plugin_thold_host_failed"); $hosts = db_fetch_assoc('SELECT id FROM host WHERE disabled="" AND status!=' . HOST_UP); $failed = ''; if (sizeof($hosts)) { foreach ($hosts as $host) { if (api_plugin_is_enabled('maint')) { if (plugin_maint_check_cacti_host ($host['id'])) { continue; } } $failed .= (strlen($failed) ? "), (":"(") . $host['id']; } $failed .= ")"; db_execute("INSERT INTO plugin_thold_host_failed (host_id) VALUES $failed"); } return $total_hosts; }
function get_IOS_dot1dTpFdbEntry_ports($site, &$device, $lowPort = 0, $highPort = 0) { global $debug, $scan_date; /* initialize port counters */ $device["ports_total"] = 0; $device["ports_active"] = 0; $device["ports_trunk"] = 0; $device["vlans_total"] = 0; /* Variables to determine VLAN information */ $vlan_ids = xform_standard_indexed_data(".1.3.6.1.4.1.9.9.46.1.3.1.1.2", $device); $vlan_names = xform_standard_indexed_data(".1.3.6.1.4.1.9.9.46.1.3.1.1.4", $device); $vlan_trunkstatus = xform_standard_indexed_data(".1.3.6.1.4.1.9.9.46.1.6.1.1.14", $device); $device["vlans_total"] = sizeof($vlan_ids) - 3; mactrack_debug("There are " . (sizeof($vlan_ids) - 3) . " VLANS."); /* get the ifIndexes for the device */ $ifIndexes = xform_standard_indexed_data(".1.3.6.1.2.1.2.2.1.1", $device); mactrack_debug("ifIndexes data collection complete"); $ifInterfaces = build_InterfacesTable($device, $ifIndexes, TRUE, TRUE); /* get the Voice VLAN information if it exists */ $portVoiceVLANs = xform_standard_indexed_data(".1.3.6.1.4.1.9.9.87.1.4.1.1.37.0", $device); if (sizeof($portVoiceVLANs) > 0) { $vvlans = TRUE; } else { $portVoiceVLANs = xform_standard_indexed_data(".1.3.6.1.4.1.9.9.68.1.5.1.1.1", $device); if (sizeof($portVoiceVLANs) > 0) { $vvlans = TRUE; } else { $vvlans = FALSE; } } mactrack_debug("Cisco Voice VLAN collection complete"); if ($vvlans) { mactrack_debug("Voice VLANs exist on this device"); } else { mactrack_debug("Voice VLANs do not exist on this device"); } if (sizeof($ifIndexes)) { foreach ($ifIndexes as $ifIndex) { $ifInterfaces[$ifIndex]["trunkPortState"] = @$vlan_trunkstatus[$ifIndex]; if ($vvlans) { $ifInterfaces[$ifIndex]["vVlanID"] = @$portVoiceVLANs[$ifIndex]; } if ($ifInterfaces[$ifIndex]["ifType"] == 6) { $device["ports_total"]++; } if ($ifInterfaces[$ifIndex]["trunkPortState"] == "1") { $device["ports_trunk"]++; } } } mactrack_debug("ifInterfaces assembly complete."); /* build VLAN array from results */ $i = 0; $j = 0; $active_vlans = array(); if (sizeof($vlan_ids)) { foreach ($vlan_ids as $vlan_number => $vlanStatus) { $vlanName = $vlan_names[$vlan_number]; if ($vlanStatus == 1) { /* vlan is operatinal */ switch ($vlan_number) { case "1002": case "1003": case "1004": case "1005": $active_vlan_ports = 0; break; default: if ($device["snmp_version"] < "3") { $snmp_readstring = $device["snmp_readstring"] . "@" . $vlan_number; $active_vlan_ports = cacti_snmp_get($device["hostname"], $snmp_readstring, ".1.3.6.1.2.1.17.1.2.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]); } else { $active_vlan_ports = cacti_snmp_get($device["hostname"], $snmp_readstring, ".1.3.6.1.2.1.17.1.2.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $vlan_number, $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]); } if (!is_numeric($active_vlan_ports) || $active_vlan_ports < 0) { $active_vlan_ports = 0; } mactrack_debug("VLAN Analysis for VLAN: " . $vlan_number . "/" . $vlanName . " is complete. ACTIVE PORTS: " . $active_vlan_ports); if ($active_vlan_ports > 0) { /* does the vlan have active ports on it */ $active_vlans[$j]["vlan_id"] = $vlan_number; $active_vlans[$j]["vlan_name"] = $vlanName; $active_vlans[$j]["active_ports"] = $active_vlan_ports; $active_vlans++; $j++; } } } $i++; } } if (sizeof($active_vlans)) { $i = 0; /* get the port status information */ foreach ($active_vlans as $active_vlan) { if ($device["snmp_version"] < "3") { $snmp_readstring = $device["snmp_readstring"] . "@" . $active_vlan["vlan_id"]; } else { $snmp_readstring = "cisco@" . $active_vlan["vlan_id"]; } mactrack_debug("Processing has begun for VLAN: " . $active_vlan["vlan_id"]); if ($highPort == 0) { $active_vlans[$i]["port_results"] = get_base_dot1dTpFdbEntry_ports($site, $device, $ifInterfaces, $snmp_readstring, FALSE); } else { $active_vlans[$i]["port_results"] = get_base_dot1dTpFdbEntry_ports($site, $device, $ifInterfaces, $snmp_readstring, FALSE, $lowPort, $highPort); } /* get bridge port mappings */ /* get bridge port to ifIndex mappings */ mactrack_debug("Bridge port information about to be collected."); mactrack_debug("VLAN_ID: " . $active_vlans[$i]["vlan_id"] . ", VLAN_NAME: " . $active_vlans[$i]["vlan_name"] . ", ACTIVE PORTS: " . sizeof($active_vlans[$i]["port_results"])); if (sizeof($active_vlans[$i]["port_results"]) > 0) { $brPorttoifIndexes[$i] = xform_standard_indexed_data(".1.3.6.1.2.1.17.1.4.1.2", $device, $snmp_readstring); mactrack_debug("Bridge port information collection complete."); } $i++; } $i = 0; $j = 0; $port_array = array(); mactrack_debug("Final cross check's now being performed."); if (sizeof($active_vlans)) { foreach ($active_vlans as $active_vlan) { if (sizeof($active_vlan["port_results"])) { foreach ($active_vlan["port_results"] as $port_result) { $ifIndex = @$brPorttoifIndexes[$j][$port_result["port_number"]]; $ifType = @$ifInterfaces[$ifIndex]["ifType"]; $portNumber = @$ifInterfaces[$ifIndex]["ifName"]; $portName = @$ifInterfaces[$ifIndex]["ifAlias"]; $portTrunk = @$portTrunking[$ifName]; if ($vvlans) { $vVlanID = @$portVoiceVLANs[$ifIndex]; } else { $vVlanID = -1; } $portTrunkStatus = @$ifInterfaces[$ifIndex]["trunkPortState"]; /* only output legitamate end user ports */ if ($ifType == 6) { if ($portTrunkStatus == "2" || empty($portTrunkStatus) || $vVlanID > 0 && $vVlanID <= 1000) { $port_array[$i]["vlan_id"] = $active_vlan["vlan_id"]; $port_array[$i]["vlan_name"] = $active_vlan["vlan_name"]; $port_array[$i]["port_number"] = $portNumber; $port_array[$i]["port_name"] = $portName; $port_array[$i]["mac_address"] = xform_mac_address($port_result["mac_address"]); $device["ports_active"]++; $i++; mactrack_debug("VLAN: " . $active_vlan["vlan_id"] . ", " . "NAME: " . $active_vlan["vlan_name"] . ", " . "PORT: " . $portNumber . ", " . "NAME: " . $portName . ", " . "MAC: " . $port_result["mac_address"]); } } } } $j++; } } /* display completion message */ print "\nINFO: HOST: " . $device["hostname"] . ", TYPE: " . substr($device["snmp_sysDescr"], 0, 40) . ", TOTAL PORTS: " . $device["ports_total"] . ", ACTIVE PORTS: " . $device["ports_active"] . "\n"; $device["last_runmessage"] = "Data collection completed ok"; $device["macs_active"] = sizeof($port_array); db_store_device_port_results($device, $port_array, $scan_date); } else { print "\nINFO: HOST: " . $device["hostname"] . ", TYPE: " . substr($device["snmp_sysDescr"], 0, 40) . ", No active end devices on this device.\n"; $device["snmp_status"] = HOST_UP; $device["last_runmessage"] = "Data collection completed ok. No active end devices on this device."; } return $device; }
function host_edit() { global $colors, $fields_host_edit, $reindex_types; display_output_messages(); if (!empty($_GET["id"])) { $host = db_fetch_row("select * from host where id=" . $_GET["id"]); $header_label = "[edit: " . $host["description"] . "]"; }else{ $header_label = "[new]"; } if (!empty($host["id"])) { ?> <table width="98%" align="center"> <tr> <td class="textInfo" colspan="2"> <?php print $host["description"];?> (<?php print $host["hostname"];?>) </td> </tr> <tr> <td class="textHeader"> SNMP Information<br> <span style="font-size: 10px; font-weight: normal; font-family: monospace;"> <?php if (($host["snmp_community"] == "") && ($host["snmp_username"] == "")) { print "<span style='color: #ab3f1e; font-weight: bold;'>SNMP not in use</span>\n"; }else{ $snmp_system = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.1.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); if ($snmp_system == "") { print "<span style='color: #ff0000; font-weight: bold;'>SNMP error</span>\n"; }else{ $snmp_uptime = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.3.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); $snmp_hostname = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.5.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); print "<strong>System:</strong> $snmp_system<br>\n"; print "<strong>Uptime:</strong> $snmp_uptime<br>\n"; print "<strong>Hostname:</strong> $snmp_hostname<br>\n"; } } ?> </span> </td> <td class="textInfo" valign="top"> <span style="color: #c16921;">*</span><a href="graphs_new.php?host_id=<?php print $host["id"];?>">Create Graphs for this Host</a> </td> </tr> </table> <br> <?php } html_start_box("<strong>Devices</strong> $header_label", "98%", $colors["header"], "3", "center", ""); /* preserve the host template id if passed in via a GET variable */ if (!empty($_GET["host_template_id"])) { $fields_host_edit["host_template_id"]["value"] = $_GET["host_template_id"]; } draw_edit_form(array( "config" => array("form_name" => "chk"), "fields" => inject_form_variables($fields_host_edit, (isset($host) ? $host : array())) )); html_end_box(); if ((isset($_GET["display_dq_details"])) && (isset($_SESSION["debug_log"]["data_query"]))) { html_start_box("<strong>Data Query Debug Information</strong>", "98%", $colors["header"], "3", "center", ""); print "<tr><td><span style='font-family: monospace;'>" . debug_log_return("data_query") . "</span></td></tr>"; html_end_box(); } if (!empty($host["id"])) { html_start_box("<strong>Associated Graph Templates</strong>", "98%", $colors["header"], "3", "center", ""); html_header(array("Graph Template Name", "Status"), 2); $selected_graph_templates = db_fetch_assoc("select graph_templates.id, graph_templates.name from graph_templates,host_graph where graph_templates.id=host_graph.graph_template_id and host_graph.host_id=" . $_GET["id"] . " order by graph_templates.name"); $available_graph_templates = db_fetch_assoc("SELECT graph_templates.id, graph_templates.name FROM snmp_query_graph RIGHT JOIN graph_templates ON snmp_query_graph.graph_template_id = graph_templates.id WHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name"); $i = 0; if (sizeof($selected_graph_templates) > 0) { foreach ($selected_graph_templates as $item) { $i++; /* get status information for this graph template */ $is_being_graphed = (sizeof(db_fetch_assoc("select id from graph_local where graph_template_id=" . $item["id"] . " and host_id=" . $_GET["id"])) > 0) ? true : false; ?> <tr> <td style="padding: 4px;"> <strong><?php print $i;?>)</strong> <?php print $item["name"];?> </td> <td> <?php print (($is_being_graphed == true) ? "<span style='color: green;'>Is Being Graphed</span> (<a href='graphs.php?action=graph_edit&id=" . db_fetch_cell("select id from graph_local where graph_template_id=" . $item["id"] . " and host_id=" . $_GET["id"] . " limit 0,1") . "'>Edit</a>)" : "<span style='color: #484848;'>Not Being Graphed</span>");?> </td> <td align='right' nowrap> <a href='host.php?action=gt_remove&id=<?php print $item["id"];?>&host_id=<?php print $_GET["id"];?>'><img src='images/delete_icon_large.gif' alt='Delete Graph Template Association' border='0' align='absmiddle'></a> </td> </tr> <?php } }else{ print "<tr><td><em>No associated graph templates.</em></td></tr>"; } ?> <tr bgcolor="#<?php print $colors["form_alternate1"];?>"> <td colspan="4"> <table cellspacing="0" cellpadding="1" width="100%"> <td nowrap>Add Graph Template: <?php form_dropdown("graph_template_id",$available_graph_templates,"name","id","","","");?> </td> <td align="right"> <input type="image" src="images/button_add.gif" alt="Add" name="add_gt" align="absmiddle"> </td> </table> </td> </tr> <?php html_end_box(); html_start_box("<strong>Associated Data Queries</strong>", "98%", $colors["header"], "3", "center", ""); html_header(array("Data Query Name", "Debugging", "Re-Index Method", "Status"), 2); $selected_data_queries = db_fetch_assoc("select snmp_query.id, snmp_query.name, host_snmp_query.reindex_method from snmp_query,host_snmp_query where snmp_query.id=host_snmp_query.snmp_query_id and host_snmp_query.host_id=" . $_GET["id"] . " order by snmp_query.name"); $available_data_queries = db_fetch_assoc("select snmp_query.id, snmp_query.name from snmp_query order by snmp_query.name"); $keeper = array(); foreach ($available_data_queries as $item) { if (sizeof(db_fetch_assoc("SELECT snmp_query_id FROM host_snmp_query " . " WHERE ((host_id=" . $_GET["id"] . ")" . " and (snmp_query_id=" . $item["id"] ."))")) > 0) { /* do nothing */ } else { array_push($keeper, $item); } } $available_data_queries = $keeper; $i = 0; if (sizeof($selected_data_queries) > 0) { foreach ($selected_data_queries as $item) { $i++; /* get status information for this data query */ $num_dq_items = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"])); $num_dq_rows = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"] . " group by snmp_index")); $status = "success"; ?> <tr> <td style="padding: 4px;"> <strong><?php print $i;?>)</strong> <?php print $item["name"];?> </td> <td> (<a href="host.php?action=query_verbose&id=<?php print $item["id"];?>&host_id=<?php print $_GET["id"];?>">Verbose Query</a>) </td> <td> <?php print $reindex_types{$item["reindex_method"]};?> </td> <td> <?php print (($status == "success") ? "<span style='color: green;'>Success</span>" : "<span style='color: green;'>Fail</span>");?> [<?php print $num_dq_items;?> Item<?php print ($num_dq_items == 1 ? "" : "s");?>, <?php print $num_dq_rows;?> Row<?php print ($num_dq_rows == 1 ? "" : "s");?>] </td> <td align='right' nowrap> <a href='host.php?action=query_reload&id=<?php print $item["id"];?>&host_id=<?php print $_GET["id"];?>'><img src='images/reload_icon_small.gif' alt='Reload Data Query' border='0' align='absmiddle'></a> <a href='host.php?action=query_remove&id=<?php print $item["id"];?>&host_id=<?php print $_GET["id"];?>'><img src='images/delete_icon_large.gif' alt='Delete Data Query Association' border='0' align='absmiddle'></a> </td> </tr> <?php } }else{ print "<tr><td><em>No associated data queries.</em></td></tr>"; } ?> <tr bgcolor="#<?php print $colors["form_alternate1"];?>"> <td colspan="5"> <table cellspacing="0" cellpadding="1" width="100%"> <td nowrap>Add Data Query: <?php form_dropdown("snmp_query_id",$available_data_queries,"name","id","","","");?> </td> <td nowrap>Re-Index Method: <?php form_dropdown("reindex_method",$reindex_types,"","","1","","");?> </td> <td align="right"> <input type="image" src="images/button_add.gif" alt="Add" name="add_dq" align="absmiddle"> </td> </table> </td> </tr> <?php html_end_box(); } form_save_button("host.php"); }
if (!isset($int['ip'])) { unset($interfaces[$key]); $cleaned++; } else { $interfaces[$key]['nicename'] = isset($int['name']) ? $int['name'] : (isset($int['descr']) ? $int['descr'] : (isset($int['alias']) ? $int['alias'] : "Interface #" . $int['index'])); } } print "Removed {$cleaned} interfaces from search, which have no IP address.\n"; $count = 0; foreach ($interfaces as $key => $int) { if (!isset($int['netmask'])) { $oid = ".1.3.6.1.2.1.4.20.1.3." . $int['ip']; $hostid = $int['host']; if ($count < 100) { print "Fetching Netmask via SNMP for Host " . $int['host'] . "//" . $int['ip'] . " from {$oid}\n"; $result = cacti_snmp_get($hosts[$hostid]["hostname"], $hosts[$hostid]["snmp_community"], $oid, $hosts[$hostid]["snmp_version"], $hosts[$hostid]["snmp_username"], $hosts[$hostid]["snmp_password"], $hosts[$hostid]["snmp_auth_protocol"], $hosts[$hostid]["snmp_priv_passphrase"], $hosts[$hostid]["snmp_priv_protocol"], $hosts[$hostid]["snmp_context"], $hosts[$hostid]["snmp_port"], $hosts[$hostid]["snmp_timeout"], SNMP_WEBUI); if ($result != FALSE && preg_match("/^\\d+.\\d+.\\d+.\\d+\$/", $result)) { print "{$result}|\n"; $interfaces[$key]['netmask'] = $result; } else { print "No useful result.\n"; unset($interfaces[$key]); } $count++; } } } $count = 0; print "Writing Netmask cache...\n"; $fd = fopen("mapper-cache.txt", "w"); foreach ($interfaces as $key => $int) {
} } } $new_host = false; $last_host = $current_host; } if (!$host_down) { switch ($item["action"]) { case POLLER_ACTION_SNMP: /* snmp */ if (($item["snmp_version"] == 0) || (($item["snmp_community"] == "") && ($item["snmp_version"] != 3))) { cacti_log("Host[$host_id] DS[$data_source] ERROR: Invalid SNMP Data Source. Please either delete it from the database, or correct it.", $print_data_to_stdout); $output = "U"; }else { $output = cacti_snmp_get($item["hostname"], $item["snmp_community"], $item["arg1"], $item["snmp_version"], $item["snmp_username"], $item["snmp_password"], $item["snmp_port"], $item["snmp_timeout"], SNMP_CMDPHP); /* remove any quotes from string */ $output = strip_quotes($output); if (!validate_result($output)) { if (strlen($output) > 20) { $strout = 20; } else { $strout = strlen($output); } cacti_log("Host[$host_id] DS[$data_source] WARNING: Result from SNMP not valid. Partial Result: " . substr($output, 0, $strout), $print_data_to_stdout); $output = "U"; } }
function update_reindex_cache($host_id, $data_query_id) { global $config; include_once($config["library_path"] . "/data_query.php"); include_once($config["library_path"] . "/snmp.php"); /* will be used to keep track of sql statements to execute later on */ $recache_stack = array(); $host = db_fetch_row("select hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout from host where id=$host_id"); $data_query = db_fetch_row("select reindex_method, sort_field from host_snmp_query where host_id=$host_id and snmp_query_id=$data_query_id"); $data_query_type = db_fetch_cell("select data_input.type_id from (data_input,snmp_query) where data_input.id=snmp_query.data_input_id and snmp_query.id=$data_query_id"); $data_query_xml = get_data_query_array($data_query_id); switch ($data_query["reindex_method"]) { case DATA_QUERY_AUTOINDEX_NONE: break; case DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME: /* the uptime backwards method requires snmp, so make sure snmp is actually enabled * on this device first */ if ($host["snmp_community"] != "") { if (isset($data_query_xml["oid_uptime"])) { $oid_uptime = $data_query_xml["uptime_oid"]; }else{ $oid_uptime = ".1.3.6.1.2.1.1.3.0"; } $assert_value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid_uptime, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_POLLER); array_push($recache_stack, "insert into poller_reindex (host_id,data_query_id,action,op,assert_value,arg1) values ($host_id,$data_query_id,0,'<','$assert_value','$oid_uptime')"); } break; case DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE: /* this method requires that some command/oid can be used to determine the * current number of indexes in the data query */ $assert_value = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id group by snmp_index")); if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { if (isset($data_query_xml["oid_num_indexes"])) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["oid_num_indexes"] . "')"); } }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { if (isset($data_query_xml["arg_num_indexes"])) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_num_indexes"], $data_query_xml["script_path"], $host_id) . "')"); } } break; case DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION: $primary_indexes = db_fetch_assoc("select snmp_index,oid,field_value from host_snmp_cache where host_id=$host_id and snmp_query_id=$data_query_id and field_name='" . $data_query["sort_field"] . "'"); if (sizeof($primary_indexes) > 0) { foreach ($primary_indexes as $index) { $assert_value = $index["field_value"]; if ($data_query_type == DATA_INPUT_TYPE_SNMP_QUERY) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 0, '=', '$assert_value', '" . $data_query_xml["fields"]{$data_query["sort_field"]}["oid"] . "." . $index["snmp_index"] . "')"); }else if ($data_query_type == DATA_INPUT_TYPE_SCRIPT_QUERY) { array_push($recache_stack, "insert into poller_reindex (host_id, data_query_id, action, op, assert_value, arg1) values ($host_id, $data_query_id, 1, '=', '$assert_value', '" . get_script_query_path((isset($data_query_xml["arg_prepend"]) ? $data_query_xml["arg_prepend"] . " ": "") . $data_query_xml["arg_get"] . " " . $data_query_xml["fields"]{$data_query["sort_field"]}["query_name"] . " " . $index["snmp_index"], $data_query_xml["script_path"], $host_id) . "')"); } } } break; } /* save the delete for last since we need to reference this table in the code above */ db_execute("delete from poller_reindex where host_id=$host_id and data_query_id=$data_query_id"); for ($i=0; $i<count($recache_stack); $i++) { db_execute($recache_stack[$i]); } }
function query_snmp_host($host_id, $snmp_query_id) { global $config; include_once($config["library_path"] . "/snmp.php"); $host = db_fetch_row("SELECT hostname, snmp_community, snmp_version, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, snmp_port, snmp_timeout FROM host WHERE id='$host_id'"); $snmp_queries = get_data_query_array($snmp_query_id); if ($host["hostname"] == "") { debug_log_insert("data_query", "Invalid host_id: $host_id"); return false; } /* invalid xml check */ if ((!is_array($snmp_queries)) || (sizeof($snmp_queries) == 0)) { debug_log_insert("data_query", "Error parsing XML file into an array."); return false; } debug_log_insert("data_query", "XML file parsed ok."); /* fetch specified index at specified OID */ $snmp_index = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $snmp_queries["oid_index"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for list of indexes @ '" . $snmp_queries["oid_index"] . "'"); /* no data found; get out */ if (!$snmp_index) { debug_log_insert("data_query", "No SNMP data returned"); return false; } /* the last octet of the oid is the index by default */ $index_parse_regexp = '.*\.([0-9]+)$'; /* parse the index if required */ if (isset($snmp_queries["oid_index_parse"])) { $index_parse_regexp = str_replace("OID/REGEXP:", "", $snmp_queries["oid_index_parse"]); for ($i=0; $i<sizeof($snmp_index); $i++) { $snmp_index[$i]["value"] = ereg_replace($index_parse_regexp, "\\1", $snmp_index[$i]["oid"]); } } db_execute("delete from host_snmp_cache where host_id='$host_id' and snmp_query_id='$snmp_query_id'"); while (list($field_name, $field_array) = each($snmp_queries["fields"])) { if ((!isset($field_array["oid"])) && ($field_array["source"] == "index")) { for ($i=0; $i<sizeof($snmp_index); $i++) { debug_log_insert("data_query", "Inserting index data [value='" . $snmp_index[$i]["value"] . "']"); db_execute("replace into host_snmp_cache (host_id, snmp_query_id, field_name, field_value, snmp_index, oid) values ('$host_id','$snmp_query_id','$field_name','" . $snmp_index[$i]["value"] . "','" . $snmp_index[$i]["value"] . "','')"); } }else if (($field_array["method"] == "get") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", "Located input field '$field_name' [get]"); if ($field_array["source"] == "value") { for ($i=0; $i<sizeof($snmp_index); $i++) { $oid = $field_array["oid"] . "." . $snmp_index[$i]["value"]; $value = cacti_snmp_get($host["hostname"], $host["snmp_community"], $oid, $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP get for data @ '$oid' [value='$value']"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','$value','" . $snmp_index[$i]["value"] . "','$oid')"); } } }else if (($field_array["method"] == "walk") && ($field_array["direction"] == "input")) { debug_log_insert("data_query", "Located input field '$field_name' [walk]"); $snmp_data = array(); $snmp_data = cacti_snmp_walk($host["hostname"], $host["snmp_community"], $field_array["oid"], $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI); debug_log_insert("data_query", "Executing SNMP walk for data @ '" . $field_array["oid"] . "'"); if ($field_array["source"] == "value") { for ($i=0; $i<sizeof($snmp_data); $i++) { $snmp_index = ereg_replace($index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . ".$snmp_index"; if ($field_name == "ifOperStatus") { if ((substr_count(strtolower($snmp_data[$i]["value"]), "down")) || ($snmp_data[$i]["value"] == "2")) { $snmp_data[$i]["value"] = "Down"; }else if ((substr_count(strtolower($snmp_data[$i]["value"]), "up")) || ($snmp_data[$i]["value"] == "1")) { $snmp_data[$i]["value"] = "Up"; }else{ $snmp_data[$i]["value"] = "Testing"; } } debug_log_insert("data_query", "Found item [$field_name='" . $snmp_data[$i]["value"] . "'] index: $snmp_index [from value]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','" . $snmp_data[$i]["value"] . "','$snmp_index','$oid')"); } }elseif (substr($field_array["source"], 0, 11) == "OID/REGEXP:") { for ($i=0; $i<sizeof($snmp_data); $i++) { $value = ereg_replace(str_replace("OID/REGEXP:", "", $field_array["source"]), "\\1", $snmp_data[$i]["oid"]); if (isset($snmp_queries["oid_index_parse"])) { $snmp_index = ereg_replace($index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); }else if ((isset($snmp_data[$i]["value"])) && ($snmp_data[$i]["value"] != "")) { $snmp_index = $snmp_data[$i]["value"]; } /* correct bogus index value */ /* found in some devices such as an EMC Cellera */ if ($snmp_index == 0) { $snmp_index = 1; } $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [$field_name='$value'] index: $snmp_index [from regexp oid parse]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','$value','$snmp_index','$oid')"); } }elseif (substr($field_array["source"], 0, 13) == "VALUE/REGEXP:") { for ($i=0; $i<sizeof($snmp_data); $i++) { $value = ereg_replace(str_replace("VALUE/REGEXP:", "", $field_array["source"]), "\\1", $snmp_data[$i]["value"]); $snmp_index = ereg_replace($index_parse_regexp, "\\1", $snmp_data[$i]["oid"]); $oid = $field_array["oid"] . "." . $value; debug_log_insert("data_query", "Found item [$field_name='$value'] index: $snmp_index [from regexp value parse]"); db_execute("replace into host_snmp_cache (host_id,snmp_query_id,field_name,field_value,snmp_index,oid) values ('$host_id','$snmp_query_id','$field_name','$value','$snmp_index','$oid')"); } } } } return true; }
function get_base_dot1qTpFdbEntry_ports($site, &$device, &$ifInterfaces, $snmp_readstring = "", $store_to_db = TRUE, $lowPort = 1, $highPort = 9999) { global $debug, $scan_date; /* initialize variables */ $port_keys = array(); $return_array = array(); $new_port_key_array = array(); $port_key_array = array(); $port_number = 0; $ports_active = 0; $active_ports = 0; $ports_total = 0; /* cisco uses a hybrid read string, if one is not defined, use the default */ if ($snmp_readstring == "") { $snmp_readstring = $device["snmp_readstring"]; } /* get the operational status of the ports */ $active_ports_array = xform_standard_indexed_data(".1.3.6.1.2.1.2.2.1.8", $device); $indexes = array_keys($active_ports_array); $i = 0; if (sizeof($active_ports_array)) { foreach ($active_ports_array as $port_info) { $port_info = mactrack_strip_alpha($port_info); if ($ifInterfaces[$indexes[$i]]["ifType"] >= 6 && $ifInterfaces[$indexes[$i]]["ifType"] <= 9 || $ifInterfaces[$indexes[$i]]["ifType"] == 71) { if ($port_info == 1) { $ports_active++; } $ports_total++; } $i++; } } if ($store_to_db) { print "INFO: HOST: " . $device["hostname"] . ", TYPE: " . substr($device["snmp_sysDescr"], 0, 40) . ", TOTAL PORTS: " . $ports_total . ", OPER PORTS: " . $ports_active; if ($debug) { print "\n"; } $device["ports_active"] = $ports_active; $device["ports_total"] = $ports_total; $device["macs_active"] = 0; } if ($ports_active > 0) { /* get bridge port to ifIndex mapping */ $bridgePortIfIndexes = xform_standard_indexed_data(".1.3.6.1.2.1.17.1.4.1.2", $device, $snmp_readstring); $port_status = xform_stripped_oid(".1.3.6.1.2.1.17.7.1.2.2.1.3", $device, $snmp_readstring); /* get device active port numbers */ $port_numbers = xform_stripped_oid(".1.3.6.1.2.1.17.7.1.2.2.1.2", $device, $snmp_readstring); /* get the ignore ports list from device */ $ignore_ports = port_list_to_array($device["ignorePorts"]); /* get the bridge root port so we don't capture active ports on it */ $bridge_root_port = @cacti_snmp_get($device["hostname"], $snmp_readstring, ".1.3.6.1.2.1.17.2.7.0", $device["snmp_version"], $device["snmp_username"], $device["snmp_password"], $device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"], $device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], $device["snmp_retries"]); /* determine user ports for this device and transfer user ports to a new array. */ $i = 0; if (sizeof($port_numbers)) { foreach ($port_numbers as $key => $port_number) { if ($highPort == 0 || $port_number >= $lowPort && $port_number <= $highPort && $bridge_root_port != $port_number) { if (!in_array($port_number, $ignore_ports)) { if (@$port_status[$key] == "3" || @$port_status[$key] == "5") { $port_key_array[$i]["key"] = $key; $port_key_array[$i]["port_number"] = $port_number; $i++; } } } } } /* compare the user ports to the brige port data, store additional relevant data about the port. */ $i = 0; if (sizeof($port_key_array)) { foreach ($port_key_array as $port_key) { /* map bridge port to interface port and check type */ if ($port_key["port_number"] > 0) { if (sizeof($bridgePortIfIndexes)) { $brPortIfIndex = @$bridgePortIfIndexes[$port_key["port_number"]]; $brPortIfType = @$ifInterfaces[$brPortIfIndex]["ifType"]; } else { $brPortIfIndex = $port_key["port_number"]; $brPortIfType = @$ifInterfaces[$port_key["port_number"]]["ifType"]; } if ($brPortIfType >= 6 && $brPortIfType <= 9 || $brPortIfType == 71) { /* set some defaults */ $new_port_key_array[$i]["vlan_id"] = "N/A"; $new_port_key_array[$i]["vlan_name"] = "N/A"; $new_port_key_array[$i]["mac_address"] = "NOT USER"; $new_port_key_array[$i]["port_number"] = "NOT USER"; $new_port_key_array[$i]["port_name"] = "N/A"; /* now set the real data */ $new_port_key_array[$i]["key"] = $port_key["key"]; $new_port_key_array[$i]["port_number"] = $port_key["port_number"]; $i++; } } } } mactrack_debug("Port number information collected."); /* map mac address */ /* only continue if there were user ports defined */ if (sizeof($new_port_key_array)) { /* get the bridges active MAC addresses */ $port_macs = xform_stripped_oid(".1.3.6.1.2.1.17.7.1.2.2.1.1", $device, $snmp_readstring); if (sizeof($port_macs)) { foreach ($port_macs as $key => $port_mac) { $port_macs[$key] = xform_mac_address($port_mac); } } if (sizeof($new_port_key_array)) { foreach ($new_port_key_array as $key => $port_key) { $new_port_key_array[$key]["mac_address"] = @$port_macs[$port_key["key"]]; mactrack_debug("INDEX: '" . $key . "' MAC ADDRESS: " . $new_port_key_array[$key]["mac_address"]); } } mactrack_debug("Port mac address information collected."); } else { mactrack_debug("No user ports on this network."); } } else { mactrack_debug("No user ports on this network."); } if ($store_to_db) { if ($ports_active <= 0) { $device["last_runmessage"] = "Data collection completed ok"; } elseif (sizeof($new_port_key_array)) { $device["last_runmessage"] = "Data collection completed ok"; $device["macs_active"] = sizeof($new_port_key_array); db_store_device_port_results($device, $new_port_key_array, $scan_date); } else { $device["last_runmessage"] = "WARNING: Poller did not find active ports on this device."; } if (!$debug) { print " - Complete\n"; } } else { return $new_port_key_array; } }
function ping_snmp() { /* initialize variables */ $this->snmp_status = "down"; $this->snmp_response = "Device did not respond to SNMP"; $output = ""; /* get start time */ $this->start_time(); /* by default, we look at sysUptime */ if ($this->avail_method == AVAIL_SNMP_GET_NEXT) { if (version_compare("5", phpversion(), "<")) { $oid = ".1.3"; } else { $oid = ".1.3.6.1.2.1.1.3.0"; } } else { if ($this->avail_method == AVAIL_SNMP_GET_SYSDESC) { $oid = ".1.3.6.1.2.1.1.1.0"; } else { $oid = ".1.3.6.1.2.1.1.3.0"; } } /* getnext does not work in php versions less than 5 */ if ($this->avail_method == AVAIL_SNMP_GET_NEXT && version_compare("5", phpversion(), "<")) { $output = cacti_snmp_getnext($this->host["hostname"], $this->host["snmp_community"], $oid, $this->host["snmp_version"], $this->host["snmp_username"], $this->host["snmp_password"], $this->host["snmp_auth_protocol"], $this->host["snmp_priv_passphrase"], $this->host["snmp_priv_protocol"], $this->host["snmp_context"], $this->host["snmp_port"], $this->host["snmp_timeout"], $this->retries, SNMP_CMDPHP); } else { $output = cacti_snmp_get($this->host["hostname"], $this->host["snmp_community"], $oid, $this->host["snmp_version"], $this->host["snmp_username"], $this->host["snmp_password"], $this->host["snmp_auth_protocol"], $this->host["snmp_priv_passphrase"], $this->host["snmp_priv_protocol"], $this->host["snmp_context"], $this->host["snmp_port"], $this->host["snmp_timeout"], $this->retries, SNMP_CMDPHP); } /* determine total time +- ~10% */ $this->time = $this->get_time($this->precision); /* check result for uptime */ if (strlen($output)) { /* calculte total time */ $this->snmp_status = $this->time * 1000; $this->snmp_response = "Device responded to SNMP"; return true; } else { $this->snmp_status = "down"; $this->snmp_response = "Device did not respond to SNMP"; return false; } }
function ping_snmp() { require_once(CACTI_BASE_PATH . "/lib/sys/snmp.php"); /* initialize variables */ $this->snmp_status = "down"; $this->snmp_response = _("Device did not respond to SNMP"); $this->packet_loss = 1; $output = ""; /* get start time */ $this->start_time(); /* poll sysUptime for status */ $retry_count = 0; $success_count = 0; $total_time = 0; while (1) { if ($retry_count >= $this->retries) { $this->packet_loss = 1 - ($success_count / $retry_count); if ($this->packet_loss == 1) { $this->snmp_status = "down"; $this->snmp_response = _("Device did not respond to SNMP"); return false; } else { $this->snmp_response = _("Device responded to SNMP"); $this->snmp_status = $total_time / $success_count; return true; } } $output = cacti_snmp_get($this->host["hostname"], $this->host["snmp_community"], ".1.3.6.1.2.1.1.3.0" , $this->host["snmp_version"], $this->host["snmpv3_auth_username"], $this->host["snmpv3_auth_password"], $this->host["snmpv3_auth_protocol"], $this->host["snmpv3_priv_passphrase"], $this->host["snmpv3_priv_protocol"], $this->host["snmp_port"], $this->host["snmp_timeout"], SNMP_CMDPHP); /* determine total time +- ~10% */ $this->time = $this->get_time($this->precision); /* check result for uptime */ if (!empty($output)) { /* calculte total time */ $total_time = $total_time + $this->time * 1000; $success_count = $success_count + 1; } $retry_count++; } } /* ping_snmp */