~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to ntop/plugins/pep/arps.pl

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# arps.pl - html generator for ntop's ARP Cache.
3
 
#
4
 
# Copyright (C) 2000 Rocco Carbone <rocco@ntop.org>
5
 
#
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.
10
 
#
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.
15
 
#
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.
20
 
#
21
 
 
22
 
#
23
 
# the %traffic hash keeps all information of interest
24
 
# for the purpose of reporting statistics access
25
 
#
26
 
# They include:
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
36
 
 
37
 
#
38
 
# useful variables
39
 
#
40
 
$__progname__   = "arps.pl";
41
 
$__version__    = "0.0.2";
42
 
$__updated__    = "10/11/2000";
43
 
$__boottime__   = time();
44
 
$__bootdate__   = localtime($__boottime__);
45
 
 
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";
51
 
 
52
 
 
53
 
my $infobg      = "\"#cccccc\"";
54
 
my $headerbg    = "\"#ccccff\"";
55
 
my @bgcolors    = ("\"#ffffff\"", "\"#eeeeee\"");
56
 
my $j = 0;
57
 
 
58
 
#
59
 
# useful variables
60
 
#
61
 
 
62
 
# The unsorted hash of ntop traffic cache (global hash)
63
 
my @traffic = sort keys %traffic;
64
 
 
65
 
#
66
 
# other useful variables
67
 
#
68
 
@reporttypes = ("hwaddress") unless @reporttypes;
69
 
@sorttypes = ("ascending") unless @sorttypes;
70
 
my $reporttype;
71
 
my $sorttype;
72
 
 
73
 
 
74
 
#
75
 
# perform all needed ascending and descending sorting
76
 
#
77
 
 
78
 
#
79
 
# sort hw address alphabetically and ip address alphabetically
80
 
#
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;
84
 
 
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;
88
 
 
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
92
 
 
93
 
 
94
 
if ($total_items % $items_per_section) {
95
 
  $sections++;
96
 
}
97
 
 
98
 
select STDOUT;
99
 
 
100
 
#
101
 
# print HTML header and title
102
 
#
103
 
html_header($htmltitle);
104
 
 
105
 
#
106
 
# start printing HTML body
107
 
#
108
 
html_start_body($bodyname);
109
 
 
110
 
#
111
 
# print report's title with its brief description
112
 
#
113
 
print "<center><font size=6><b>$description</b></font></table>\n";
114
 
 
115
 
foreach $reporttype (@reporttypes) {
116
 
 
117
 
  my $sorted_by     = "sorted_by_$reporttype";
118
 
  my $sorted_by_rev = "sorted_by_rev_$reporttype";
119
 
 
120
 
  my @sorted     = eval("\@$sorted_by");
121
 
  my @sorted_rev = eval("\@$sorted_by_rev");
122
 
 
123
 
 
124
 
  foreach $sorttype (@sorttypes) {
125
 
    my $current = 0;
126
 
 
127
 
    for ($section = 1; $section <= $sections; $section++) {
128
 
      # the sub array for this section
129
 
      my @sub_sorted;
130
 
 
131
 
      #
132
 
      # useful calculations before starting playing with sections
133
 
      #
134
 
      my $last = $current + $items_per_section - 1;
135
 
 
136
 
      if ($last >= $total_items) {
137
 
        $last = $total_items - 1;
138
 
      }
139
 
      my $itemno = $last - $current + 1;
140
 
 
141
 
      #
142
 
      # isolate the sub array
143
 
      #
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";
148
 
 
149
 
        @sub_sorted = @sorted[$current..$last];
150
 
      }
151
 
      else {
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";
155
 
 
156
 
        @sub_sorted = @sorted_rev[$current..$last];
157
 
      }
158
 
 
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>&nbsp;</td><tr bgcolor=$bgcolors[(++ $j) % 2]><td>&nbsp;</td>";
162
 
      print "<td colspan=15>Displaying ";
163
 
      print $itemno;
164
 
      print " items [";
165
 
      print $current + 1;
166
 
      print "-";
167
 
      print $last + 1;
168
 
      print "] of $total_items sorted by $sorttype $reporttype</td></tr>\n";
169
 
      print "<tr bgcolor=$bgcolors[(++ $j) % 2]>\n";
170
 
      print "</tr>\n";
171
 
      print "<tr><td>&nbsp;</td>\n";
172
 
      print "</table>\n";
173
 
 
174
 
      print "<table align=center cellpadding=1 cellspacing=0 border=0>\n";
175
 
 
176
 
      # table header (column's name separated by blanks)
177
 
      if ($itemno == 1) {
178
 
        print "<tr bgcolor=$headerbg>\n";
179
 
        # itemno
180
 
        print "<td width=10>&nbsp;</td>\n";
181
 
        print "<td align=left><font size=4><b>#</b></font></td>\n";
182
 
        # Hostname
183
 
        print "<td width=20>&nbsp;</td>\n";
184
 
        print "<td align=center><font size=4><b>Hostname</b></font></td>\n";
185
 
        # Hw address
186
 
        print "<td width=20>&nbsp;</td>\n";
187
 
        print "<td align=center><font size=4><b>Hw&nbsp;Address</b></font></td>\n";
188
 
        # IP address
189
 
        print "<td width=20>&nbsp;</td>\n";
190
 
        print "<td align=center><font size=4><b>IP&nbsp;Address</b></font></td>\n";
191
 
        print "<td width=10>&nbsp;</td></tr>";
192
 
      }
193
 
      else {
194
 
        print "<tr bgcolor=$headerbg>\n";
195
 
        # itemno
196
 
        print "<td width=10>&nbsp;</td>\n";
197
 
        print "<td align=left><font size=4><b>#</b></font></td>\n";
198
 
        # Hostname
199
 
        print "<td width=20>&nbsp;</td>\n";
200
 
        print "<td align=center><font size=4><b><a href=$sort1>Hostname</a></b></font></td>\n";
201
 
        # Hw address
202
 
        print "<td width=20>&nbsp;</td>\n";
203
 
        print "<td align=center><font size=4><b><a href=$sort2>Hw&nbsp;Address</a></b></font></td>\n";
204
 
        # IP address
205
 
        print "<td width=20>&nbsp;</td>\n";
206
 
        print "<td align=center><font size=4><b><a href=$sort4>IP&nbsp;Address</a></b></font></td>\n";
207
 
        print "<td width=10>&nbsp;</td></tr>";
208
 
      }
209
 
 
210
 
      $no = $current + 1;
211
 
      foreach $key (@sub_sorted) {
212
 
 
213
 
        my $host = ${$key}{'hostname'};
214
 
           $host =~ s/ /\&nbsp;/g; # replace blanks for html rendering
215
 
 
216
 
        # bgcolor - separator - value(s) - separator
217
 
        print "<tr bgcolor=$bgcolors[(++ $j) % 2]>\n";
218
 
        print "<td width=10>&nbsp;</td>\n";
219
 
        print "<td align=center>$no</td>\n";                        # itemno
220
 
        print "<td width=20>&nbsp;</td>\n";
221
 
        print "<td align=left>$host&nbsp;</td>\n";                  # Hostname
222
 
        print "<td width=20>&nbsp;</td>\n";
223
 
        print "<td align=left>${$key}{'hwaddress'}&nbsp;</td>\n";   # Hw Address
224
 
        print "<td width=20>&nbsp;</td>\n";
225
 
        print "<td align=left>${$key}{'ipaddress'}&nbsp;</td>\n";   # IP Address
226
 
        print "<td width=10>&nbsp;</td>\n";
227
 
        print "</tr>\n";
228
 
 
229
 
        $no++;
230
 
      } # next item on the same section
231
 
 
232
 
      # end table
233
 
      print
234
 
        "</table><p>";
235
 
 
236
 
      $current += $items_per_section;
237
 
    }   # next section
238
 
  }     # next sorting type
