jablonka.czprosek.czf

switches

Subversion Repositories:
[/] [switchesol.cgi] - Rev 8 Go to most recent revision

Compare with Previous - Blame - Download


#!/usr/bin/perl -w
use strict;
#
# Petr Simandl, www.simandl.cz

my $snmpwalk = "/usr/bin/snmpwalk";

my %map = (
 "edimax_es-3116p" => [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
 ],
 "repotec_g3224x" => [[1,2,3,4,5,6,7,8,9,10,11,12,0,0],
 [13,14,15,16,17,18,19,20,21,22,23,24,25,26],
 ],
 "repotec_RP-6326I" => [[1,2,3,4,5,6,7,8,9,10,11,12,0,0],
 [13,14,15,16,17,18,19,20,21,22,23,24,25,26],
 ],
 "repotec_RP-G2402I"=> [[1,2,3,4,5,6,7,8,9,10,11,12],
 [13,14,15,16,17,18,19,20,21,22,23,24],
 ],
 "repotec_GEL2-SW8" => [[1,2,3,4,5,6,7,8],
 ],
 "repotec_10G" => [[1,2,3,4,5,6,7,8,9,10],
 ],
 "linksys_SRW224G4" => [[1,2,3,4,5,6,7,8,9,10,11,12,25,27],
 [13,14,15,16,17,18,19,20,21,22,23,24,26,28],
 ],
 "SMCGS24C" => [[1,2,3,4,5,6,7,8,9,10,11,12],

 [13,14,15,16,17,18,19,20,21,22,23,24,21,22,23,24],

 ],
 "tplink_SG5426" => [[1,3,5,7,9,11,13,15,17,19,21,23,25,26,0,0],
 [2,4,6,8,10,12,14,16,18,20,22,24,21,22,23,24],
 ],
 "TL-SL3452" => [[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,0,0],
 [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,51,52],
 ],
 "TL-SL3428" => [[1,3,5,7,9,11,13,15,17,19,21,23,25,0,0],
 [2,4,6,8,10,12,14,16,18,20,22,24,26,27,28],
 ],
 "TL-SG3424" => [[2,4,6,8,10,12,14,16,18,20,22,24], 
 [1,3,5,7,9,11,13,15,17,19,21,23], 
 ],
 "NetGear" => [[1,2,3,4,5,6,7,8,9,10,11,12],
 ],
  "HuaS5624P"        => [[4227625,4227641,4227657,4227673,4227689,4227705,4227721,4227737,4227753,4227769,4227785,4227801,0,0,0,0],
                         [4227633,4227649,4227665,4227681,4227697,4227713,4227729,4227745,4227761,4227777,4227793,4227809,4227817,4227825,4227833,4227841],
                        ],
  "C4900MNFX1"       => [[63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,54,90,121,0],
                         [132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,55,56,57,58,59,60,61,62],
                        ],
  "C4900MNFX2"       => [[60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,51,87,123,0],
                         [133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,52,53,54,55,56,57,58,59],
                        ],
 );

my @ports = ();
my @sw = ();
my %sw_d = ();

my $portlist = "";
my $p = 0;
my $d = "";
my ($fnc,$mac,$if,$bin,$geom,$desc);

print "Content-type: text/html\n\n";

print "<HTML><HEAD>\n<META HTTP-EQUIV=Pragma CONTENT=no-cache>\n<style type=\"text/css\" media=\"screen\"> \@import \"../style.css\" ;</style>\n";
print "<META http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-2\">\n";
print "<script type=\"text/javascript\" src=\"overlib.js\"><!-- overLIB (c) Erik Bosrup --></script>\n";
print "</HEAD><BODY>\n";
#nocache!

#print "<a href=\"javascript:void(0);\" onmouseover=\"return overlib('This is an ordinary popup.');\" onmouseout=\"return nd();\">here</a>";

