jablonka.czprosek.czf

switches

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

 

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

Powered by WebSVN 2.2.1