-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWritePoePortsFile.pm
277 lines (239 loc) · 8.8 KB
/
WritePoePortsFile.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
package WritePoePortsFile;
use strict;
use Log::Log4perl qw(get_logger);
use Portically;
my %ApprovedDevicesCounts;
my %ApprovedManufacturersCounts;
my %PortsWithApprovedMacs;
my @UnapprovedPoeHtmlLines;
sub DeviceTypeIsApproved($) {
my $DeviceType = shift;
# my $logger = get_logger('log6');
# $logger->debug("called");
# $logger->debug("\$DeviceType = \"$DeviceType\"");
if (grep /^$DeviceType$/, @ThisSite::DevicesApprovedForPoe) {
# $logger->debug("returning true");
return 1;
}
# $logger->debug("returning false");
return 0;
}
sub MacIsApproved($) {
my $Mac = shift;
# my $logger = get_logger('log6');
# $logger->debug("called");
# $logger->debug("\$Mac = \"$Mac\"");
if (exists $ThisSite::MacsApprovedForPoe{$Mac}) {
# $logger->debug("returning true");
return 1;
}
# $logger->debug("returning false");
return 0;
}
sub ManufacturerIsApproved($) {
my $Manufacturer = shift;
# my $logger = get_logger('log6');
# $logger->debug("called");
# $logger->debug("\$Mac = \"$Mac\", \$Manufacturer = \"$Manufacturer\"");
if (exists $ThisSite::ManufacturersApprovedForPoe{$Manufacturer}) {
# $logger->debug("returning true");
return 1;
}
# $logger->debug("returning false");
return 0;
}
sub getUnapprovedPoePorts($@) {
my $Switch = shift;
my $UnapprovedPoePorts = shift;
my $logger = get_logger('log5');
my $SwitchName = GetName $Switch;
$logger->debug("called");
my @returnedUnapprovedPoePorts;
foreach my $PortName (Portically::PortSort keys %{$Switch->{Ports}}) {
# $logger->debug("processing \"$SwitchName\", $PortName");
my $Port = $Switch->{Ports}{$PortName};
if ($Port->{PoeStatus} == $Constants::DELIVERING_POWER) {
my $DeviceType = $Port->{CdpCachePlatform};
my $Mac = '';
my $NbrMacs = keys %{$Port->{Mac}};
if ($NbrMacs == 1) { # if exactly one MAC exists on the port (we just don't handle more than one)
foreach my $PortMac (keys %{$Port->{Mac}}) {
next if $PortMac eq '';
$Mac = $PortMac;
last;
}
}
if ($Mac ne '') {
# $logger->debug("Mac = $Mac");
my $Manufacturer = substr $Mac, 0, 6;
if (DeviceTypeIsApproved($DeviceType)) {
# $logger->debug("$Mac: device type is approved");
$ApprovedDevicesCounts{$DeviceType}++;
} elsif (ManufacturerIsApproved($Manufacturer)) {
# $logger->debug("$Mac: manufacturer is approved");
$ApprovedManufacturersCounts{$Manufacturer}++;
} elsif (MacIsApproved($Mac)) {
# $logger->debug("$Mac: MAC is approved");
$PortsWithApprovedMacs{$Mac} = "$SwitchName#$PortName";
} else {
# $logger->debug("unapproved");
push @returnedUnapprovedPoePorts, $Port;
}
}
}
}
$logger->debug("returning");
return @returnedUnapprovedPoePorts;
}
sub addBlockToUnapprovedPoeHtmlLines($@) {
my $Switch = shift;
my $MacIpAddrRef = shift;
my $MacHostNameRef = shift;
my $UnapprovedPoePorts = shift;
my $logger = get_logger('log5');
$logger->debug("called");
if (@$UnapprovedPoePorts) {
my $SwitchName = GetName $Switch;
$logger->debug("for switch \"$SwitchName\", appending lines...");
push @UnapprovedPoeHtmlLines, <<POE1;
<h2>Switch $SwitchName</h2>
<p>
POE1
push @UnapprovedPoeHtmlLines, SwitchUtils::HtmlPortTableHeader();
foreach my $Port (@$UnapprovedPoePorts) {
my $PortName = $Port->{Name};
$logger->debug("for switch \"$SwitchName\", appending a line for port $PortName");
push @UnapprovedPoeHtmlLines, SwitchUtils::MakeHtmlRow($Switch,
$Port,
$MacIpAddrRef,
$MacHostNameRef,
SwitchUtils::GetDirectoryDepth($Constants::PortsDirectory));
}
push @UnapprovedPoeHtmlLines, "</table>\n";
}
$logger->debug("returning");
}
sub MakePoeTable ($) {
my $SwitchesRef = shift; # passed in
my $logger = get_logger('log4');
$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 @UnapprovedPoePorts;
getUnapprovedPoePorts($Switch, \@UnapprovedPoePorts);
foreach my $uport (@UnapprovedPoePorts) {
$logger->debug("uport = \"$uport\"");
}
addBlockToUnapprovedPoeHtmlLines($Switch, $MacIpAddrRef, $MacHostNameRef, \@UnapprovedPoePorts);
}
$logger->debug("returning");
}
sub BuildApprovedManufacturersTable () {
my $logger = get_logger('log4');
$logger->debug("called");
my $RetVal;
if (keys %ThisSite::ManufacturersApprovedForPoe == 0) {
$RetVal = "The list of explicitly approved manufacturer OIDs is empty\n";
} elsif (keys %ApprovedManufacturersCounts == 0) {
$RetVal = "No devices match the list of explicitly approved manufacturer OIDs\n";
} else {
$RetVal = <<POE2;
<h3>Counts of ports delivering Power-over-Ethernet to devices whose manufacturer OIDs are approved for PoE:</h3>
<table class="Stats">
<tr class="tblhead"><th>Count</th><th>Manufacturer Oid</th><th>comment (from list of approved manufacturer OIDs)</th></tr>
POE2
foreach my $Oid (keys %ThisSite::ManufacturersApprovedForPoe) {
my $Count = (exists $ApprovedManufacturersCounts{$Oid}) ? $ApprovedManufacturersCounts{$Oid} : 0;
my $Comment = (exists $ThisSite::ManufacturersApprovedForPoe{$Oid}) ? $ThisSite::ManufacturersApprovedForPoe{$Oid} : ' ';
$RetVal .= <<POE3;
<tr><td>$Count</td><td>$Oid</td><td>$Comment</td></tr>
POE3
}
$RetVal .= "</table>\n";
}
$logger->debug("returning");
return $RetVal;
}
sub BuildApprovedDevicesTable () {
my $logger = get_logger('log4');
$logger->debug("called");
my $RetVal = <<POE4;
<h3>Counts of ports delivering Power-over-Ethernet to devices that are approved for PoE:</h3>
<table class="Stats">
<tr class="tblhead"><th>Count</th><th>device type</th></tr>
POE4
foreach my $DeviceType (@ThisSite::DevicesApprovedForPoe) {
my $Count = (exists $ApprovedDevicesCounts{$DeviceType}) ? $ApprovedDevicesCounts{$DeviceType} : 0;
$RetVal .= <<POE5;
<tr><td>$Count</td><td>$DeviceType</td></tr>
POE5
}
$RetVal .= "</table>\n";
$logger->debug("returning");
return $RetVal;
}
sub BuildApprovedMacsTable () {
my $logger = get_logger('log4');
$logger->debug("called");
my $RetVal = <<POE6;
<h3>Ports delivering Power-over-Ethernet to explicit MAC addresses that are approved for PoE:</h3>
<table class="Stats">
<tr class="tblhead"><th>Switch</th><th>Port</th><th>MAC address</th><th>comment (from list of approved MACs)</th></tr>
POE6
foreach my $Mac (sort keys %PortsWithApprovedMacs) {
my ($SwitchName, $PortName) = split /#/, $PortsWithApprovedMacs{$Mac};
my $Comment = (exists $ThisSite::MacsApprovedForPoe{$Mac}) ? $ThisSite::MacsApprovedForPoe{$Mac} : ' ';
$RetVal .= <<POE7;
<tr><td>$SwitchName</td><td>$PortName</td><td>$Mac</td><td>$Comment</td></tr>
POE7
}
$RetVal .= "</table>\n";
$logger->debug("returning");
return $RetVal;
}
sub printPoeHtmlFileContents() {
my $logger = get_logger('log3');
$logger->debug("called");
print POEHTMLFILE SwitchUtils::HtmlHeader("Power-over-Ethernet ports");
print POEHTMLFILE BuildApprovedManufacturersTable();
print POEHTMLFILE BuildApprovedDevicesTable();
print POEHTMLFILE BuildApprovedMacsTable();
if (@ThisSite::DevicesApprovedForPoe) { # if there is an approved devices list
print POEHTMLFILE "<hr><h3>Ports delivering Power-over-Ethernet to unapproved devices or MACs:</h3>\n";
if (@UnapprovedPoeHtmlLines) {
print POEHTMLFILE @UnapprovedPoeHtmlLines;
} else {
print POEHTMLFILE "None.\n";
}
} else {
print POEHTMLFILE "The list of approved devices in ThisSite.pm is empty, so there are no unapproved POE devices.\n";
}
print POEHTMLFILE SwitchUtils::HtmlTrailer();
$logger->debug("returning");
}
sub WriteTheFile () {
my $logger = get_logger('log2');
$logger->debug("called");
my $PoeFile = File::Spec->catfile($Constants::PortsDirectory, $Constants::PoeFile);
$logger->debug("writing $PoeFile");
open POEHTMLFILE, ">$PoeFile" or do {
$logger->fatal("Couldn't open $PoeFile for writing, $!");
exit;
};
printPoeHtmlFileContents();
close POEHTMLFILE;
SwitchUtils::AllowAllToReadFile $PoeFile;
$logger->debug("returning");
}
sub WritePoePortsFile ($) {
my $SwitchesRef = shift; # passed in
my $logger = get_logger('log2');
$logger->debug("called");
MakePoeTable($SwitchesRef); # fill all the global arrays
WriteTheFile();
$logger->debug("returning");
}
1;