-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetMacsFromQBridgeMib.pm
169 lines (154 loc) · 6.51 KB
/
GetMacsFromQBridgeMib.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package GetMacsFromQBridgeMib;
use strict;
use Log::Log4perl qw(get_logger);
# sub GetPortToVlanTableFromQBridgeMib ($$$$$) {
# my $Switch = shift; # passed in
# my $Session = shift; # passed in
# my $IfToIfNameRef = shift; # passed in
# my $BifNbrToIfNbrRef = shift; # passed in
# my $VlansRef = shift; # passed in empty, filled by this function
# my $logger = get_logger('log4');
# $logger->debug("called");
# # get a hash of bitfields, one for each VLAN on the switch
# my %dot1qVlanCurrentEgressPorts = SwitchUtils::GetVlanBitFields($Session,
# 'dot1qVlanCurrentEgressPorts');
# if (%dot1qVlanCurrentEgressPorts) {
# $logger->debug("got dot1qVlanCurrentEgressPorts bit field from Q-BRIDGE MIB");
# foreach my $vlan (keys %dot1qVlanCurrentEgressPorts) {
# $logger->debug("processing vlan $vlan");
# # $logger->debug("Size of scalar is " . size( $dot1qVlanCurrentEgressPorts{$vlan} ) . " bytes");
# # if ($vlan == 402) {
# for (my $bit=1; $bit<4094; $bit++) { # dbg
# if (vec $dot1qVlanCurrentEgressPorts{$vlan}, $bit, 1) {
# $logger->debug("bit number $bit of bit vector for vlan $vlan is on");
# }
# }
# # }
# foreach my $ifnbr (sort keys %{$IfToIfNameRef}) {
# my $PortName = $$IfToIfNameRef{$ifnbr};
# $logger->debug("Portname for interface $ifnbr is \"$PortName\"");
# if (vec $dot1qVlanCurrentEgressPorts{$vlan}, $ifnbr+1, 1) {
# $logger->debug("setting VlansRef{$PortName} to $vlan");
# $$VlansRef{$PortName} = $vlan;
# }
# }
# }
# } else {
# $logger->debug("Couldn't get dot1qVlanCurrentEgressPorts from Q-bridge MIB.");
# }
# $logger->debug("returning");
# return;
# }
#
# If the switch supports the Q-BRIDGE MIB (802.1q), use it. I've
# learned that to process the bitfields in the Q-BRIDGE MIB in
# Ciscos, I need the BifNbrToIfNbr hash. Also, there are issues of
# endian-ness. Anyway, Foundry switches have a 1-to-1 BifNbrToIfNbr
# table, so it has no effect. For some reason, I'm not getting all
# 24+2+2 ports from the Foundry - I'm just getting 1-22.
#
# my %TmpVlans;
# GetPortToVlanTableFromQBridgeMib($Switch, $localSession, $IfToIfNameRef, \%BifNbrToIfNbr, \%TmpVlans);
# foreach my $vlan (sort keys %TmpVlans) {
# $logger->debug("-------------TmpVlans{$vlan} = \"$TmpVlans{$vlan}\"");
# }
sub GetMacsFromQBridgeMib($$$$) {
my $Switch = shift;
my $Session = shift;
my $IfToIfNameRef = shift;
my $PortsRef = shift;
my $logger = get_logger('log5');
$logger->debug("called");
#
# Get the table that maps bridge interface numbers to ifEntry numbers.
#
my %BifNbrToIfNbr;
# $localSession->debug(DEBUG_ALL);
my $status = SwitchUtils::GetSnmpTable($Session, # SNMP session
'dot1dBasePortIfIndex', # table name
$Constants::INTERFACE, # table type
\%BifNbrToIfNbr); # returned table contents
# $localSession->debug(DEBUG_NONE);
if ($status != $Constants::SUCCESS) {
$logger->debug("returning, couldn't get dot1dBasePortIfIndex (BifNbrToIfNbr) table");
return;
}
# SwitchUtils::DbgPrintHash('BifNbrToIfNbr', \%BifNbrToIfNbr); # dbg
my $TableName = 'dot1qTpFdbTable';
if (!exists $Constants::SnmpOids{$TableName}) {
$logger->fatal("Internal error: Unknown SNMP OID $TableName");
exit;
}
my $TableOid = $Constants::SnmpOids{$TableName};
my $Table = $Session->get_table($TableOid);
if (!defined $Table) {
$logger->debug("Couldn't get $TableName table: " . $Session->error() .
", SNMP error status: " . $Session->error_status() .
", SNMP error index: " . $Session->error_index);
$logger->debug("returning");
return;
}
my %ports;
my %statuses;
foreach my $Oid (keys %{$Table}) {
$logger->debug("\$Oid = $Oid");
$Oid =~/\.(\d+)\.(\d+)\.(\d+\.\d+\.\d+\.\d+\.\d+\.\d+)$/;
my $vlanId = $2;
my $macPattern = $3;
my $PortOrStatus = $Table->{$Oid};
my $tmpMacString = sprintf '%02x%02x%02x%02x%02x%02x', split(/\./, $macPattern);
if ($1 eq '2') { # 2 means that $PortOrStatus holds a "port number"
my $PortNbr = $PortOrStatus;
$logger->debug("\$1 = \"$1, vlanId = \"$vlanId\", mac = \"$tmpMacString\", Port = \"$PortNbr\"");
$ports{$macPattern} = $PortNbr;
} elsif ($1 eq '3') { # 3 means that $PortOrStatus holds a "status"
my $Status = $PortOrStatus;
my $StatusString = '';
if ($Status eq '1') {
$StatusString = 'other';
$logger->debug("unexpected 'other' status!");
} elsif ($Status eq '2') {
$StatusString = 'invalid';
$logger->debug("unexpected 'invalid' status!");
} elsif ($Status eq '3') {
$StatusString = 'learned';
} elsif ($Status eq '4') {
$StatusString = 'self';
$logger->debug("unexpected 'self' status!");
} elsif ($Status eq '5') {
$StatusString = 'mgmt';
$logger->debug("unexpected 'mgmt' status!");
} else {
$logger->warn("illegal status!");
}
if ($Status eq '3') { # 3 means 'learned'
$logger->debug("\$1 = \"$1, vlanId = \"$vlanId\", mac = \"$tmpMacString\", Status = $StatusString");
$statuses{$macPattern} = $Status;
}
}
}
my $SwitchName = GetName $Switch;
foreach my $mac (keys %ports) {
my $tmpMacString = sprintf '%02x%02x%02x%02x%02x%02x', split(/\./, $mac);
next if !exists $statuses{$mac};
next if $statuses{$mac} != 3; # 3 means "learned"
my $portNbr = $ports{$mac};
my $IfNbr = $BifNbrToIfNbr{$portNbr};
# $logger->debug("\$mac = \"$tmpMacString, \$portNbr = $portNbr\", \$IfNbr = $IfNbr\n"); # dbg
if (!defined $IfNbr) {
$logger->warn("for $SwitchName, got bridge interface number $ports{$mac} for $mac, but couldn't map it to an SNMP ifIndex, skipping this MAC");
} else {
if (!exists $$IfToIfNameRef{$IfNbr}) {
$logger->warn("Warning: no interface name for SNMP ifIndex $IfNbr on $SwitchName, skipping $tmpMacString");
} else {
my $PortName = $$IfToIfNameRef{$IfNbr};
my $Port = $$PortsRef{$PortName};
$logger->debug("Adding $tmpMacString to Port->{Mac} hash for port $PortName");
$Port->AddMac($tmpMacString);
}
}
}
$logger->debug("returning at bottom");
return;
}
1;