jablonka.czprosek.czf

switches

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

 

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

Powered by WebSVN 2.2.1