jablonka.czprosek.czf

switches

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

 

Line No. Rev Author Line
11simandl#!/usr/bin/perl -w
2use strict;
3#
4# Petr Simandl, www.simandl.cz
5 
6my $snmpwalk = "/usr/bin/snmpwalk";
7 
8my %map = (
9 "edimax_es-3116p" => [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
10 ],
11 "repotec_g3224x" => [[1,2,3,4,5,6,7,8,9,10,11,12,0,0],
12 [13,14,15,16,17,18,19,20,21,22,23,24,25,26],
13 ],
14 "repotec_RP-6326I" => [[1,2,3,4,5,6,7,8,9,10,11,12,0,0],
15 [13,14,15,16,17,18,19,20,21,22,23,24,25,26],
16 ],
17 "repotec_RP-G2402I"=> [[1,2,3,4,5,6,7,8,9,10,11,12],
18 [13,14,15,16,17,18,19,20,21,22,23,24],
19 ],
20 "repotec_GEL2-SW8" => [[1,2,3,4,5,6,7,8],
21 ],
22 "linksys_SRW224G4" => [[1,2,3,4,5,6,7,8,9,10,11,12,25,27],
23 [13,14,15,16,17,18,19,20,21,22,23,24,26,28],
24 ],
25 "NetGear" => [[1,2,3,4,5,6,7,8,9,10,11,12],
26 ],
27 "SignaMax_065-7851"=> [[1,3,5,7,9,11,13,15,17,19,21,23],
28 [2,4,6,8,10,12,14,16,18,20,22,24],
29 ],
302simandl "AL11FIDU" => [[14,15,0,0],
31 [6,7,10,11],
32 ],
331simandl );
34 
35my @ports = ();
36my @sw = ();
37my %sw_d = ();
38 
39my $portlist = "";
40my $p = 0;
41my $d = "";
42my ($fnc,$mac,$if,$bin,$geom,$desc);
43 
44print "Content-type: text/html\n\n";
45 
46print "<HTML><HEAD>\n<META HTTP-EQUIV=Pragma CONTENT=no-cache>\n<style type=\"text/css\" media=\"screen\"> \@import \"../style.css\" ;</style>\n";
472simandlprint "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF8\">\n";
481simandl#print "<script type=\"text/javascript\" src=\"overlib.js\"><!-- overLIB (c) Erik Bosrup --></script>\n";
49print "</HEAD><BODY>\n";
50#nocache!
51 
52print "<div id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></div>";
53 
54#print "<a href=\"javascript:void(0);\" onmouseover=\"return overlib('This is an ordinary popup.');\" onmouseout=\"return nd();\">here</a>";
55 
56open(CONF,"< /etc/switches.conf") || die("can't open datafile: $!");
57while (<CONF>) {
58 chomp;
59 next if (/^#/);
60 next if (/^$/);
61 if (/^draw_/) { push @sw, $_; next; }
62 if (/^([0-9a-fA-F]{2}:[0-9a-fA-F]{2}:)|(([0-9]{1,3}\.){3}[0-9]{1,3})/) {
63 ($mac, $portlist) = split(/\|/, $_, 2);
64 @ports = ();
65 foreach ( split(/\|/, $portlist) ) {
66 ($p, $d) = split /;/;
67 $ports[$p] = $d;
68 }
69 $sw_d{$mac} = [ @ports ];
70 }
71}
72close CONF;
73 
74foreach (@sw) {
75 ($fnc, $mac, $if, $bin, $geom, $desc) = split /\|/;
76 SWITCH: {
77 if ($fnc eq "draw_rrcp_switch") {
78 draw_rrcp_switch($mac,$if,$bin,$desc,$map{$geom},\%sw_d);
79 last SWITCH;
80 }
81 if ($fnc eq "draw_snmp_switch") {
82 draw_snmp_switch($mac,$if,$bin,$desc,$map{$geom},\%sw_d);
83 last SWITCH;
84 }
85 }
86 print "<br>\n";
87}
88 
89my $date = `date`;
90print "<BR>$date\n<BR>";
91 
92print "</center></BODY></HTML>";
93 
94################################################################################
95 
96sub draw_rrcp_switch {
97 my $mac = shift(@_);
98 my $if = shift(@_);
99 my $bin = shift(@_);
100 my $desc = shift(@_);
101 my @map = @{shift(@_)};
102 my $sw_d = shift(@_);
103 
104 my $w = scalar( @{$map[0]} );
105 my $h = scalar @map;
106 my $x = 0;
107 my $y = 0;
108 my $e = "";
109 my $f = "";
110 my $sirka = 70;
111 my $label = "";
112 my $bg = "";
113 my $enab = "";
114 my $mod = "";
115 my $stat = "";
116 my $link = "";
117 my $flow = "";
118 my $port = 0;
119 my @row = ();
120 my @pole = ();
121 
122 my $porty=`sudo $bin $mac\@$if show int | grep ^$if | sed 's/://g' | sed 's/$if\\///g'`;
123 print "<b>$desc</b> $mac";
124# print "<pre>$porty\n</pre>";
125 foreach (split("\n",$porty)) {
126 s/auto1000M-/auto 1000M-/;
127 @row = split ;
128 @pole[$row[0]] = [ @row[1..5] ];
129 }
130 
131 print "<table border=1>\n";
132 for ($y = 0; $y < $h; $y++) {
133 print "<tr>\n";
134 for ($x = 0; $x < $w; $x++) {
135 $bg = "\"#FFFFFF\"";
136 print " <td valign=top width=\"$sirka\" ";
137 if ($port = $map[$y][$x]) {
138 ($enab,$mod,$stat,$link,$flow) = @{$pole[$port]}[0..4];
139 if ($link eq "LINK") { $bg = "\"#00FF00\""; }
140 }
141 print "bgcolor=$bg>";
142 if ($port) {
143 $e = substr($enab,0,1);
144 if (defined($sw_d->{$mac}[$port])) {$label = $sw_d->{$mac}[$port];}
145 else { $label = ""; }
146 if ($flow eq "flowctl") { $f = "Y"; } else { $f = "N"; }
147 print "$port $label<br />$e $mod $stat $f</td>\n";
148 } else {
149 print "&nbsp;</td>\n";
150 }
151 }
152 print "</tr>\n";
153 }
154 print "</table>";
155}
156 
157sub draw_snmp_switch {
158 my $myip = shift(@_);
159 my $com = shift(@_);
160 my $dhcp = shift(@_);
161 my $desc = shift(@_);
162 my @map = @{shift(@_)};
163 my $sw_d = shift(@_);
164 
165 my @pole_operstatus = ();
166 my @pole_adminstatus = ();
167 my @pole_speed = ();
168 my %pole_dhcpdhosts = ();
169 my %hash_mac = ();
170 my %mactab = ();
171 my $macind = 0;
172 my $port = 0;
173 
174 my $w = scalar( @{$map[0]} );
175 my $h = scalar @map;
176 my $x = 0;
177 my $y = 0;
178 my $sirka = 70;
179 my $label = "";
180 my $bg = "";
181 my $enab = "";
182 my $e = "";
183 my $mod = "";
184 my $stat = "";
185 my $link = "";
186 my $flow = "";
187 my $barva = "";
188 my $title = "";
189 my $popis = "";
190 my $macinfo = "";
191 my $macip = "";
192 my @row = ();
193 
194 print "<b>$desc</b> $myip";
195 
196 my $speedall = `$snmpwalk -Os -c $com -v 1 $myip .1.3.6.1.2.1.2.2.1.5`;
197 my $adminstatusall = `$snmpwalk -Os -c $com -v 1 $myip .1.3.6.1.2.1.2.2.1.7`;
198 my $operstatusall = `$snmpwalk -Os -c $com -v 1 $myip .1.3.6.1.2.1.2.2.1.8`;
199 my $mactable = `$snmpwalk -Os -c $com -v 1 $myip .1.3.6.1.2.1.17.4.3.1.1`;
200 my $mactableport = `$snmpwalk -Os -c $com -v 1 $myip .1.3.6.1.2.1.17.4.3.1.2`;
201 my $dhcpdconf = `wget -t 1 -T 1 http://$dhcp/cgi-bin/dhcp.cgi -q -O -`;
202# $arpn = `wget -t 1 -T 1 http://$dhcp/cgi-bin/arpn.cgi -q -O -`;
203 
204 foreach (split("\n",$operstatusall)) {
205 s/^ifOperStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
206 @row = split ;
207 $pole_operstatus[$row[0]] = $row[1];
208 }
209 foreach (split("\n",$adminstatusall)) {
210 s/^ifAdminStatus\.([0-9]+) = INTEGER: (.*)$/$1 $2/;
211 @row = split ;
212 $pole_adminstatus[$row[0]] = $row[1];
213 }
214 foreach (split("\n",$speedall)) {
215 s/^ifSpeed\.([0-9]+) = Gauge32: (.*)$/$1 $2/;
216 @row = split ;
217 $row[1] =~ s/000000$/M/;
218 $pole_speed[$row[0]] = $row[1];
219 }
220 foreach (split("\n",$mactable)) {
221 s/ = Hex-STRING://;
222 ($macind, my $tmpmac) = split(" ", $_, 2);
223 $tmpmac =~ s/ $//g;
224 $tmpmac =~ s/ /:/g;
225 $hash_mac{$macind} = $tmpmac;
226 }
227 foreach (split("\n",$mactableport)) {
228 s/ = INTEGER://;
229 ($macind, $port) = split ;
230 $macind =~ s/^mib-2\.17\.4\.3\.1\.2/mib-2.17.4.3.1.1/;
231 if (exists $mactab{$port} ) {
232 push @{ $mactab{$port} }, $hash_mac{$macind}; # pridam do pole k portu
233 } else {
234 $mactab{$port} = [ $hash_mac{$macind} ]; # zalozim nove pole pro port
235 }
236 }
237 
238#@pole_arpn = split("\n",$arpn);
239 
240 $dhcpdconf =~ s/\n/ /g;
241 $dhcpdconf =~ s/\s+/ /g;
242 $dhcpdconf =~ s/host /\nhost /g;
243 $dhcpdconf =~ s/}/}\n/g;
244 $dhcpdconf =~ s/\^host //g;
245 
246 my @pole_dhcpdconf = split("\n",$dhcpdconf);
247 
248 for (@pole_dhcpdconf) {
249 if (/^host/) {
250 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/;
251 (my $mac, my $host, my $ip) = split;
252 $mac =~ tr/a-f/A-F/;
253 $pole_dhcpdhosts{$mac} = "$host $ip";
254 }
255 }
256 
257 print "<table border=1>\n";
258 for ($y = 0; $y < $h; $y++) {
259 print "<tr>\n";
260 for ($x = 0; $x < $w; $x++) {
261 $bg = "\"#FFFFFF\"";
262 print " <td valign=top width=\"$sirka\" ";
263 if ($port = $map[$y][$x]) {
264 if (defined($pole_adminstatus[$port])) { $enab = $pole_adminstatus[$port]; } else { $enab = ""; }
265 $mod = "";
266 if (defined($pole_speed[$port])) { $stat = $pole_speed[$port]; } else { $stat = ""; }
267 if (defined($pole_operstatus[$port])) { $link = $pole_operstatus[$port]; } else { $link = ""; }
268 $flow = "";
269 if ($link eq "up(1)") { $bg = "\"#00FF00\""; }
270 }
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 = ""; }
276 print "$port $label<br />$e $stat<br />\n";
277 print "<pre>";
278 foreach $mac (@{ $mactab{$port} }) {
279 if (exists $pole_dhcpdhosts{$mac}) {
280 $barva = "black";
281 $title = "$mac " . $pole_dhcpdhosts{$mac};
282 $popis = $pole_dhcpdhosts{$mac};
283 } else {
284 $barva = "red";
285 $popis = $mac;
286 $macinfo = `wget -t 1 -T 1 http://$dhcp/cgi-bin/macinfo.cgi?$mac -q -O -`;
287 if ($macinfo) { ($macip) = split(" ", $macinfo, 1) } else { $macip = "none" };
288 $title = "$mac je neznama MAC adresa! Nalezena IP : $macip";
289 }
290 print "<font color=$barva title=\"$title\">$popis</font><br>"
291 };
292 print "</pre></td>";
293 } else {
294 print "&nbsp;</td>\n";
295 }
296 }
297 print "</tr>\n";
298 }
299 print "</table>";
300};

Powered by WebSVN 2.2.1