2
# arps.pl - html generator for ntop's ARP Cache.
4
# Copyright (C) 2000 Rocco Carbone <rocco@ntop.org>
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; see the file COPYING. If not, write to
18
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
# Boston, MA 02111-1307, USA.
23
# the %traffic hash keeps all information of interest
24
# for the purpose of reporting statistics access
27
# hwaddress => unique ethernet address (MAC address)
28
# vendor => unique MAC address vendor name
29
# ipaddress => its IP address in dot notation
30
# hostname => its, locally resolved, hostname
31
# domain => its, locally resolved, DNS domain name
32
# nbtname => its NetBIOS name
33
# nbtdomain => its NetBIOS domain
34
# first => time the host was first seen
35
# last => time the host was first seen
40
$__progname__ = "arps.pl";
41
$__version__ = "0.0.2";
42
$__updated__ = "10/11/2000";
43
$__boottime__ = time();
44
$__bootdate__ = localtime($__boottime__);
46
# Hey Netscape, this is for you
47
my $htmltitle = "NTOP Statistics -- ARP Cache";
48
my $bodyname = "ARP Cache Report";
49
my $title = "ARP Cache Summary";
50
my $description = "Shows ntop's ARP Cache";
53
my $infobg = "\"#cccccc\"";
54
my $headerbg = "\"#ccccff\"";
55
my @bgcolors = ("\"#ffffff\"", "\"#eeeeee\"");
62
# The unsorted hash of ntop traffic cache (global hash)
63
my @traffic = sort keys %traffic;
66
# other useful variables
68
@reporttypes = ("hwaddress") unless @reporttypes;
69
@sorttypes = ("ascending") unless @sorttypes;
75
# perform all needed ascending and descending sorting
79
# sort hw address alphabetically and ip address alphabetically
81
my @sorted_by_hwaddress = sort { ${$a}{'hwaddress'} cmp ${$b}{'hwaddress'} } values %traffic;
82
my @sorted_by_ipaddress = sort ipnumerically values %traffic;
83
my @sorted_by_hostname = sort { ${$a}{'hostname'} cmp ${$b}{'hostname'} } values %traffic;
85
my @sorted_by_rev_hwaddress = reverse @sorted_by_hwaddress;
86
my @sorted_by_rev_ipaddress = reverse @sorted_by_ipaddress;
87
my @sorted_by_rev_hostname = reverse @sorted_by_hostname;
89
my $items_per_section = 25;
90
my $total_items = $#sorted_by_hwaddress + 1; # items to display
91
my $sections = int($total_items / $items_per_section); # sections to create
94
if ($total_items % $items_per_section) {
101
# print HTML header and title
103
html_header($htmltitle);
106
# start printing HTML body
108
html_start_body($bodyname);
111
# print report's title with its brief description
113
print "<center><font size=6><b>$description</b></font></table>\n";
115
foreach $reporttype (@reporttypes) {
117
my $sorted_by = "sorted_by_$reporttype";
118
my $sorted_by_rev = "sorted_by_rev_$reporttype";
120
my @sorted = eval("\@$sorted_by");
121
my @sorted_rev = eval("\@$sorted_by_rev");
124
foreach $sorttype (@sorttypes) {
127
for ($section = 1; $section <= $sections; $section++) {
128
# the sub array for this section
132
# useful calculations before starting playing with sections
134
my $last = $current + $items_per_section - 1;
136
if ($last >= $total_items) {
137
$last = $total_items - 1;
139
my $itemno = $last - $current + 1;
142
# isolate the sub array
144
if ($sorttype eq "ascending") {
145
$sort1 = "/plugins/pep?$__progname__&sort=descending&field=hostname";
146
$sort2 = "/plugins/pep?$__progname__&sort=descending&field=hwaddress";
147
$sort4 = "/plugins/pep?$__progname__&sort=descending&field=ipaddress";
149
@sub_sorted = @sorted[$current..$last];
152
$sort1 = "/plugins/pep?$__progname__&sort=ascending&field=hostname";
153
$sort2 = "/plugins/pep?$__progname__&sort=ascending&field=hwaddress";
154
$sort4 = "/plugins/pep?$__progname__&sort=ascending&field=ipaddress";
156
@sub_sorted = @sorted_rev[$current..$last];
159
print "<p><table align=center cellpadding=1 cellspacing=0 border=0>\n";
160
print "<b>$title section $section of $sections</b>\n";
161
print "<tr><td> </td><tr bgcolor=$bgcolors[(++ $j) % 2]><td> </td>";
162
print "<td colspan=15>Displaying ";
168
print "] of $total_items sorted by $sorttype $reporttype</td></tr>\n";
169
print "<tr bgcolor=$bgcolors[(++ $j) % 2]>\n";
171
print "<tr><td> </td>\n";
174
print "<table align=center cellpadding=1 cellspacing=0 border=0>\n";
176
# table header (column's name separated by blanks)
178
print "<tr bgcolor=$headerbg>\n";
180
print "<td width=10> </td>\n";
181
print "<td align=left><font size=4><b>#</b></font></td>\n";
183
print "<td width=20> </td>\n";
184
print "<td align=center><font size=4><b>Hostname</b></font></td>\n";
186
print "<td width=20> </td>\n";
187
print "<td align=center><font size=4><b>Hw Address</b></font></td>\n";
189
print "<td width=20> </td>\n";
190
print "<td align=center><font size=4><b>IP Address</b></font></td>\n";
191
print "<td width=10> </td></tr>";
194
print "<tr bgcolor=$headerbg>\n";
196
print "<td width=10> </td>\n";
197
print "<td align=left><font size=4><b>#</b></font></td>\n";
199
print "<td width=20> </td>\n";
200
print "<td align=center><font size=4><b><a href=$sort1>Hostname</a></b></font></td>\n";
202
print "<td width=20> </td>\n";
203
print "<td align=center><font size=4><b><a href=$sort2>Hw Address</a></b></font></td>\n";
205
print "<td width=20> </td>\n";
206
print "<td align=center><font size=4><b><a href=$sort4>IP Address</a></b></font></td>\n";
207
print "<td width=10> </td></tr>";
211
foreach $key (@sub_sorted) {
213
my $host = ${$key}{'hostname'};
214
$host =~ s/ /\ /g; # replace blanks for html rendering
216
# bgcolor - separator - value(s) - separator
217
print "<tr bgcolor=$bgcolors[(++ $j) % 2]>\n";
218
print "<td width=10> </td>\n";
219
print "<td align=center>$no</td>\n"; # itemno
220
print "<td width=20> </td>\n";
221
print "<td align=left>$host </td>\n"; # Hostname
222
print "<td width=20> </td>\n";
223
print "<td align=left>${$key}{'hwaddress'} </td>\n"; # Hw Address
224
print "<td width=20> </td>\n";
225
print "<td align=left>${$key}{'ipaddress'} </td>\n"; # IP Address
226
print "<td width=10> </td>\n";
230
} # next item on the same section
236
$current += $items_per_section;
238
} # next sorting type
249
# print HTML header and title
252
my $titlepage = shift;
255
# immediately start printing the magic line which tells to the world
256
# we are playing with an HTML document
258
# print "Content-type: text/html\n\n";
260
print "Content-type: text/html\n";
261
print "Cache-Control: no-cache\n";
262
print "Expires: 0\n\n";
266
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n",
268
"<!-- ntop statistic page -->\n",
269
"<!-- Rocco Carbone <rocco\@ntop.org> -->\n",
271
# "<meta http-equiv=refresh content=120>\n",
272
"<link rel=stylesheet href=/style.css type=\"text/css\">\n",
273
"<meta http-equiv=Pragma content=no-cache>\n",
274
"<meta http-equiv=Cache-Control content=no-cache>\n",
275
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">",
276
"<meta name=\"robots\" content=\"ntop statistics\">\n",
277
"<meta name=\"keywords\" content=\"ntop\">\n",
279
"<title>$titlepage</title>\n";
284
# start printing HTML body
286
sub html_start_body {
289
print "<body bgcolor=#ffffff text=#000000 link=#1111cc vlink=#cc0000 alink=#888888>\n";
291
print "<table border=0 cellpadding=1 cellspacing=2>\n";
293
print "<td align=center>\n";
294
print "<a href=\"/plugins/pep?available.pl\">all available scripts</a>\n";
296
print "<td width=15></td>\n";
297
print "<td align=center>\n";
305
# finish printing HTML body (aka footer)
309
print "Generated by $__progname__ version $__version__\n";
311
print "<table border=0 cellpadding=1 cellspacing=2>\n";
313
print "<td align=left>\n";
314
print "<font size=2>\n";
315
print "Questions or comments: <a href=\"mailto:rocco\@ntop.org\"</a><br>\n";
316
print "© 2000 by Rocco Carbone - Updated $__updated__\n";
328
my $ip1 = ${$a}{'ipaddress'};
329
my $ip2 = ${$b}{'ipaddress'};
331
my ($a1, $b1, $c1, $d1) = split (/\./, $ip1);
332
my ($a2, $b2, $c2, $d2) = split (/\./, $ip2);
334
$ip1 = sprintf ("%03d%03d%03d%03d", $a1, $b1, $c1, $d1);
335
$ip2 = sprintf ("%03d%03d%03d%03d", $a2, $b2, $c2, $d2);