-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPopulatePorts.pm
1378 lines (1238 loc) · 54.3 KB
/
PopulatePorts.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package PopulatePorts;
use strict;
#use Data::Dumper;
use Log::Log4perl qw(get_logger);
use Portically;
#use Devel::Size qw(size);
#use Net::SNMP qw(:snmp DEBUG_ALL DEBUG_NONE);
use GetCiscoPortTrunkStatuses;
use GetMacsFromCiscoMibs;
use GetMacsFromQBridgeMib;
use GetVlansOnPorts;
use Port;
# Unlike Switch.pm and Vlan.pm, this file doesn't implement a class.
# It's just a place to modularize the PopulatePorts function and all
# the code that it calls.
#
# Given a speed code that came from a MIB, return a string that's easy
# for a human to understand and is as short as possible so it fits
# nicely into narrow HTML table cells.
#
# Some 10G modules will report ifSpeeds of 4294967295. This has been seen on
# Cisco 6509 switches with 10 gig modules WS-X6716-10GE
# Cisco 6509-E switches with WS-X6704-10GE modules
# Cisco 3560E switches with 10gig transceivers
# Cisco 3750 switches with 1 ten gig interface WS-C3750G-16TD-S
#
sub SpeedMap ($) {
my $SpeedCode = shift;
return 'none' if ($SpeedCode == 0);
return 'auto' if ($SpeedCode == 1) or # autoDetect
($SpeedCode == 2); # autoDetect10100
return '10G' if ($SpeedCode == 10) or
($SpeedCode == 4294967295);
my $GIG = 1000000000;
my $MEG = 1000000;
my $KILO= 1000;
return ($SpeedCode / $GIG) . 'G' if ($SpeedCode % $GIG) == 0;
return ($SpeedCode / $MEG) . 'M' if ($SpeedCode % $MEG) == 0;
return ($SpeedCode / $KILO) . 'K' if ($SpeedCode % $KILO) == 0;
return ($SpeedCode . 'bps') if ($SpeedCode < 10000);
return ($SpeedCode / $MEG) . 'M';
}
#
# Get the speeds of all the ports. Get the administrative speed if
# possible. Then get the operational speed. Convert the raw SNMP
# speed numbers and/or codes into something a human can understand.
#
sub GetPortSpeeds ($$$$$$) {
my $Switch = shift; # passed in
my $Session = shift; # passed in
my $IfToIfNameRef = shift; # passed in
my $PortIfIndexRef = shift; # passed in
my $AdminSpeedsRef = shift; # passed in empty, filled in by this function
my $IfSpeedsRef = shift; # passed in empty, filled in by this function
my $logger = get_logger('log5');
$logger->debug("called");
$logger->info("getting portAdminSpeeds (administrative speeds)...");
my %PortAdminSpeed;
my $status = SwitchUtils::GetSnmpTable($Session,
'portAdminSpeed',
$Constants::PORT,
\%PortAdminSpeed);
if ($status != $Constants::SUCCESS) {
$logger->warn("Couldn't get the portAdminSpeed table from $Switch->{Name}");
} else {
foreach my $PortName (keys %PortAdminSpeed) {
if ((exists $PortIfIndexRef->{$PortName}) and ($PortIfIndexRef->{$PortName} != 0)) {
$AdminSpeedsRef->{$IfToIfNameRef->{$PortIfIndexRef->{$PortName}}} = SpeedMap($PortAdminSpeed{$PortName});
}
}
}
$logger->info("getting ifSpeeds (operational speeds)...");
my %ifSpeed;
$status = SwitchUtils::GetSnmpTable($Session,
'ifSpeed',
$Constants::INTERFACE,
\%ifSpeed);
if ($status != $Constants::SUCCESS) { # if we couldn't reach it or it's real slow
$logger->warn("Couldn't get the ifSpeed table from $Switch->{Name}, skipping");
return $Constants::FAILURE;
}
foreach my $ifNbr (keys %ifSpeed) {
my $ifName = $$IfToIfNameRef{$ifNbr};
$IfSpeedsRef->{$ifName} = SpeedMap($ifSpeed{$ifNbr});
}
$logger->debug("returning success");
return $Constants::SUCCESS;
}
#
# Set the Vlan field for each port.
#
sub GetPortNameToVlanTable ($$$$$) {
my $Switch = shift; # passed in
my $Session = shift; # passed in
my $IfToIfNameRef = shift; # passed in
my $PortIfIndexRef = shift; # passed in
my $VlansRef = shift; # passed in empty, filled by this function
my $logger = get_logger('log5');
$logger->debug("called");
#
# On switches that support the Cisco Stack MIB, like 6509s, the
# port-to-vlan table is in the vlanPortVlan table.
#
$logger->info("getting port-to-VLAN mapping table from Cisco Stack MIB...");
my %vlanPortVlan;
my $status = SwitchUtils::GetSnmpTable($Session,
'vlanPortVlan',
$Constants::PORT,
\%vlanPortVlan);
if ($status == $Constants::SUCCESS) {
# print Dumper(%vlanPortVlan);
my $NbrPorts = keys %vlanPortVlan;
$logger->debug("got $NbrPorts values from port-to-VLAN mapping table named vlanPortVlan");
foreach my $PortName (keys %vlanPortVlan) {
if ((exists $PortIfIndexRef->{$PortName}) and ($PortIfIndexRef->{$PortName} != 0)) {
$VlansRef->{$IfToIfNameRef->{$PortIfIndexRef->{$PortName}}} = $vlanPortVlan{$PortName};
}
}
} else {
my $SwitchName = GetName $Switch;
# If we made it to here, we couldn't reach the switch or it's real
# slow or it doesn't do the Cisco Stack MIB. It might be a Cisco
# switch that doesn't support the Cisco Stack MIB, like a 3524.
# On such switches, the port-to-vlan table is in a combination of
# tables: trunk ports are in the vlanTrunkPortNativeVlan table in
# the ciscoVtpMIB and non-trunk ports are in the vmVlan table in
# the ciscoVlanMembershipMIB. Some 3524s may support neither
# because no trunks are configured at all.
$logger->debug("it doesn't support the Cisco Stack MIB, trying the Cisco VTP MIB (vmVlan)");
my %vmVlan;
$status = SwitchUtils::GetSnmpTable($Session,
'vmVlan',
$Constants::INTERFACE,
\%vmVlan);
if ($status == $Constants::SUCCESS) {
# SwitchUtils::DbgPrintHash('vmVlan', \%vmVlan);
foreach my $ifNbr (keys %vmVlan) {
my $PortName = $$IfToIfNameRef{$ifNbr};
my $tmp = $vmVlan{$ifNbr};
$logger->debug("$SwitchName: vmVlan table means I'm setting \$\$VlansRef{$PortName} to \$vmVlan{$ifNbr} = $tmp");
$$VlansRef{$PortName} = $vmVlan{$ifNbr};
}
} else {
# If we made it to here, we couldn't reach the switch or it's real
# slow or it doesn't do the Cisco Stack MIB or Ciso VTP MIB. Try
# the Juniper MIBs.
$logger->debug("it doesn't support the Cisco VTP MIB, trying the Juniper MIB (jnxExVlanPortAccessMode)");
my %jnxExVlanPortAccessMode;
$status = SwitchUtils::GetSnmpTable($Session,
'jnxExVlanPortAccessMode',
$Constants::PORT,
\%jnxExVlanPortAccessMode);
if ($status == $Constants::SUCCESS) {
$logger->debug("it supports the Juniper VLAN MIB");
my %jnxExVlanTag;
$status = SwitchUtils::GetSnmpTable($Session,
'jnxExVlanTag',
$Constants::INTERFACE,
\%jnxExVlanTag);
if ($status == $Constants::SUCCESS) {
$logger->debug("got the jnxExVlanTag table");
foreach my $vlanId (sort keys %jnxExVlanTag) {
$logger->debug("\%jnxExVlanTag{$vlanId} = \"$jnxExVlanTag{$vlanId}\"");
}
}
foreach my $vlanPort (sort keys %jnxExVlanPortAccessMode) {
my ($vlanId, $BifNbr) = split '/', $vlanPort;
$logger->debug(" \$vlanport = \"$vlanPort\", vlanId = $vlanId, \$BifNbr = $BifNbr");
if (exists $$IfToIfNameRef{$BifNbr}) {
my $PortName = $$IfToIfNameRef{$BifNbr};
my $vlanNbr = $jnxExVlanTag{$vlanId};
$logger->debug(" port $PortName is in VLAN $vlanNbr");
$$VlansRef{$PortName} = $jnxExVlanTag{$vlanId};
}
}
} else {
$logger->debug("it doesn't support the Juniper MIB, proceeding without Port-to-VLAN information");
}
}
}
# When I get around to fetching VLAN information from Juniper devices,
# I'll look in jnxExVlanTable, jnxExVlanInterfaceTable, and
# jnxExVlanPortGroupTable. Or, the QBridge MIB?
#
# Perhaps of value is dot1qNumVlans.0, the number of VLANs on the
# switch. GetPortToMac loops through all the VLANs on the switch.
# Are those contained in the Q-BRIDGE MIB?
#
# And the following also works, suggesting that maybe we can use the
# Q-BRIDGE MIB to access Ciscos as well as Foundry switches.
# snmpwalk -v 2c -c ncar-read ml-16c-c1-gs .1.3.6.1.2.1.17.7.1.1
#
# The mechanism for identifying the VLAN per port is bit maps, with
# bit set for each port that is in a VLAN. So you get the bitmap for
# each VLAN, and loop through the bits to figure out which ports are
# in the VLAN. So I should be able to extend GetPortToVlanTable - after
# trying the Cisco MIBs, it can access the Q-BRIDGE MIB. Then it
# should try the Q-BRIDGE MIB first if it works for Ciscos and
# Foundrys.
#
my $SwitchName = GetName $Switch;
$logger->debug("testing a GET of the dot1qNumVlans");
my $dot1qNumVlans;
$status = SwitchUtils::GetOneOidValue($Session,
'dot1qNumVlans',
\$dot1qNumVlans);
if ($status) {
$logger->debug("GET succeeded, dot1qNumVlans = $dot1qNumVlans on switch $SwitchName");
} else {
$logger->debug("GET failed, couldn't get dot1qNumVlans from switch $SwitchName");
}
# Now try to get the table of native VLAN numbers. Then for each port
# in the table, if the native VLAN is something other than 1, override
# the VLAN number that's already been set.
my %vlanTrunkPortNativeVlan;
$status = SwitchUtils::GetSnmpTable($Session,
'vlanTrunkPortNativeVlan',
$Constants::INTERFACE,
\%vlanTrunkPortNativeVlan);
if ($status == $Constants::SUCCESS) {
my $NbrVlans = keys %vlanTrunkPortNativeVlan;
$logger->debug("got $NbrVlans native VLAN numbers from vlanTrunkPortNativeVlan table");
foreach my $ifNbr (keys %vlanTrunkPortNativeVlan) {
if ($vlanTrunkPortNativeVlan{$ifNbr} != 1) {
my $PortName = $$IfToIfNameRef{$ifNbr};
$$VlansRef{$PortName} = $vlanTrunkPortNativeVlan{$ifNbr};
}
}
}
# This block of code was on attempt to make SwitchMap work on
# switches that don't support Cisco MIBs. Try the Foundry MIBs...
#
# $logger->debug("it doesn't support the Cisco VTP MIB, trying the Foundry MIBs");
# my %snSwPortVlanId;
# my $status = SwitchUtils::GetSnmpTable($Session,
# 'snSwPortVlanId',
# $Constants::PORT,
# \%snSwPortVlanId);
# if ($status == $Constant::SUCCESS) {
## This "worked" - the status was "success" but there was simply no returned data. I can't explain this.
## If you back aff and walk .1.3.6.1.4.1.1991.1.1.3.3, you get all the tables, including snSwPortVlanId.
## Why is SNMP behaving this way? I gave up and went after the Q-bridge MIB instead...
# if (%snSwPortVlanId) {
# $logger->fatal("got VLAN data from Foundry MIB, but code to interpret it hasn't been written yet");
# exit;
# $logger->debug("returning");
# return;
# } else {
# $logger->debug("Got SUCCESS from SNMP function call, but no returned data.");
# }
# }
#
# If we made it to here, the switch doesn't support either Cisco or
# Foundry MIBs. Try the standard Q-Bridge MIB. (??????????) What
# if it supports the MIB but is configured to use ISL trunking?
#
# $logger->debug("it doesn't support the Foundry MIBs, trying the standard Q-Bridge MIB");
# my %TmpVlans;
# GetMacsFromQBridgeMib::GetPortToVlanTableFromQBridgeMib($Switch, $Session, $IfToIfNameRef, \%TmpVlans);
# if (%TmpVlans) {
# foreach my $vlan (sort keys %TmpVlans) {
# $logger->debug("-------------TmpVlans{$vlan} = \"$TmpVlans{$vlan}\"");
# $logger->fatal("got VLAN data from Q-bridge MIB, but code to interpret it hasn't been written yet");
# exit;
# $logger->debug("returning");
# return;
# }
# }
$logger->debug("returning");
}
#
# Set the AuxiliaryVlan field for each port.
#
sub GetPortNameToAuxiliaryVlanTable ($$$$$) {
my $Switch = shift; # passed in
my $Session = shift; # passed in
my $IfToIfNameRef = shift; # passed in
my $PortIfIndexRef = shift; # passed in
my $PortNameToAuxiliaryVlan = shift; # passed in empty, filled by this function
my $logger = get_logger('log5');
$logger->debug("called");
#
# On switches that support the Cisco VLAN Membership MIB, like the
# 6509, the port-to-auxiliary-vlan table is in the vmVoiceVlanId
# table. Switches that don't support the Cisco Stack MIB might
# support the Cisco 2900 MIB, in which case it's in the
# c2900PortVoiceVlanId table. If the switch doesn't support the
# 2900 MIB, like the 5500 named fl2-2076-c1-es at NCAR, then the
# switch doesn't support auxiliary VLANs at all.
#
$logger->info("getting port-to-auxiliary-VLAN mapping table...");
$logger->info("trying vmVoiceVlanId...");
my %vmVoiceVlanId;
my $status = SwitchUtils::GetSnmpTable($Session,
'vmVoiceVlanId',
$Constants::INTERFACE,
\%vmVoiceVlanId);
if ($status == $Constants::SUCCESS) {
my $NbrPorts = keys %vmVoiceVlanId;
$logger->debug("got auxiliary VLAN info for $NbrPorts from vmVoiceVlanId from Cisco VLAN Membership MIB");
# SwitchUtils::DbgPrintHash('vmVoiceVlanId', \%vmVoiceVlanId);
foreach my $ifNbr (keys %vmVoiceVlanId) {
my $PortName = $$IfToIfNameRef{$ifNbr};
my $AuxVlan = $vmVoiceVlanId{$ifNbr};
# $logger->debug("got vmVoiceVlanId table, setting \$\$AuxiliarysRef\{$PortName\} to $AuxVlan");
$$PortNameToAuxiliaryVlan{$PortName} = $AuxVlan if $AuxVlan != 4096;
}
} else { # if we couldn't reach it or it's real slow or it doesn't do vmVoiceVlanId
$logger->debug("no vmVoiceVlanId (no Cisco VLAN Membership MIB), trying c2900PortVoiceVlanId");
my %c2900PortVoiceVlanId;
$status = SwitchUtils::GetSnmpTable($Session,
'c2900PortVoiceVlanId',
$Constants::INTERFACE,
\%c2900PortVoiceVlanId);
if ($status == $Constants::SUCCESS) {
my $NbrPorts = keys %c2900PortVoiceVlanId;
$logger->debug("got auxiliary VLAN info for $NbrPorts from c2900PortVoiceVlanId from Cisco c2900 MIB");
# SwitchUtils::DbgPrintHash('c2900PortVoiceVlanId', \%c2900PortVoiceVlanId);
foreach my $ifNbr (keys %c2900PortVoiceVlanId) {
my $PortName = $$IfToIfNameRef{$ifNbr};
my $AuxVlan = $c2900PortVoiceVlanId{$ifNbr};
# $logger->debug("got c2900PortVoiceVlanId table, setting \$\$AuxiliarysRef\{$PortName\} to $AuxVlan");
$$PortNameToAuxiliaryVlan{$PortName} = $AuxVlan if $AuxVlan != 4096;
}
} else {
$logger->debug("no Cisco 2900 MIB, we don't have any Auxiliary VLAN data.");
}
}
$logger->debug("returning");
return;
}
sub GetPortToLabel ($$$$$) {
my $Switch = shift; # passed in
my $Session = shift; # passed in
my $IfToIfNameRef = shift; # passed in
my $PortIfIndexRef = shift; # passed in
my $LabelsRef = shift; # passed in empty, filled by this function
my $logger = get_logger('log5');
$logger->debug("called");
$logger->info("getting port-to-label mapping table...");
# Try to get the Cisco Stack MIB 'portName' table. 6500s have it.
my %portName;
my $status = SwitchUtils::GetSnmpTable($Session,
'portName',
$Constants::PORT,
\%portName);
if ($status == $Constants::SUCCESS) { # if we got the portName table
# 3550s have a portName table, but all the entries are empty, and
# true port labels are found in the ifAlias table. So use
# $portNameValid to indicate whether the portName table has
# non-blank entries.
my $portNameValid = 0;
my $count = 0;
foreach my $PortName (keys %portName) {
next if (!exists $PortIfIndexRef->{$PortName}) or ($PortIfIndexRef->{$PortName} == 0);
my $pn = $portName{$PortName};
$portNameValid = 1 if $pn ne '';
$pn =~ s/\r//g;
$pn =~ s/\n//g;
$LabelsRef->{$IfToIfNameRef->{$PortIfIndexRef->{$PortName}}} = $pn;
$count++;
}
if ($portNameValid) {
$logger->debug("got $count interface labels from portname table, returning success");
return $Constants::SUCCESS;
}
}
# If we made it to here, then we couldn't reach it or it's real slow
# or it doesn't have the portName table. Try to get the IF MIB
# 'ifAlias' table. 3524s and 3550s use it.
my %ifAlias;
$status = SwitchUtils::GetSnmpTable($Session,
'ifAlias',
$Constants::INTERFACE,
\%ifAlias);
if ($status == $Constants::SUCCESS) {
my $count = 0;
foreach my $ifNbr (keys %ifAlias) {
my $pn = $ifAlias{$ifNbr};
$pn =~ s/\r//g;
$pn =~ s/\n//g;
my $PortName = $$IfToIfNameRef{$ifNbr};
$$LabelsRef{$PortName} = $pn;
$count++;
}
$logger->debug("got $count interface labels from ifAlias table, returning success");
return $Constants::SUCCESS;
}
# If we made it to here, then we couldn't reach it or it's real slow
# or - it doesn't have the portName table and it doesn't have the
# ifAlias table. 1900s are like this, but they support the ESSWITCH
# MIB, which has the swPortName table. Try it.
my %swPortName;
$status = SwitchUtils::GetSnmpTable($Session,
'swPortName',
$Constants::INTERFACE,
\%swPortName);
if ($status == $Constants::SUCCESS) {
my $count = 0;
foreach my $IfNbr (keys %swPortName) {
my $pn = $swPortName{$IfNbr};
$pn =~ s/\r//g;
$pn =~ s/\n//g;
my $PortName = $$IfToIfNameRef{$IfNbr};
$$LabelsRef{$PortName} = $pn;
$count++;
}
$logger->debug("got $count interface labels from swPortName table, returning success");
return $Constants::SUCCESS;
}
# If we made it to here, then we couldn't reach it or it's real slow
# or - it doesn't do portName and it doesn't do ifAlias and it
# doesn't do swPortName. We give up!
$logger->warn("Couldn't get the ifAlias, portName or swPortName table from $Switch->{Name}, skipping");
return $Constants::FAILURE;
}
sub ReadableDot3StatsDuplexStatusStrings ($) {
my $dot3StatsDuplexStatus = shift;
my %DuplexMapForDot3Table = (
1 => 'unknown',
2 => 'half',
3 => 'full'
);
return '' if !exists $DuplexMapForDot3Table{$dot3StatsDuplexStatus};
return $DuplexMapForDot3Table{$dot3StatsDuplexStatus};
}
sub ReadablePortDuplexStrings ($) {
my $portDuplex = shift;
my %readablePortDuplexStrings = (
1 => 'half',
2 => 'full',
3 => 'disagree', # this happens when autonegotiation fails
4 => 'auto'
);
return '' if !exists $readablePortDuplexStrings{$portDuplex};
return $readablePortDuplexStrings{$portDuplex};
}
sub ReadableC2900PortDuplexStateStrings ($) {
my $c2900PortDuplexState = shift;
my %c2900PortDuplexStateMap = (
0 => 'n/a', # not defined, but I've seen a switch return it!
1 => 'full',
2 => 'half',
3 => 'auto'
);
return '' if !exists $c2900PortDuplexStateMap{$c2900PortDuplexState};
return $c2900PortDuplexStateMap{$c2900PortDuplexState};
}
sub ReadableC2900PortDuplexStatusStrings ($) {
my $c2900PortDuplexStatus = shift;
my %DuplexMapFor2900and3524 = (
0 => 'n/a', # not defined, but I've seen a switch return it!
1 => 'a-full',
2 => 'a-half'
);
return '' if !exists $DuplexMapFor2900and3524{$c2900PortDuplexStatus};
return $DuplexMapFor2900and3524{$c2900PortDuplexStatus};
}
sub ReadableSwPortDuplexStatus ($) {
my $swPortDuplexStatus = shift;
my %DuplexMapFor1900 = (
1 => 'full',
2 => 'half',
3 => 'full-flow'
);
return '' if !exists $DuplexMapFor1900{$swPortDuplexStatus};
return $DuplexMapFor1900{$swPortDuplexStatus};
}
sub ReadablePethPsePortDetectionStatus ($) {
my $pethPsePortDetectionStatus = shift;
my %PoEPortDetectionStatusStrings = (
1 => 'disabled',
2 => 'searching',
3 => 'deliveringPower',
4 => 'fault',
5 => 'test',
6 => 'otherFault'
);
return '' if !exists $PoEPortDetectionStatusStrings{$pethPsePortDetectionStatus};
return $PoEPortDetectionStatusStrings{$pethPsePortDetectionStatus};
}
sub GetPoEDetectionStatuses ($$$$$) {
my $Switch = shift; # passed in
my $Session = shift; # passed in
my $IfToIfNameRef = shift; # passed in
my $PortIfIndexRef = shift; # passed in
my $PoeDetectionStatusesRef = shift; # passed in empty, filled by this function
my $logger = get_logger('log5');
$logger->debug("called");
my $SwitchName = GetName $Switch;
# Get the Power-over-Ethernet table.
my %pethPsePortDetectionStatus;
my $status = SwitchUtils::GetSnmpTable($Session,
'pethPsePortDetectionStatus',
$Constants::PORT,
\%pethPsePortDetectionStatus);
if ($status == $Constants::SUCCESS) {
$logger->debug("Power-over-Ethernet MIB found on $SwitchName");
foreach my $PortName (Portically::PortSort keys %pethPsePortDetectionStatus) {
my $PDStatus = $pethPsePortDetectionStatus{$PortName};
# my $PDStatusString = ReadablePethPsePortDetectionStatus($PDStatus);
if ((exists $PortIfIndexRef->{$PortName}) and ($PortIfIndexRef->{$PortName} != 0)) {
$PoeDetectionStatusesRef->{$IfToIfNameRef->{$PortIfIndexRef->{$PortName}}} = $PDStatus;
}
}
} else {
$logger->debug("Power-over-Ethernet MIB not found on $SwitchName, status = $status");
}
$logger->debug("returning");
return $Constants::SUCCESS;
}
sub GetPortToDuplex ($$$$$$) {
my $Switch = shift; # passed in
my $Session = shift; # passed in
my $IfToIfNameRef = shift; # passed in
my $PortIfIndexRef = shift; # passed in
my $AdminSpeedsRef = shift; # passed in
my $DuplexRef = shift; # passed in empty, filled by this function
my $logger = get_logger('log5');
$logger->debug("called");
#
# It would be nice if I could get duplex status from just one table,
# but Cisco stores duplex state in different places on different
# switches. For example, the Stack MIB gives the administrative
# setting but not the operational setting, so it might give "auto",
# but not "full". So I get multiple tables, and build strings in
# $Duplex that represent the administrative and operational duplex.
# This following logic wasn't designed, it grew as I made SwitchMap
# work for more and more switches.
#
my $status;
# Try to get the dot3StatsDuplexStatus table, which exists in at
# least 3560s and 4500s.
$logger->info("trying to get the dot3StatsDuplexStatus table from the EtherLike-MIB...");
my %dot3StatsDuplexStatus;
$status = SwitchUtils::GetSnmpTable($Session,
'dot3StatsDuplexStatus',
$Constants::INTERFACE,
\%dot3StatsDuplexStatus);
if ($status == $Constants::SUCCESS) {
$logger->info("got dot3StatsDuplexStatus table from EtherLike-MIB.");
foreach my $ifNbr (keys %dot3StatsDuplexStatus) {
my $DuplexCode = $dot3StatsDuplexStatus{$ifNbr};
my $DuplexString = ReadableDot3StatsDuplexStatusStrings($DuplexCode);
my $PortName = $$IfToIfNameRef{$ifNbr};
# $logger->debug("setting \$DuplexRef->{$PortName} to $DuplexString from dot3StatsDuplexStatus table");
$DuplexRef->{$PortName} = $DuplexString if $PortName;
}
}
#
# Try to get the duplex values from the portDuplex table in the
# Cisco Stack MIB. This works for 6509s but not for 3524s or 1900s.
#
$logger->info("trying to get the port-to-duplex table from the Cisco Stack MIB...");
my %portDuplex;
$status = SwitchUtils::GetSnmpTable($Session,
'portDuplex',
$Constants::PORT,
\%portDuplex);
if ($status == $Constants::SUCCESS) {
$logger->info("got the portDuplex table from the Stack MIB.");
# SwitchUtils::DbgPrintHash('portDuplex', \%portDuplex);
foreach my $PortName (keys %portDuplex) {
my $DuplexCode = $portDuplex{$PortName};
my $DuplexString = ReadablePortDuplexStrings($DuplexCode);
next if (!exists $PortIfIndexRef->{$PortName}) or ($PortIfIndexRef->{$PortName} == 0);
my $ifName = $IfToIfNameRef->{$PortIfIndexRef->{$PortName}};
# $logger->debug("PortName = \"$PortName\", ifName = \"$ifName\", DuplexCode = \"$DuplexCode\", DuplexString = \"$DuplexString\"");
if (($DuplexString eq 'auto') and (exists $DuplexRef->{$ifName}) and ($DuplexRef->{$ifName} eq 'half')) {
$DuplexString = 'a-half';
} elsif (($DuplexString eq 'auto') and (exists $DuplexRef->{$ifName}) and ($DuplexRef->{$ifName} eq 'full')) {
$DuplexString = 'a-full';
} elsif (($DuplexString ne 'auto') and (exists $AdminSpeedsRef->{$PortName}) and ($AdminSpeedsRef->{$PortName} eq 'auto')) {
$DuplexString = 'a-' . $DuplexString;
}
# $logger->debug("setting \$\$DuplexRef{$ifName} to $DuplexString from portDuplex table");
$DuplexRef->{$ifName} = $DuplexString;
}
$logger->debug("returning");
return $Constants::SUCCESS;
}
# Ok, we couldn't reach it or it's real slow or it doesn't do portDuplex.
# Assume no stack MIB, so try the 2900 MIB. Believe it or not, this
# is where 3524s keep the duplex values.
#
# First, get the administrative duplex settings.
$logger->info("couldn't get portDuplex, trying c2900PortDuplexState from Cisco 2900 MIB...");
my %c2900PortDuplexState;
$status = SwitchUtils::GetSnmpTable($Session,
'c2900PortDuplexState',
$Constants::INTERFACE,
\%c2900PortDuplexState);
if ($status == $Constants::SUCCESS) {
$logger->info("got the c2900PortDuplexState table from the 2900 MIB.");
foreach my $ifNbr (keys %c2900PortDuplexState) {
my $DuplexCode = $c2900PortDuplexState{$ifNbr};
my $DuplexString = ReadableC2900PortDuplexStateStrings($DuplexCode);
my $PortName = $$IfToIfNameRef{$ifNbr};
$DuplexRef->{$PortName} = $DuplexString;
}
#
# Now that we've set the administrative string for each port, replace
# the ones that are set to 'auto' if we can, with a more descriptive
# string like 'a-half' or 'a-full'.
#
my %c2900PortDuplexStatus;
$status = SwitchUtils::GetSnmpTable($Session,
'c2900PortDuplexStatus',
$Constants::INTERFACE,
\%c2900PortDuplexStatus);
if ($status == $Constants::SUCCESS) {
$logger->info("got the c2900PortDuplexStatus table from the 2900 MIB.");
# We can't interpret the duplex status without knowing the linkbeat status, so...
$logger->info("getting c2900PortLinkbeatStatus from Cisco 2900 MIB...");
my %c2900PortLinkbeatStatus;
$status = SwitchUtils::GetSnmpTable($Session,
'c2900PortLinkbeatStatus',
$Constants::INTERFACE,
\%c2900PortLinkbeatStatus);
if ($status == $Constants::SUCCESS) {
foreach my $ifNbr (keys %c2900PortDuplexStatus) {
if ($c2900PortDuplexState{$ifNbr} == 3) { # if it's 'auto-negotiate'
my $DuplexCode = $c2900PortDuplexStatus{$ifNbr};
my $DuplexString = ReadableC2900PortDuplexStatusStrings($DuplexCode);
# 3524s report "half" duplex when a port has no "linkbeat".
# Dunno what "linkbeat" is, really - it's not the port's link
# state, because I have pinged a machine attached to a 3524
# port and watched the 3524 report "nolinkbeat". Anyway, if
# there is no linkbeat on a port, a 3524 will report "half",
# so to clarify things, I report that as an "unknown" duplex.
my $NOLINKBEAT = 3;
if ($c2900PortLinkbeatStatus{$ifNbr} == $NOLINKBEAT) {
$DuplexString = 'unknown';
}
my $PortName = $$IfToIfNameRef{$ifNbr};
$DuplexRef->{$PortName} = $DuplexString;
}
}
}
}
$logger->debug("returning");
return $Constants::SUCCESS;
}
# We couldn't reach it or it's real slow or it doesn't do portDuplex
# or c2900PortDuplexStatus. Ok, no stack MIB or 2900 MIB. Try the
# ESSWITCH MIB. It's used by at least 1900s.
$logger->info("couldn't get c2900PortDuplexStatus, trying swPortDuplexStatus from ESSWITCH MIB...");
my %swPortDuplexStatus;
$status = SwitchUtils::GetSnmpTable($Session,
'swPortDuplexStatus',
$Constants::INTERFACE,
\%swPortDuplexStatus);
if ($status == $Constants::SUCCESS) {
$logger->info("got swPortDuplexStatus table from the ESSWITCH MIB.");
foreach my $ifNbr (keys %swPortDuplexStatus) {
my $DuplexCode = $swPortDuplexStatus{$ifNbr};
my $DuplexString = ReadableSwPortDuplexStatus($DuplexCode);
if ($DuplexString) {
my $PortName = $$IfToIfNameRef{$ifNbr};
$DuplexRef->{$PortName} = $DuplexString;
}
}
}
$logger->debug("returning");
return $Constants::SUCCESS;
}
sub GetMacsFromBridgeTables($$$$) {
my $Switch = shift;
my $Session = shift;
my $IfToIfNameRef = shift;
my $PortsRef = shift;
my $logger = get_logger('log5');
$logger->debug("called");
GetMacsFromQBridgeMib::GetMacsFromQBridgeMib($Switch, $Session, $IfToIfNameRef, $PortsRef);
GetMacsFromCiscoMibs::GetMacsFromCiscoMibs ($Switch, $IfToIfNameRef, $PortsRef);
}
sub GetMacsFromArpCache ($$$$) {
my $Switch = shift;
my $Session = shift;
my $IfToIfNameRef = shift;
my $PortsRef = shift;
my $logger = get_logger('log5');
$logger->debug("called");
my $SwitchName = GetName $Switch;
my %ipNetToMediaIfIndex;
my $status = SwitchUtils::GetSnmpTable($Session,
'ipNetToMediaIfIndex',
$Constants::IP_ADDRESS,
\%ipNetToMediaIfIndex);
if ($status != $Constants::SUCCESS) {
$logger->warn("Couldn't get ARP cache interfaces from $SwitchName, skipping\n");
return;
}
my %ipNetToMediaPhysAddress;
$status = SwitchUtils::GetSnmpTable($Session,
'ipNetToMediaPhysAddress',
$Constants::IP_ADDRESS,
\%ipNetToMediaPhysAddress);
if ($status != $Constants::SUCCESS) {
$logger->warn("Couldn't get ARP physical addresses from $SwitchName, skipping\n");
return;
}
#
# Build a hash of the number of MACs we found on each interface.
#
my %ifIndexCounts;
foreach my $ip (keys %ipNetToMediaIfIndex) {
my $tmp = $ipNetToMediaIfIndex{$ip};
$ifIndexCounts{$ipNetToMediaIfIndex{$ip}}++;
}
my $ArpCount = 0;
foreach my $ip (sort keys %ipNetToMediaPhysAddress) {
next if !exists $ipNetToMediaIfIndex{$ip};
my $ifIndex = $ipNetToMediaIfIndex{$ip};
next if !exists $$IfToIfNameRef{$ifIndex};
my $PortName = $$IfToIfNameRef{$ifIndex};
next if !exists $PortsRef->{$PortName};
my $Port = $$PortsRef{$PortName};
my $Mac = unpack 'H12', $ipNetToMediaPhysAddress{$ip};
if (!exists $Switch->{IfMacs}{$Mac}) { # if it's not one of the switch's own MACs
if ($ifIndexCounts{$ifIndex} <= $ThisSite::ArpMacLimit) { # if the interface doesn't have too many MACs
$Port->AddMac($Mac);
# $logger->debug("\$Mac = \"$Mac\"");
$ArpCount++;
} else {
$Port->{ArpMacCount}++;
}
}
}
$logger->debug("returning success, got $ArpCount ARP entries");
return $Constants::SUCCESS;
}
sub SetExplicitTrunkStatuses($) {
my $Switch = shift;
my $logger = get_logger('log6');
$logger->debug("called");
my $SwitchName = GetName $Switch;
$logger->debug("checking switch $SwitchName");
if (exists $ThisSite::LocalSwitchTrunkPorts{$SwitchName}) {
my $numberPorts = $#{ $ThisSite::LocalSwitchTrunkPorts{$SwitchName} } + 1;
$logger->debug("switch $SwitchName has $numberPorts explicit trunk ports");
if ($numberPorts == 0) {
$logger->warn("!!! In ThisSite.pm, in \$LocalSwitchTrunkPorts, the list of ports for switch $SwitchName is empty, skipping");
} else {
my $SwitchTrunkPorts = $ThisSite::LocalSwitchTrunkPorts{$SwitchName};
foreach my $PortName (@$SwitchTrunkPorts) {
$logger->debug("\$PortName = $PortName");
my $Port = $Switch->{Ports}{$PortName};
if (defined $Port) {
$logger->debug("setting $SwitchName port $PortName as a trunk port");
$Port->{IsTrunking} = 1;
} else {
$logger->warn("!!! In ThisSite.pm, in \$LocalSwitchTrunkPorts, port $PortName doesn't exist in switch $SwitchName, skipping !!!");
}
}
}
}
$logger->debug("returning");
}
# Get the trunking status of all the ports - status will be NotTrunking,
# IslTrunking or 8021qTrunking.
#
# In a set of switches at a site, it is likely that uplink ports (the
# ports that connect core switches or that connect edge switches to
# core switches) will be trunking, and will therefore have a great
# many MAC addresses in their CAM tables. It's annoying to actually
# see all those MAC addresses, because they aren't really bound to the
# trunk ports. So when the SwitchMap code outputs the MAC addresses
# that are bound to ports, it treats trunk ports specially - it
# outputs "trunk port" instead of the MAC addresses.
#
# I think this idea is sound, but it assumes that SwitchMap can tell
# trunk ports from non-trunk ports. This function makes that
# determination.
#
sub GetPortTrunkStatuses ($$$$) {
my $Switch = shift;
my $Session = shift;
my $IfToIfNameRef = shift;
my $PortIfIndexRef = shift;
my $logger = get_logger('log5');
$logger->debug("called");
my %jnxExVlanPortAccessMode;
my $status = SwitchUtils::GetSnmpTable($Session,
'jnxExVlanPortAccessMode',
$Constants::PORT,
\%jnxExVlanPortAccessMode);
if ($status == $Constants::SUCCESS) {
#
# This part doesn't work yet - I haven't figured out how to get the
# trunk statuses out of the jnxExVlanPortAccessMode table. So this
# code doesn't actually set the trunk status of any ports, it just
# emits debugging statements.
#
my $ACCESS_PORT = 1;
my $TRUNK_PORT = 2;
foreach my $port (sort keys %jnxExVlanPortAccessMode) {
my $portAccessMode = $jnxExVlanPortAccessMode{$port};
my $textAccessMode = '';
if ($portAccessMode == $ACCESS_PORT) {
$textAccessMode = 'access';
} elsif ($portAccessMode == $TRUNK_PORT) {
$textAccessMode = 'trunk';
} else {
$logger->warn("!!! expected access mode = 1 or 2, got $portAccessMode!!!");
}
# $logger->debug("\$jnxExVlanPortAccessMode{$port} = $portAccessMode = $textAccessMode"); # dbg
my ($unknown, $BifNbr) = split '/', $port;
$logger->debug(" \$port = \"$port\", unknown part = $unknown, \$BifNbr = $BifNbr");
if (exists $$IfToIfNameRef{$BifNbr}) {
my $ifName = $$IfToIfNameRef{$BifNbr};
my $ttt = ($portAccessMode == $TRUNK_PORT) ? 'trunk' : 'access';
$logger->debug(" port $ifName is a $ttt port");
}
}
} else {
my $SwitchName = GetName $Switch;
$logger->debug("Couldn't get jnxExVlanPortAccessMode from $SwitchName, trying Cisco MIBs\n");
#
# Get the trunk status of all the ports. If the switch isn't
# trunking (only has default VLANS) then this call won't do
# anything.
#
GetCiscoPortTrunkStatuses::GetCiscoPortTrunkStatuses($Switch,
$Session,
$PortIfIndexRef,
$IfToIfNameRef);
}
SetExplicitTrunkStatuses($Switch);
$logger->debug("returning");
}
#
# Set the "State" of the port.
#
sub SetState ($$$$) {
my $Switch = shift;
my $IdleSinceFile = shift;
my $ifAdminStatusRef = shift;
my $Port = shift;
my $logger = get_logger('log7');
$logger->debug("called for port $Port->{Name}");
my $State = 'Unknown';
if (exists $Port->{IfNbr}) {
my $IfNbr = $Port->{IfNbr};
if (defined $$ifAdminStatusRef{$IfNbr}) {
$State = 'Disabled' if $$ifAdminStatusRef{$IfNbr} == $Constants::DISABLED;
}
}
if ($State ne 'Disabled') {
if (defined $Port->{Type}) {
if ($Port->{IdleSince} == -1) { # if no .idlesince value exists
$logger->error("$Switch->{Name}: no idlesince data found for port $Port->{Name}, you need to run ScanSwitch.pl");
} elsif ($Port->{IdleSince} == 0) { # 0 means the port was active last time we checked
$State = 'Active';
} else {
$State = 'Inactive';
}
}
}
$Port->{State} = $State;
$logger->debug("returning with State set to \"$State\"");
}
sub SetDaysInactive ($$) {
my $Switch = shift;
my $Port = shift;
my $logger = get_logger('log5');
$logger->debug("called");
my $DaysInactive = '';
my $Unused = 0;
if ($Port->{State} ne 'Active') {
if ($Port->{IdleSince} == -1) { # if no .idlesince value exists
$DaysInactive = 'unknown';
} else {
my $SecondsDead = (time - $Port->{IdleSince});
if ($SecondsDead > $Constants::SecondsPerDay) {
$DaysInactive = int ($SecondsDead / $Constants::SecondsPerDay);
}
if ($SecondsDead > ($ThisSite::UnusedAfter * $Constants::SecondsPerDay)) {
$Unused = 1;
$Switch->{NbrUnusedPorts}++;
}
}
}
$Port->{Unused} = $Unused;
$Port->{DaysInactive} = $DaysInactive;
$logger->debug("returning");
}
sub SetPortStateAndDaysInactive ($$$$) {
my $Switch = shift;
my $IdleSinceFile = shift;
my $ifAdminStatusRef = shift;
my $Port = shift;
SetState($Switch, $IdleSinceFile, $ifAdminStatusRef, $Port);
SetDaysInactive($Switch, $Port);
}
sub SetStatesAndDaysInactive ($$$$) {
my $Switch = shift;
my $IdleSinceFile = shift;
my $ifAdminStatusRef = shift;
my $PortsRef = shift;
my $logger = get_logger('log5');