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