jablonka.czprosek.czf

switches

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

 

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

Powered by WebSVN 2.2.1