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