jablonka.czprosek.czf

switches

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

 

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";
251simandl#print "<script type=\"text/javascript\" src=\"overlib.js\"><!-- overLIB (c) Erik Bosrup --></script>\n";
26print "</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 
73sub draw_rrcp_switch {
74 my $mac = shift(@_);
75 my $if = shift(@_);
76 my $bin = shift(@_);
77 my $desc = shift(@_);
78 my @map = @{shift(@_)};
79 my $sw_d = shift(@_);
80 
81 my $w = scalar( @{$map[0]} );
82 my $h = scalar @map;
83 my $x = 0;
84 my $y = 0;
85 my $e = "";
86 my $f = "";
87 my $sirka = 70;
88 my $label = "";
89 my $bg = "";
90 my $enab = "";
91 my $mod = "";
92 my $stat = "";
93 my $link = "";
94 my $flow = "";
95 my $port = 0;
96 my @row = ();
97 my @pole = ();
98 
99 my $porty=`sudo $bin $mac\@$if show int | grep ^$if | sed 's/://g' | sed 's/$if\\///g'`;
100 print "<b>$desc</b> $mac";
101# print "<pre>$porty\n</pre>";
102 foreach (split("\n",$porty)) {
103 s/auto1000M-/auto 1000M-/;
104 @row = split ;
105 @pole[$row[0]] = [ @row[1..5] ];
106 }
107 
108 print "<table border=1>\n";
109 for ($y = 0; $y < $h; $y++) {
110 print "<tr>\n";
111 for ($x = 0; $x < $w; $x++) {
112 $bg = "\"#FFFFFF\"";
113 print " <td valign=top width=\"$sirka\" ";
114 if ($port = $map[$y][$x]) {
115 ($enab,$mod,$stat,$link,$flow) = @{$pole[$port]}[0..4];
116 if ($link eq "LINK") { $bg = "\"#00FF00\""; }
117 }
118 print "bgcolor=$bg>";
119 if ($port) {
120 $e = substr($enab,0,1);
121 if (defined($sw_d->{$mac}[$port])) {$label = $sw_d->{$mac}[$port];}
122 else { $label = ""; }
123 if ($flow eq "flowctl") { $f = "Y"; } else { $f = "N"; }
124 print "$port $label<br />$e $mod $stat $f</td>\n";
125 } else {
126 print "&nbsp;</td>\n";
127 }
128 }
129 print "</tr>\n";
130 }
131 print "</table>";
132}
133 
134sub draw_snmp_switch {
135 my $myip = shift(@_);
136 my $com = shift(@_);
137 my $dhcp = shift(@_);
138 my $desc = shift(@_);
139 my @map = @{shift(@_)};
140 my $sw_d = shift(@_);
141 
142 my @pole_operstatus = ();
143 my @pole_adminstatus = ();
144 my @pole_speed = ();
145 my %pole_dhcpdhosts = ();
146 my %hash_mac = ();
147 my %mactab = ();
148 my $macind = 0;
149 my $port = 0;
150 
151 my $w = scalar( @{$map[0]} );
152 my $h = scalar @map;
153 my $x = 0;
154 my $y = 0;
155 my $sirka = 70;
156 my $label = "";
157 my $bg = "";
158 my $enab = "";
159 my $e = "";
160 my $mod = "";
161 my $stat = "";
162 my $link = "";
163 my $flow = "";
164 my $barva = "";
165 my $title = "";
166 my $popis = "";
167 my $macinfo = "";
168 my $macip = "";
169 my @row = ();
170 
171 print "<b>$desc</b> $myip";
172 
1738simandl my $speedall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.5`;
174 my $adminstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.7`;
175 my $operstatusall = `$snmpwalk -Os -c $com -v 2c $myip .1.3.6.1.2.1.2.2.1.8`;
176 my $mactable = `$snmpwalk -Os -c $com -v 2c $myip atPhysAddress`;
177 my $mactableport = `$snmpwalk -Os -c $com -v 2c $myip ipNetToMediaIfIndex`;
1781simandl my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp/cgi-bin/dhcp.cgi -q -O -`;
179# $arpn = `wget -t 1 -T 1 http://$dhcp/cgi-bin/arpn.cgi -q -O -`;
180 
181 foreach (split("\n",$operstatusall)) {
182 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
183 @row = split ;
184 $pole_operstatus[$row[0]] = $row[1];
185 }
186 foreach (split("\n",$adminstatusall)) {
187 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
188 @row = split ;
189 $pole_adminstatus[$row[0]] = $row[1];
190 }
191 foreach (split("\n",$speedall)) {
192 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
193 @row = split ;
194 $row[1] =~ s/000000$/M/;
195 $pole_speed[$row[0]] = $row[1];
196 }
197 foreach (split("\n",$mactable)) {
198 s/ = Hex-STRING://;
199 ($macind, my $tmpmac) = split(" ", $_, 2);
200 $tmpmac =~ s/ $//g;
201 $tmpmac =~ s/ /:/g;
202 $hash_mac{$macind} = $tmpmac;
203 }
204 foreach (split("\n",$mactableport)) {
205 s/ = INTEGER://;
206 ($macind, $port) = split ;
207 $macind =~ s/^mib-2\.17\.4\.3\.1\.2/mib-2.17.4.3.1.1/;
208 if (exists $mactab{$port} ) {
209 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
210 } else {
211 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
212 }
213 }
214 
215#@pole_arpn = split("\n",$arpn);
216 
217 $dhcpdconf =~ s/\n/ /g;
218 $dhcpdconf =~ s/\s+/ /g;
219 $dhcpdconf =~ s/host /\nhost /g;
220 $dhcpdconf =~ s/}/}\n/g;
221 $dhcpdconf =~ s/\^host //g;
222 
223 my @pole_dhcpdconf = split("\n",$dhcpdconf);
224 
225 for (@pole_dhcpdconf) {
226 if (/^host/) {
227 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/;
228 (my $mac, my $host, my $ip) = split;
229 $mac =~ tr/a-f/A-F/;
230 $pole_dhcpdhosts{$mac} = "$host $ip";
231 }
232 }
233 
234 print "<table border=1>\n";
235 for ($y = 0; $y < $h; $y++) {
236 print "<tr>\n";
237 for ($x = 0; $x < $w; $x++) {
238 $bg = "\"#FFFFFF\"";
239 print " <td valign=top width=\"$sirka\" ";
240 if ($port = $map[$y][$x]) {
241 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
242 $mod = "";
243 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
244 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
245 $flow = "";
246 if ($link eq "up(1)") { $bg = "\"#00FF00\""; }
247 }
248 print "bgcolor=$bg>";
249 if ($port) {
250 if ($enab =~ /^up/) {$e = "E";} else {$e = "D";}
251 if (defined($sw_d->{$myip}[$port])) {$label = $sw_d->{$myip}[$port];}
252 else { $label = ""; }
253 print "$port $label<br />$e $stat<br />\n";
254 print "<pre>";
255 foreach $mac (@{ $mactab{$port} }) {
256 if (exists $pole_dhcpdhosts{$mac}) {
257 $barva = "black";
258 $title = "$mac " . $pole_dhcpdhosts{$mac};
259 $popis = $pole_dhcpdhosts{$mac};
260 } else {
261 $barva = "red";
262 $popis = $mac;
263 $macinfo = `wget -t 1 -T 1 http://$dhcp/cgi-bin/macinfo.cgi?$mac -q -O -`;
264 if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
265 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
266 }
267 print "<font color=$barva title=\"$title\">$popis</font><br>"
268 };
269 print "</pre></td>";
270 } else {
271 print "&nbsp;</td>\n";
272 }
273 }
274 print "</tr>\n";
275 }
276 print "</table>";
277};

Powered by WebSVN 2.2.1