| 1 | # SNMP::Info::CDP | ||
| 2 | # | ||
| 3 | # Changes since Version 0.7 Copyright (c) 2004 Max Baker | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Copyright (c) 2002,2003 Regents of the University of California | ||
| 7 | # All rights reserved. | ||
| 8 | # | ||
| 9 | # Redistribution and use in source and binary forms, with or without | ||
| 10 | # modification, are permitted provided that the following conditions are met: | ||
| 11 | # | ||
| 12 | # * Redistributions of source code must retain the above copyright notice, | ||
| 13 | # this list of conditions and the following disclaimer. | ||
| 14 | # * Redistributions in binary form must reproduce the above copyright | ||
| 15 | # notice, this list of conditions and the following disclaimer in the | ||
| 16 | # documentation and/or other materials provided with the distribution. | ||
| 17 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 18 | # names of its contributors may be used to endorse or promote products | ||
| 19 | # derived from this software without specific prior written permission. | ||
| 20 | # | ||
| 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 22 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 25 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 26 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 27 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 28 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 29 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 30 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 31 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 32 | |||
| 33 | package SNMP::Info::CDP; | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use warnings; | ||
| 37 | use Exporter; | ||
| 38 | use SNMP::Info; | ||
| 39 | |||
| 40 | @SNMP::Info::CDP::ISA = qw/SNMP::Info Exporter/; | ||
| 41 | @SNMP::Info::CDP::EXPORT_OK = qw//; | ||
| 42 | |||
| 43 | our | ||
| 44 | ($VERSION, $DEBUG, %FUNCS, %GLOBALS, %MIBS, %MUNGE, $INIT, %CDP_CAPABILITIES); | ||
| 45 | |||
| 46 | $VERSION = '3.985003'; | ||
| 47 | |||
| 48 | # Five data structures required by SNMP::Info | ||
| 49 | %MIBS = ( 'CISCO-CDP-MIB' => 'cdpGlobalRun' ); | ||
| 50 | |||
| 51 | # Notice we dont inherit the default GLOBALS and FUNCS | ||
| 52 | # only the default MUNGE. | ||
| 53 | %GLOBALS = ( | ||
| 54 | 'cdp_run' => 'cdpGlobalRun', | ||
| 55 | 'cdp_interval' => 'cdpGlobalMessageInterval', | ||
| 56 | 'cdp_holdtime' => 'cdpGlobalHoldTime', | ||
| 57 | 'cdp_gid' => 'cdpGlobalDeviceId', | ||
| 58 | ); | ||
| 59 | |||
| 60 | %FUNCS = ( | ||
| 61 | 'cdp_proto' => 'cdpCacheAddressType', | ||
| 62 | 'cdp_addr' => 'cdpCacheAddress', | ||
| 63 | 'cdp_ver' => 'cdpCacheVersion', | ||
| 64 | 'cdp_dev_id' => 'cdpCacheDeviceId', | ||
| 65 | 'cdp_dev_port' => 'cdpCacheDevicePort', | ||
| 66 | 'cdp_platform' => 'cdpCachePlatform', | ||
| 67 | 'cdp_capabilities' => 'cdpCacheCapabilities', | ||
| 68 | 'cdp_domain' => 'cdpCacheVTPMgmtDomain', | ||
| 69 | 'cdp_vlan' => 'cdpCacheNativeVLAN', | ||
| 70 | 'cdp_duplex' => 'cdpCacheDuplex', | ||
| 71 | 'cdp_power' => 'cdpCachePowerConsumption', | ||
| 72 | 'cdp_pri_mgmt_type'=> 'cdpCachePrimaryMgmtAddrType', | ||
| 73 | 'cdp_pri_mgmt_addr'=> 'cdpCachePrimaryMgmtAddr', | ||
| 74 | 'cdp_sec_mgmt_type'=> 'cdpCacheSecondaryMgmtAddrType', | ||
| 75 | 'cdp_sec_mgmt_addr'=> 'cdpCacheSecondaryMgmtAddr', | ||
| 76 | ); | ||
| 77 | |||
| 78 | %MUNGE = ( | ||
| 79 | 'cdp_capabilities' => \&SNMP::Info::munge_bits, | ||
| 80 | 'cdp_platform' => \&SNMP::Info::munge_null, | ||
| 81 | 'cdp_domain' => \&SNMP::Info::munge_null, | ||
| 82 | 'cdp_ver' => \&SNMP::Info::munge_null, | ||
| 83 | 'cdp_ip' => \&SNMP::Info::munge_ip, | ||
| 84 | 'cdp_power' => \&munge_power, | ||
| 85 | ); | ||
| 86 | |||
| 87 | %CDP_CAPABILITIES = ( | ||
| 88 | 'Router' => 0x001, | ||
| 89 | 'Trans-Bridge' => 0x002, | ||
| 90 | 'Source-Route-Bridge' => 0x004, | ||
| 91 | 'Switch' => 0x008, | ||
| 92 | 'Host' => 0x010, | ||
| 93 | 'IGMP' => 0x020, | ||
| 94 | 'Repeater' => 0x040, | ||
| 95 | 'VoIP-Phone' => 0x080, | ||
| 96 | 'Remotely-Managed-Device' => 0x100, | ||
| 97 | 'Supports-STP-Dispute' => 0x200, | ||
| 98 | 'Two-port Mac Relay' => 0x400, | ||
| 99 | ); | ||
| 100 | |||
| 101 | sub munge_power { | ||
| 102 | my $power = shift; | ||
| 103 | my $decimal = substr( $power, -3 ); | ||
| 104 | $power =~ s/$decimal$/\.$decimal/; | ||
| 105 | return $power; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub hasCDP { | ||
| 109 | my $cdp = shift; | ||
| 110 | |||
| 111 | # Check the global that is supposed to indicate CDP is running | ||
| 112 | my $cdp_run = $cdp->cdp_run(); | ||
| 113 | return 1 if $cdp_run; | ||
| 114 | |||
| 115 | # SNMP v1 clients don't have the globals, fallback | ||
| 116 | # by checking if it would report neighbors | ||
| 117 | my $cdp_ip = $cdp->cdp_ip() || {}; | ||
| 118 | return 1 if scalar keys %$cdp_ip; | ||
| 119 | |||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub cdp_if { | ||
| 124 | my $cdp = shift; | ||
| 125 | |||
| 126 | my $cdp_ip = $cdp->cdp_ip(); | ||
| 127 | unless ( defined $cdp_ip ) { | ||
| 128 | $cdp->error_throw( | ||
| 129 | "SNMP::Info::CDP:cdp_if() - Device doesn't have cdp_ip() data. Can't fake cdp_index()" | ||
| 130 | ); | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | my %cdp_if; | ||
| 135 | foreach my $key ( keys %$cdp_ip ) { | ||
| 136 | next unless defined $key; | ||
| 137 | my $iid = $key; | ||
| 138 | |||
| 139 | # Truncate .1 from cdp cache entry | ||
| 140 | $iid =~ s/\.\d+$//; | ||
| 141 | $cdp_if{$key} = $iid; | ||
| 142 | } | ||
| 143 | |||
| 144 | return \%cdp_if; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub cdp_ip { | ||
| 148 | my $cdp = shift; | ||
| 149 | my $partial = shift; | ||
| 150 | |||
| 151 | my $cdp_addr = $cdp->cdp_addr($partial) || {}; | ||
| 152 | my $cdp_proto = $cdp->cdp_proto($partial) || {}; | ||
| 153 | |||
| 154 | my %cdp_ip; | ||
| 155 | foreach my $key ( keys %$cdp_addr ) { | ||
| 156 | my $addr = $cdp_addr->{$key}; | ||
| 157 | my $proto = $cdp_proto->{$key}; | ||
| 158 | next unless defined $addr; | ||
| 159 | next if ( defined $proto and $proto ne 'ip' ); | ||
| 160 | |||
| 161 | my $ip = join( '.', unpack( 'C4', $addr ) ); | ||
| 162 | $cdp_ip{$key} = $ip; | ||
| 163 | } | ||
| 164 | return \%cdp_ip; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub cdp_cap { | ||
| 168 | my $cdp = shift; | ||
| 169 | my $partial = shift; | ||
| 170 | |||
| 171 | # Some devices return a hex-string, others return a space separated | ||
| 172 | # string, we need the raw data to determine return value and | ||
| 173 | # take appropriate action | ||
| 174 | my $cdp_caps = $cdp->cdp_capabilities_raw($partial) || {}; | ||
| 175 | |||
| 176 | my %cdp_cap; | ||
| 177 | foreach my $key ( keys %$cdp_caps ) { | ||
| 178 | my $cap_raw = $cdp_caps->{$key}; | ||
| 179 | next unless $cap_raw; | ||
| 180 | |||
| 181 | # Simple check, smallest single string is either Host or IGMP with a | ||
| 182 | # space added on the end for a length of 5, hex string is normally | ||
| 183 | # 4 bytes, but since only one byte was traditionally needed process | ||
| 184 | # as hex for a length of 4 or less | ||
| 185 | if ( length $cap_raw < 5 ) { | ||
| 186 | my $cap_hex = join( '', | ||
| 187 | map { sprintf "%x", $_ } unpack( 'C*', $cap_raw ) ); | ||
| 188 | foreach my $capability ( keys %CDP_CAPABILITIES ) { | ||
| 189 | if ( ( hex $cap_hex ) & $CDP_CAPABILITIES{$capability} ) { | ||
| 190 | push( @{ $cdp_cap{$key} }, $capability ); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | my @caps = split /\s/, $cap_raw; | ||
| 196 | push( @{ $cdp_cap{$key} }, @caps ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return \%cdp_cap; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub cdp_id { | ||
| 203 | my $cdp = shift; | ||
| 204 | my $partial = shift; | ||
| 205 | |||
| 206 | my $ch = $cdp->cdp_dev_id($partial) || {}; | ||
| 207 | |||
| 208 | my %cdp_id; | ||
| 209 | foreach my $key ( sort keys %$ch ) { | ||
| 210 | my $id = $ch->{$key}; | ||
| 211 | next unless $id; | ||
| 212 | $id = SNMP::Info::munge_mac($id) || SNMP::Info::munge_null($id); | ||
| 213 | $cdp_id{$key} = $id; | ||
| 214 | } | ||
| 215 | return \%cdp_id; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub cdp_port { | ||
| 219 | my $cdp = shift; | ||
| 220 | my $partial = shift; | ||
| 221 | |||
| 222 | my $ch = $cdp->cdp_dev_port($partial) || {}; | ||
| 223 | |||
| 224 | # most devices return a string with the interface name here (Port-ID TLV) | ||
| 225 | # see https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/cdp/command/cdp-cr-book/cdp-cr-a1.html | ||
| 226 | # it seems however that some devices report hex encoded mac addresses for this, see | ||
| 227 | # https://github.com/netdisco/snmp-info/issues/252 | ||
| 228 | # once these bad devices get known we can figure out workarounds for them | ||
| 229 | |||
| 230 | my %cdp_port; | ||
| 231 | foreach my $key ( sort keys %$ch ) { | ||
| 232 | my $port = $ch->{$key}; | ||
| 233 | next unless $port; | ||
| 234 | $port = SNMP::Info::munge_null($port); | ||
| 235 | $cdp_port{$key} = $port; | ||
| 236 | } | ||
| 237 | return \%cdp_port; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; | ||
| 241 | __END__ |