jablonka.czprosek.czf

switches

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

 

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\""; }
129 }
130 print "bgcolor=$bg>";
131 if ($port) {
132 $e = substr($enab,0,1);
133 if (defined($sw_d->{$mac}[$port])) {$label = $sw_d->{$mac}[$port];}
134 else { $label = ""; }
135 if ($flow eq "flowctl") { $f = "Y"; } else { $f = "N"; }
13610simandl $hmac = $mac;
137 $hmac =~ s/:/_/g;
138 print_port_graph("10.33.0.74","${hmac}_${port}.html","${hmac}_${port}-day.png","$mac port $port $label","$port $label");
139 print "<br />$e $mod $stat $f</td>\n";
1401simandl } else {
141 print "&nbsp;</td>\n";
142 }
143 }
144 print "</tr>\n";
145 }
146 print "</table>";
147}
148 
149sub draw_snmp_switch {
150 my $myip = shift(@_);
151 my $com = shift(@_);
152 my $dhcp = shift(@_);
153 my $desc = shift(@_);
154 my @map = @{shift(@_)};
155 my $sw_d = shift(@_);
156 
157 my @pole_operstatus = ();
158 my @pole_adminstatus = ();
159 my @pole_speed = ();
160 my %pole_dhcpdhosts = ();
161 my %hash_mac = ();
162 my %mactab = ();
163 my $macind = 0;
164 my $port = 0;
165 
166 my $w = scalar( @{$map[0]} );
167 my $h = scalar @map;
168 my $x = 0;
169 my $y = 0;
170 my $sirka = 70;
171 my $label = "";
172 my $bg = "";
173 my $enab = "";
174 my $e = "";
175 my $mod = "";
176 my $stat = "";
177 my $link = "";
178 my $flow = "";
179 my $barva = "";
180 my $title = "";
181 my $popis = "";
182 my $macinfo = "";
183 my $macip = "";
18410simandl my $hip = "";
1851simandl my @row = ();
186 
18712simandl print "<b>$desc</b> <a href=http:\/\/$myip>$myip</a>";
1881simandl 
1898simandl my $speedall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.5`;
190 my $adminstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.7`;
191 my $operstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.8`;
192 my $mactable = `$snmpwalk -Os -c $com -v 2c $myip atPhysAddress`;
193 my $mactableport = `$snmpwalk -Os -c $com -v 2c $myip ipNetToMediaIfIndex`;
1941simandl my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp/cgi-bin/dhcp.cgi -q -O -`;
195# $arpn = `wget -t 1 -T 1 http://$dhcp/cgi-bin/arpn.cgi -q -O -`;
196 
197 foreach (split("\n",$operstatusall)) {
198 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
199 @row = split ;
200 $pole_operstatus[$row[0]] = $row[1];
201 }
202 foreach (split("\n",$adminstatusall)) {
203 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
204 @row = split ;
205 $pole_adminstatus[$row[0]] = $row[1];
206 }
207 foreach (split("\n",$speedall)) {
208 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
209 @row = split ;
210 $row[1] =~ s/000000$/M/;
211 $pole_speed[$row[0]] = $row[1];
212 }
213 foreach (split("\n",$mactable)) {
214 s/ = Hex-STRING://;
215 ($macind, my $tmpmac) = split(" ", $_, 2);
216 $tmpmac =~ s/ $//g;
217 $tmpmac =~ s/ /:/g;
218 $hash_mac{$macind} = $tmpmac;
219 }
220 foreach (split("\n",$mactableport)) {
221 s/ = INTEGER://;
222 ($macind, $port) = split ;
223 $macind =~ s/^mib-2\.17\.4\.3\.1\.2/mib-2.17.4.3.1.1/;
224 if (exists $mactab{$port} ) {
225 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
226 } else {
227 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
228 }
229 }
230 
231#@pole_arpn = split("\n",$arpn);
232 
233 $dhcpdconf =~ s/\n/ /g;
234 $dhcpdconf =~ s/\s+/ /g;
235 $dhcpdconf =~ s/host /\nhost /g;
236 $dhcpdconf =~ s/}/}\n/g;
237 $dhcpdconf =~ s/\^host //g;
238 
239 my @pole_dhcpdconf = split("\n",$dhcpdconf);
240 
241 for (@pole_dhcpdconf) {
242 if (/^host/) {
243 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/;
244 (my $mac, my $host, my $ip) = split;
245 $mac =~ tr/a-f/A-F/;
246 $pole_dhcpdhosts{$mac} = "$host $ip";
247 }
248 }
249 
250 print "<table border=1>\n";
251 for ($y = 0; $y < $h; $y++) {
252 print "<tr>\n";
253 for ($x = 0; $x < $w; $x++) {
254 $bg = "\"#FFFFFF\"";
255 print " <td valign=top width=\"$sirka\" ";
256 if ($port = $map[$y][$x]) {
257 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
258 $mod = "";
259 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
260 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
261 $flow = "";
26213simandl if ($link eq "up(1)") { $bg = "\"#FFFF00\""; }
263 if ($stat eq "1000M") { $bg = "\"#00FF00\""; }
2641simandl }
265 print "bgcolor=$bg>";
266 if ($port) {
267 if ($enab =~ /^up/) {$e = "E";} else {$e = "D";}
268 if (defined($sw_d->{$myip}[$port])) {$label = $sw_d->{$myip}[$port];}
269 else { $label = ""; }
27010simandl $hip = $myip;
271 $hip =~ s/\./_/g;
27212simandl print_port_graph("$dhcp","${hip}_port${port}.html","${hip}_port${port}-day.png","$myip port $port $label","$port $label");
27310simandl print "<br />$e $stat<br />\n";
2741simandl print "<pre>";
275 foreach $mac (@{ $mactab{$port} }) {
276 if (exists $pole_dhcpdhosts{$mac}) {
277 $barva = "black";
278 $title = "$mac " . $pole_dhcpdhosts{$mac};
279 $popis = $pole_dhcpdhosts{$mac};
280 } else {
281 $barva = "red";
282 $popis = $mac;
283 $macinfo = `wget -t 1 -T 1 http://$dhcp/cgi-bin/macinfo.cgi?$mac -q -O -`;
284 if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
285 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
286 }
287 print "<font color=$barva title=\"$title\">$popis</font><br>"
288 };
289 print "</pre></td>";
290 } else {
291 print "&nbsp;</td>\n";
292 }
293 }
294 print "</tr>\n";
295 }
296 print "</table>";
297};

Powered by WebSVN 2.2.1