jablonka.czprosek.czf

switches

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

 

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

Powered by WebSVN 2.2.1