-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteCsvDirectory.pm
134 lines (114 loc) · 4.26 KB
/
WriteCsvDirectory.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
package WriteCsvDirectory;
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
#use Data::Dumper;
use VerifyPortLabels;
sub WriteCsvRow ($$$$) {
my $Switch = shift;
my $Port = shift;
my $MacIpAddrRef = shift;
my $MacHostNameRef = shift;
my $logger = get_logger('log5');
$logger->debug("called");
# MAC, IP, SwitchName, Mod/Port, Label
my $SwitchName = GetName $Switch;
my $SwitchModel = GetChassisModel $Switch;
my $PortName = $Port->{Name};
my $VlanNbr = (exists $Port->{VlanNbr}) ? $Port->{VlanNbr} : '';
my $State = $Port->{State};
my $DaysInactive = ($Port->{DaysInactive} ne '') ? $Port->{DaysInactive} : '';
my $Speed = (defined $Port->{Speed}) ? $Port->{Speed} : 'n/a';
my $Duplex = (exists $Port->{Duplex}) ? $Port->{Duplex} : 'n/a';
my $PortLabel = (defined $Port->{Label}) ? $Port->{Label} : '';
my $WhatViaCdp = ($Port->{CdpCachePlatform} ne '') ? $Port->{CdpCachePlatform} : '';
my $Mac = '';
my $Nic = '';
my $Ip = '';
my $Dns = '';
my $LeftMostRowCells = "$SwitchName,$PortName,$VlanNbr,$State,$DaysInactive,$Speed,$Duplex,$PortLabel,$WhatViaCdp,";
if ($Port->{IsTrunking}) {
if ($ThisSite::ShowOnlyActiveNonTrunkPortsInCsv) {
print CSVFILE $LeftMostRowCells . "Trunk Port\n";
}
} else {
my $NbrMacs = keys %{$Port->{Mac}};
if ($NbrMacs == 0) {
if ($ThisSite::ShowOnlyActiveNonTrunkPortsInCsv) {
print CSVFILE $LeftMostRowCells . "No Active MAC Addresses\n";
}
} else {
my @MacIps;
foreach my $PortMac (keys %{$Port->{Mac}}) {
next if $PortMac eq '';
next if $PortMac =~ / Etherchannel$/;
my $first6 = substr $PortMac, 0, 6;
my $ia = (exists $$MacIpAddrRef {$PortMac}) ? $$MacIpAddrRef {$PortMac} : '' ; # IP Address
my $hn = (exists $$MacHostNameRef{$PortMac}) ? $$MacHostNameRef{$PortMac} : '' ; # DNS Name
push @MacIps, join ';', $PortMac, $ia, $hn;
}
foreach (sort @MacIps) {
($Mac, $Ip, $Dns) = split ';', $_;
print CSVFILE $LeftMostRowCells . "$Mac,$Nic,$Ip,$Dns\n";
}
}
}
$logger->debug("returning");
}
sub WriteCsvFile ($$$) {
my $Switch = shift;
my $MacIpAddrRef = shift;
my $MacHostNameRef = shift;
my $logger = get_logger('log4');
$logger->debug("called");
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};
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, $MacHostNameRef);
}
}
}
} else {
foreach my $PortName (@PortNames) {
my $Port = $Switch->{Ports}{$PortName};
WriteCsvRow($Switch, $Port, $MacIpAddrRef, $MacHostNameRef);
}
}
$logger->debug("returning");
}
sub WriteCsvFiles ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
my $MacIpAddrRef = MacIpTables::getMacIpAddr();
my $MacHostNameRef = MacIpTables::getMacHostName();
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---/; # skip it if it's a group name
my $CsvFileName = File::Spec->catfile($Constants::CsvDirectory, $SwitchName . '.csv');
$logger->info("writing $CsvFileName");
open CSVFILE, ">$CsvFileName" or do {
$logger->fatal("Couldn't open $CsvFileName for writing, $!");
exit;
};
WriteCsvFile($Switch, $MacIpAddrRef, $MacHostNameRef);
close CSVFILE;
SwitchUtils::AllowAllToReadFile $CsvFileName;
}
my $timstr = SwitchUtils::TimeStr;
$logger->debug("returning");
}
sub WriteSwitchCsvFiles ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log2');
$logger->debug("called");
SwitchUtils::SetupDirectory $Constants::CsvDirectory; # create or empty out the directory
WriteCsvFiles($SwitchesRef);
$logger->debug("returning");
}
1;