239
 
}       # next report type
240
 
 
241
 
#
242
 
# finish HTML output
243
 
#
244
 
html_stop_body();
245
 
 
246
 
 
247
 
 
248
 
#
249
 
# print HTML header and title
250
 
#
251
 
sub html_header {
252
 
  my $titlepage = shift;
253
 
 
254
 
  #
255
 
  # immediately start printing the magic line which tells to the world
256
 
  # we are playing with an HTML document
257
 
  #
258
 
  # print "Content-type: text/html\n\n";
259
 
 
260
 
  print "Content-type: text/html\n";
261
 
  print "Cache-Control: no-cache\n";
262
 
  print "Expires: 0\n\n";
263
 
 
264
 
 
265
 
  print
266
 
    "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n",
267
 
    "<html>\n",
268
 
    "<!-- ntop statistic page -->\n",
269
 
    "<!-- Rocco Carbone <rocco\@ntop.org> -->\n",
270
 
    "<head>\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",
278
 
    "</head>\n",
279
 
    "<title>$titlepage</title>\n";
280
 
}
281
 
 
282
 
 
283
 
#
284
 
# start printing HTML body
285
 
#
286
 
sub html_start_body {
287
 
  my $title = shift;
288
 
 
289
 
  print "<body bgcolor=#ffffff text=#000000 link=#1111cc vlink=#cc0000 alink=#888888>\n";
290
 
  print "<center>\n";
291
 
  print "<table border=0 cellpadding=1 cellspacing=2>\n";
292
 
  print "<tr>\n";
293
 
  print "<td align=center>\n";
294
 
  print "<a href=\"/plugins/pep?available.pl\">all available scripts</a>\n";
295
 
  print "</td>\n";
296
 
  print "<td width=15></td>\n";
297
 
  print "<td align=center>\n";
298
 
  print "</td>\n";
299
 
  print "</tr>\n";
300
 
  print "</table>\n";
301
 
}
302
 
 
303
 
 
304
 
#
305
 
# finish printing HTML body (aka footer)
306
 
#
307
 
sub html_stop_body {
308
 
  print "<p>\n";
309
 
  print "Generated by $__progname__ version $__version__\n";
310
 
  print "</p>\n";
311
 
  print "<table border=0 cellpadding=1 cellspacing=2>\n";
312
 
  print "<tr>\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 "&copy; 2000 by Rocco Carbone - Updated $__updated__\n";
317
 
  print "</font>\n";
318
 
  print "</td>\n";
319
 
  print "</tr>\n";
320
 
  print "</table>\n";
321
 
  print "</center>\n";
322
 
  print "</body>\n";
323
 
  print "</html>\n";
324
 
}
325
 
 
326
 
 
327
 
sub ipnumerically {
328
 
  my $ip1 = ${$a}{'ipaddress'};
329
 
  my $ip2 = ${$b}{'ipaddress'};
330
 
 
331
 
  my ($a1, $b1, $c1, $d1) = split (/\./, $ip1);
332
 
  my ($a2, $b2, $c2, $d2) = split (/\./, $ip2);
333
 
 
334
 
  $ip1 = sprintf ("%03d%03d%03d%03d", $a1, $b1, $c1, $d1);
335
 
  $ip2 = sprintf ("%03d%03d%03d%03d", $a2, $b2, $c2, $d2);
336
 
 
337
 
  $ip1 <=> $ip2;
338
 
}
339
 
 
340
 
 
341
 
1;