jablonka.czprosek.czf

switches

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

 

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 = ();
188 
18912simandl print "<b>$desc</b> <a href=http:\/\/$myip>$myip</a>";
1901simandl 
19117simandl my $speedall = `$snmpwalk -Os -c $com -v $ver $myip .1.3.6.1.2.1.2.2.1.5`;
192 my $adminstatusall = `$snmpwalk -Os -c $com -v $ver $myip .1.3.6.1.2.1.2.2.1.7`;
193 my $operstatusall = `$snmpwalk -Os -c $com -v $ver $myip .1.3.6.1.2.1.2.2.1.8`;
194 my $mactable = `$snmpwalk -Os -c $com -v $ver $myip atPhysAddress`;
195 my $mactableport = `$snmpwalk -Os -c $com -v $ver $myip ipNetToMediaIfIndex`;
1961simandl my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp/cgi-bin/dhcp.cgi -q -O -`;
197# $arpn = `wget -t 1 -T 1 http://$dhcp/cgi-bin/arpn.cgi -q -O -`;
198 
199 foreach (split("\n",$operstatusall)) {
200 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
201 @row = split ;
202 $pole_operstatus[$row[0]] = $row[1];
203 }
204 foreach (split("\n",$adminstatusall)) {
205 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
206 @row = split ;
207 $pole_adminstatus[$row[0]] = $row[1];
208 }
209 foreach (split("\n",$speedall)) {
210 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
211 @row = split ;
21216simandl $row[1] =~ s/10000000$/10M/;
213 $row[1] =~ s/100000000$/100M/;
214 $row[1] =~ s/1000000000$/1G/;
215 $row[1] =~ s/4294967295$/10G/;
2161simandl $pole_speed[$row[0]] = $row[1];
217 }
218 foreach (split("\n",$mactable)) {
219 s/ = Hex-STRING://;
220 ($macind, my $tmpmac) = split(" ", $_, 2);
221 $tmpmac =~ s/ $//g;
222 $tmpmac =~ s/ /:/g;
223 $hash_mac{$macind} = $tmpmac;
224 }
225 foreach (split("\n",$mactableport)) {
226 s/ = INTEGER://;
227 ($macind, $port) = split ;
228 $macind =~ s/^mib-2\.17\.4\.3\.1\.2/mib-2.17.4.3.1.1/;
229 if (exists $mactab{$port} ) {
230 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
231 } else {
232 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
233 }
234 }
235 
236#@pole_arpn = split("\n",$arpn);
237 
238 $dhcpdconf =~ s/\n/ /g;
239 $dhcpdconf =~ s/\s+/ /g;
240 $dhcpdconf =~ s/host /\nhost /g;
241 $dhcpdconf =~ s/}/}\n/g;
242 $dhcpdconf =~ s/\^host //g;
243 
244 my @pole_dhcpdconf = split("\n",$dhcpdconf);
245 
246 for (@pole_dhcpdconf) {
247 if (/^host/) {
248 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/;
249 (my $mac, my $host, my $ip) = split;
250 $mac =~ tr/a-f/A-F/;
251 $pole_dhcpdhosts{$mac} = "$host $ip";
252 }
253 }
254 
255 print "<table border=1>\n";
256 for ($y = 0; $y < $h; $y++) {
257 print "<tr>\n";
258 for ($x = 0; $x < $w; $x++) {
259 $bg = "\"#FFFFFF\"";
260 print " <td valign=top width=\"$sirka\" ";
261 if ($port = $map[$y][$x]) {
262 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
263 $mod = "";
264 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
265 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
266 $flow = "";
26715simandl if ($enab eq "up(1)") { $bg = "\"#FFFFFF\""; } else { $bg = "\"#FF0000\""; }
268 if ($link eq "up(1)") {
26916simandl if ($stat eq "1G" || $stat eq "10G") { $bg = "\"#00FF00\""; } else { $bg = "\"#FFFF00\""; }
27015simandl }
2711simandl }
272 print "bgcolor=$bg>";
273 if ($port) {
274 if ($enab =~ /^up/) {$e = "E";} else {$e = "D";}
275 if (defined($sw_d->{$myip}[$port])) {$label = $sw_d->{$myip}[$port];}
276 else { $label = ""; }
27710simandl $hip = $myip;
278 $hip =~ s/\./_/g;
27912simandl print_port_graph("$dhcp","${hip}_port${port}.html","${hip}_port${port}-day.png","$myip port $port $label","$port $label");
28010simandl print "<br />$e $stat<br />\n";
2811simandl foreach $mac (@{ $mactab{$port} }) {
282 if (exists $pole_dhcpdhosts{$mac}) {
283 $barva = "black";
284 $title = "$mac " . $pole_dhcpdhosts{$mac};
285 $popis = $pole_dhcpdhosts{$mac};
286 } else {
287 $barva = "red";
288 $popis = $mac;
289 $macinfo = `wget -t 1 -T 1 http://$dhcp/cgi-bin/macinfo.cgi?$mac -q -O -`;
290 if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
291 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
292 }
293 print "<font color=$barva title=\"$title\">$popis</font><br>"
294 };
29516simandl print "</td>";
2961simandl } else {
297 print "&nbsp;</td>\n";
298 }
299 }
300 print "</tr>\n";
301 }
302 print "</table>";
303};

Powered by WebSVN 2.2.1