jablonka.czprosek.czf

switches

Subversion Repositories:
[/] [switchesol.cgi] - Blame information for rev 8

 

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

Powered by WebSVN 2.2.1