jablonka.czprosek.czf

switches

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

 

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 = "";
19my ($fnc,$mac,$if,$bin,$geom,$desc);
20 
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) {
52 ($fnc, $mac, $if, $bin, $geom, $desc) = split /\|/;
53 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") {
59 draw_snmp_switch($mac,$if,$bin,$desc,$map{$geom},\%sw_d);
60 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(@_);
153 my $dhcp = shift(@_);
154 my $desc = shift(@_);
155 my @map = @{shift(@_)};
156 my $sw_d = shift(@_);
157 
158 my @pole_operstatus = ();
159 my @pole_adminstatus = ();
160 my @pole_speed = ();
161 my %pole_dhcpdhosts = ();
162 my %hash_mac = ();
163 my %mactab = ();
164 my $macind = 0;
165 my $port = 0;
166 
167 my $w = scalar( @{$map[0]} );
168 my $h = scalar @map;
169 my $x = 0;
170 my $y = 0;
171 my $sirka = 70;
172 my $label = "";
173 my $bg = "";
174 my $enab = "";
175 my $e = "";
176 my $mod = "";
177 my $stat = "";
178 my $link = "";
179 my $flow = "";
180 my $barva = "";
181 my $title = "";
182 my $popis = "";
183 my $macinfo = "";
184 my $macip = "";
18510simandl my $hip = "";
1861simandl my @row = ();
187 
18812simandl print "<b>$desc</b> <a href=http:\/\/$myip>$myip</a>";
1891simandl 
1908simandl my $speedall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.5`;
191 my $adminstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.7`;
192 my $operstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.8`;
193 my $mactable = `$snmpwalk -Os -c $com -v 2c $myip atPhysAddress`;
194 my $mactableport = `$snmpwalk -Os -c $com -v 2c $myip ipNetToMediaIfIndex`;
1951simandl my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp/cgi-bin/dhcp.cgi -q -O -`;
196# $arpn = `wget -t 1 -T 1 http://$dhcp/cgi-bin/arpn.cgi -q -O -`;
197 
198 foreach (split("\n",$operstatusall)) {
199 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
200 @row = split ;
201 $pole_operstatus[$row[0]] = $row[1];
202 }
203 foreach (split("\n",$adminstatusall)) {
204 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
205 @row = split ;
206 $pole_adminstatus[$row[0]] = $row[1];
207 }
208 foreach (split("\n",$speedall)) {
209 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
210 @row = split ;
21116simandl $row[1] =~ s/10000000$/10M/;
212 $row[1] =~ s/100000000$/100M/;
213 $row[1] =~ s/1000000000$/1G/;
214 $row[1] =~ s/4294967295$/10G/;
2151simandl $pole_speed[$row[0]] = $row[1];
216 }
217 foreach (split("\n",$mactable)) {
218 s/ = Hex-STRING://;
219 ($macind, my $tmpmac) = split(" ", $_, 2);
220 $tmpmac =~ s/ $//g;
221 $tmpmac =~ s/ /:/g;
222 $hash_mac{$macind} = $tmpmac;
223 }
224 foreach (split("\n",$mactableport)) {
225 s/ = INTEGER://;
226 ($macind, $port) = split ;
227 $macind =~ s/^mib-2\.17\.4\.3\.1\.2/mib-2.17.4.3.1.1/;
228 if (exists $mactab{$port} ) {
229 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
230 } else {
231 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
232 }
233 }
234 
235#@pole_arpn = split("\n",$arpn);
236 
237 $dhcpdconf =~ s/\n/ /g;
238 $dhcpdconf =~ s/\s+/ /g;
239 $dhcpdconf =~ s/host /\nhost /g;
240 $dhcpdconf =~ s/}/}\n/g;
241 $dhcpdconf =~ s/\^host //g;
242 
243 my @pole_dhcpdconf = split("\n",$dhcpdconf);
244 
245 for (@pole_dhcpdconf) {
246 if (/^host/) {
247 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/;
248 (my $mac, my $host, my $ip) = split;
249 $mac =~ tr/a-f/A-F/;
250 $pole_dhcpdhosts{$mac} = "$host $ip";
251 }
252 }
253 
254 print "<table border=1>\n";
255 for ($y = 0; $y < $h; $y++) {
256 print "<tr>\n";
257 for ($x = 0; $x < $w; $x++) {
258 $bg = "\"#FFFFFF\"";
259 print " <td valign=top width=\"$sirka\" ";
260 if ($port = $map[$y][$x]) {
261 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
262 $mod = "";
263 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
264 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
265 $flow = "";
26615simandl if ($enab eq "up(1)") { $bg = "\"#FFFFFF\""; } else { $bg = "\"#FF0000\""; }
267 if ($link eq "up(1)") {
26816simandl if ($stat eq "1G" || $stat eq "10G") { $bg = "\"#00FF00\""; } else { $bg = "\"#FFFF00\""; }
26915simandl }
2701simandl }
271 print "bgcolor=$bg>";
272 if ($port) {
273 if ($enab =~ /^up/) {$e = "E";} else {$e = "D";}
274 if (defined($sw_d->{$myip}[$port])) {$label = $sw_d->{$myip}[$port];}
275 else { $label = ""; }
27610simandl $hip = $myip;
277 $hip =~ s/\./_/g;
27812simandl print_port_graph("$dhcp","${hip}_port${port}.html","${hip}_port${port}-day.png","$myip port $port $label","$port $label");
27910simandl print "<br />$e $stat<br />\n";
2801simandl foreach $mac (@{ $mactab{$port} }) {
281 if (exists $pole_dhcpdhosts{$mac}) {
282 $barva = "black";
283 $title = "$mac " . $pole_dhcpdhosts{$mac};
284 $popis = $pole_dhcpdhosts{$mac};
285 } else {
286 $barva = "red";
287 $popis = $mac;
288 $macinfo = `wget -t 1 -T 1 http://$dhcp/cgi-bin/macinfo.cgi?$mac -q -O -`;
289 if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
290 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
291 }
292 print "<font color=$barva title=\"$title\">$popis</font><br>"
293 };
29416simandl print "</td>";
2951simandl } else {
296 print "&nbsp;</td>\n";
297 }
298 }
299 print "</tr>\n";
300 }
301 print "</table>";
302};

Powered by WebSVN 2.2.1