-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteNcarFiles.pm
140 lines (120 loc) · 4.31 KB
/
WriteNcarFiles.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
package WriteNcarFiles;
#
# This module contains code to write files that are used only at NCAR,
# where Pete (the author of SwitchMap) works. As of 2006-06-27, when
# I wrote this module, it writes only one file, which contains
# information needed by Jim's Cisco VOIP Call Manager database.
#
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
#use Data::Dumper;
use VerifyPortLabels;
my %seenMacs;
sub WriteCsvRow ($$$) {
my $Switch = shift;
my $Port = shift;
my $MacIpAddrRef = shift;
my $logger = get_logger('log5');
$logger->debug("called");
my $SwitchModel = GetChassisModel $Switch;
my $PortName = $Port->{Name};
my $SwitchName = GetName $Switch;
if ($Port->{IsSwitching} and
(!$Port->{IsVirtual}) and
($Port->{State} eq 'Active') and
(!SwitchUtils::IsAncillaryPort($Port)) and
((!$Port->{IsTrunking}) or (($SwitchModel =~ /3524/) and ($PortName !~ /^G/)))) {
my $NbrMacs = keys %{$Port->{Mac}};
if ($NbrMacs > 0) { # one or more MACs exist on the port
my @MacIps;
foreach my $PortMac (keys %{$Port->{Mac}}) {
next if $PortMac eq '';
next if $PortMac eq '000000000000';
next if $PortMac =~ / Etherchannel$/;
my $first6 = substr $PortMac, 0, 6;
if (exists $$MacIpAddrRef{$PortMac}) {
push @MacIps, join ';', $$MacIpAddrRef{$PortMac}, $PortMac;
} else {
push @MacIps, join ';', '', $PortMac;
}
}
my $PortLabel = (defined $Port->{Label}) ? $Port->{Label} : '';
foreach (sort @MacIps) {
my ($Ip, $Mac) = split ';', $_;
if (exists $seenMacs{$Mac}) {
$logger->debug("skipping output of a line for MAC = \"$Mac\", we've already output a line for this MAC");
} else {
$seenMacs{$Mac}++;
print NCARCSVFILE "$Mac,$Ip,$SwitchName,$PortName,$PortLabel\n";
}
}
}
}
$logger->debug("returning");
}
#
# These models of switches will never have phones attached, because
# they have only fiber or 10Gig interfaces. We'll skip them because
# it confuses Jim Van Dyke's code if entries from these guys appear in
# the output file.
#
my @SkipModels = ( 'WS-X6408-GBIC',
'WS-X6408A-GBIC',
'WS-X6416-GBIC',
'WS-X6704-10GE',
'WS-X6748-SFP');
sub WriteNcarCallManagerCsvFileBody ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log4');
$logger->debug("called");
my $MacIpAddrRef = MacIpTables::getMacIpAddr();
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---(.+)/; # if it's a group name, not a switch
my @PortNames = Portically::PortSort keys %{$Switch->{Ports}};
if ($Switch->{NbrModules} > 1) { # if it has modules (i.e. 6509s have modules, 3524s don't)
foreach my $ModNbr (sort {$a<=>$b} keys %{$Switch->{ModuleList}{Model}}) {
my $Model = $Switch->{ModuleList}{Model}->{$ModNbr};
next if (grep (/^$Model$/, @SkipModels) == 1);
foreach my $PortName (@PortNames) {
$PortName =~ /[^\d]*(\d+)/; # this has to match "3/4", "Ga9/6" or "Fa2/0/15"
if ((defined $1) and ($1 eq $ModNbr)) {
my $Port = $Switch->{Ports}{$PortName};
WriteCsvRow($Switch, $Port, $MacIpAddrRef);
}
}
}
} else {
foreach my $PortName (@PortNames) {
my $Port = $Switch->{Ports}{$PortName};
WriteCsvRow($Switch, $Port, $MacIpAddrRef);
}
}
}
$logger->debug("returning");
}
sub WriteNcarCallManagerCsvFile ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
my $CsvFileName = File::Spec->catfile($ThisSite::DestinationDirectory, 'NcarCallManager.csv');
$logger->info("writing $CsvFileName");
open NCARCSVFILE, ">$CsvFileName" or do {
$logger->fatal("Couldn't open $CsvFileName for writing, $!");
exit;
};
WriteNcarCallManagerCsvFileBody($SwitchesRef);
close NCARCSVFILE;
SwitchUtils::AllowAllToReadFile $CsvFileName;
$logger->debug("returning");
}
sub WriteNcarFiles ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log2');
$logger->debug("called");
WriteNcarCallManagerCsvFile($SwitchesRef);
VerifyPortLabels::WritePortLabelAnalysisFile($SwitchesRef);
$logger->debug("returning");
}
1;