| 1 | # SNMP::Info::Layer2::C2900 | ||
| 2 | # | ||
| 3 | # Copyright (c) 2008 Max Baker changes from version 0.8 and beyond. | ||
| 4 | # | ||
| 5 | # Copyright (c) 2002,2003 Regents of the University of California | ||
| 6 | # All rights reserved. | ||
| 7 | # | ||
| 8 | # Redistribution and use in source and binary forms, with or without | ||
| 9 | # modification, are permitted provided that the following conditions are met: | ||
| 10 | # | ||
| 11 | # * Redistributions of source code must retain the above copyright notice, | ||
| 12 | # this list of conditions and the following disclaimer. | ||
| 13 | # * Redistributions in binary form must reproduce the above copyright | ||
| 14 | # notice, this list of conditions and the following disclaimer in the | ||
| 15 | # documentation and/or other materials provided with the distribution. | ||
| 16 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 17 | # names of its contributors may be used to endorse or promote products | ||
| 18 | # derived from this software without specific prior written permission. | ||
| 19 | # | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 24 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | |||
| 32 | package SNMP::Info::Layer2::C2900; | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | use Exporter; | ||
| 37 | use SNMP::Info::Layer2::Cisco; | ||
| 38 | |||
| 39 | @SNMP::Info::Layer2::C2900::ISA = qw/SNMP::Info::Layer2::Cisco Exporter/; | ||
| 40 | @SNMP::Info::Layer2::C2900::EXPORT_OK = qw//; | ||
| 41 | |||
| 42 | our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE); | ||
| 43 | |||
| 44 | $VERSION = '3.985003'; | ||
| 45 | |||
| 46 | %GLOBALS = ( | ||
| 47 | %SNMP::Info::Layer2::Cisco::GLOBALS, | ||
| 48 | ); | ||
| 49 | |||
| 50 | %FUNCS = ( | ||
| 51 | %SNMP::Info::Layer2::Cisco::FUNCS, | ||
| 52 | 'i_name' => 'ifAlias', | ||
| 53 | |||
| 54 | # C2900PortEntry | ||
| 55 | 'c2900_p_index' => 'c2900PortIfIndex', | ||
| 56 | 'c2900_p_duplex' => 'c2900PortDuplexStatus', | ||
| 57 | 'c2900_p_duplex_admin' => 'c2900PortDuplexState', | ||
| 58 | 'c2900_p_speed_admin' => 'c2900PortAdminSpeed', | ||
| 59 | ); | ||
| 60 | |||
| 61 | %MIBS = ( | ||
| 62 | %SNMP::Info::Layer2::Cisco::MIBS, | ||
| 63 | 'CISCO-C2900-MIB' => 'ciscoC2900MIB', | ||
| 64 | ); | ||
| 65 | |||
| 66 | %MUNGE = ( | ||
| 67 | %SNMP::Info::Layer2::Cisco::MUNGE, | ||
| 68 | ); | ||
| 69 | |||
| 70 | sub vendor { | ||
| 71 | return 'cisco'; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub cisco_comm_indexing { | ||
| 75 | return 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub i_duplex { | ||
| 79 | my $c2900 = shift; | ||
| 80 | my $partial = shift; | ||
| 81 | |||
| 82 | my $interfaces = $c2900->interfaces($partial); | ||
| 83 | my $c2900_p_index = $c2900->c2900_p_index() || {}; | ||
| 84 | my $c2900_p_duplex = $c2900->c2900_p_duplex(); | ||
| 85 | |||
| 86 | my %reverse_2900 = reverse %$c2900_p_index; | ||
| 87 | |||
| 88 | my %i_duplex; | ||
| 89 | foreach my $if ( keys %$interfaces ) { | ||
| 90 | my $port_2900 = $reverse_2900{$if}; | ||
| 91 | next unless defined $port_2900; | ||
| 92 | my $duplex = $c2900_p_duplex->{$port_2900}; | ||
| 93 | next unless defined $duplex; | ||
| 94 | |||
| 95 | $duplex = 'half' if $duplex =~ /half/i; | ||
| 96 | $duplex = 'full' if $duplex =~ /full/i; | ||
| 97 | $i_duplex{$if} = $duplex; | ||
| 98 | } | ||
| 99 | return \%i_duplex; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub i_duplex_admin { | ||
| 103 | my $c2900 = shift; | ||
| 104 | my $partial = shift; | ||
| 105 | |||
| 106 | my $interfaces = $c2900->interfaces($partial); | ||
| 107 | my $c2900_p_index = $c2900->c2900_p_index() || {}; | ||
| 108 | my $c2900_p_admin = $c2900->c2900_p_duplex_admin(); | ||
| 109 | |||
| 110 | my %reverse_2900 = reverse %$c2900_p_index; | ||
| 111 | |||
| 112 | my %i_duplex_admin; | ||
| 113 | foreach my $if ( keys %$interfaces ) { | ||
| 114 | my $port_2900 = $reverse_2900{$if}; | ||
| 115 | next unless defined $port_2900; | ||
| 116 | my $duplex = $c2900_p_admin->{$port_2900}; | ||
| 117 | next unless defined $duplex; | ||
| 118 | |||
| 119 | $duplex = 'half' if $duplex =~ /half/i; | ||
| 120 | $duplex = 'full' if $duplex =~ /full/i; | ||
| 121 | $duplex = 'auto' if $duplex =~ /auto/i; | ||
| 122 | $i_duplex_admin{$if} = $duplex; | ||
| 123 | } | ||
| 124 | return \%i_duplex_admin; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub i_speed_admin { | ||
| 128 | my $c2900 = shift; | ||
| 129 | my $partial = shift; | ||
| 130 | |||
| 131 | my $interfaces = $c2900->interfaces($partial); | ||
| 132 | my $c2900_p_index = $c2900->c2900_p_index() || {}; | ||
| 133 | my $c2900_p_admin = $c2900->c2900_p_speed_admin(); | ||
| 134 | |||
| 135 | my %reverse_2900 = reverse %$c2900_p_index; | ||
| 136 | |||
| 137 | my %i_speed_admin; | ||
| 138 | foreach my $if ( keys %$interfaces ) { | ||
| 139 | my $port_2900 = $reverse_2900{$if}; | ||
| 140 | next unless defined $port_2900; | ||
| 141 | my $speed = $c2900_p_admin->{$port_2900}; | ||
| 142 | next unless defined $speed; | ||
| 143 | |||
| 144 | $speed = 'auto' if $speed eq 'autoDetect'; | ||
| 145 | $speed = '10 Mbps' if $speed eq 's10000000'; | ||
| 146 | $speed = '100 Mbps' if $speed eq 's100000000'; | ||
| 147 | $i_speed_admin{$if} = $speed; | ||
| 148 | } | ||
| 149 | return \%i_speed_admin; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub set_i_speed_admin { | ||
| 153 | my $c2900 = shift; | ||
| 154 | my ( $speed, $iid ) = @_; | ||
| 155 | |||
| 156 | # map speeds to those the switch will understand | ||
| 157 | my %speeds = qw/auto 1 10 10000000 100 100000000/; | ||
| 158 | |||
| 159 | my $c2900_p_index = $c2900->c2900_p_index() || {}; | ||
| 160 | my %reverse_2900 = reverse %$c2900_p_index; | ||
| 161 | |||
| 162 | $speed = lc($speed); | ||
| 163 | |||
| 164 | return unless defined $speeds{$speed}; | ||
| 165 | |||
| 166 | # account for weirdness of c2900 mib | ||
| 167 | $iid = $reverse_2900{$iid}; | ||
| 168 | |||
| 169 | return $c2900->set_c2900_p_speed_admin( $speeds{$speed}, $iid ); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub set_i_duplex_admin { | ||
| 173 | my $c2900 = shift; | ||
| 174 | my ( $duplex, $iid ) = @_; | ||
| 175 | |||
| 176 | # map a textual duplex to an integer one the switch understands | ||
| 177 | my %duplexes = qw/full 1 half 2 auto 3/; | ||
| 178 | |||
| 179 | my $c2900_p_index = $c2900->c2900_p_index() || {}; | ||
| 180 | my %reverse_2900 = reverse %$c2900_p_index; | ||
| 181 | |||
| 182 | $duplex = lc($duplex); | ||
| 183 | |||
| 184 | return unless defined $duplexes{$duplex}; | ||
| 185 | |||
| 186 | # account for weirdness of c2900 mib | ||
| 187 | $iid = $reverse_2900{$iid}; | ||
| 188 | |||
| 189 | return $c2900->set_c2900_p_duplex_admin( $duplexes{$duplex}, $iid ); | ||
| 190 | } | ||
| 191 | |||
| 192 | # Use i_description for port key, cuz i_name can be manually entered. | ||
| 193 | sub interfaces { | ||
| 194 | my $c2900 = shift; | ||
| 195 | my $partial = shift; | ||
| 196 | |||
| 197 | my $interfaces = $c2900->i_index($partial) || {}; | ||
| 198 | my $i_descr = $c2900->i_description($partial) || {}; | ||
| 199 | |||
| 200 | my %if; | ||
| 201 | foreach my $iid ( keys %$interfaces ) { | ||
| 202 | my $port = $i_descr->{$iid}; | ||
| 203 | next unless defined $port; | ||
| 204 | |||
| 205 | $port =~ s/\./\//g if ( $port =~ /\d+\.\d+$/ ); | ||
| 206 | $port =~ s/[^\d\/,()\w]+//gi; | ||
| 207 | |||
| 208 | $if{$iid} = $port; | ||
| 209 | } | ||
| 210 | |||
| 211 | return \%if; | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; | ||
| 215 | __END__ |