-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriteVlansDirectory.pm
122 lines (108 loc) · 4.74 KB
/
WriteVlansDirectory.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
package WriteVlansDirectory;
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
sub WriteVlansIndexFile (%) {
my $VlanPortCount = shift;
my $logger = get_logger('log3');
my $ByVlanIndexFileName = File::Spec->catfile($Constants::VlansDirectory, 'index.html');
$logger->debug("called, writing $ByVlanIndexFileName");
open BYVLANINDEXFILE, ">$ByVlanIndexFileName" or do {
$logger->fatal("Couldn't open $ByVlanIndexFileName for writing, $!");
exit;
};
print BYVLANINDEXFILE SwitchUtils::HtmlHeader("VLANs");
print BYVLANINDEXFILE "<table class=\"noborder\" width=1200>\n";
#
# Arrange the list of VLAN pages in a table, 5 per row so that
# a reasonable number fit on the page without needing to scroll.
#
my $i = 0;
my $columns = 4;
my @VlanNbrs = keys %$VlanPortCount; # an unsorted list of VLAN names, perhaps including "0"
my $rows = ($#VlanNbrs / $columns) + 1;
my @RowBody = ();
foreach my $VlanNbr ( sort {$a<=>$b} @VlanNbrs ) {
my $VlanName= $ThisSite::VlanNames[$VlanNbr]? $ThisSite::VlanNames[$VlanNbr] : '[unknown]';
$logger->debug("VlanNbr = \"$VlanNbr\"");
my $PortCount = $VlanPortCount->{$VlanNbr};
my $VlanDisplayName = ($VlanNbr == 0) ? "no VLAN" : "$VlanNbr: $VlanName";
$RowBody[$i] .= <<RBODY;
<td><a href="vlan$VlanNbr.html">$VlanDisplayName</a> <small>(</small>$PortCount<small> ports)</small></td>
RBODY
$i = 0 if ++$i >= $rows;
}
foreach my $row (@RowBody) {
print BYVLANINDEXFILE "<tr>$row </tr>\n";
}
print BYVLANINDEXFILE "</table>\n";
print BYVLANINDEXFILE SwitchUtils::HtmlTrailer();
close BYVLANINDEXFILE;
SwitchUtils::AllowAllToReadFile $ByVlanIndexFileName;
$logger->debug("returning");
}
#
# Given a hash of hashes, write the Vlan tables.
#
sub WriteVlansDataFiles ($) {
my $VlanBodiesRef = shift; # passed in hash of hashes
my $logger = get_logger('log3');
$logger->debug("called");
foreach my $VlanNbr (sort keys %$VlanBodiesRef ) {
my $VlanName= $ThisSite::VlanNames[$VlanNbr]? $ThisSite::VlanNames[$VlanNbr] : '[unknown]';
my $ByVlanFileName = File::Spec->catfile($Constants::VlansDirectory, 'vlan' . $VlanNbr . '.html');
$logger->debug("writing $ByVlanFileName");
open VLANHTMLFILE, ">$ByVlanFileName" or do {
$logger->fatal("Couldn't open $ByVlanFileName for writing, $!");
exit;
};
my $Title = ($VlanNbr == 0) ? "Ports in no VLAN" : "Ports in VLAN $VlanNbr: $VlanName";
print VLANHTMLFILE SwitchUtils::HtmlHeader($Title);
my $VlanBodies = $$VlanBodiesRef{$VlanNbr};
foreach my $SwitchName (sort keys %$VlanBodies) {
print VLANHTMLFILE "<h2>Switch <a href=\"../switches/$SwitchName.html\">$SwitchName</a></h2>\n";
print VLANHTMLFILE "<p>\n";
print VLANHTMLFILE SwitchUtils::HtmlPortTableHeader();
print VLANHTMLFILE $$VlanBodies{$SwitchName};
print VLANHTMLFILE "</table>\n";
print VLANHTMLFILE "</p>\n\n";
}
print VLANHTMLFILE SwitchUtils::HtmlTrailer();
close VLANHTMLFILE;
SwitchUtils::AllowAllToReadFile $ByVlanFileName;
}
$logger->debug("returning");
}
sub WriteVlansDirectory ($) {
my $SwitchesRef = shift; # passed in
my $logger = get_logger('log2');
$logger->debug("called");
SwitchUtils::SetupDirectory $Constants::VlansDirectory; # create or empty out the directory
my %VlanPortCount;
my %VlanBodies;
my $MacIpAddrRef = MacIpTables::getMacIpAddr();
my $MacHostNameRef = MacIpTables::getMacHostName();
# Go through all the ports in all the switches, filling in the local
# anonymous hashes. In each local anonymous hash, each key is a
# switch name and each value is a text string that is the body of
# the HTML table that represents the ports that are in the VLAN.
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---/; # skip it if it's a group name
foreach my $PortName (Portically::PortSort keys %{$Switch->{Ports}}) {
my $Port = $Switch->{Ports}{$PortName};
my $VlanNbr = (exists $Port->{VlanNbr}) ? $Port->{VlanNbr} : 0; # for ports that aren't in a VLAN, call it VLAN 0 for now
$VlanBodies{$VlanNbr} = {} if !defined $VlanBodies{$VlanNbr};
$VlanBodies{$VlanNbr}{$SwitchName} .= SwitchUtils::MakeHtmlRow($Switch,
$Port,
$MacIpAddrRef,
$MacHostNameRef,
SwitchUtils::GetDirectoryDepth($Constants::VlansDirectory));
$VlanPortCount{$VlanNbr}++;
}
}
WriteVlansDataFiles(\%VlanBodies);
WriteVlansIndexFile(\%VlanPortCount);
$logger->debug("returning");
}
1;