Files
scripts/Perl Switchview/v2/view_html.pl
2024-10-14 00:08:40 +02:00

873 lines
32 KiB
Perl

#!/usr/bin/perl
$|=1;
use Net::SNMP qw/:ALL/;
use feature qw/say switch/;
use strict;
my @switches = qw /10.99.0.201 10.99.0.202 10.99.0.203 10.99.0.204 10.99.0.211 10.99.0.212 10.99.0.215 10.1.1.101 10.1.1.102 10.1.1.105 10.1.1.106 10.1.1.107 10.1.1.108 10.1.1.109 10.1.1.110 10.1.1.111 10.1.1.112 10.130.30.249 10.130.10.241 10.130.10.242 10.130.10.243 10.130.41.201 10.130.40.239/;
#my @switches = qw /10.99.0.215/;
my $community="con";
my $wwwPath="/mnt/syslog/switchview";
my $refreshInterval=60;
my $OnlyErrors = 0; #1|0
my $SpecialPorts = 0; #1|0, VLAN und loopback ausblenden wenn 0
my $PortErrorFile = '/mnt/syslog/switchview/port-errors';
my $AllPortErrorFile = '/mnt/syslog/switchview/port-errors-alltime';
my $PortErrorWOPath = 'port-errors';
my $AllPortErrorWOPath = 'port-errors-alltime';
foreach (@switches) {
read_switch("$_", "$community");
}
my @HTML;
{ # HTML Datei bauen
push @HTML, "<HTML>
<HEAD>
<meta http-equiv='refresh' content='$refreshInterval; URL=top.html'>
</HEAD>
<BODY>";
push @HTML, "<b id='top'>\n";
foreach (@switches) {
push @HTML, "<a href='$_.html' target='Daten'>$_</a>\n";
}
push @HTML, "</b><br>\n";
if ($OnlyErrors) {
if ($SpecialPorts) {
push @HTML, "Nur Ports mit Fehlern\n";
}
else {
push @HTML, "Nur Ports mit Fehlern. VLAN/Loopback/Stack/Null Ports werden ausgeblendet.\n";
}
}
else {
if ($SpecialPorts) {
push @HTML, "Alle Ports\n";
}
else {
push @HTML, "Alle Ports. VLAN/Loopback/Stack/Null Ports werden ausgeblendet\n";
}
}
push @HTML, "<br><font size=+1>Neu:</font>";
my $date=`date +"%Y-%m-%d"`;
chomp $date;
my $PortErrorHeader;
my $PortErrorFooter;
$PortErrorHeader = "<html><head><meta http-equiv='refresh' content='$refreshInterval; URL=$PortErrorWOPath'></head><body>";
$PortErrorFooter = "</body></html>";
`echo "$PortErrorHeader" > $PortErrorFile`;
`echo "select priority,syslogtag,receivedat,fromhost,message from SystemEvents where receivedat like '$date%' and (fromhost like 'conhp%' or fromhost like 'switch%') and (message like '%collision%' or message like '%duplex%' or message like '%mismatch%' or message like '%excessive%') order by receivedat desc;" | mysql -uroot -p\@Defender2013! -Dsyslog -H >> $PortErrorFile`;
`echo "$PortErrorFooter" >> $PortErrorFile`;
$PortErrorHeader = "<html><head><meta http-equiv='refresh' content='$refreshInterval; URL=$AllPortErrorWOPath'></head><body>";
`echo "$PortErrorHeader" > $AllPortErrorFile`;
`echo "select priority,syslogtag,receivedat,fromhost,message from SystemEvents where (fromhost like 'conhp%' or fromhost like 'switch%') and (message like '%collision%' or message like '%duplex%' or message like '%mismatch%' or message like '%excessive%') order by receivedat desc;" | mysql -uroot -p\@Defender2013! -Dsyslog -H >> $AllPortErrorFile`;
`echo "$PortErrorFooter" >> $AllPortErrorFile`;
push @HTML, "<a href='port-errors' target='Daten'>Portfehler (heute, alle Switche)</a>\n";
push @HTML, "<a href='port-errors-alltime' target='Daten'>Portfehler (alle Switche)</a>\n";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
my $timestr=sprintf("Aktualisiert am %02d.%02d.%4d %02d:%02d Uhr",$mday,$mon+1,$year+1900,$hour,$min);
push @HTML, "<div align='right'>$timestr</div>\n";
#push @HTML, "<hr>\n";
#foreach (@switches) { # Einzelausgaben der Switches in
# open FIN, "</tmp/$_";
# my @FI=<FIN>;
# close FIN;
# push @HTML, "<br>\n<br>\n<b id='$_'>$_ <a href='#top'>Oben</a></b><br>";
# push @HTML, @FI;
#}
push @HTML, "</BODY>\n</HTML>";
open INDEX, ">$wwwPath/top.html";
foreach (@HTML) {
printf INDEX "$_";
}
close INDEX;
}
#sleep (10);
#exec("view_html.pl");
print "\n";
exit 0;
sub create_switch_table {
my $SwitchIP = $_[0];
my %RSwitch = %{$_[1]};
my $SwitchUptime = ticks_to_time($RSwitch{$SwitchIP}->{'Switch'}->{'Uptime'});
my $SwitchHostname = $RSwitch{$SwitchIP}->{'Switch'}->{'Hostname'};
my $SwitchType = $RSwitch{$SwitchIP}->{'Switch'}->{'Type'};
my $IPstr = $RSwitch{$SwitchIP}->{'Switch'}->{'IPs'};
my @ta;
my %interface;
my @STP;
my @ASTATUS;
my @OSTATUS;
{ #Tabellenkopf
push @ta, "<html>
<head>
<meta http-equiv='refresh' content='$refreshInterval; URL=$SwitchIP.html'>
<title>Switch Viewer</title>
<script language='JavaScript' type='text/javascript'>
function popup(URL) {
w = window.open(URL, '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400');
}
</script>
<style type='text/css'>
td { padding-left:10px; padding-right:10px; }
a { position:relative; }
span.druber { display:none; }
a:hover span.druber {
display:block;
position:fixed;
top:30px;
right:30px;
padding: 15px;
background-color:#999999;
color:#000;
width:300px;
font-size: 80%;
}
</style>
</head>
<body>";
push @ta, "<table border=1 cellspacing=0 cellpadding=0>";
push @ta, " <tr>";
push @ta, " <td>";
push @ta, " <a name=$SwitchIP>IP</a>";
push @ta, " </td>";
push @ta, " <td>";
push @ta, " Hostname";
push @ta, " </td>";
push @ta, " <td>";
push @ta, " Uptime";
push @ta, " </td>";
push @ta, " <td>";
push @ta, " Typ";
push @ta, " </td>";
push @ta, " </tr>";
push @ta, " <tr>";
push @ta, "<td>";
push @ta, "<a href='javascript:void()'>";
push @ta, "$SwitchIP";
push @ta, "<span class='druber'>";
push @ta, "<table border=0>";
my @IPs = split/,/,$IPstr;
foreach (@IPs) {
my ($ip,$mask) = split / /,$_;
push @ta, "<tr><td><font size=-1>$ip</font></td><td><font size=-1>$mask</font></td></tr>";
}
push @ta, "</table>";
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, " <td>";
push @ta, " $SwitchHostname";
push @ta, " </td>";
push @ta, " <td>";
push @ta, " $SwitchUptime";
push @ta, " </td>";
push @ta, " <td>";
push @ta, " $SwitchType";
push @ta, " </td>";
push @ta, " </tr>";
push @ta, "</table>";
push @ta, "<br>";
@STP = qw / undef disabled blocking listening learning forwarding broken/;
@ASTATUS = qw / undef up down testing/;
@OSTATUS = qw / undef up down testing unknown dormant notPresent lowerLayerDown/;
push @ta, "<table border=1 cellspacing=0 cellpadding=0>";
push @ta, "<tr>";
push @ta, "<td>";
push @ta, "Port<br><font size=-1>Interface</font>";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "Admin Status<br>Oper Status";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "Speed/Duplex configured<br>Speed/Duplex running";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "Description";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "VLAN";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "Last Change"; # Letze Portstatusänderung vor ....
push @ta, "</td>";
push @ta, "<td>";
push @ta, "in octets<br>out octets";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "in errors<br>out errors";
push @ta, "</td>";
push @ta, "<td>";
push @ta, "STP status";
push @ta, "</td>";
push @ta, "</tr>";
}
my $j=0;
foreach (sort keys %{$RSwitch{$SwitchIP}->{'Ports'}}) {
my $PortName;
{ # Portname
$PortName = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifName'};
next if ($PortName =~ /loopback/i or $PortName =~ /vlan/i);
if ($PortName =~ /^$/i) {
$PortName = "not plugged";
}
}
my $PortIndex;
my $STP_text;
my $STP_color;
{ # Portindex
$PortIndex=sprintf("%d",$_);
}
{ # STP status
$STP_text = $STP[$RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifSTPStatus'}];
$STP_text = "" if ($STP_text eq "undef");
$STP_color = "yellow";
$STP_color = "green" if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifSTPStatus'} == 5);
$STP_color = "red" if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifSTPStatus'} == 2);
}
my $ASTATUS_text;
my $ASTATUS_color;
{ # Admin Status, soll Port aktiv sein
$ASTATUS_text = $ASTATUS[$RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifAdminStatus'}];
$ASTATUS_text = "" if ($ASTATUS_text eq "undef");
$ASTATUS_color = "yellow";
$ASTATUS_color = "green" if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifAdminStatus'} == 1);
$ASTATUS_color = "red" if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifAdminStatus'} == 2);
}
my $OSTATUS_text;
my $OSTATUS_color;
{ # Oper Status, Port aktiv?
$OSTATUS_text = $OSTATUS[$RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifOperStatus'}];
$OSTATUS_text = "" if ($OSTATUS_text eq "undef");
$OSTATUS_color = "yellow";
$OSTATUS_color = "green" if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifOperStatus'} == 1);
$OSTATUS_color = "red" if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifOperStatus'} == 2);
}
my $ifInOctets;
my $ifInOctetsString;
{ # in octets, eingehende Datenmenge
$ifInOctets = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifInOctets'};
if ($ifInOctets >= 1000000000) {
$ifInOctets /= 1000000000;
$ifInOctetsString = sprintf "%.2f GBytes", $ifInOctets;
}
elsif ($ifInOctets >= 1000000) {
$ifInOctets /= 1000000;
$ifInOctetsString = sprintf "%.2f MBytes", $ifInOctets;
}
elsif ($ifInOctets >= 1000) {
$ifInOctets /= 1000;
$ifInOctetsString = sprintf "%.2f KBytes", $ifInOctets;
}
else {
$ifInOctetsString = sprintf "%.2f Bytes", $ifInOctets;
}
}
my $ifEtherMode;
my $ifEtherModeString;
{ # Ethermode, konfigurierte Porteinstellung
$ifEtherMode = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifEtherMode'};
given($ifEtherMode) {
when(1) { $ifEtherModeString = "10-HDx" }
when(2) { $ifEtherModeString = "100-HDx" }
when(3) { $ifEtherModeString = "10-FDx" }
when(4) { $ifEtherModeString = "100-FDx" }
when(5) { $ifEtherModeString = "Auto" }
when(6) { $ifEtherModeString = "1000-FDx" }
when(7) { $ifEtherModeString = "Auto-10" }
when(8) { $ifEtherModeString = "Auto-100" }
when(9) { $ifEtherModeString = "Auto-1000" }
when(10) { $ifEtherModeString = "Auto-10G" }
when(11) { $ifEtherModeString = "Auto-10-100" }
default { $ifEtherModeString = "not conf." }
}
}
my $ifAlias;
{ # Alias
$ifAlias = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifAlias'};
}
my $ifVLAN;
{ # VLAN Name
$ifVLAN = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifVLAN'};
}
my $ifInErrors;
{ # in errors, eingehende Fehler
$ifInErrors = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifInErrors'};
}
my $ifOutOctets;
my $ifOutOctetsString;
{ # out octets, ausgehende Datenmenge
$ifOutOctets = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifOutOctets'};
if ($ifOutOctets >= 1000000000) {
$ifOutOctets /= 1000000000;
$ifOutOctetsString = sprintf "%.2f GBytes", $ifOutOctets;
}
elsif ($ifOutOctets >= 1000000) {
$ifOutOctets /= 1000000;
$ifOutOctetsString = sprintf "%.2f MBytes", $ifOutOctets;
}
elsif ($ifOutOctets >= 1000) {
$ifOutOctets /= 1000;
$ifOutOctetsString = sprintf "%.2f KBytes", $ifOutOctets;
}
else {
$ifOutOctetsString = sprintf "%.2f Bytes", $ifOutOctets;
}
}
my $ifOutErrors;
{ # out errors, ausgehende Fehler
$ifOutErrors = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifOutErrors'};
}
my $ifSpeed;
my $ifSpeedString;
my $ifDuplexMode;
{ # speed/duplex, aktuelle Portgeschwindigkeit
# Speed
$ifSpeed = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifSpeed'};
$ifSpeedString = $ifSpeed;
$ifSpeedString = "10 Gbit/s" if ($ifSpeed == 10000000000);
$ifSpeedString = "1 Gbit/s" if ($ifSpeed == 1000000000);
$ifSpeedString = "100 Mbit/s" if ($ifSpeed == 100000000);
$ifSpeedString = "10 Mbit/s" if ($ifSpeed == 10000000);
# Duplexmodus
$ifDuplexMode = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifDuplexMode'};
given ($ifDuplexMode) {
when (1) { $ifDuplexMode="HDx" }
when (2) { $ifDuplexMode="FDx" }
default { $ifDuplexMode="??"}
}
}
my $ifPortType;
{ # Interface, Typ des physikalischen Ports
if ($SwitchType =~ /hp/i) {
$ifPortType = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifPortTypehp'}
}
else {
$ifPortType = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifPortType'};
}
given ($ifPortType) {
when (2) { $ifPortType="none" }
when (3) { $ifPortType="unknown (3)" }
when (6) { $ifPortType="ethernetCsmacd" }
when (7) { $ifPortType="iso88023Csmacd" }
when (15) { $ifPortType="fddi" }
when (37) { $ifPortType="atm" }
when (53) { $ifPortType="Port-Channel/Stack" }
when (54) { $ifPortType="propMultiplexor" }
when (55) { $ifPortType="ieee80212" }
when (56) { $ifPortType="(virtual) FibreChannel" }
when (62) { $ifPortType="fastEther" }
when (69) { $ifPortType="fastEtherFX" }
when (117) { $ifPortType="gigabitEthernetSX" }
when (118) { $ifPortType="gigabitEthernetLX" }
when (119) { $ifPortType="gigabitEthernetT" }
when (120) { $ifPortType="gigabitEthernetStk"}
when (121) { $ifPortType="gigabitEthernetLH" }
when (122) { $ifPortType="tenGbE-CX4" }
when (123) { $ifPortType="gigabitEthernetESP" }
when (124) { $ifPortType="tenGbE-SR" }
when (125) { $ifPortType="tenGbE-ER" }
when (126) { $ifPortType="tenGbE-LR" }
when (127) { $ifPortType="gigabitEthernetT-sfp" }
default { $ifPortType="unknown value ($ifPortType)" }
}
}
my $TimeDiff;
{ # Last change, letzte Statusänderung des Ports
$TimeDiff = $RSwitch{$SwitchIP}->{'Switch'}->{'Uptime'} - $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifLastChange'};
# wenn zeitspanne zw. reboot und portstatuswechsel < 3 sek oder der letzte wechsel vor reboot war
# dann wird behauptet das es nie einen statuswechsel gab
if ($RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifLastChange'} < 300 or $TimeDiff < 0) {
$TimeDiff = "Never";
}
else {
$TimeDiff = ticks_to_time ($TimeDiff);
}
}
my $ifType;
{ # Port ein Trunk?
$ifType=$RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifTrkName'};
$ifType=sprintf("%d",$ifType);
given ($ifType) {
when (0) {$ifType="Access"}
when (1) {$ifType="Mesh"}
default {$ifType--;$ifType="Trunk ($ifType)"}
}
given ($PortName) {
when (/Mesh/) {$ifType="Mesh"}
when (/Trk/) {$ifType="Trunk"}
}
}
my $ifVLANs;
{ # VLANs
$ifVLANs = $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifVLANs'};
}
my $display;
{ ### Should this port be displayed?
$display=1;
$display=0 if ($OnlyErrors and $ifOutErrors==0 and $ifInErrors==0);
$display=0 if ($SpecialPorts==0 and $PortName=~/VLAN|loopback|Null|Stack/i);
}
if ($display) { # Port in Tabelle anzeigen
push @ta, "<tr>\n";
my @MAC = split / /,$RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'onifMAC'};
my $str;
foreach (@MAC) {
$str .= "$_<br>";
}
push @ta, "<td>\n";
push @ta, "<b>$PortName</b> <font size=-1>($PortIndex)</font><br><font size=-1>$ifPortType</font>";
my $para="<b>Switch:</b> $SwitchIP<br><br><b>Port:</b> $PortName<br><br><b>Port-MAC:</b> $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifMAC'}<br><br><b>Angeschlossene MACs:</b><br>$str";
push @ta, "<a href=\"javascript:popup('popup.html?text=$para')\"><img src=info.ico>";
push @ta, "<span class='druber'>";
push @ta, "MAC Addresse des Interface: $RSwitch{$SwitchIP}->{'Ports'}->{$_}->{'ifMAC'}<br>Angeschlossene MAC Adressen:<br>";
push @ta, "$str";
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, "<td width=100><a href='javascript:void()'>";
push @ta, "<table width=100 border=0>\n<tr>\n<td bgcolor=$ASTATUS_color>";
push @ta, "$ASTATUS_text";
push @ta, "</td>\n</tr>\n";
push @ta, "<tr>\n<td bgcolor=$OSTATUS_color>";
push @ta, "$OSTATUS_text";
push @ta, "</td>\n</tr>\n</table>";
push @ta, "<span class='druber'>";
push @ta, "Interface ist abgeschaltet." if ($ASTATUS_text eq 'down');
push @ta, "Interface ist eingeschaltet aber ohne Link." if ($ASTATUS_text eq 'up' and $OSTATUS_text eq 'down');
push @ta, "Interface hat einen Link." if ($ASTATUS_text eq 'up' and $OSTATUS_text eq 'up');
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, "<td>";
push @ta, "$ifEtherModeString<br>$ifSpeedString $ifDuplexMode";
push @ta, "<a href='javascript:void()'><img src=info.ico>";
push @ta, "<span class='druber'>";
push @ta, "Interface auf $ifEtherModeString konfiguriert und läuft mit $ifSpeedString $ifDuplexMode";
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, "<td>";
push @ta, "$ifAlias";
push @ta, "<a href='javascript:void()'><img src=info.ico>";
push @ta, "<span class='druber'>";
push @ta, "Konfigurierte Beschreibung";
push @ta, "</span></a>";
push @ta, "</td>\n";
$para = $ifVLANs;
$para =~ s/ /<br>/g;
$para = "<b>Switch:</b> $SwitchIP<br><br><b>Port:</b> $PortName<br><br><b>VLANs:</b><br>" . $para;
push @ta, "<td>";
push @ta, "<b>$ifType</b><br>\n$ifVLAN<a href=\"javascript:popup('popup.html?text=$para')\"><img src=info.ico><span class='druber'>\n";
my @VL;
@VL = split/ /,$ifVLANs;
# my $str;
$str='<table border=0>';
$j=0;
foreach (@VL) {
$_ =~ s/\(/ \(/;
if ($j%2==0) {
$str .= "<tr>\n<td><font size=-1>&bull; $_</font></td>\n";
}
else {
$str .= "<td><font size=-1>&bull; $_</font></td>\n</tr>\n";
}
$j++;
}
$str .= "</table>\n";
push @ta, $str;
push @ta, "</span></a></td>\n";
push @ta, "<td>";
push @ta, "$TimeDiff";
push @ta, "<a href='javascript:void()'><img src=info.ico>";
push @ta, "<span class='druber'>";
if ($TimeDiff eq "Never") {
push @ta, "Interface seit dem letzten Switchstart ohne Link";
}
else {
push @ta, "Interface seit $TimeDiff ohne Link" if ($OSTATUS_text eq "down");
push @ta, "Interface hat seit $TimeDiff einen Link" if ($OSTATUS_text eq "up");
}
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, "<td>";
push @ta, "$ifInOctetsString<br>$ifOutOctetsString";
push @ta, "<a href='javascript:void()'><img src=info.ico>";
push @ta, "<span class='druber'>";
push @ta, "Empfangene Datenmenge: $ifInOctetsString<br>Gesendete Datenmenge: $ifOutOctetsString";
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, "<td>";
push @ta, "$ifInErrors<br>$ifOutErrors";
push @ta, "<a href='javascript:void()'><img src=info.ico>";
push @ta, "<span class='druber'>";
push @ta, "Anzahl fehlerhafter Pakete die<br>empfangen wurden: $ifInErrors<br>nicht gesendet werden konnten: $ifOutErrors";
push @ta, "</span></a>";
push @ta, "</td>\n";
push @ta, "<td bgcolor=$STP_color>";
push @ta, "$STP_text";
push @ta, "</td>\n";
push @ta, "</tr>\n";
}
}
push @ta, "</table></body></html>\n";
return @ta;
}
sub snmp_get {
my ($OID, $IP, $COMMUNITY) = @_;
my ($session, $error) = Net::SNMP->session(
-hostname => $IP,
-community => $COMMUNITY,
-translate => [
-all => 0,
-octetstring => 0,
-null => 0,
-timeticks => 0,
-opaque => 0,
-nosuchobject => 0,
-nosuchinstance => 0,
-endofmibview => 0,
-unsigned => 0,
]
);
my $result = $session->get_request(-varbindlist => [ $OID ],);
$session->close();
return $result->{$OID};
}
sub snmp_walk2 {
my ($OID, $IP, $COMMUNITY) = @_;
my ($session, $error) = Net::SNMP->session(
-hostname => $IP,
-community => $COMMUNITY,
-version => "snmpv2c",
-translate => [
-all => 0,
-octetstring => 0,
-null => 0,
-timeticks => 0,
-opaque => 0,
-nosuchobject => 0,
-nosuchinstance => 0,
-endofmibview => 0,
-unsigned => 0,
]
);
my $result = $session->get_table(
-baseoid => $OID,
);
if (!defined $result) {
$session->close();
return 1;
}
$session->close();
return %{$result};
}
sub read_switch {
my $SwitchIP = $_[0];
my $SwitchSNMPcommunity = $_[1];
printf "\n\nPruefe Switch %s mit community %s\n", $SwitchIP, $SwitchSNMPcommunity;
print " SwitchType\n";
my $SwitchType = snmp_get("1.3.6.1.2.1.1.1.0", $SwitchIP, $SwitchSNMPcommunity);
my $SwType;
$SwType = "cisco" if $SwitchType =~ /Cisco/;
$SwType = "hp" if $SwitchType =~ /HP/i;
print " SwitchHostname\n";
my $SwitchHostname = snmp_get("1.3.6.1.2.1.1.5.0", $SwitchIP, $SwitchSNMPcommunity);
print " SwitchUptime\n";
my $SwitchUptime = snmp_get("1.3.6.1.2.1.1.3.0", $SwitchIP, $SwitchSNMPcommunity);
print " SwitchIPs\n";
my %SwitchIPs = snmp_walk2("1.3.6.1.2.1.4.20.1.3", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifIndex\n";
my %RifIndex = snmp_walk2("1.3.6.1.2.1.2.2.1.1", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifName\n";
my %RifName = snmp_walk2("1.3.6.1.2.1.2.2.1.2", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifAdminStatus\n";
my %RifAdminStatus = snmp_walk2("1.3.6.1.2.1.2.2.1.7", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifOperStatus\n";
my %RifOperStatus = snmp_walk2("1.3.6.1.2.1.2.2.1.8", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifSpeed\n";
my %RifSpeed = snmp_walk2("1.3.6.1.2.1.2.2.1.5", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifLastChange\n";
my %RifLastChange = snmp_walk2("1.3.6.1.2.1.2.2.1.9", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifInOctets\n";
my %RifInOctets = snmp_walk2("1.3.6.1.2.1.2.2.1.10", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifOutOctets\n";
my %RifOutOctets = snmp_walk2("1.3.6.1.2.1.2.2.1.16", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifSTPStatus\n";
my %RifSTPStatus = snmp_walk2("1.3.6.1.2.1.17.2.15.1.3", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifInErrors\n";
my %RifInErrors = snmp_walk2("1.3.6.1.2.1.2.2.1.14", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifOutErrors\n";
my %RifOutErrors = snmp_walk2("1.3.6.1.2.1.2.2.1.20", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifHighSpeed\n";
my %RifHighSpeed = snmp_walk2("1.3.6.1.2.1.31.1.1.1.15", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifEtherMode\n";
my %RifEtherMode = snmp_walk2("1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.10", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifVLAN\n";
my %RifVLAN;
if ($SwType eq "hp") {
%RifVLAN = snmp_walk2("1.3.6.1.2.1.17.7.1.4.5.1.1", "$SwitchIP", "$SwitchSNMPcommunity");
}
if ($SwType eq "cisco") {
%RifVLAN = snmp_walk2("1.3.6.1.4.1.9.9.68.1.2.2.1.2", "$SwitchIP", "$SwitchSNMPcommunity");
foreach (keys %RifVLAN) {
my $VL = sprintf ("%s", $RifVLAN{$_});
$RifVLAN{$_} = $VL;
#print "$_ $VL\n";
}
}
print " RifTrunk\n";
my %RifTrunk = snmp_walk2("1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.8", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifPortType\n";
my %RifPortTypehp = snmp_walk2("1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.2", "$SwitchIP", "$SwitchSNMPcommunity");
my %RifPortType = snmp_walk2("1.3.6.1.2.1.2.2.1.3", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifDuplexMode\n";
my %RifDuplexMode = snmp_walk2("1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.5", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifAlias\n";
my %RifAlias = snmp_walk2("1.3.6.1.2.1.31.1.1.1.18", "$SwitchIP", "$SwitchSNMPcommunity");
print " RifMAC\n";
my %RifMAC = snmp_walk2("1.3.6.1.2.1.2.2.1.6", "$SwitchIP", "$SwitchSNMPcommunity");
print " RTagging\n";
my %RTagging;
if ($SwType eq "hp") {
%RTagging = snmp_walk2("1.3.6.1.2.1.17.7.1.4.3.1.2", "$SwitchIP", "$SwitchSNMPcommunity");
}
elsif ($SwType eq "cisco") {
%RTagging = snmp_walk2("1.3.6.1.4.1.9.9.68.1.2.2.1.2", "$SwitchIP", "$SwitchSNMPcommunity");
foreach (keys %RTagging) {
my $VL = sprintf ("%s", $RTagging{$_});
$RTagging{$_} = $VL;
}
}
print " RonifMAC\n";
my %RonifMAC = snmp_walk2("1.3.6.1.2.1.17.4.3.1.2", "$SwitchIP", "$SwitchSNMPcommunity");
my @ALLVLAN;
if ($SwType eq "hp") {
print " VLANs\n";
foreach my $VLANID (sort keys %RTagging) {
$VLANID =~ s/1.3.6.1.2.1.17.7.1.4.3.1.2.//;
$ALLVLAN[$VLANID] = snmp_get("1.3.6.1.2.1.17.7.1.4.3.1.1.$VLANID", $SwitchIP, $SwitchSNMPcommunity);
}
}
elsif ($SwType eq "cisco") {
print " VLANs\n";
}
else { print " VLANs skipped\n"; }
my %RSwitch;
my %RTagg;
$RSwitch{$SwitchIP}->{'Switch'}->{'Type'} = $SwitchType;
$RSwitch{$SwitchIP}->{'Switch'}->{'Hostname'} = $SwitchHostname;
$RSwitch{$SwitchIP}->{'Switch'}->{'Uptime'} = $SwitchUptime;
my $IPs;
foreach (keys %SwitchIPs) {
my $IP = $_;
$IP =~ s/1.3.6.1.2.1.4.20.1.3.//;
$IPs .= $IP . " $SwitchIPs{$_},"
}
$RSwitch{$SwitchIP}->{'Switch'}->{'IPs'} = $IPs;
print " VLANs zu Ports\n";
foreach my $ifindexoid (keys %RifIndex) {
my ($IntIdx)=$ifindexoid=~/\.([0-9]*)$/;
my $myifindex = sprintf "%05s", $RifIndex{$ifindexoid};
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifIndex'} = $RifIndex{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.1/1.3.6.1.2.1.2.2.1.2/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifName'} = $RifName{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.2/1.3.6.1.2.1.2.2.1.7/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifAdminStatus'} = $RifAdminStatus{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.7/1.3.6.1.2.1.2.2.1.8/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifOperStatus'} = $RifOperStatus{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.8/1.3.6.1.2.1.2.2.1.5/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifSpeed'} = $RifSpeed{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.5/1.3.6.1.2.1.2.2.1.9/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifLastChange'} = $RifLastChange{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.9/1.3.6.1.2.1.2.2.1.10/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifInOctets'} = $RifInOctets{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.10/1.3.6.1.2.1.2.2.1.16/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifOutOctets'} = $RifOutOctets{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.16/1.3.6.1.2.1.17.2.15.1.3/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifSTPStatus'} = $RifSTPStatus{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.17.2.15.1.3/1.3.6.1.2.1.2.2.1.14/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifInErrors'} = $RifInErrors{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.14/1.3.6.1.2.1.2.2.1.20/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifOutErrors'} = $RifOutErrors{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.20/1.3.6.1.2.1.31.1.1.1.15/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifHighSpeed'} = $RifHighSpeed{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.31.1.1.1.15/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.10/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifEtherMode'} = $RifEtherMode{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.10/1.3.6.1.2.1.31.1.1.1.18/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifAlias'} = $RifAlias{$ifindexoid};
if ($SwType eq "hp") {
$ifindexoid =~ s/1.3.6.1.2.1.31.1.1.1.18/1.3.6.1.2.1.17.7.1.4.5.1.1/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifVLAN'} = snmp_get("1.3.6.1.2.1.17.7.1.4.3.1.1.$RifVLAN{$ifindexoid}", $SwitchIP, $SwitchSNMPcommunity);
$ifindexoid =~ s/1.3.6.1.2.1.17.7.1.4.5.1.1/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.8/;
}
elsif ($SwType eq "cisco") {
$ifindexoid =~ s/1.3.6.1.2.1.31.1.1.1.18/1.3.6.1.4.1.9.9.68.1.2.2.1.2/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifVLAN'} = $RifVLAN{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.4.1.9.9.68.1.2.2.1.2/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.8/;
}
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifTrkName'} = $RifTrunk{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.8/1.3.6.1.2.1.2.2.1.3/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifPortType'} = $RifPortType{$ifindexoid};
my $ifPortType = $RifPortType{$ifindexoid};
$ifindexoid =~ s/1.3.6.1.2.1.2.2.1.3/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.2/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifPortTypehp'} = $RifPortTypehp{$ifindexoid} if ($SwitchType =~ /hp/i);
$ifindexoid =~ s/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.2/1.3.6.1.4.1.11.2.14.11.5.1.7.1.3.1.1.5/;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifDuplexMode'} = $RifDuplexMode{$ifindexoid};
my $str=$RifMAC{"1.3.6.1.2.1.2.2.1.6.$IntIdx"};
my @MAC=split//,$str;
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifMAC'} = sprintf ("%02X:%02X:%02X:%02X:%02X:%02X", ord($MAC[0]),ord($MAC[1]),ord($MAC[2]),ord($MAC[3]),ord($MAC[4]),ord($MAC[5]) );
my $str1;
my @MAC1;
################
print "\n Suche MAC zu Interface $RSwitch{$SwitchIP}->{'Ports'}->{$myifindex}->{'ifName'}: ";
foreach (keys %RonifMAC) {
last if ($ifPortType != 6 ); # not ethernetCsmacd
my $BID=$RonifMAC{$_};
my $Int = snmp_get("1.3.6.1.2.1.17.1.4.1.2.$BID", "$SwitchIP", "$SwitchSNMPcommunity");
($MAC1[0], $MAC1[1], $MAC1[2], $MAC1[3], $MAC1[4], $MAC1[5]) = $_ =~ /\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/;
if ($Int == $IntIdx) {
my $s = sprintf ("%02X:%02X:%02X:%02X:%02X:%02X ", $MAC1[0],$MAC1[1],$MAC1[2],$MAC1[3],$MAC1[4],$MAC1[5]);
$str1 .= $s;
print "!";
}
else {
print ".";
}
}
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'onifMAC'} = $str1;
# ifIndex
my $VLANS;
if($SwType eq "hp") {
foreach my $VLANID (sort keys %RTagging) {
my $HEXSTR = $RTagging{$VLANID};
$VLANID =~ s/1.3.6.1.2.1.17.7.1.4.3.1.2.//;
my $i=0;
my $II = $IntIdx;
while ($II>8) {
$i++;
$II-=8;
}
my $MASK=128>>($II -1);
my $IN=$MASK & ord(substr($HEXSTR, $i, 1));
if ($IN != 0) {
$VLANS = $VLANS . $ALLVLAN[$VLANID] . "($VLANID)" . ' ';
}
}
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifVLANs'} = $VLANS;
}
if($SwType eq "cisco") {
$RSwitch{$SwitchIP}->{'Ports'}->{"$myifindex"}->{'ifVLANs'} = $RTagging{"1.3.6.1.4.1.9.9.68.1.2.2.1.2.$myifindex"};
#"aaa";
}
}
my @switchtable = create_switch_table("$_", \%RSwitch);
my @HTML;
@HTML = (@HTML, @switchtable);
open HTMLFILE, ">$wwwPath/$SwitchIP.html";
printf HTMLFILE "@HTML";
close HTMLFILE;
}