public function walk($object_id, $timeout = 1000000) { $array = @snmp2_walk($this->host, $this->community, $object_id, $timeout); // We often do many consecutive walks; it is easier to just throw an exception and // catch it than check every result after every walk if ($array === False) throw new Exception("Walk failed."); return $this->clean_array($array); }
function snmp_walk($snmp_host, $snmp_community, $snmp_oid, $snmp_version) { if (extension_loaded("snmp")) { if ($snmp_version == "1") { if ($snmp_return = @snmpwalk($snmp_host, $snmp_community, $snmp_oid)) { return $snmp_return; } else { show_help("SNMP"); } } else { if ($snmp_version == "2c" || $snmp_version == "2") { if ($snmp_return = @snmp2_walk($snmp_host, $snmp_community, $snmp_oid)) { return $snmp_return; } else { show_help("SNMP"); } } else { show_help("SNMP"); } } } else { show_help("SNMP_MODULE"); } }
function walk($oid, $suffix_as_key = FALSE) { switch ($this->version) { case self::VERSION_1: if ($suffix_as_key) { $this->result = snmpwalk($this->host, $this->community, $oid); } else { $this->result = snmprealwalk($this->host, $this->community, $oid); } break; case self::VERSION_2C: case self::VERSION_2c: if ($suffix_as_key) { $this->result = snmp2_walk($this->host, $this->community, $oid); } else { $this->result = snmp2_real_walk($this->host, $this->community, $oid); } break; case self::VERSION_3: if ($suffix_as_key) { $this->result = snmp3_walk($this->host, $this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase, $oid); } else { $this->result = snmp3_real_walk($this->host, $this->community, $this->sec_level, $this->auth_protocol, $this->auth_passphrase, $this->priv_protocol, $this->priv_passphrase, $oid); } break; } return $this->result; }
function get_snmp($details) { $CI =& get_instance(); if (!isset($details->system_id) or $details->system_id == '') { $details->snmp_community = 'public'; $details->snmp_version = '2c'; $details->snmp_port = '161'; } else { $encrypted_access_details = $CI->m_system->get_access_details($details->system_id); if ($encrypted_access_details == '') { } $decoded_access_details = $CI->encrypt->decode($encrypted_access_details); $decoded_access_details = json_decode($decoded_access_details); $details->snmp_community = @$decoded_access_details->snmp_community; $details->snmp_version = @$decoded_access_details->snmp_version; $details->snmp_port = @$decoded_access_details->snmp_port; $details->man_ip_address = @$decoded_access_details->ip_address; } if (!isset($details->snmp_community) or $details->snmp_community == '') { $details->snmp_community = 'public'; } if (!isset($details->snmp_version) or $details->snmp_version == '') { $details->snmp_version = ''; } if (!isset($details->snmp_port) or $details->snmp_port == '') { $details->snmp_port = '161'; } # we may only have been given a system_id # but if the access_details were completed, that may be enough # if they weren't, we would at least need a hostname ot ip address if (!isset($details->man_ip_address) and !isset($details->hostname)) { return; } $module = new stdclass(); if ((!isset($details->hostname) or $details->hostname == '') and (isset($details->man_ip_address) and $details->man_ip_address != '' and $details->man_ip_address != '000.000.000.000' and $details->man_ip_address != '0.0.0.0')) { $details->hostname = gethostbyaddr(ip_address_from_db($details->man_ip_address)); } if ((!isset($details->man_ip_address) or $details->man_ip_address == '' or $details->man_ip_address == '0.0.0.0' or $details->man_ip_address == '000.000.000.000') and (isset($details->hostname) and $details->hostname != '')) { $details->man_ip_address = gethostbyname($details->hostname); } if (filter_var($details->hostname, FILTER_VALIDATE_IP)) { # we did not get a valid hostname back :-( } else { # we have a name of some sort if (strpos($details->hostname, ".") !== FALSE) { # fqdn - explode it if (!isset($details->fqdn) or $details->fqdn == '') { $details->fqdn = $details->hostname; } $i = explode(".", $details->hostname); $details->hostname = $i[0]; if (!isset($details->domain) or $details->domain == '') { unset($i[0]); $details->domain = implode(".", $i); } } else { # it's just a name } } $timeout = '300000'; $retries = '2'; if (!extension_loaded('snmp')) { return $details; } # test for SNMP version # to do - test for v3 if ($details->snmp_version == '') { if ($test_v1 = @snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.5.0", $timeout)) { # ok, we have v1 $details->snmp_version = '1'; } if ($test_v2 = @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.5.0", $timeout)) { # ok, we have v2 $details->snmp_version = '2c'; } } if ($details->snmp_version == '2c') { snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC); // test for connectivity $test_connect = @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.5.0", $timeout, $retries); if ($test_connect) { // printer duplex $details->printer_duplex = ''; $i = @snmp2_walk($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.43.13.4.1.10.1"); if (count($i) > 0) { $details->printer_duplex = 'False'; for ($k = 0; $k < count($i); $k++) { if (mb_strpos($i[$k], "Duplex") !== FALSE) { $details->printer_duplex = 'True'; } } } // sysObjectID #$i = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.1.0" )); $details->snmp_oid = str_replace("OID: .", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.2.0")); if (strtolower($details->snmp_oid) == 'no such object available on this agent at this oid') { $details->snmp_oid = ''; } if ($details->snmp_oid > '') { $oid = get_oid($details->snmp_oid); $details->manufacturer = $oid->manufacturer; $details->man_manufacturer = $oid->manufacturer; $details->model = $oid->model; $details->man_model = $oid->model; $details->type = $oid->type; $details->device_type = $oid->type; $details->man_type = $oid->type; $details->os_group = $oid->os_group; $details->man_os_group = $oid->os_group; } if ($details->snmp_oid == '1.3.6.1.4.1.8072.3.2.255') { # a generic OID - take some guesses... $i = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.1.0")); if (strpos($i, "Darwin Kernel Version 12") !== FALSE) { # looks like an OSX device $details->manufacturer = "Apple Inc"; } } if ($details->snmp_oid == '1.3.6.1.4.1.6876.4.1') { # grab some specific details for VMware ESX $model = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.6876.1.1.0")); $version = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.6876.1.2.0")); $details->model = $model . " (" . $version . ")"; $details->model = str_replace("\"", "", $details->model); $details->man_model = $details->model; $details->os_group = "VMware"; $details->man_os_group = "VMware"; $details->os_family = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.6876.1.1.0")); $details->os_family = str_replace("\"", "", $details->os_family); $details->man_os_family = $details->os_family; $details->os_name = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.1.0")); $details->os_name = str_replace("\"", "", $details->os_name); $details->man_os_name = $details->os_name; $details->type = 'computer'; $details->man_type = 'computer'; $details->device_type = 'computer'; $details->man_class = 'hypervisor'; } if (substr($details->snmp_oid, 0, 13) == '1.3.6.1.4.1.9') { # grab some Cisco specific details $i = explode("\$", @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.9.9.25.1.1.1.2.5")); $details->os_version = trim($i[1]); $i = @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.9.9.25.1.1.1.2.7"); if (strpos($i, "IOS") !== FALSE) { $details->os_group = 'Cisco'; $details->man_os_group = 'Cisco'; $details->os_family = 'Cisco IOS'; $details->man_os_family = 'Cisco IOS'; $details->os_name = "Cisco IOS version " . $details->os_version; $details->man_os_name = "Cisco IOS version " . $details->os_version; } if (strpos($i, "Catalyst Operating") !== FALSE) { $details->os_group = 'Cisco'; $details->man_os_group = 'Cisco'; $details->os_family = 'Cisco Catalyst OS'; $details->man_os_family = 'Cisco Catalyst OS'; $detail->os_name = "Cisco Catalyst OS version " . $details->os_version; $detail->man_os_name = "Cisco Catalyst OS version " . $details->os_version; } } // manufacturer if (!isset($details->manufacturer) or $details->manufacturer == '') { $hex = @snmp2_walk($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.43.8.2.1.14.1"); if (count($hex) > 0) { if (isset($hex[1])) { if (mb_strpos($hex[1], "Hex-STRING: ") !== FALSE) { $hex[1] = str_replace("Hex-STRING: ", "", $hex[1]); for ($i = 0; $i < strlen($hex[1]); $i++) { $details->manufacturer .= chr(hexdec(substr($hex[1], $i, 2))); } } else { $details->manufacturer = str_replace("STRING: ", "", $hex[1]); $details->manufacturer = str_replace('"', '', $details->manufacturer); } } } if ($details->manufacturer == '' or $details->manufacturer == 'No Such Object available on this agent at this OID') { $details->manufacturer = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.47.1.1.1.1.12.1"))); } } if ($details->manufacturer == 'No Such Instance currently exists at this OID') { $details->manufacturer = ''; } if ($details->manufacturer == 'No Such Object available on this agent at this OID') { $details->manufacturer = ''; } // serial if (!isset($details->serial) or $details->serial == '') { # generic snmp $details->serial = str_replace('"', '', str_replace("STRING: ", "", @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.43.5.1.1.17.1"))); if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Object available on this agent at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } # Generic Cisco if ($details->serial == '') { $details->serial = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.47.1.1.1.1.11.1"))); if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Object available on this agent at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } } # Cisco 37xx stack if ($details->serial == '') { $details->serial = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.9.5.1.2.19.0"))); if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Object available on this agent at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } } # remove false if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Object available on this agent at this OID') { $details->serial = ''; } if ($details->serial == 'No Such Instance currently exists at this OID') { $details->serial = ''; } } $details->man_serial = $details->serial; // mac address if (!isset($details->mac_address) or $details->mac_address == '') { $interface_number = @str_replace("INTEGER: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.20.1.2." . $details->man_ip_address)); $details->mac_address = @str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.2.2.1.6." . $interface_number)); $details->mac_address = @trim(str_replace("Hex-", "", $details->mac_address)); $details->mac_address = @str_replace(" ", ":", $details->mac_address); # need to split and join because of the dropped 0's $i = explode(":", $details->mac_address); for ($k = 0; $k < count($i); $k++) { if (strlen($i[$k]) == 1) { $i[$k] = '0' . $i[$k]; } if (strlen($i[$k]) == 0) { $i[$k] = '00'; } } $details->mac_address = strtoupper(implode(":", $i)); if ($details->mac_address == '00') { $details->mac_address = ''; } } // type if (!isset($details->type) or $details->type == '') { $h = str_replace("OID: HOST-RESOURCES-TYPES::hrDevice", "", str_replace("STRING: ", "", @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.25.3.2.1.2.1"))); if (strtolower($h) == 'no such object available on this agent at this oid' or $h == '') { # If the device is a Switch, the OID 1.3.6.1.2.1.17.1.2.0 and OID 1.3.6.1.2.1.4.1.0 should have a value of 2 $i = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.17.1.2.0")); $j = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.1.0")); if ($i == '2' and $j == '2') { $details->type = 'switch'; } # If the device is a Router, the OID 1.3.6.1.2.1.4.1.0 should have a value of 1 if (!isset($details->type) or $details->type == '') { $i = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.1.0")); if ($i == '1') { $device->type = 'router'; } } # If the device is a Printer, the OID 1.3.6.1.2.1.43.5.1.1.1.1 should have a value if (!isset($details->type) or $details->type == '') { $i = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.43.5.1.1.1.1")); if ($i == '1') { $device->type = 'printer'; } } } } if (isset($details->type) and $details->type != '' and (!isset($details->man_type) or $details->man_type == '')) { $details->man_type = $details->type; } // model if (!isset($details->model) or $details->model == '') { $details->model = str_replace("STRING: ", "", @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.25.3.2.1.3.1")); if ($details->model == '' or $details->model == 'No Such Object available on this agent at this OID') { if (strpos(strtolower($details->manufacturer), 'cisco') !== FALSE) { $details->model = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.47.1.1.1.1.13.1"))); } } } if (strtolower($details->model) == 'no such object available on this agent at this oid') { $details->model = ''; } // name if (!isset($details->sysname) or $details->sysname == '') { $details->sysname = strtolower(str_replace("\"", "", str_replace("STRING: ", "", @snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.5.0")))); } if (strtolower($details->sysname) == 'no such object available on this agent at this oid') { $details->sysname = ''; } // description if (!isset($details->description) or $details->description == '') { $details->description = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.1.0"))); $details->description = str_replace("\r\n", " ", $details->description); } if (strtolower($details->description) == 'no such object available on this agent at this oid') { $details->description = ''; } // uptime if (!isset($details->uptime) or $details->uptime == '') { $i = str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.3.0")); if ($i > '' and strpos($i, ")") !== FALSE) { $j = explode(")", $i); $details->uptime = trim($j[1]); } else { $details->uptime = ''; } } // location if (!isset($details->location) or $details->location == '') { $details->location = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.6.0"))); } if ($details->location == 'Unknown (edit /etc/snmp/snmpd.conf)') { $details->location = ''; } if ($details->location == 'not set') { $details->location = ''; } if ($details->location == 'No Such Object available on this agent at this OID') { $details->location = ''; } if ($details->location > '') { $details->description = "Location: " . $details->location . ". " . $details->description; } // contact if (!isset($details->contact) or $details->contact == '') { $details->contact = str_replace("\"", "", str_replace("STRING: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.4.0"))); } if ($details->contact == 'Root (configure /etc/snmp/snmpd.conf)') { $details->contact = ''; } if ($details->contact == 'not set') { $details->contact = ''; } if ($details->contact > '') { $details->description = "Contact: " . $details->contact . ". " . $details->description; } // subnet if (!isset($details->subnet) or $details->subnet == '') { $details->subnet = @str_replace("IpAddress: ", "", snmp2_get($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.20.1.3." . $details->man_ip_address)); } if ($details->subnet == 'No Such Instance currently exists at this OID') { $details->subnet = ''; } if ($details->subnet == 'No Such Object available on this agent at this OID') { $details->subnet = ''; } /* // installed modules with serial numbers $i = @snmp2_walk($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.47.1.1.1.1.11"); if (count($i) > 0) { if (($i[0] == 'No more variables left in this MIB View (It is past the end of the MIB tree)') or ($i[0] == '')){unset($i); $i = array();} if (count($i) > 0) { $count = 0; for ($j=0; $j<count($i); $j++) { if ((mb_strpos($i[$j], $details->serial) === FALSE) and ($i[$j] != "") and ($i[$j] != "\"\"")){ $k = $j + 1; $k = "1.3.6.1.2.1.47.1.1.1.1.3." . $k; $oid = snmp2_get($details->man_ip_address, $details->snmp_community, $k); $oid = str_replace("OID: .", "", $oid); $module->$count = get_oid($oid); $module->$count->serial = str_replace("STRING: ", "", $i[$j]); $module->$count->serial = str_replace('"', '', $module->$count->serial); $count++; } } $details->modules = $module; } } */ } } if ($details->snmp_version == '1') { $details->snmp_oid = ''; $details->snmp_oid = str_replace("OID: .", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.2.0")); if (strtolower($details->snmp_oid) == 'no such object available on this agent at this oid') { $details->snmp_oid = ''; } if ($details->snmp_oid > '') { $oid = get_oid($details->snmp_oid); $details->manufacturer = $oid->manufacturer; $details->man_manufacturer = $oid->manufacturer; $details->model = $oid->model; $details->man_model = $oid->model; $details->type = $oid->type; $details->device_type = $oid->type; $details->man_type = $oid->type; $details->os_group = $oid->os_group; $details->man_os_group = $oid->os_group; } if ($details->oid == '1.3.6.1.4.1.714.1.2.6') { # We have a Wyse thin client - some specifics. $details->serial = str_replace("String: .", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.4.1.714.1.2.6.2.1.0")); $details->sysname = str_replace("String: .", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.5.0")); $details->description = str_replace("String: .", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.1.0")); $details->contact = str_replace("String: .", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.4.0")); if ($details->contact > '') { $details->description = "Contact: " . $details->contact . ". " . $details->description; } $details->location = str_replace("String: .", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.6.0")); if ($details->location > '') { $details->description = "Location: " . $details->location . ". " . $details->description; } } $interface_number = str_replace("INTEGER: ", "", @snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.20.1.2." . $details->man_ip_address)); $i = "1.3.6.1.2.1.2.2.1.6." . $interface_number; $details->mac_address = snmpget($details->man_ip_address, $details->snmp_community, $i); $details->mac_address = trim(str_replace("Hex-STRING: ", "", $details->mac_address)); $details->mac_address = str_replace(" ", ":", $details->mac_address); $details->subnet = str_replace("IpAddress: ", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.20.1.3." . $details->man_ip_address)); $details->hostname = trim(str_replace("\"", "", str_replace("STRING: ", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.1.5.0")))); $details->next_hop = str_replace("IpAddress: ", "", snmpget($details->man_ip_address, $details->snmp_community, "1.3.6.1.2.1.4.21.1.7.0.0.0.0")); } // update the system #if ($details->snmp_version > '') { #$CI2->m_system->process_snmp($details); return $details; #} }
function my_snmp_walk($ip, $credentials, $oid) { $timeout = '30000000'; $retries = '0'; switch ($credentials->credentials->version) { case '1': $array = @snmpwalk($ip, $credentials->credentials->community, $oid, $timeout, $retries); break; case '2': $array = @snmp2_walk($ip, $credentials->credentials->community, $oid, $timeout, $retries); break; case '3': $sec_name = $credentials->credentials->security_name ?: ''; $sec_level = $credentials->credentials->security_level ?: ''; $auth_protocol = $credentials->credentials->authentication_protocol ?: ''; $auth_passphrase = $credentials->credentials->authentication_passphrase ?: ''; $priv_protocol = $credentials->credentials->privacy_protocol ?: ''; $priv_passphrase = $credentials->credentials->privacy_passphrase ?: ''; $array = @snmp3_walk($ip, $sec_name, $sec_level, $auth_protocol, $auth_passphrase, $priv_protocol, $priv_passphrase, $oid, $timeout, $retries); break; default: return false; break; } if (!is_array($array)) { return false; } foreach ($array as $i => $value) { $array[$i] = trim($array[$i]); if ($array[$i] == '""') { $array[$i] = ''; } if (strpos($array[$i], '.') === 0) { $array[$i] = substr($array[$i], 1); } // remove the first and last characters if they are " if (substr($array[$i], 0, 1) == "\"") { $array[$i] = substr($array[$i], 1, strlen($array[$i])); } if (substr($array[$i], -1, 1) == "\"") { $array[$i] = substr($array[$i], 0, strlen($array[$i]) - 1); } // remove some return strings if (strpos(strtolower($array[$i]), '/etc/snmp') !== false or strpos(strtolower($array[$i]), 'no such instance') !== false or strpos(strtolower($array[$i]), 'no such object') !== false or strpos(strtolower($array[$i]), 'not set') !== false or strpos(strtolower($array[$i]), 'unknown value type') !== false) { $array[$i] = ''; } // remove any quotation marks $array[$i] = str_replace('"', ' ', $array[$i]); // replace any line breaks with spaces $array[$i] = str_replace(array("\r", "\n"), " ", $array[$i]); } return $array; }
<?php if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $time = microtime(1); snmp_read_mib("mibs/DFL860E_A1_FW_v2.27.03.25-MIB.txt"); $servers = array('10.10.1.213' => array(), '10.10.10.213' => array(), '10.10.11.213' => array(), '10.10.12.213' => array(), '10.11.2.13' => array()); $cols = array('Name', 'CurrentBps'); $names = array(); $measure = isset($_GET['m']) ? $_GET['m'] : null; $bpsDiv = $measure == 'B' ? 8 : 1; $bpsPrefix = $measure == 'B' ? 'B' : 'b'; foreach ($servers as $server => $info) { $data = snmp2_walk($server, "1234567890", "dfl860eRules"); for ($i = 0, $l = count($data) / 3; $i < $l; $i++) { $key = explode(': ', $data[$i + $l], 2); $val = explode(': ', $data[$i + $l * 2], 2); $servers[$server][$key[1]] = $val[1]; } } echo json_encode($servers); die; } ?> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>DFL-860e Rule overview</title> <link rel="stylesheet" href="logger/assets/css/bootstrap.min.css"> <link rel="stylesheet" href="logger/assets/css/bootstrap-datetimepicker.min.css" />
$hostfb = str($hostfbb); } $community = 'public'; $object_idb = 'interfaces.ifTable.ifEntry.ifDescr'; $sysDescrb = snmpget("{$hostfb}", "{$community}", "system.sysDescr.0"); $ifDescrb = snmpwalk("{$hostfb}", "{$community}", "interfaces.ifTable.ifEntry.ifDescr"); $ifIndexb = snmpwalk("{$hostfb}", "{$community}", "interfaces.ifTable.ifEntry.ifIndex"); $ifAdminStatusb = snmpwalk("{$hostfb}", "{$community}", "interfaces.ifTable.ifEntry.ifAdminStatus"); $ifOperStatusb = snmpwalk("{$hostfb}", "{$community}", "interfaces.ifTable.ifEntry.ifOperStatus"); $ifLastChangeb = snmpwalk("{$hostfb}", "{$community}", "interfaces.ifTable.ifEntry.ifLastChange"); $ipRouteNextHopb = snmpwalk("{$hostf}", "{$community}", "ipRouteNextHop"); $ipNetToMediaNetAddressb = snmpwalk("{$hostfb}", "{$community}", "ipNetToMediaNetAddress"); $ipRouteDestb = snmpwalk("{$hostfb}", "{$community}", "ipRouteDest"); $ipRouteTypeb = snmpwalk("{$hostfb}", "{$community}", "ipRouteType"); $nextb = snmp2_walk("{$hostfb}", "{$community}", ".1.3.6.1.4.1.9.9.23.1.2.1.1.6"); $nameb = snmp2_walk("{$hostfb}", "{$community}", "1.3.6.1.2.1.1.5"); $inoctetsb = snmpwalk("{$hostfb}", "{$community}", "1.3.6.1.2.1.2.2.1.10"); $outoctetsb = snmpwalk("{$hostfb}", "{$community}", "1.3.6.1.2.1.2.2.1.16"); $mac = snmpwalk("{$hostfb}", "{$community}", ".1.3.6.1.2.1.3.1.1.2"); $sysb = substr($sysDescrb, 8, 15); $name1b = substr($nameb[0], 8, 50); $ipNetToMediaNetphysAddressb = snmpwalk("{$hostfb}", "{$community}", "1.3.6.1.2.1.2.2.1.6"); $ipNetToMediaTypeb = snmpwalk("{$hostfb}", "{$community}", "1.3.6.1.2.1.4.22.1.4"); $tempb = snmpwalk("{$hostfb}", "{$community}", "1.3.6.1.4.1.9.9.13.1.3.1.3"); /////////////////////////////////////////////////////////////////////////////////////////////// $n = 0; for ($n = 0; $n < count($ifIndexb); $n++) { $sql9physb = mysql_query("INSERT INTO mac(interface,mac,hostname,hostip,id) VALUES ('{$ipNetToMediaNetAddressb[$n]}','{$ipNetToMediaNetphysAddressb[$n]}','{$name1b}','{$ipNetToMediaTypeb[$n]}','{$n}')"); } for ($i = 0; $i < count($ifIndexb); $i++) { $cb = count($ifIndexb);
var_dump(snmpget($hostname, $community, '.1.3.6.1.2.1.1.1.0', $timeout, $retries)); echo "Get with SNMP_VALUE_OBJECT\n"; snmp_set_valueretrieval(SNMP_VALUE_OBJECT); $z = snmpget($hostname, $community, '.1.3.6.1.2.1.1.1.0', $timeout, $retries); echo gettype($z) . "\n"; var_dump($z->type); var_dump($z->value); echo "Get with SNMP_VALUE_OBJECT | SNMP_VALUE_PLAIN\n"; snmp_set_valueretrieval(SNMP_VALUE_OBJECT | SNMP_VALUE_PLAIN); $z = snmpget($hostname, $community, '.1.3.6.1.2.1.1.1.0', $timeout, $retries); echo gettype($z) . "\n"; var_dump($z->type); var_dump($z->value); echo "Get with SNMP_VALUE_OBJECT for BITS OID\n"; snmp_set_valueretrieval(SNMP_VALUE_OBJECT); $z = snmpget($hostname, $community, '.1.3.6.1.2.1.88.1.4.2.1.3.6.95.115.110.109.112.100.95.108.105.110.107.68.111.119.110', $timeout, $retries); echo gettype($z) . "\n"; var_dump($z->type); var_dump($z->value); echo "Get with SNMP_VALUE_OBJECT | SNMP_VALUE_PLAIN for BITS OID\n"; snmp_set_valueretrieval(SNMP_VALUE_OBJECT | SNMP_VALUE_PLAIN); $z = snmpget($hostname, $community, '.1.3.6.1.2.1.88.1.4.2.1.3.6.95.115.110.109.112.100.95.108.105.110.107.68.111.119.110', $timeout, $retries); echo gettype($z) . "\n"; var_dump($z->type); var_dump(is_numeric($z->value)); var_dump(is_string($z->value)); var_dump(bin2hex($z->value)); echo "Check parsing of different OID types\n"; snmp_set_valueretrieval(SNMP_VALUE_PLAIN); var_dump(count(snmp2_walk($hostname, $community, '.', $timeout, $retries)));
var_dump(snmp2_walk($hostname, $community, '.1.3.6.1.2.1.1', $timeout, '')); echo "Checking working\n"; echo "Single OID\n"; $return = snmp2_walk($hostname, $community, '.1.3.6.1.2.1.1', $timeout, $retries); var_dump(gettype($return)); var_dump(sizeof($return)); var_dump(gettype($return[0])); var_dump(gettype($return[1])); echo "Single OID in array\n"; $return = snmp2_walk($hostname, $community, array('.1.3.6.1.2.1.1'), $timeout, $retries); var_dump(gettype($return)); var_dump(gettype($return[0])); echo "Default OID\n"; $return = snmpwalk($hostname, $community, '', $timeout, $retries); var_dump(gettype($return)); var_dump(gettype($return[0])); echo "More error handling\n"; echo "Single incorrect OID\n"; $return = snmpwalk($hostname, $community, '.1.3.6...1', $timeout, $retries); var_dump($return); echo "Multiple correct OID\n"; $return = snmp2_walk($hostname, $community, array('.1.3.6.1.2.1.1', '.1.3.6'), $timeout, $retries); var_dump($return); echo "Multiple OID with wrong OID\n"; $return = snmp2_walk($hostname, $community, array('.1.3.6.1.2.1.1', '.1.3.6...1'), $timeout, $retries); var_dump($return); $return = snmp2_walk($hostname, $community, array('.1.3.6...1', '.1.3.6.1.2.1.1'), $timeout, $retries); var_dump($return); echo "Single nonexisting OID\n"; $return = snmp2_walk($hostname, $community, array('.1.3.6.99999.0.99999.111'), $timeout, $retries); var_dump($return);
function process_printer($input, $details) { if ($input->printer_connection_status == 'OK') { # only put printers that actually exist in the DB $exist_type = ''; # check for a local printer $sql = "SELECT system_id FROM system WHERE man_type = 'printer' AND linked_sys = ? AND (timestamp = ? OR timestamp = ?) AND system_key = ?"; $data = array("{$details->system_id}", "{$details->original_timestamp}", "{$details->timestamp}", "{$input->system_key}"); $query = $this->db->query($sql, $data); if ($query->num_rows() > 0) { $row = $query->row(); $system_id = $row->system_id; $exist_type = 'local'; } if ($exist_type == '') { # if the above is empty, check for an network printer $sql = "SELECT system_id FROM system WHERE (man_type = 'printer' or type = 'network printer') AND man_ip_address = ? AND man_ip_address <> '000.000.000.000' "; $data = array($this->ip_address_to_db("{$input->man_ip_address}")); $query = $this->db->query($sql, $data); if ($query->num_rows() > 0) { $row = $query->row(); $system_id = $row->system_id; $exist_type = 'network'; } } $serial = ''; $input->serial = ''; $snmp_model = ""; $snmp_manufacturer = ""; $manufacturer = ""; $hex = FALSE; $snmp_description = ""; if ($input->man_ip_address > '') { $type = 'network printer'; $linked_sys = ''; # Network printer, should try SNMP # Check if SNMP is enabled in PHP if (extension_loaded('snmp')) { # SNMP is installed in PHP # Now run the SNMP query $serial = @snmp2_get($input->man_ip_address, "public", "1.3.6.1.2.1.43.5.1.1.17.1"); if ($serial !== FALSE) { # SNMP query did return a result $serial = str_replace("STRING: ", "", $serial); $serial = str_replace('"', '', $serial); if (strpos($serial, "No Such Object available on this agent at this OID") === FALSE) { if (strpos($serial, "Hex-") === FALSE) { # The result was a serial number $input->serial = $serial; } else { $serial = str_replace("Hex-STRING: ", "", $serial); for ($i = 0; $i < strlen($serial); $i++) { $new_serial .= chr(hexdec(substr($serial, $i, 2))); } $input->serial = $new_serial; } } } $hex = @snmp2_walk($input->man_ip_address, "public", "1.3.6.1.2.1.43.8.2.1.14.1"); if (is_array($hex) and count($hex) > 0) { if (mb_strpos($hex[1], "Hex-STRING: ") !== FALSE) { $hex[1] = str_replace("Hex-STRING: ", "", $hex[1]); for ($i = 0; $i < strlen($hex[1]); $i++) { $manufacturer .= chr(hexdec(substr($hex[1], $i, 2))); } $snmp_manufacturer = str_replace("\"", "", str_replace("STRING: ", "", $manufacturer)); } else { $snmp_manufacturer = str_replace("\"", "", str_replace("STRING: ", "", $hex[1])); } } if ($snmp_manufacturer > "") { if (stripos($snmp_manufacturer, "Hewlett") !== FALSE) { $snmp_manufacturer = "Hewlett Packard"; } $snmp_manufacturer = ucfirst($snmp_manufacturer); $input->manufacturer = $snmp_manufacturer; } $snmp_model = @snmp2_get($input->man_ip_address, "public", "1.3.6.1.2.1.25.3.2.1.3.1"); $snmp_model = str_replace("\"", "", str_replace("STRING: ", "", $snmp_model)); if ($snmp_model > "") { $input->model = $snmp_model; } $snmp_description = @snmp2_get($input->man_ip_address, "public", "1.3.6.1.2.1.1.1.0"); $snmp_description = str_replace("\"", "", str_replace("STRING: ", "", $snmp_description)); } else { # SNMP extension not loaded } } else { $type = 'printer'; $linked_sys = $details->system_id; } if ($exist_type > '') { # update a printer $sql = "UPDATE system SET timestamp = ?, serial = ?, type = ?, man_type = ? WHERE system_id = ?"; $data = array("{$details->timestamp}", "{$input->serial}", "{$type}", "{$type}", "{$system_id}"); $query = $this->db->query($sql, $data); } if ($exist_type == '') { # actually insert a new printer $sql = "INSERT INTO system (system_key, uuid, hostname, description, type, icon, linked_sys, serial, \r\n\t\t\t\t\tman_serial, model, manufacturer, printer_port_name, printer_shared, printer_shared_name, \r\n\t\t\t\t\tprinter_color, printer_duplex, man_status, man_environment, man_description, man_type, \r\n\t\t\t\t\tman_ip_address, man_model, man_manufacturer, man_icon, first_timestamp, timestamp, \r\n\t\t\t\t\tlast_seen, last_seen_by ) \r\n\t\t\t\t\tVALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"; $data = array("{$input->system_key}", "{$input->uuid}", "{$input->hostname}", "{$input->description}", "{$type}", "printer", "{$linked_sys}", "{$input->serial}", "{$input->serial}", "{$input->model}", "{$input->manufacturer}", "{$input->printer_port_name}", "{$input->printer_shared}", "{$input->printer_shared_name}", "{$input->printer_color}", "{$input->printer_duplex}", "production", "production", "{$input->description}", "{$type}", $this->ip_address_to_db($input->man_ip_address), "{$input->model}", "{$input->manufacturer}", "printer", "{$details->timestamp}", "{$details->timestamp}", "{$details->timestamp}", "audit"); $query = $this->db->query($sql, $data); $system_id = $this->db->insert_id(); $inserted_via = 'audit'; $inserted_user = ''; $sql = "INSERT INTO sys_man_audits ( system_id, system_audits_username, system_audits_type, system_audits_time, timestamp ) VALUES (?, ?, ?, ?, ?)"; $data = array("{$system_id}", "{$inserted_user}", "{$inserted_via}", "{$details->timestamp}", "{$details->timestamp}"); $query = $this->db->query($sql, $data); } } }
/** * Méthode d'interrogation snmp. * Comparée au snmpget elle gère les type compteurs * * @param $oid string oid de la donnée * @param $time_derived boolean optionnel : false par défaut, le compteur doit il être divisé par le temp * @return string */ protected final function _snmpWalk($oid) { trigger_error('start', E_USER_NOTICE); trigger_error("oid={$oid}", E_USER_NOTICE); switch ($this->getInput('version')) { case 'auto': $retour = snmpwalk($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries')); break; case '2': $retour = snmp2_walk($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries')); break; case '3': $retour = snmp3_walk($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries')); break; } $retour = snmpwalk($this->getInput('host') . ":" . $this->getInput('port'), $this->getInput('community'), $oid, $this->getInput('timeout') * 1000000, $this->getInput('retries')); foreach ($retour as $i => $valeur) { $retour[$i] = $this->_makeValueClean($valeur); } return $retour; }
<?php $time = microtime(1); snmp_read_mib("mibs/DFL860E_A1_FW_v2.27.03.25-MIB.txt"); $servers = array('10.10.1.213' => array(), '10.10.10.213' => array(), '10.10.11.213' => array(), '10.10.12.213' => array(), '10.11.2.13' => array()); $cols = array('Name', 'CurrentBps'); $names = array(); $measure = isset($_GET['m']) ? $_GET['m'] : null; $bpsDiv = $measure == 'B' ? 8 : 1; $bpsPrefix = $measure == 'B' ? 'B' : 'b'; foreach ($servers as $server => $info) { foreach ($cols as $key) { $data = snmp2_walk($server, "1234567890", "dfl860ePipe" . $key); foreach ($data as $i => $val) { $val = explode(': ', $data[$i], 2); $val = $val[1]; switch ($key) { case 'Name': $servers[$server][$val] = array(); $names[] = $val; break; case 'CurrentBps': if ($val == 0) { $str = "0 b/s"; } else { $raw = number_format($val, 0) . " b/s"; if ($val > 1048576) { $str = number_format($val / 1048576 / $bpsDiv, 2) . " M" . $bpsPrefix . "/s"; } elseif ($val > 1024) { $str = number_format($val / 1024 / $bpsDiv, 2) . " K" . $bpsPrefix . "/s"; } else {
function readmacHP18($switchf, $macarrayf) { $a = snmp2_walk("{$switchf}", "public", ".1.3.6.1.2.1.17.4.3.1.1.0"); foreach ($a as $val) { $mac_ = substr($val, 12, -1); $pattern = '/([A-F0-9]*)\\ ([A-F0-9]*)\\ ([A-F0-9]*)\\ ([A-F0-9]*)\\ ([A-F0-9]*)\\ ([A-F0-9]*)$/'; preg_match($pattern, $mac_, &$mac); $rmac = $mac[1] . ":" . $mac[2] . ":" . $mac[3] . ":" . $mac[4] . ":" . $mac[5] . ":" . $mac[6]; $ip_ = $switchf; $npattern = '/([0-9]*)\\.([0-9]*)\\.([0-9]*)\\.([0-9]*)$/'; preg_match($npattern, $ip_, &$ip); $network = $ip[2] . "." . $ip[3]; if ($network == "43.128") { $network = "43.28"; } if ($network == "43.126") { $network = "43.26"; } $macarrayf[] = "" . $rmac . ";" . $network . ""; } return $macarrayf; }