| 1 | # SNMP::Info::SONMP | ||
| 2 | # | ||
| 3 | # Copyright (c) 2016 Eric Miller | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # * Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # * Redistributions in binary form must reproduce the above copyright | ||
| 12 | # notice, this list of conditions and the following disclaimer in the | ||
| 13 | # documentation and/or other materials provided with the distribution. | ||
| 14 | # * Neither the name of the University of California, Santa Cruz nor the | ||
| 15 | # names of its contributors may be used to endorse or promote products | ||
| 16 | # derived from this software without specific prior written permission. | ||
| 17 | # | ||
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 22 | # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 28 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 29 | |||
| 30 | package SNMP::Info::SONMP; | ||
| 31 | |||
| 32 | use warnings; | ||
| 33 | use strict; | ||
| 34 | use Exporter; | ||
| 35 | use SNMP::Info; | ||
| 36 | |||
| 37 | @SNMP::Info::SONMP::ISA = qw/SNMP::Info Exporter/; | ||
| 38 | @SNMP::Info::SONMP::EXPORT_OK = qw//; | ||
| 39 | |||
| 40 | our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE); | ||
| 41 | |||
| 42 | $VERSION = '3.985003'; | ||
| 43 | |||
| 44 | %MIBS = ( | ||
| 45 | 'SYNOPTICS-ROOT-MIB' => 'synoptics', | ||
| 46 | 'S5-ETH-MULTISEG-TOPOLOGY-MIB' => 's5EnMsTopInfo', | ||
| 47 | ); | ||
| 48 | |||
| 49 | %GLOBALS = ( | ||
| 50 | 'sonmp_gid' => 's5EnMsTopIpAddr', | ||
| 51 | 'sonmp_run' => 's5EnMsTopStatus', | ||
| 52 | ); | ||
| 53 | |||
| 54 | %FUNCS = ( | ||
| 55 | |||
| 56 | # From S5-ETH-MULTISEG-TOPOLOGY-MIB::s5EnMsTopNmmTable | ||
| 57 | 'sonmp_topo_slot' => 's5EnMsTopNmmSlot', | ||
| 58 | 'sonmp_topo_port' => 's5EnMsTopNmmPort', | ||
| 59 | 'sonmp_topo_ip' => 's5EnMsTopNmmIpAddr', | ||
| 60 | 'sonmp_topo_seg' => 's5EnMsTopNmmSegId', | ||
| 61 | 'sonmp_topo_mac' => 's5EnMsTopNmmMacAddr', | ||
| 62 | 'sonmp_topo_platform' => 's5EnMsTopNmmChassisType', | ||
| 63 | 'sonmp_topo_localseg' => 's5EnMsTopNmmLocalSeg', | ||
| 64 | |||
| 65 | # From S5-ETH-MULTISEG-TOPOLOGY-MIB::s5EnMsTopNmmEnhancedTable | ||
| 66 | # Note: indexes are not-accessible in enhanced table | ||
| 67 | 'sonmp_topo_e_mac' => 's5EnMsTopNmmEnhancedMacAddr', | ||
| 68 | 'sonmp_topo_e_platform' => 's5EnMsTopNmmEnhancedChassisType', | ||
| 69 | ); | ||
| 70 | |||
| 71 | %MUNGE = ( | ||
| 72 | 'sonmp_topo_mac' => \&SNMP::Info::munge_mac, | ||
| 73 | 'sonmp_topo_e_mac' => \&SNMP::Info::munge_mac, | ||
| 74 | ); | ||
| 75 | |||
| 76 | sub index_factor { | ||
| 77 | return 32; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub slot_offset { | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub port_offset { | ||
| 85 | return 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub hasSONMP { | ||
| 89 | my $sonmp = shift; | ||
| 90 | |||
| 91 | return 1 if defined $sonmp->sonmp_run(); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | # Break up the s5EnMsTopNmmEntry or s5EnMsTopNmmEnhancedEntry INDEX | ||
| 96 | # into Port, SubPort, IP, and Segment Id. | ||
| 97 | sub _sonmp_topnmm_index { | ||
| 98 | my @values = split( /\./, shift ); | ||
| 99 | my $seg = pop(@values); | ||
| 100 | my $s = shift(@values); | ||
| 101 | my $p = shift(@values); | ||
| 102 | my $sp = 0; | ||
| 103 | |||
| 104 | if ( scalar @values > 4 ) { | ||
| 105 | $sp = shift(@values); | ||
| 106 | } | ||
| 107 | |||
| 108 | return ( $s, $p, $sp, join( '.', @values ), $seg ); | ||
| 109 | } | ||
| 110 | |||
| 111 | # Note: Using platform to get the index because it's commonly accessible and | ||
| 112 | # will be cached after first method call, we now extract the other values | ||
| 113 | # from the index. | ||
| 114 | sub sonmp_if { | ||
| 115 | my $sonmp = shift; | ||
| 116 | my $partial = shift; | ||
| 117 | |||
| 118 | my $sonmp_topo_idx | ||
| 119 | = $sonmp->sonmp_topo_platform($partial) | ||
| 120 | || $sonmp->sonmp_topo_e_platform($partial) | ||
| 121 | || {}; | ||
| 122 | my $interfaces = $sonmp->interfaces() || {}; | ||
| 123 | my %r_interfaces = reverse %$interfaces; | ||
| 124 | my $index_factor = $sonmp->index_factor(); | ||
| 125 | my $slot_offset = $sonmp->slot_offset(); | ||
| 126 | my $port_offset = $sonmp->port_offset(); | ||
| 127 | my $model = $sonmp->model(); | ||
| 128 | |||
| 129 | my %sonmp_if; | ||
| 130 | foreach my $idx ( keys %$sonmp_topo_idx ) { | ||
| 131 | my ( $slot, $port, $sub_port, $ip, $seg_id ) | ||
| 132 | = _sonmp_topnmm_index($idx); | ||
| 133 | next unless defined $port; | ||
| 134 | next if $port == 0; | ||
| 135 | |||
| 136 | if ( $model eq 'Baystack Hub' ) { | ||
| 137 | my $comidx = $slot; | ||
| 138 | if ( !( $comidx % 5 ) ) { | ||
| 139 | $slot = ( $slot / 5 ); | ||
| 140 | } | ||
| 141 | elsif ( $comidx =~ /[16]$/ ) { | ||
| 142 | $slot = int( $slot / 5 ); | ||
| 143 | $port = 25; | ||
| 144 | } | ||
| 145 | elsif ( $comidx =~ /[27]$/ ) { | ||
| 146 | $slot = int( $slot / 5 ); | ||
| 147 | $port = 26; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | my $index; | ||
| 152 | my $int = $sub_port ? "$slot.$port.$sub_port" : "$slot.$port"; | ||
| 153 | |||
| 154 | if ( exists $r_interfaces{$int} ) { | ||
| 155 | $index = $r_interfaces{$int}; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $index = ( ( $slot - $slot_offset ) * $index_factor ) | ||
| 159 | + ( $port - $port_offset ); | ||
| 160 | } | ||
| 161 | $sonmp_if{$idx} = $index; | ||
| 162 | } | ||
| 163 | return \%sonmp_if; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub sonmp_ip { | ||
| 167 | my $sonmp = shift; | ||
| 168 | my $partial = shift; | ||
| 169 | |||
| 170 | my $sonmp_topo_idx | ||
| 171 | = $sonmp->sonmp_topo_platform($partial) | ||
| 172 | || $sonmp->sonmp_topo_e_platform($partial) | ||
| 173 | || {}; | ||
| 174 | |||
| 175 | my %sonmp_ip; | ||
| 176 | foreach my $idx ( keys %$sonmp_topo_idx ) { | ||
| 177 | my ( $slot, $port, $sub_port, $ip, $seg_id ) | ||
| 178 | = _sonmp_topnmm_index($idx); | ||
| 179 | next unless defined $port; | ||
| 180 | next if $port == 0; | ||
| 181 | |||
| 182 | $sonmp_ip{$idx} = $ip; | ||
| 183 | } | ||
| 184 | return \%sonmp_ip; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub sonmp_port { | ||
| 188 | my $sonmp = shift; | ||
| 189 | my $partial = shift; | ||
| 190 | |||
| 191 | my $sonmp_topo_idx | ||
| 192 | = $sonmp->sonmp_topo_platform($partial) | ||
| 193 | || $sonmp->sonmp_topo_e_platform($partial) | ||
| 194 | || {}; | ||
| 195 | |||
| 196 | my $sonmp_topo_port = $sonmp->sonmp_topo_port($partial) || {}; | ||
| 197 | my $sonmp_topo_seg = $sonmp->sonmp_topo_seg($partial) || {}; | ||
| 198 | my $sonmp_topo_platform = $sonmp->sonmp_topo_platform($partial) || {}; | ||
| 199 | |||
| 200 | my %sonmp_port; | ||
| 201 | foreach my $idx ( keys %$sonmp_topo_idx ) { | ||
| 202 | my ( $slot, $port, $sub_port, $ip, $seg_id ) | ||
| 203 | = _sonmp_topnmm_index($idx); | ||
| 204 | next unless defined $port; | ||
| 205 | next if $port == 0; | ||
| 206 | |||
| 207 | my $platform = $sonmp_topo_idx->{$idx}; | ||
| 208 | |||
| 209 | # AP-222x Series does not adhere to port numbering | ||
| 210 | if ( $platform =~ /AccessPoint/i ) { | ||
| 211 | $sonmp_port{$idx} = 'dp0'; | ||
| 212 | } | ||
| 213 | |||
| 214 | # BayHubs send the lower three bytes of the MAC not the slot/port | ||
| 215 | elsif ( $platform =~ /BayStack[E12]/ ) { | ||
| 216 | $sonmp_port{$idx} = 'unknown'; | ||
| 217 | } | ||
| 218 | else { | ||
| 219 | |||
| 220 | # Segment id is (256 * remote slot_num) + (remote_port) | ||
| 221 | # Enhanced is subport +((256 * slot) + port) | ||
| 222 | my $subport = 0; | ||
| 223 | if ( $seg_id > 65535 ) { | ||
| 224 | $subport = int( $seg_id / 65536 ); | ||
| 225 | $seg_id = $seg_id % 65536; | ||
| 226 | } | ||
| 227 | |||
| 228 | my $port = $seg_id % 256; | ||
| 229 | my $slot = int( $seg_id / 256 ); | ||
| 230 | |||
| 231 | $sonmp_port{$idx} | ||
| 232 | = $subport ? "$slot.$port.$subport" : "$slot.$port"; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | return \%sonmp_port; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub sonmp_platform { | ||
| 239 | my $sonmp = shift; | ||
| 240 | my $partial = shift; | ||
| 241 | |||
| 242 | my $sonmp_topo_idx | ||
| 243 | = $sonmp->sonmp_topo_platform($partial) | ||
| 244 | || $sonmp->sonmp_topo_e_platform($partial) | ||
| 245 | || {}; | ||
| 246 | |||
| 247 | my %sonmp_platform; | ||
| 248 | foreach my $idx ( keys %$sonmp_topo_idx ) { | ||
| 249 | my ( $slot, $port, $sub_port, $ip, $seg_id ) | ||
| 250 | = _sonmp_topnmm_index($idx); | ||
| 251 | next unless defined $port; | ||
| 252 | next if $port == 0; | ||
| 253 | |||
| 254 | my $platform = $sonmp_topo_idx->{$idx}; | ||
| 255 | |||
| 256 | $sonmp_platform{$idx} = $platform; | ||
| 257 | } | ||
| 258 | return \%sonmp_platform; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub mac { | ||
| 262 | my $sonmp = shift; | ||
| 263 | |||
| 264 | my $sonmp_topo_idx | ||
| 265 | = $sonmp->sonmp_topo_mac() | ||
| 266 | || $sonmp->sonmp_topo_e_mac() | ||
| 267 | || {}; | ||
| 268 | |||
| 269 | foreach my $idx ( keys %$sonmp_topo_idx ) { | ||
| 270 | my ( $slot, $port, $sub_port, $ip, $seg_id ) | ||
| 271 | = _sonmp_topnmm_index($idx); | ||
| 272 | next unless $port == 0; | ||
| 273 | my $mac = $sonmp_topo_idx->{$idx}; | ||
| 274 | return $mac; | ||
| 275 | } | ||
| 276 | |||
| 277 | # Topology turned off, not supported. | ||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | 1; | ||
| 282 | __END__ |