-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriteGigePerVlansDirectory.pm
131 lines (115 loc) · 4.95 KB
/
WriteGigePerVlansDirectory.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
package WriteGigePerVlansDirectory;
# This module was created to satisfy a one-time need at my site. It's
# not really a well-planned part of the SwitchMap scripts, and was
# created by copying-and-modifying WriteVlansDirectory.pm. After all,
# WriteVlansDirectory produces the "Ports By Vlan" page, so it was
# reasonable to modify it slightly to produce "Gige Ports by Vlan".
# It's likely that this module will be deleted in the future.
# Pete Siemsen. 2005-10-17.
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
sub WriteGigePerVlansIndexFile ($$) {
my $VlansRef = shift;
my $GigePortsPerVlanRef = shift;
my $logger = get_logger('log3');
my $ByVlanIndexFileName = File::Spec->catfile($Constants::GigePerVlansDirectory, '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("Gigabit Ports Organized By VLAN");
print BYVLANINDEXFILE "<table class=\"noborder\" width=1200>\n";
my $i = 0;
my $columns = 4;
my @Vlans = keys %$VlansRef; # an unsorted list of all VLANs
my $rows = ($#Vlans / $columns) + 1;
my @RowBody = ();
foreach my $VlanNbr ( sort {$a<=>$b} keys %$VlansRef ) {
my $Vlan = $$VlansRef{$VlanNbr};
my $VlanName= $ThisSite::VlanNames[$VlanNbr]? $ThisSite::VlanNames[$VlanNbr] : '[unknown]';
my $PortCount = (exists $GigePortsPerVlanRef->{$VlanNbr}) ? $GigePortsPerVlanRef->{$VlanNbr} : 0;
$RowBody[$i] .= <<RBODY;
<td>
<a href="vlan$VlanNbr.html">$VlanNbr: $VlanName</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");
} # WriteGigePerVlansIndexFile
#
# Given a hash of hashes, write the Vlan tables.
#
sub WriteGigePerVlansDataFiles ($) {
my $VlanBodiesRef = shift; # passed in hash of hashes
my $logger = get_logger('log3');
$logger->debug("called");
foreach my $VlanNbr (sort keys %$VlanBodiesRef ) {
my $ByVlanFileName = File::Spec->catfile($Constants::GigePerVlansDirectory, 'vlan' . $VlanNbr . '.html');
$logger->debug("writing $ByVlanFileName");
open VLANHTMLFILE, ">$ByVlanFileName" or do {
$logger->fatal("Couldn't open $ByVlanFileName for writing, $!");
exit;
};
print VLANHTMLFILE SwitchUtils::HtmlHeader("Gigabit ports on VLAN $VlanNbr");
my $VlanBodies = $$VlanBodiesRef{$VlanNbr};
foreach my $SwitchName (sort keys %$VlanBodies) {
print VLANHTMLFILE "<h2>Switch $SwitchName</h2>\n";
print VLANHTMLFILE SwitchUtils::HtmlPortTableHeader();
print VLANHTMLFILE $$VlanBodies{$SwitchName};
print VLANHTMLFILE "</table></p>\n\n";
}
print VLANHTMLFILE SwitchUtils::HtmlTrailer();
close VLANHTMLFILE;
SwitchUtils::AllowAllToReadFile $ByVlanFileName;
}
$logger->debug("returning");
}
sub WriteGigePerVlansDirectory ($$) {
my $SwitchesRef = shift; # passed in
my $VlansRef = shift; # passed in
my $logger = get_logger('log2');
$logger->debug("called");
SwitchUtils::SetupDirectory $Constants::GigePerVlansDirectory; # create or empty out the directory
# Create a local hash of empty anonymous hashes, one for each VLAN.
my %VlanBodies;
foreach my $Vlan (keys %$VlansRef) {
$VlanBodies{$Vlan} = {};
}
my %GigePortsPerVlan;
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};
next if (!defined $Port->{Speed}) or ($Port->{Speed} ne '1G');
my $VlanNbr = (exists $Port->{VlanNbr}) ? $Port->{VlanNbr} : 0; # for ports that aren't in a VLAN, call it VLAN 0 for now
$VlanBodies{$VlanNbr}{$SwitchName} .=
SwitchUtils::MakeHtmlRow($Switch,
$Port,
$MacIpAddrRef,
$MacHostNameRef,
SwitchUtils::GetDirectoryDepth($Constants::GigePerVlansDirectory));
$GigePortsPerVlan{$VlanNbr}++;
}
}
WriteGigePerVlansDataFiles(\%VlanBodies);
WriteGigePerVlansIndexFile($VlansRef, \%GigePortsPerVlan);
$logger->debug("returning");
}
1;