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