From 730a1e6edc8290ed3a7007f2ac47b7c84813ebc4 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Tue, 10 Nov 2009 15:43:13 +0000 Subject: [PATCH] rewritten CDP polling git-svn-id: http://www.observium.org/svn/observer/trunk@508 61d68cd4-352d-0410-923a-c4978735b2b8 --- discover-cdp.php | 63 ++++++++++++++++--------------- includes/functions-poller.inc.php | 22 ++++++++++- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/discover-cdp.php b/discover-cdp.php index 6ca754b0e..20f526f75 100755 --- a/discover-cdp.php +++ b/discover-cdp.php @@ -1,67 +1,67 @@ #!/usr/bin/php getports(); -# $cdp = $snmp->explore_cdp($ports); - -# $cdp_links = ""; - -# foreach (array_keys($cdp) as $key) { -# $port = $ports[$key]; -# $link = $cdp[$key]; -# $cdp_links .= $hostname . "," . $port['desc'] . "," . $link['host'] . "," . $link['port'] . "\n"; -# } - -# $cdp_links = trim($cdp_links); + echo("CISCO-CDP-MIB: "); + + unset($cdp_array); + $cdp_array = snmpwalk_cache_twopart_oid("cdpCache", $device, $cdp_array, "CISCO-CDP-MIB"); + $cdp_array = $cdp_array[$device[device_id]]; + unset($cdp_links); + foreach( array_keys($cdp_array) as $key) { + $interface = mysql_fetch_array(mysql_query("SELECT * FROM `interfaces` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '".$key."'")); + $cdp_if_array = $cdp_array[$key]; + foreach( array_keys($cdp_if_array) as $entry_key) { + $cdp_entry_array = $cdp_if_array[$entry_key]; + if($device['hostname'] && $interface['ifDescr'] && $cdp_entry_array['cdpCacheDeviceId'] && $cdp_entry_array['cdpCacheDevicePort']){ + $cdp_links .= $device['hostname'] . "," . $interface['ifDescr'] . "," . $cdp_entry_array['cdpCacheDeviceId'] . "," . $cdp_entry_array['cdpCacheDevicePort'] . "\n"; + } + } + } + #echo("$cdp_links"); foreach ( explode("\n" ,$cdp_links) as $link ) { if ($link == "") { break; } list($src_host,$src_if, $dst_host, $dst_if) = explode(",", $link); - $dst_host = strtolower($dst_host); + $dst_host = strtolower($dst_host); $dst_if = strtolower($dst_if); $src_host = strtolower($src_host); $src_if = strtolower($src_if); $ip = gethostbyname($dst_host); - if ( match_network($config['nets'], $ip) ) { + if ( match_network($config['nets'], $ip) ) { if ( mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `sysName` = '$dst_host'"), 0) == '0' ) { if($config['cdp_autocreate']) { echo("++ Creating: $dst_host \n"); createHost ($dst_host, $community, "v2c"); } - } else { - echo("."); + } else { + echo("."); } - } else { - echo("!($dst_host)"); + } else { + echo("!($dst_host)"); } if ( mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `sysName` = '$dst_host'"), 0) == '1' && @@ -83,7 +83,10 @@ while ($device = mysql_fetch_array($device_query)) { } else { } +# } } + + echo("\n"); } echo("\n"); diff --git a/includes/functions-poller.inc.php b/includes/functions-poller.inc.php index 213ade38e..eb3ee4260 100644 --- a/includes/functions-poller.inc.php +++ b/includes/functions-poller.inc.php @@ -50,9 +50,29 @@ global $config; return $array; } +function snmpwalk_cache_twopart_oid($oid, $device, $array, $mib = 0) { + global $config; + $cmd = $config['snmpbulkwalk'] . " -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " " . + $device['hostname'].":".$device['port'] . " "; + if($mib) { $cmd .= "-m $mib "; } + $cmd .= $oid; + $data = trim(shell_exec($cmd)); + $device_id = $device['device_id']; + foreach(explode("\n", $data) as $entry) { + list($oid,$value) = explode("=", $entry); + $oid = trim($oid); $value = trim($value); + list($oid, $first, $second) = explode(".", $oid); + if (!strstr($this_value, "No Such Instance currently exists at this OID") && $oid && $first && $second) { + $array[$device_id][$first][$second][$oid] = $value; + } + } + return $array; +} + function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0) { global $config; - $cmd = $config['snmpbulkwalk'] . " -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " " . $device['hostname'].":".$device['port'] . " "; + $cmd = $config['snmpbulkwalk'] . " -O Qs -" . $device['snmpver'] . " -c " . $device['community'] . " " . + $device['hostname'].":".$device['port'] . " "; if($mib) { $cmd .= "-m $mib "; } $cmd .= $oid; $data = trim(shell_exec($cmd));