-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteUnusedDirectory.pm
234 lines (203 loc) · 7.41 KB
/
WriteUnusedDirectory.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
package WriteUnusedDirectory;
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
#use Data::Dumper;
sub WriteUnusedRow ($$) {
my $Switch = shift;
my $Port = shift;
my $logger = get_logger('log5');
$logger->debug("called");
my $PortName = $Port->{Name};
my $VlanNbr = (exists $Port->{VlanNbr}) ? $Port->{VlanNbr} : ' ';
my $DaysInactive = $Port->{DaysInactive};
my $Speed = (defined $Port->{Speed}) ? $Port->{Speed} : 'n/a';
my $Duplex = (exists $Port->{Duplex}) ? $Port->{Duplex} : ' ';
my $Label = (defined $Port->{Label} and ($Port->{Label} ne '')) ? $Port->{Label} : ' ';
my $RetVal = <<UROW;
<tr class="cellUnused">
<td>$PortName</td>
<td>$VlanNbr</td>
<td>$DaysInactive</td>
<td>$Speed</td>
<td>$Duplex</td>
<td>$Label</td>
</tr>
UROW
$logger->debug("returning");
return $RetVal;
}
sub HtmlUnusedPortTableHeader () {
return <<UPTH;
<tr class="tblHead">
<th>Port</th>
<th>VLAN</th>
<th>Days<br>Inactive</th>
<th>Speed</th>
<th>Duplex</th>
<th>Port Label</th>
</tr>
UPTH
}
sub WriteUnusedByVlanFiles ($) {
my $VlansRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
foreach my $VlanNbr (sort {$a <=> $b} keys %$VlansRef) {
my $Vlan = $$VlansRef{$VlanNbr};
my $VlanName= $ThisSite::VlanNames[$VlanNbr]? $ThisSite::VlanNames[$VlanNbr] : '[unknown]';
next if $Vlan->{NbrUnusedPorts} == 0; # skip this VLAN if it has no unused ports on any switch
my $UbvFileName = 'vlan' . $VlanNbr . '-unused.html';
my $UnusedByVlanFileName = File::Spec->catfile($Constants::UnusedDirectory, $UbvFileName);
$logger->info("writing $UnusedByVlanFileName");
open UBVFILE, ">$UnusedByVlanFileName" or do {
$logger->fatal("Couldn't open $UnusedByVlanFileName for writing, $!");
exit;
};
print UBVFILE SwitchUtils::HtmlHeader("Unused Ports (idle for > $ThisSite::UnusedAfter days) on VLAN $VlanNbr: $VlanName");
foreach my $SwitchName (sort keys %{$Vlan->{Switches}}) {
print UBVFILE "<h2>$SwitchName</h2>";
print UBVFILE "<table class=\"UnusedPorts\">\n";
print UBVFILE HtmlUnusedPortTableHeader();
my $Switch = $Vlan->{Switches}{$SwitchName};
foreach my $PortName (Portically::PortSort keys %{$Switch->{Ports}}) {
my $Port = $Switch->{Ports}{$PortName};
if ((defined $Port->{VlanNbr}) and
($Port->{VlanNbr} == $VlanNbr)) {
print UBVFILE WriteUnusedRow($Switch, $Port) if $Port->{Unused};
}
}
print UBVFILE "</table>\n";
}
close UBVFILE;
SwitchUtils::AllowAllToReadFile $UnusedByVlanFileName;
}
$logger->debug("returning");
}
sub WriteUnusedBySwitchData ($) {
my $Switch = shift;
my $logger = get_logger('log4');
$logger->debug("called");
my $RetVal = '';
foreach my $PortName (Portically::PortSort keys %{$Switch->{Ports}}) {
my $Port = $Switch->{Ports}{$PortName};
if ($Port->{Unused}) {
$RetVal .= WriteUnusedRow($Switch, $Port);
}
}
$logger->debug("returning");
return $RetVal;
}
sub WriteUnusedBySwitchFiles ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
foreach my $Switch (sort @$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---/; # skip this switch if it's a group name
next if $Switch->{NbrUnusedPorts} == 0; # skip this switch if it doesn't have any unused ports
my $UbsFileName = $SwitchName . '.html';
my $UnusedBySwitchFileName = File::Spec->catfile($Constants::UnusedDirectory, $UbsFileName);
$logger->info("writing $UnusedBySwitchFileName");
open UBSFILE, ">$UnusedBySwitchFileName" or do {
$logger->fatal("Couldn't open $UnusedBySwitchFileName for writing, $!");
exit;
};
print UBSFILE SwitchUtils::HtmlHeader("Unused Ports (idle for > $ThisSite::UnusedAfter days) on $SwitchName");
print UBSFILE "<table class=\"UnusedPorts\">\n";
print UBSFILE HtmlUnusedPortTableHeader();
print UBSFILE WriteUnusedBySwitchData($Switch);
print UBSFILE "</table>\n\n";
print UBSFILE SwitchUtils::HtmlTrailer;
close UBSFILE;
SwitchUtils::AllowAllToReadFile $UnusedBySwitchFileName;
}
$logger->debug("returning");
}
sub UnusedByVlanIndex ($) {
my $VlansRef = shift; # passed in
my $logger = get_logger('log4');
$logger->debug("called");
my $i = 0;
my $columns = 4;
my $NbrVlans = keys %$VlansRef;
my $rows = ($NbrVlans / $columns) + 1;
my @OutList = ();
my $RetVal .= "<h3>Unused Ports Organized By Vlan</h3>\n" .
"<table class=\"noborder\" width=1200>\n";
foreach my $VlanNbr (sort {$a <=> $b} keys %$VlansRef) {
my $Vlan = $$VlansRef{$VlanNbr};
my $VlanName= $ThisSite::VlanNames[$VlanNbr]? $ThisSite::VlanNames[$VlanNbr] : '[unknown]';
if ($Vlan->{NbrUnusedPorts} == 0) {
$OutList[$i] .= "<td>$VlanNbr: $VlanName <small>(</small>0<small> unused)</small></td>";
} else {
my $UbvFileName = 'vlan' . $VlanNbr . '-unused.html';
$OutList[$i] .= "<td><a href=\"$UbvFileName\">$VlanNbr: $VlanName</a> <small>(</small>$Vlan->{NbrUnusedPorts}<small> unused)</small></td>";
}
$i = 0 if ++$i >= $rows;
}
foreach my $Row (@OutList) {
$RetVal .= "<tr>$Row</tr>\n";
}
$RetVal .= "</table>\n";
$logger->debug("returning");
return $RetVal;
}
sub UnusedBySwitchIndex ($) {
my $SwitchesRef = shift; # passed in
my $logger = get_logger('log4');
$logger->debug("called");
my $i = 0;
my $columns = 3;
my $rows = ($#{$SwitchesRef} / $columns) + 1;
my @OutList = ();
my $RetVal .= "<h3>Unused Ports Organized By Switch</h3>\n" .
"<table class=\"noborder\">\n";
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---/; # skip it if it's a group name
if ($Switch->{NbrUnusedPorts} == 0) {
$OutList[$i] .= "<td>$SwitchName <small>(</small>0<small> unused)</small></td>";
} else {
$OutList[$i] .= "<td><a href=\"$SwitchName.html\">$SwitchName</a> <small>(</small>$Switch->{NbrUnusedPorts}<small> unused)</small></td>";
}
$i = 0 if ++$i >= $rows;
}
foreach my $Row (@OutList) {
$RetVal .= "<tr>$Row</tr>\n";
}
$RetVal .= "</table>\n";
$logger->debug("returning");
return $RetVal;
}
sub WriteUnusedIndex ($$) {
my $SwitchesRef = shift; # passed in
my $VlansRef = shift; # passed in
my $logger = get_logger('log3');
my $IndexFileName = File::Spec->catfile($Constants::UnusedDirectory, 'index.html');
$logger->debug("called, writing $IndexFileName");
$logger->info("writing $IndexFileName");
open INDEXFILE, ">$IndexFileName" or do {
$logger->fatal("Couldn't open $IndexFileName for writing, $!");
exit;
};
print INDEXFILE SwitchUtils::HtmlHeader("Unused Ports (idle for > $ThisSite::UnusedAfter days)");
print INDEXFILE UnusedBySwitchIndex($SwitchesRef);
print INDEXFILE UnusedByVlanIndex($VlansRef);
print INDEXFILE SwitchUtils::HtmlTrailer;
close INDEXFILE;
SwitchUtils::AllowAllToReadFile $IndexFileName;
$logger->debug("returning");
}
sub WriteUnusedDirectory ($$) {
my $SwitchesRef = shift;
my $VlansRef = shift;
my $logger = get_logger('log2');
$logger->debug("called");
SwitchUtils::SetupDirectory $Constants::UnusedDirectory; # create or empty out the directory
WriteUnusedBySwitchFiles($SwitchesRef);
WriteUnusedByVlanFiles($VlansRef);
WriteUnusedIndex($SwitchesRef, $VlansRef);
$logger->debug("returning");
}
1;