jablonka.czprosek.czf

switches

Subversion Repositories:
[/] [switches.cgi] - Blame information for rev 29

 

Line No. Rev Author Line
11simandl#!/usr/bin/perl -w
2use strict;
3#
4# Petr Simandl, www.simandl.cz
5 
69simandl#require "/home/www/cgi-bi"/home/www/cgi-bin/switches_definitions.pl"n/switches_definitions.pl";
71simandl 
89simandlmy %map = do("/etc/switches_definitions.conf");
91simandl 
109simandlmy $snmpwalk = "/usr/bin/snmpwalk";
11 
121simandlmy @ports = ();
13my @sw = ();
14my %sw_d = ();
15 
16my $portlist = "";
17my $p = 0;
18my $d = "";
1917simandlmy ($fnc,$mac,$if,$ver,$bin,$geom,$desc);
201simandl 
21print "Content-type: text/html\n\n";
22 
23print "<HTML><HEAD>\n<META HTTP-EQUIV=Pragma CONTENT=no-cache>\n<style type=\"text/css\" media=\"screen\"> \@import \"../style.css\" ;</style>\n";
249simandlprint "<META http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-2\">\n";
2510simandlprint "<script type=\"text/javascript\" src=\"../overlib.js\"><!-- overLIB (c) Erik Bosrup --></script>\n";
261simandlprint "</HEAD><BODY>\n";
27#nocache!
28 
29print "<div id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></div>";
30 
31#print "<a href=\"javascript:void(0);\" onmouseover=\"return overlib('This is an ordinary popup.');\" onmouseout=\"return nd();\">here</a>";
32 
33open(CONF,"< /etc/switches.conf") || die("can't open datafile: $!");
34while (<CONF>) {
35 chomp;
36 next if (/^#/);
37 next if (/^$/);
38 if (/^draw_/) { push @sw, $_; next; }
39 if (/^([0-9a-fA-F]{2}:[0-9a-fA-F]{2}:)|(([0-9]{1,3}\.){3}[0-9]{1,3})/) {
40 ($mac, $portlist) = split(/\|/, $_, 2);
41 @ports = ();
42 foreach ( split(/\|/, $portlist) ) {
43 ($p, $d) = split /;/;
44 $ports[$p] = $d;
45 }
46 $sw_d{$mac} = [ @ports ];
47 }
48}
49close CONF;
50 
51foreach (@sw) {
5217simandl ($fnc, $mac, $if, $ver, $bin, $geom, $desc) = split /\|/;
531simandl SWITCH: {
54 if ($fnc eq "draw_rrcp_switch") {
55 draw_rrcp_switch($mac,$if,$bin,$desc,$map{$geom},\%sw_d);
56 last SWITCH;
57 }
58 if ($fnc eq "draw_snmp_switch") {
5917simandl draw_snmp_switch($mac,$if,$ver,$bin,$desc,$map{$geom},\%sw_d);
601simandl last SWITCH;
61 }
62 }
63 print "<br>\n";
64}
65 
66my $date = `date`;
67print "<BR>$date\n<BR>";
68 
69print "</center></BODY></HTML>";
70 
71################################################################################
72 
7310simandlsub print_port_graph {
74 my $ip = shift(@_);
75 my $page = shift(@_);
76 my $graph = shift(@_);
77 my $caption = shift(@_);
78 my $text = shift(@_);
79 
80print "<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>";
81 
82} # end print_port_graph
83 
841simandlsub draw_rrcp_switch {
85 my $mac = shift(@_);
86 my $if = shift(@_);
87 my $bin = shift(@_);
88 my $desc = shift(@_);
89 my @map = @{shift(@_)};
90 my $sw_d = shift(@_);
91 
92 my $w = scalar( @{$map[0]} );
93 my $h = scalar @map;
9410simandl my $hmac = "";
951simandl my $x = 0;
96 my $y = 0;
97 my $e = "";
98 my $f = "";
99 my $sirka = 70;
100 my $label = "";
101 my $bg = "";
102 my $enab = "";
103 my $mod = "";
104 my $stat = "";
105 my $link = "";
106 my $flow = "";
107 my $port = 0;
108 my @row = ();
109 my @pole = ();
110 
111 my $porty=`sudo $bin $mac\@$if show int | grep ^$if | sed 's/://g' | sed 's/$if\\///g'`;
112 print "<b>$desc</b> $mac";
113# print "<pre>$porty\n</pre>";
114 foreach (split("\n",$porty)) {
115 s/auto1000M-/auto 1000M-/;
116 @row = split ;
117 @pole[$row[0]] = [ @row[1..5] ];
118 }
119 
120 print "<table border=1>\n";
121 for ($y = 0; $y < $h; $y++) {
122 print "<tr>\n";
123 for ($x = 0; $x < $w; $x++) {
124 $bg = "\"#FFFFFF\"";
125 print " <td valign=top width=\"$sirka\" ";
126 if ($port = $map[$y][$x]) {
127 ($enab,$mod,$stat,$link,$flow) = @{$pole[$port]}[0..4];
128 if ($link eq "LINK") { $bg = "\"#00FF00\""; }
12916simandl if ($mod eq "1000M") { $bg = "\"#FFFF00\""; }
1301simandl }
131 print "bgcolor=$bg>";
132 if ($port) {
133 $e = substr($enab,0,1);
134 if (defined($sw_d->{$mac}[$port])) {$label = $sw_d->{$mac}[$port];}
135 else { $label = ""; }
136 if ($flow eq "flowctl") { $f = "Y"; } else { $f = "N"; }
13710simandl $hmac = $mac;
138 $hmac =~ s/:/_/g;
13916simandl print_port_graph("10.33.0.2","${hmac}_${port}.html","${hmac}_${port}-day.png","$mac port $port $label","$port $label");
14010simandl print "<br />$e $mod $stat $f</td>\n";
1411simandl } else {
142 print "&nbsp;</td>\n";
143 }
144 }
145 print "</tr>\n";
146 }
147 print "</table>";
148}
149 
150sub draw_snmp_switch {
151 my $myip = shift(@_);
152 my $com = shift(@_);
15317simandl my $ver = shift(@_);
1541simandl my $dhcp = shift(@_);
155 my $desc = shift(@_);
156 my @map = @{shift(@_)};
157 my $sw_d = shift(@_);
158 
159 my @pole_operstatus = ();
160 my @pole_adminstatus = ();
161 my @pole_speed = ();
162 my %pole_dhcpdhosts = ();
163 my %hash_mac = ();
164 my %mactab = ();
165 my $macind = 0;
166 my $port = 0;
167 
168 my $w = scalar( @{$map[0]} );
169 my $h = scalar @map;
170 my $x = 0;
171 my $y = 0;
172 my $sirka = 70;
173 my $label = "";
174 my $bg = "";
175 my $enab = "";
176 my $e = "";
177 my $mod = "";
178 my $stat = "";
179 my $link = "";
180 my $flow = "";
181 my $barva = "";
182 my $title = "";
183 my $popis = "";
184 my $macinfo = "";
185 my $macip = "";
18610simandl my $hip = "";
1871simandl my @row = ();
18827simandl my $dhcp2 = $dhcp;
189 
19012simandl print "<b>$desc</b> <a href=http:\/\/$myip>$myip</a>";
1911simandl 
19217simandl my $speedall = `$snmpwalk -Os -c $com -v $ver $myip .1.3.6.1.2.1.2.2.1.5`;
193 my $adminstatusall = `$snmpwalk -Os -c $com -v $ver $myip .1.3.6.1.2.1.2.2.1.7`;
194 my $operstatusall = `$snmpwalk -Os -c $com -v $ver $myip .1.3.6.1.2.1.2.2.1.8`;
19527simandl# my $mactable = `$snmpwalk -Os -c $com -v $ver $myip atPhysAddress`;
196# my $mactableport = `$snmpwalk -Os -c $com -v $ver $myip ipNetToMediaIfIndex`;
197 my $mactable = `$snmpwalk -Os -Cc -c $com -v $ver $myip mib-2.17.4.3.1.1`;
198 my $mactableport = `$snmpwalk -Os -Cc -c $com -v $ver $myip mib-2.17.4.3.1.2`;
199 my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp2/cgi-bin/dhcp.cgi -q -O -`;
200 my $arpn = `wget -t 1 -T 1 http://$dhcp2/cgi-bin/arpn.cgi -q -O -`;
201 
2021simandl foreach (split("\n",$operstatusall)) {
203 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
204 @row = split ;
205 $pole_operstatus[$row[0]] = $row[1];
206 }
207 foreach (split("\n",$adminstatusall)) {
208 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
209 @row = split ;
210 $pole_adminstatus[$row[0]] = $row[1];
211 }
212 foreach (split("\n",$speedall)) {
213 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
214 @row = split ;
21516simandl $row[1] =~ s/10000000$/10M/;
216 $row[1] =~ s/100000000$/100M/;
217 $row[1] =~ s/1000000000$/1G/;
218 $row[1] =~ s/4294967295$/10G/;
2191simandl $pole_speed[$row[0]] = $row[1];
220 }
221 foreach (split("\n",$mactable)) {
222 s/ = Hex-STRING://;
223 ($macind, my $tmpmac) = split(" ", $_, 2);
224 $tmpmac =~ s/ $//g;
225 $tmpmac =~ s/ /:/g;
226 $hash_mac{$macind} = $tmpmac;
227 }
228 foreach (split("\n",$mactableport)) {
229 s/ = INTEGER://;
230 ($macind, $port) = split ;
231 $macind =~ s/^mib-2\.17\.4\.3\.1\.2/mib-2.17.4.3.1.1/;
232 if (exists $mactab{$port} ) {
233 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
234 } else {
235 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
236 }
237 }
238 
239#@pole_arpn = split("\n",$arpn);
240 
241 $dhcpdconf =~ s/\n/ /g;
242 $dhcpdconf =~ s/\s+/ /g;
243 $dhcpdconf =~ s/host /\nhost /g;
244 $dhcpdconf =~ s/}/}\n/g;
245 $dhcpdconf =~ s/\^host //g;
246 
247 my @pole_dhcpdconf = split("\n",$dhcpdconf);
248 
249 for (@pole_dhcpdconf) {
250 if (/^host/) {
251 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/;
252 (my $mac, my $host, my $ip) = split;
253 $mac =~ tr/a-f/A-F/;
254 $pole_dhcpdhosts{$mac} = "$host $ip";
255 }
256 }
257 
258 print "<table border=1>\n";
259 for ($y = 0; $y < $h; $y++) {
260 print "<tr>\n";
261 for ($x = 0; $x < $w; $x++) {
262 $bg = "\"#FFFFFF\"";
263 print " <td valign=top width=\"$sirka\" ";
264 if ($port = $map[$y][$x]) {
265 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
266 $mod = "";
267 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
268 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
269 $flow = "";
27015simandl if ($enab eq "up(1)") { $bg = "\"#FFFFFF\""; } else { $bg = "\"#FF0000\""; }
271 if ($link eq "up(1)") {
27216simandl if ($stat eq "1G" || $stat eq "10G") { $bg = "\"#00FF00\""; } else { $bg = "\"#FFFF00\""; }
27315simandl }
2741simandl }
275 print "bgcolor=$bg>";
276 if ($port) {
277 if ($enab =~ /^up/) {$e = "E";} else {$e = "D";}
278 if (defined($sw_d->{$myip}[$port])) {$label = $sw_d->{$myip}[$port];}
279 else { $label = ""; }
28010simandl $hip = $myip;
281 $hip =~ s/\./_/g;
28212simandl print_port_graph("$dhcp","${hip}_port${port}.html","${hip}_port${port}-day.png","$myip port $port $label","$port $label");
28310simandl print "<br />$e $stat<br />\n";
2841simandl foreach $mac (@{ $mactab{$port} }) {
285 if (exists $pole_dhcpdhosts{$mac}) {
286 $barva = "black";
287 $title = "$mac " . $pole_dhcpdhosts{$mac};
288 $popis = $pole_dhcpdhosts{$mac};
289 } else {
290 $barva = "red";
291 $popis = $mac;
29227simandl $macinfo = `wget -t 1 -T 1 http://$dhcp2/cgi-bin/macinfo.cgi?$mac -q -O -`;
2931simandl if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
294 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
295 }
296 print "<font color=$barva title=\"$title\">$popis</font><br>"
297 };
29816simandl print "</td>";
2991simandl } else {
300 print "&nbsp;</td>\n";
301 }
302 }
303 print "</tr>\n";
304 }
305 print "</table>";
306};

Powered by WebSVN 2.2.1