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