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