open(CONF,"< /etc/switches.conf") || die("can't open datafile: $!");
while (<CONF>) {
 chomp;
 next if (/^#/);
 next if (/^$/);
 if (/^draw_/) { push @sw, $_; next; }
 if (/^([0-9a-fA-F]{2}:[0-9a-fA-F]{2}:)|(([0-9]{1,3}\.){3}[0-9]{1,3})/) {
 ($mac, $portlist) = split(/\|/, $_, 2);
 @ports = ();
 foreach ( split(/\|/, $portlist) ) {
 ($p, $d) = split /;/;
 $ports[$p] = $d;
 }
 $sw_d{$mac} = [ @ports ];
 }
}
close CONF;

foreach (@sw) {
 ($fnc, $mac, $if, $bin, $geom, $desc) = split /\|/;
 SWITCH: {
 if ($fnc eq "draw_rrcp_switch") {
 draw_rrcp_switch($mac,$if,$bin,$desc,$map{$geom},\%sw_d);
 last SWITCH;
 }
 if ($fnc eq "draw_snmp_switch") {
 draw_snmp_switch($mac,$if,$bin,$desc,$map{$geom},\%sw_d);
 last SWITCH;
 }
 }
 print "<br>\n";
}

my $date = `date`;
print "<BR>$date\n<BR>";

print "</center></BODY></HTML>";

################################################################################

sub print_port_graph {
 my $ip = shift(@_);
 my $page = shift(@_);
 my $graph = shift(@_);
 my $caption = shift(@_);
 my $text = shift(@_);

print "<a href=\"http://${ip}/hotsanic/traffic/${page}\" onmouseover=\"return overlib('&lt;img src=http://${ip}/hotsanic/traffic/${graph}&gt;',DELAY,250,HAUTO,VAUTO,CAPTION,'${caption}');\" onmouseout=\"return nd();\" shape=\"rect\">$text</a>";

} # end print_port_graph

sub draw_rrcp_switch {
 my $mac = shift(@_);
 my $if = shift(@_);
 my $bin = shift(@_);
 my $desc = shift(@_);
 my @map = @{shift(@_)};
 my $sw_d = shift(@_);

 my $w = scalar( @{$map[0]} );
 my $h = scalar @map;
 my $hmac = "";
 my $x = 0;
 my $y = 0;
 my $e = "";
 my $f = "";
 my $sirka = 70;
 my $label = "";
 my $bg = "";
 my $enab = "";
 my $mod = "";
 my $stat = "";
 my $link = "";
 my $flow = "";
 my $port = 0;
 my @row = ();
 my @pole = ();

 my $porty=`sudo $bin $mac\@$if show int | grep ^$if | sed 's/://g' | sed 's/$if\\///g'`;
 print "<b>$desc</b> $mac";
# print "<pre>$porty\n</pre>";
 foreach (split("\n",$porty)) {
 s/auto1000M-/auto 1000M-/;
 @row = split ;
 @pole[$row[0]] = [ @row[1..5] ];
 }

 print "<table border=1>\n";
 for ($y = 0; $y < $h; $y++) {
 print "<tr>\n";
 for ($x = 0; $x < $w; $x++) {
 $bg = "\"#FFFFFF\"";
 print " <td valign=top width=\"$sirka\" ";
 if ($port = $map[$y][$x]) {
 ($enab,$mod,$stat,$link,$flow) = @{$pole[$port]}[0..4];
 if ($link eq "LINK") { $bg = "\"#00FF00\""; }
 }
 print "bgcolor=$bg>";
 if ($port) {
 $e = substr($enab,0,1);
 if (defined($sw_d->{$mac}[$port])) {$label = $sw_d->{$mac}[$port];}
 else { $label = ""; }
 if ($flow eq "flowctl") { $f = "Y"; } else { $f = "N"; }
        $hmac = $mac;
        $hmac =~ s/:/_/g;
 print_port_graph("10.33.0.23","${hmac}_${port}.html","${hmac}_${port}-day.png","$mac port $port $label","$port $label");
 print "<br />$e $mod $stat $f</td>\n";
 } else {
 print "&nbsp;</td>\n";
 }
 }
 print "</tr>\n";
 }
 print "</table>";
}

sub draw_snmp_switch {
 my $myip = shift(@_);
 my $com = shift(@_);
 my $dhcp = shift(@_);
 my $desc = shift(@_);
 my @map = @{shift(@_)};
 my $sw_d = shift(@_);

 my @pole_operstatus = ();
 my @pole_adminstatus = ();
 my @pole_speed = ();
 my %pole_dhcpdhosts = ();
 my %hash_mac = ();
 my %mactab = ();
 my $macind = 0;
 my $port = 0;

 my $w = scalar( @{$map[0]} );
 my $h = scalar @map;
 my $x = 0;
 my $y = 0;
 my $sirka = 70;
 my $label = "";
 my $bg = "";
 my $enab = "";
 my $e = "";
 my $mod = "";
 my $stat = "";
 my $link = "";
 my $flow = "";
 my $barva = "";
 my $title = "";
 my $popis = "";
 my $macinfo = "";
 my $macip = "";
 my $hip = "";
 my @row = ();

 print "<b>$desc</b> $myip";

 my $names = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.2 | sed 's/gabitEthernet//g'  | sed 's/nGi//g'`;
 my $speedall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.5`;
 my $adminstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.7`;
 my $operstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.8`;
 my $mactable = `$snmpwalk -Os -c $com -v 2c $myip atPhysAddress2`;
 my $mactableport = `$snmpwalk -Os -c $com -v 2c $myip ipNetToMediaIfIndex2`;
 my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp/cgi-bin/dhcp.cgi -q -O -`;
# $arpn         = `wget -t 1 -T 1 http://$dhcp/cgi-bin/arpn.cgi -q -O -`;

 foreach (split("\n",$operstatusall)) {
 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
 @row = split ;
 $pole_operstatus[$row[0]] = $row[1];
 }
 foreach (split("\n",$adminstatusall)) {
 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
 @row = split ;
 $pole_adminstatus[$row[0]] = $row[1];
 }
 foreach (split("\n",$speedall)) {
 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
 @row = split ;
 $row[1] =~ s/000000$/M/;
 $pole_speed[$row[0]] = $row[1];
 }
 foreach (split("\n",$mactable)) {
 s/ = Hex-STRING://;
 ($macind, my $tmpmac) = split(" ", $_, 2);
 $tmpmac =~ s/ $//g;
 $tmpmac =~ s/ /:/g;
 $macind =~ s/\.1\./\./g;
 $hash_mac{$macind} = $tmpmac;
# print "$tmpmac $macind\n";
 }
 foreach (split("\n",$mactableport)) {
 s/ = INTEGER://;
 ($macind, $port) = split ;
 $macind =~ s/^ipNetToMediaIfIndex/atPhysAddress/;
# print "$port $macind\n";
 if (exists $mactab{$port} ) {
 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
 } else {
 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
 }
 }

#@pole_arpn = split("\n",$arpn);

 $dhcpdconf =~ s/\n/ /g;
 $dhcpdconf =~ s/\s+/ /g;
 $dhcpdconf =~ s/host /\nhost /g;
 $dhcpdconf =~ s/}/}\n/g;
 $dhcpdconf =~ s/\^host //g;

 my @pole_dhcpdconf = split("\n",$dhcpdconf);

 for (@pole_dhcpdconf) {
 if (/^host/) {
 s/^host ([^\s]+) {\s*hardware\s+ethernet\s+(([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})\s*;\s*fixed-address\s+(([0-9]{1,3}\.){3}[0-9]{1,3})\s*;\s*}/$2 $1 $4/;
 (my $mac, my $host, my $ip) = split;
 $mac =~ tr/a-f/A-F/;
 $pole_dhcpdhosts{$mac} = "$host $ip";
 }
 }

 print "<table border=1>\n";
 for ($y = 0; $y < $h; $y++) {
 print "<tr>\n";
 for ($x = 0; $x < $w; $x++) {
 $bg = "\"#FFFFFF\"";
 print " <td valign=top width=\"$sirka\" ";
 if ($port = $map[$y][$x]) {
 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
 $mod = "";
 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
 $flow = "";
 if ($link eq "up(1)") { $bg = "\"#00FF00\""; }
 }
 print "bgcolor=$bg>";
 if ($port) {
 if ($enab =~ /^up/) {$e = "E";} else {$e = "D";}
 if (defined($sw_d->{$myip}[$port])) {$label = $sw_d->{$myip}[$port];}
 else { $label = ""; }
        $hip = $myip;
        $hip =~ s/\./_/g;
 print_port_graph("10.33.0.23","${hip}_${port}.html","${hip}_${port}-day.png","$myip port $port $label","$port $label");
 print "<br />$e $stat<br />\n";
 print "<pre>";
 foreach $mac (@{ $mactab{$port} }) {
 if (exists $pole_dhcpdhosts{$mac}) {
 $barva = "black";
 $title = "$mac " . $pole_dhcpdhosts{$mac};
 $popis = $pole_dhcpdhosts{$mac};
 } else {
 $barva = "red";
 $popis = $mac;
 $macinfo = `wget -t 1 -T 1 http://$dhcp/cgi-bin/macinfo.cgi?$mac -q -O -`;
 if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
 }
 print "<font color=$barva title=\"$title\">$popis</font><br>"
 };
 print "</pre></td>";
 } else {
 print "&nbsp;</td>\n";
 }
 }
 print "</tr>\n";
 }
 print "</table>";
};

Powered by WebSVN 2.2.1