-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stats.pm
373 lines (325 loc) · 12.5 KB
/
Stats.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
package Stats;
use strict;
use Log::Log4perl qw(get_logger :levels);
use ModuleList;
my %ModuleModelCounts;
my %ModuleModelDescriptions;
my %SwitchModelCounts;
my @SwitchDetails;
my $TotalActivePorts =
my $TotalActiveVirtualPorts =
my $TotalAncillaryPorts =
my $TotalDisabledPorts =
my $TotalDisabledVirtualPorts =
my $TotalEmptyPortLabels =
my $TotalEtherChannelPorts =
my $TotalInactivePorts =
my $TotalInactiveVirtualPorts =
my $TotalPorts =
my $TotalPortsConnectedToIpPhone =
my $TotalPortsWithAuxiliaryVlans =
my $TotalTrunkPorts = 0;
my %TotalPortsPerVlan; # each key is a VLAN number and each value is the total number of ports in the VLAN
#
# Scan the Switches array and collect some statistics.
#
sub CollectStats ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log3');
$logger->debug("called");
my $TotalExaminedPorts = 0;
foreach my $Switch (@$SwitchesRef) {
my $SwitchName = GetName $Switch;
next if $SwitchName =~ /^---/; # skip it if it's a group name
my $Details = {};
push @SwitchDetails, $Details;
$Details->{Name} = $SwitchName;
my $SwitchModel = $Switch->GetChassisModel;
$logger->debug("$SwitchName: SwitchModel = \"$SwitchModel\"");
$SwitchModelCounts{$SwitchModel}++;
# collect module statistics
if ($Switch->{NbrModules} > 1) { # if it has modules (i.e. 6509s have modules, 3524s don't)
foreach my $ModNbr (keys %{$Switch->{ModuleList}{Model}}) { # for each module in the switch
my $ModuleModel = $Switch->{ModuleList}->GetModuleModel($ModNbr);
$ModuleModelCounts{$ModuleModel}++;
$ModuleModelDescriptions{$ModuleModel} = $Switch->{ModuleList}->GetModuleDescription($ModNbr);
}
}
# collect port statistics
my $active = 0;
my $inactive = 0;
my $disabled = 0;
my $ancillary = 0;
foreach my $PortName (keys %{$Switch->{Ports}}) {
# $logger->debug("port name = $PortName");
my $Port = $Switch->{Ports}{$PortName};
$TotalExaminedPorts++;
if (SwitchUtils::IsAncillaryPort($Port)) {
$TotalAncillaryPorts++;
$ancillary++;
} else {
my $State = $Port->{State};
if ($State eq 'Active') {
if ($Port->{IsVirtual}) {
$TotalActiveVirtualPorts++;
} else {
$TotalActivePorts++;
}
$active++;
if ($State eq '') {
$TotalEmptyPortLabels++;
}
} elsif ($State eq 'Inactive') {
if ($Port->{IsVirtual}) {
$TotalInactiveVirtualPorts++;
} else {
$TotalInactivePorts++;
}
$inactive++;
} elsif ($State eq 'Disabled') {
if ($Port->{IsVirtual}) {
$TotalDisabledVirtualPorts++;
} else {
$TotalDisabledPorts++;
}
$disabled++;
} elsif ($State eq 'Unknown') {
$logger->warn("port in \"unknown\" state encountered");
} else {
$logger->warn("internal error: unexpected port state \"$State\" encountered");
}
$TotalPortsWithAuxiliaryVlans++ if $Port->{AuxiliaryVlanNbr};
$TotalPortsConnectedToIpPhone++ if $Port->{IsConnectedToIpPhone};
$TotalTrunkPorts++ if $Port->{IsTrunking};
$TotalEtherChannelPorts++ if $Port->{EtherChannel};
$TotalPortsPerVlan{$Port->{VlanNbr}}++ if defined $Port->{VlanNbr};
}
}
$Details->{active} = $active;
$Details->{inactive} = $inactive;
$Details->{disabled} = $disabled;
$Details->{ancillary} = $ancillary;
$Details->{total} = $active + $inactive + $disabled;
}
$logger->debug("TotalActivePorts = $TotalActivePorts");
$logger->debug("TotalInactivePorts = $TotalInactivePorts");
$logger->debug("TotalDisabledPorts = $TotalDisabledPorts");
my $tmps = $TotalActivePorts + $TotalInactivePorts + $TotalDisabledPorts;
$logger->debug("tmps = $tmps");
$logger->debug("returning, examined $TotalExaminedPorts ports");
}
#
# Output a table summarizing the number of each switch type.
#
sub WriteSwitchesTable () {
my $logger = get_logger('log3');
$logger->debug("called");
print SWITCHSTATSFILE <<TTABLE;
<table class="Stats">
<caption class="stats">Switches</caption>
<tr class="tblHead"><th>Count</th><th>Model</th></tr>
TTABLE
my $TotalSwitches = 0;
foreach my $stype (sort keys %SwitchModelCounts) {
print SWITCHSTATSFILE "<tr><td>$SwitchModelCounts{$stype}</td><td>$stype</td></tr>\n";
$TotalSwitches += $SwitchModelCounts{$stype};
}
print SWITCHSTATSFILE
"<tr><td>$TotalSwitches</td><td><b>Total</b></td></tr>\n" .
"</table>\n";
$logger->debug("returning");
}
sub WriteModulesTable () {
my $logger = get_logger('log3');
$logger->debug("called");
print SWITCHSTATSFILE <<MTABLE;
<table class="Stats">
<caption class="stats">Modules</caption>
<tr class="tblHead"><th>Count</th><th>Model</th><th>Description</th></tr>
MTABLE
my $TotalModules = 0;
foreach my $Model (sort keys %ModuleModelCounts) {
print SWITCHSTATSFILE <<MTABLE2;
<tr>
<td>$ModuleModelCounts{$Model}</td>
<td>$Model</td>
<td>$ModuleModelDescriptions{$Model}</td>
</tr>
MTABLE2
$TotalModules += $ModuleModelCounts{$Model};
}
print SWITCHSTATSFILE <<MTABLE3;
<tr>
<td>$TotalModules</td>
<td colspan="2"><b>Total</b></td>
</tr>
</table>
MTABLE3
$logger->debug("returning");
}
sub WritePortStatesTable () {
my $logger = get_logger('log3');
$logger->debug("called");
my $TotalVlans = keys %TotalPortsPerVlan; # in scalar context, keys returns the number of keys in a hash
my $TotalPorts = $TotalActivePorts + $TotalInactivePorts + $TotalDisabledPorts;
my $TotalVirtualPorts = $TotalActiveVirtualPorts + $TotalInactiveVirtualPorts + $TotalDisabledVirtualPorts;
print SWITCHSTATSFILE <<TOTALS;
<table class="Stats">
<caption class="stats">Port by State</caption>
<tr class="tblhead"><th> </th><th>virtual</th><th>non-virtual</th></tr>
<tr><td>active ports</td><td>$TotalActiveVirtualPorts</td><td>$TotalActivePorts</td></tr>
<tr><td>inactive ports</td><td>$TotalInactiveVirtualPorts</td><td>$TotalInactivePorts</td></tr>
<tr><td>disabled ports</td><td>$TotalDisabledVirtualPorts</td><td>$TotalDisabledPorts</td></tr>
<tr><td><b>Total</b></td><td>$TotalVirtualPorts</td><td>$TotalPorts</td></tr>
</table>
TOTALS
$logger->debug("returning");
}
sub WritePortCountsTable () {
my $logger = get_logger('log3');
$logger->debug("called");
my $TotalVlans = keys %TotalPortsPerVlan; # in scalar context, keys returns the number of keys in a hash
print SWITCHSTATSFILE <<TOTALS;
<table class="Stats">
<caption class="stats">Port Types</caption>
<tr class="tblhead"><th>Count</th><th>What</th></tr>
<tr><td>$TotalPortsWithAuxiliaryVlans</td><td>ports with a defined auxiliary VLAN</td></tr>
<tr><td>$TotalPortsConnectedToIpPhone</td><td>ports connected to an IP phone</td></tr>
<tr><td>$TotalTrunkPorts</td><td>trunk ports</td></tr>
<tr><td>$TotalEtherChannelPorts</td><td>etherchannel ports</td></tr>
<tr><td>$TotalEmptyPortLabels</td><td>active ports with empty "Port Label" fields</td></tr>
</table>
TOTALS
$logger->debug("returning");
}
sub WriteVlansTable () {
my $logger = get_logger('log3');
$logger->debug("called");
my $TotalVlans = keys %TotalPortsPerVlan; # in scalar context, keys returns the number of keys in a hash
print SWITCHSTATSFILE <<TOTALS;
<table class="Stats">
<caption class="stats">VLANs</caption>
<tr class="tblhead"><th>Count</th><th>What</th></tr>
<tr><td>$TotalVlans</td><td>VLANs</td></tr>
</table>
TOTALS
$logger->debug("returning");
}
sub WriteIpAddressesTable () {
my $logger = get_logger('log3');
$logger->debug("called");
my $TotalIpAddresses = SwitchUtils::getUniqueIpAddresses(); # in scalar context, keys returns the number of keys in a hash
print SWITCHSTATSFILE <<TOTALS;
<table class="Stats">
<caption class="stats">Unique IP Addresses</caption>
<tr><td>$TotalIpAddresses</td></tr>
</table>
TOTALS
$logger->debug("returning");
}
sub getSite($) {
my $SwitchName = shift;
return '910' if $SwitchName =~ /^910/;
return 'cg' if $SwitchName =~ /^cg/;
return 'fb' if $SwitchName =~ /^fb/;
return 'fl' if $SwitchName =~ /^fl/;
return 'frgp' if $SwitchName =~ /^frgp/;
return 'l3' if $SwitchName =~ /^l3/;
return 'mar' if $SwitchName =~ /^mar/;
return 'ml' if $SwitchName =~ /^ml/;
return 'raf' if $SwitchName =~ /^raf/;
return 'ral' if $SwitchName =~ /^ral/;
return 'tcom' if $SwitchName =~ /^tcom/;
return 'wy' if $SwitchName =~ /^wy/;
return substr $SwitchName, 0, 2;
}
sub WriteSwitchDetailTable () {
my $logger = get_logger('log3');
$logger->debug("called");
my $TotalVlans = keys %TotalPortsPerVlan; # in scalar context, keys returns the number of keys in a hash
print SWITCHSTATSFILE <<DTOTALS;
<table class="Stats">
<caption class="stats">Switch Details</caption>
<tr class="tblhead"><th>Switch</th><th>active</th><th>inactive</th><th>disabled</th><th>total</th></tr>
DTOTALS
my $site = '';
my $lastSite = '';
my $GrandActive = 0;
my $GrandInactive = 0;
my $GrandDisabled = 0;
my $GrandAncillary = 0;
my $GrandTotal = 0;
my $SiteActive = 0;
my $SiteInactive = 0;
my $SiteDisabled = 0;
my $SiteAncillary = 0;
my $SiteTotal = 0;
foreach my $SwitchDetail (@SwitchDetails) {
my $SwitchName = $SwitchDetail->{Name};
if ($ThisSite::DnsDomain eq '.ucar.edu') { # if we're at NCAR
$site = getSite($SwitchName);
$lastSite = $site if $lastSite eq '';
if ($site ne $lastSite) {
print SWITCHSTATSFILE "<tr class=\"sitetotals\"><td><b>$lastSite totals</b></td><td>$SiteActive</td><td>$SiteInactive</td><td>$SiteDisabled</td><td>$SiteTotal</td></tr>";
$GrandActive += $SiteActive;
$GrandInactive += $SiteInactive;
$GrandDisabled += $SiteDisabled;
$GrandAncillary += $SiteAncillary;
$GrandTotal += $SiteTotal;
$SiteActive = $SiteInactive = $SiteDisabled = $SiteAncillary = $SiteTotal = 0;
$lastSite = $site;
}
}
print SWITCHSTATSFILE "<tr>";
print SWITCHSTATSFILE "<td>$SwitchName</td>";
my $active = $SwitchDetail->{active}; print SWITCHSTATSFILE "<td>$active</td>"; $SiteActive += $active;
my $inactive = $SwitchDetail->{inactive}; print SWITCHSTATSFILE "<td>$inactive</td>"; $SiteInactive += $inactive;
my $disabled = $SwitchDetail->{disabled}; print SWITCHSTATSFILE "<td>$disabled</td>"; $SiteDisabled += $disabled;
my $total = $SwitchDetail->{total}; print SWITCHSTATSFILE "<td>$total</td>"; $SiteTotal += $total;
print SWITCHSTATSFILE "</tr>";
}
print SWITCHSTATSFILE "<tr class=\"sitetotals\"><td><b>$lastSite totals</b></td><td>$SiteActive</td><td>$SiteInactive</td><td>$SiteDisabled</td><td>$SiteTotal</td></tr>";
$GrandActive += $SiteActive;
$GrandInactive += $SiteInactive;
$GrandDisabled += $SiteDisabled;
$GrandAncillary += $SiteAncillary;
$GrandTotal += $SiteTotal;
print SWITCHSTATSFILE "<tr><td> </td><td> </td><td> </td><td> </td><td> </td></tr>";
print SWITCHSTATSFILE "<tr><tr class=\"grandtotals\"><td><b>Grand totals</b></td><td>$GrandActive</td><td>$GrandInactive</td><td>$GrandDisabled</td><td>$GrandTotal</td></tr>";
print SWITCHSTATSFILE "</table>";
$logger->debug("returning");
}
sub WriteStatisticsFile ($) {
my $SwitchesRef = shift;
my $logger = get_logger('log2');
my $SwitchStatsFileName = File::Spec->catfile($ThisSite::DestinationDirectory, $Constants::SwitchStatsFile);
$logger->debug("called, writing $SwitchStatsFileName");
CollectStats($SwitchesRef);
open SWITCHSTATSFILE, ">$SwitchStatsFileName" or do {
$logger->fatal("Couldn't open $SwitchStatsFileName for writing, $!");
exit;
};
print SWITCHSTATSFILE SwitchUtils::HtmlHeader("Statistics");
print SWITCHSTATSFILE "<table class=\"noborder\"><tr><td>\n";
WriteSwitchesTable();
print SWITCHSTATSFILE "</td><td>\n";
WriteModulesTable();
print SWITCHSTATSFILE "</td></tr></table>\n";
print SWITCHSTATSFILE "<p> </p>\n";
print SWITCHSTATSFILE "<table class=\"noborder\"><tr><td>\n";
WritePortStatesTable();
print SWITCHSTATSFILE "</td><td>\n";
WritePortCountsTable();
print SWITCHSTATSFILE "</td><td>\n";
WriteVlansTable();
print SWITCHSTATSFILE "</td><td>\n";
WriteIpAddressesTable();
print SWITCHSTATSFILE "</td></tr></table>\n";
print SWITCHSTATSFILE "<p> </p>\n";
WriteSwitchDetailTable();
print SWITCHSTATSFILE SwitchUtils::HtmlTrailer();
close SWITCHSTATSFILE;
SwitchUtils::AllowAllToReadFile $SwitchStatsFileName;
$logger->debug("returning");
}
1;