-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteSwitchesDirectory.pm
393 lines (335 loc) · 12.1 KB
/
WriteSwitchesDirectory.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
package WriteSwitchesDirectory;
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
#use Data::Dumper;
sub HeaderBlurb () {
return <<HEADERBLURB;
<p>
Most of the following information came from the switch
itself. Some information in the <strong>MAC address</strong>,
<strong>NIC Manufacturer</strong>,
<strong>IP address</strong> or <strong>DNS Name</strong>
columns may be incorrect due to unavoidable problems with switch
bridge table incompleteness, timing of SNMP requests, or obsolete MAC
data. A comment at the bottom of this page indicates when this page
was created. Green rows are active ports. White rows are ports that
have been inactive for less than $ThisSite::UnusedAfter days. Grey
rows are ports that have been inactive for more than
$ThisSite::UnusedAfter days. Red indicates problems that should be
fixed, like an empty <strong>Port Label</strong> field on an
active port.
</p>
HEADERBLURB
}
sub ModuleDetailTable ($) {
my $Switch = shift;
my $logger = get_logger('log5');
$logger->debug("called");
my $RetVal = "<table border class=\"Switch\" summary=\"Switch information\">\n";
$RetVal .= "<caption><strong>Switch information</strong></caption>\n";
my $SwitchChassisModel = $Switch->GetChassisModel;
if ($SwitchChassisModel ne 'unknown') {
$RetVal .= "<tr><td class=\"tblHead\"><strong>Model</strong></td><td>$SwitchChassisModel</td></tr>\n";
}
my $ProductName = $Switch->GetProductName;
if (($ProductName ne '') and ($ProductName ne $SwitchChassisModel)) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>Cisco Product name</strong></td><td>$ProductName</td></tr>\n";
}
my $ProductDescription = $Switch->GetProductDescription;
if ($ProductDescription) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>Cisco Product comment</strong></td><td>$ProductDescription</td></tr>\n";
}
my $SysDescription = $Switch->GetSysDescription;
$SysDescription =~ s/\n/<br>\n/g;
if ($SysDescription) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>sysDescr</strong></td><td>$SysDescription</td></tr>\n";
}
my $SysName = $Switch->GetSysName;
$SysName =~ s/\n/<br>\n/g;
if ($SysName) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>sysName</strong></td><td>$SysName</td></tr>\n";
}
my $SwitchLocation = $Switch->GetLocation;
if ($SwitchLocation) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>Location</strong></td><td>$SwitchLocation</td></tr>\n";
}
my $SwitchContact = $Switch->GetContact;
if ($SwitchContact) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>Contact</strong></td><td>$SwitchContact</td></tr>\n";
}
my $SysUptime = $Switch->GetSysUptime;
if ($SysUptime) {
$RetVal .= "<tr><td class=\"tblHead\"><strong>sysUptime</strong></td><td>$SysUptime</td></tr>\n";
}
$RetVal .= "</table>\n\n";
$logger->debug("returning");
return $RetVal;
}
sub GetSectionName ($$) {
my $Switch = shift;
my $PortName = shift;
my $SectionName = '';
my $Port = $Switch->{Ports}{$PortName};
if (SwitchUtils::IsAncillaryPort $Port) {
$SectionName = 'Ancillary Ports';
} elsif ($Port->{IsVirtual}) {
$SectionName = 'Virtual Ports';
} else {
if ($Switch->{NbrModules} > 1) { # if it has modules (i.e. 6509s have modules, 3524s don't)
$PortName =~ /[^\d]*(\d+)\//; # this matches "3/4", "Ga9/6", "Fa2/0/15", etc.
if (defined $1) {
# When you remove a module from a running 6500, its ports won't
# disappear from the switch until the switch is rebooted. So we
# have to check if the module actually exists here to avoid
# showing modules that don't really exist. The "model" hash
# uses module numbers as keys.
my $ModuleList= $Switch->{ModuleList};
if (exists $ModuleList->{Model}{$1} && $ModuleList->{Description}{$1} ne 'StackWise notMember') {
$SectionName = "Module $1";
} else {
$SectionName = 'Removed Ports';
}
}
}
if ($SectionName eq '') {
$SectionName = 'Ports';
}
}
return $SectionName;
}
my %Sections; # hash of arrays of rows
#
# Loop through the ports, creating an HTML row for each port. Put
# each row into a section on the page. Sections have names like
# 'Module 1', 'Virtual Ports', etc.
#
sub BuildSections ($$$) {
my $Switch = shift;
my $MacIpAddrRef = shift;
my $MacHostNameRef = shift;
my $logger = get_logger('log4');
$logger->debug("called");
%Sections = ();
foreach my $PortName (Portically::PortSort keys %{$Switch->{Ports}}) {
$logger->debug("PortName = \"$PortName\"");
my $Port = $Switch->{Ports}{$PortName};
my $HtmlRow = SwitchUtils::MakeHtmlRow($Switch,
$Port,
$MacIpAddrRef,
$MacHostNameRef,
SwitchUtils::GetDirectoryDepth($Constants::SwitchesDirectory));
my $SectionName = GetSectionName($Switch, $PortName);
if (!exists $Sections{$SectionName}) {
my @Rows = ();
$Sections{$SectionName} = [ @Rows ];
}
$logger->debug("for $PortName, storing a row in $SectionName");
push @{$Sections{$SectionName}}, $HtmlRow;
}
$logger->debug("returning");
}
sub printModuleSections($) {
my $Switch = shift;
my $logger = get_logger('log4');
$logger->debug("called");
my $SwitchName = GetName $Switch;
foreach my $SectionName (Portically::PortSort keys %Sections) {
if ($SectionName =~ /Module (\d+)/) {
my $ModNbr = $1;
print HTMLFILE ModuleList::WriteHtmlModuleTable $Switch->{ModuleList}, $SwitchName, $ModNbr;
print HTMLFILE SwitchUtils::HtmlPortTableHeader();
foreach my $i ( 0 .. $#{ $Sections{$SectionName} } ) {
print HTMLFILE $Sections{$SectionName}[$i];
}
print HTMLFILE "</table>\n\n";
}
}
$logger->debug("returning");
}
sub printSection($) {
my $SectionName = shift;
my $logger = get_logger('log4');
$logger->debug("called for section $SectionName");
return if !exists $Sections{$SectionName};
print HTMLFILE <<SECTIONHEADER;
<hr size="5" noshade>
<p class="section-title">$SectionName</p>
SECTIONHEADER
print HTMLFILE SwitchUtils::HtmlPortTableHeader();
foreach my $i ( 0 .. $#{ $Sections{$SectionName} } ) {
print HTMLFILE $Sections{$SectionName}[$i];
}
print HTMLFILE "</table>\n\n";
$logger->debug("returning");
}
sub WriteSwitchHtmlData ($$$) {
my $Switch = shift;
my $MacIpAddrRef = shift;
my $MacHostNameRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
BuildSections($Switch, $MacIpAddrRef, $MacHostNameRef);
print HTMLFILE ModuleDetailTable($Switch);
printModuleSections($Switch);
printSection('Ports');
printSection('Virtual Ports');
printSection('Removed Ports');
printSection('Ancillary Ports') if $ThisSite::ShowAncillaryPorts;
$logger->debug("returning");
}
sub GetSwitchesIndex ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
my $NbrRows = ($#{$SwitchesRef} / $ThisSite::SwitchIndexColumns) + 1;
my @OutList = ();
my $RowNbr = 0;
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
if ($SwitchName =~ /^---(.+)/) { # if it's a group name, not a switch
$OutList[$RowNbr] .= "<th>$1</th>\n";
} else {
my $HtmlName = '';
if ($ThisSite::UseSysNames) {
my $SName = $Switch->GetSysName();
$HtmlName = "$SName<br><span class='small'>($SwitchName)<br></span>\n";
} else {
$HtmlName = $SwitchName;
}
$OutList[$RowNbr] .= "<td><a href=\"$SwitchName.html\">$HtmlName</a></td>\n";
}
$RowNbr = 0 if ++$RowNbr >= $NbrRows;
}
my $RetVal = '';
foreach (@OutList) {
$RetVal .= "<tr>$_</tr>\n";
}
$logger->debug("returning");
return $RetVal;
}
sub GetSwitchesIndexByGroup ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
#
# Build a hash of groups, where each group is a ;-delimited list of switches within the group.
#
my %Group;
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
if ($SwitchName =~ /^---(.+)/) { # if it's a group name, not a switch
$logger->warn("Encountered a string starting with \"---\" in \@LocalSwitches, which is incompatible with \$UseGroups=1, skipping the string");
next;
}
$SwitchName =~ s/$ThisSite::DnsDomain//;
my $group = ThisSite::WhichGroup($SwitchName);
if (!defined $group) {
$group = 'undefined';
}
if (defined($Group{$group})) {
$Group{$group} .= ';' . $SwitchName;
} else {
$Group{$group} = $SwitchName;
}
}
my $row = 0;
my $column = 0;
my $RowsPerColumn = int(($#ThisSite::LocalSwitches + keys %Group) / $ThisSite::SwitchIndexColumns);
my @output; # each $output[N] contains an output row
foreach my $group (sort keys %Group) {
$logger->debug("loop: group = \"$group\"");
my @GroupSwitches = split(/;/, $Group{$group});
$output[$row][$column] = "<th>$group</th>";
$row++;
foreach my $Switch (@GroupSwitches) {
$logger->debug("loop: Switch = \"$Switch\"");
my $SwitchName = $Switch;
if ($ThisSite::UseSysNames) {
my $SName = $Switch->GetSysName();
$output[$row][$column] = "<td><a href=\"$SwitchName.html\">$SName<br><span class='small'>($SwitchName)<br></span></a></td>";
} else {
$output[$row][$column] = "<td><a href=\"$SwitchName.html\">$SwitchName</a></td>";
}
$row++;
}
if ($row >= $RowsPerColumn) {
$column++;
$row = 0;
}
}
#
# Final output
#
my $RetVal = '';
my $done = 0;
$row = 0;
while (!$done) {
$done=1;
$RetVal .= " <tr>";
for (my $column = 0; $column < $ThisSite::SwitchIndexColumns; $column++) {
if (defined($output[$row][$column])) {
$RetVal .= "$output[$row][$column]";
$done=0;
} else {
$RetVal .= "<td>--</td>\t";
}
}
$row++;
$RetVal .= "</tr>\n";
}
$logger->debug("returning");
return $RetVal;
}
sub WriteSwitchesIndexFile ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log2');
my $IndexFileName = File::Spec->catfile($Constants::SwitchesDirectory, 'index.html');
$logger->debug("called, writing $IndexFileName");
$logger->info("writing $IndexFileName");
open PORTSBYSWITCHFILE, ">$IndexFileName" or do {
$logger->fatal("Couldn't open $IndexFileName for writing, $!");
exit;
};
print PORTSBYSWITCHFILE SwitchUtils::HtmlHeader("Switches");
print PORTSBYSWITCHFILE "<table class=\"SwitchList\">\n";
if ($ThisSite::UseGroups) {
print PORTSBYSWITCHFILE GetSwitchesIndexByGroup($SwitchesRef);
} else {
print PORTSBYSWITCHFILE GetSwitchesIndex($SwitchesRef);
}
print PORTSBYSWITCHFILE "</table>\n";
print PORTSBYSWITCHFILE SwitchUtils::HtmlTrailer;
close PORTSBYSWITCHFILE;
SwitchUtils::AllowAllToReadFile $IndexFileName;
$logger->debug("returning");
}
sub WriteSwitchesFiles ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log2');
$logger->debug("called");
SwitchUtils::SetupDirectory $Constants::SwitchesDirectory; # create or empty out the directory
my $MacIpAddrRef = MacIpTables::getMacIpAddr();
my $MacHostNameRef = MacIpTables::getMacHostName();
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---(.+)/; # if it's a group name, not a switch
my $HtmlFileName = File::Spec->catfile($Constants::SwitchesDirectory, $SwitchName . '.html');
$logger->info("writing $HtmlFileName");
open HTMLFILE, ">$HtmlFileName" or do {
$logger->fatal("Couldn't open $HtmlFileName for writing, $!");
exit;
};
my $TitleLine = "$SwitchName ports list";
print HTMLFILE SwitchUtils::HtmlHeader($TitleLine);
print HTMLFILE HeaderBlurb();
WriteSwitchHtmlData($Switch, $MacIpAddrRef, $MacHostNameRef);
# UpdateVlanPortCount($SwitchNameitchName);
print HTMLFILE SwitchUtils::HtmlTrailer;
close HTMLFILE;
SwitchUtils::AllowAllToReadFile $HtmlFileName;
}
WriteSwitchesIndexFile($SwitchesRef);
$logger->debug("returning");
}
1;