File: FlowScan.pm

package info (click to toggle)
flowscan 1.006-13.2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 732 kB
  • ctags: 185
  • sloc: sh: 1,670; perl: 1,518; makefile: 148
file content (335 lines) | stat: -rw-r--r-- 8,696 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#  FlowScan.pm - a base class for scanning and reporting on flows
#  Copyright (C) 1998-2001  Dave Plonka
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# $Id: FlowScan.pm,v 1.6 2001/03/27 20:48:01 dplonka Exp $
# Dave Plonka <plonka@doit.wisc.edu>

use strict;
use RRDs;

package FlowScan;

require 5;
require Exporter;

@FlowScan::ISA=qw(Exporter);
@FlowScan::EXPORT_OK=qw(ip2name);
# convert the RCS revision to a reasonable Exporter VERSION:
'$Revision: 1.6 $' =~ m/(\d+)\.(\d+)/ && (( $FlowScan::VERSION ) = sprintf("%d.%03d", $1, $2));

=head1 NAME

FlowScan -

=head1 SYNOPSIS

   $ flowscan FlowScanDerivedClass [...]

=head1 DESCRIPTION

This package implements a base-class solely for use with the flowscan utility.
Once you author derived classes, those class names are passed as arguments.

The following methods and subroutines are defined:

=over 4

=cut

=item new

The B<new> method constructs and returns a B<FlowScan> object.
You must define a report method in your derived class.

=cut

sub new {
   die "you must define a new method in your derived class!\n"
}

=item wanted

You must define a report method in your derived class.

=cut

sub wanted {
   die "you must define a wanted method in your derived class!\n"
}

=item perfile

You may define a perfile method in your derived class.
To maintain the functionality of the base-class method, do something like this:

   sub perfile {
      my $self = shift;
      $self->SUPER::perfile(@_);
      # ...
   }

=cut

sub perfile {
   my $self = shift;
   my $file = shift;
   $self->{filetime} = file2time_t($file)
}

sub file2time_t {
   my $file = shift;
   if ($file =~
      m/(\d\d\d\d)-?(\d\d)-?(\d\d)[_.](\d\d):?(\d\d):?(\d\d)([+-])(\d\d)(\d\d)/) {
      # The file name contains an "hours east of GMT" component
      my(@tm) = ($6, $5, $4, $3, $2-1, $1-1900, 0, 0, -1);
      my($tm_sec, $tm_min, $tm_hour, $tm_mday, $tm_mon, $tm_year,
	 $tm_wday, $tm_yday, $tm_isdst) = (0 .. 8); # from "man perlfunc"
      if ('+' eq $7) { # subtract hours and minutes to get UTC
	 $tm[$tm_min] -= 60*$8+$9
      } else { # add hours and minutes to get UTC
	 $tm[$tm_min] += 60*$8+$9
      }
      mutt_normalize_time(@tm);
      return mutt_mktime(@tm, -1, 0)
   } elsif ($file =~ m/(\d\d\d\d)-?(\d\d)-?(\d\d)[_.](\d\d):?(\d\d):?(\d\d)$/) {
      # The file name contains just the plain old localtime
      return mutt_mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0, -1, 1)
   } else {
      return -1
   }
   # NOTREACHED
}

sub mkdirs_as_necessary {
   my $n = 0;
   foreach my $file (@_) {
      my $pos = 0;
      my $len;
      while (-1 < ($len = index($file, '/', $pos))) {
	 $len++;
         my $dir = substr($file, 0, $len);
         $pos = $len;
	 next if -d $dir;
         if (!mkdir($dir, 0777)) {
            warn "mkdir \"$dir\": $!\n";
            return 0
         }
         $n++;
      }
   }
   return $n # no. of successful mkdir(2)s
}

sub createGeneralRRD {
   my $self = shift;
   die unless ref($self);
   my $file = shift;

   die unless @_; # DS types and names are required

   my $time_t = $self->{filetime};

   my $startwhen = $time_t - 300;
   my($name, $type, @DS);
   while (($type = shift(@_)) &&
	  ($name = shift(@_))) {
      push(@DS, "DS:${name}:${type}:400:U:U")
   }
   RRDs::create($file,
      '--start', $startwhen,
      '--step', 300,
      @DS,
      qw(
         RRA:AVERAGE:0:1:600
         RRA:AVERAGE:0:6:600
         RRA:AVERAGE:0:24:600
         RRA:AVERAGE:0:288:1827
         RRA:MAX:0:24:600
         RRA:MAX:0:288:1827
	 )
      );
   my $err=RRDs::error;
   warn "ERROR creating $file: $err\n" if $err;
}

=item report

You must define a report method in your derived class.

=cut

sub report {
   die "you must define a report method in your derived class!\n"
}

=head1 BUGS

=head1 AUTHOR

Dave Plonka <plonka@doit.wisc.edu>

Copyright (C) 1998-2001  Dave Plonka.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=head1 VERSION

The version number is the module file RCS revision number (B<$Revision: 1.6 $>)
with the minor number printed right justified with leading zeroes to 3
decimal places.  For instance, RCS revision 1.1 would yield a package version
number of 1.001.

This is so that revision 1.10 (which is version 1.010), for example, will
test greater than revision 1.2 (which is version 1.002) when you want to
B<require> a minimum version of this module.

=cut

# The following routines are my rewrites from mutt's "date.c", which is:
# 
#  Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
# 
#      This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; either version 2 of the License, or
#      (at your option) any later version.
# 
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
# 
#      You should have received a copy of the GNU General Public License
#      along with this program; if not, write to the Free Software
#      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 

# returns the seconds east of UTC given `g' and its corresponding gmtime()
# representation
sub compute_tz {
   my($g, @utc) = @_;
   my @lt = localtime($g);
   my $t;
   my $yday;

   my($tm_hour, $tm_min, $tm_yday) = (2, 1, 7); # from "man perlfunc"
   $t = ((($lt[$tm_hour] - $utc[$tm_hour]) * 60) +
         ($lt[$tm_min] - $utc[$tm_min])) * 60;

   if ($yday = ($lt[$tm_yday] - $utc[$tm_yday])) {
      # This code is optimized to negative timezones (West of Greenwich)
      if ($yday == -1 ||   # UTC passed midnight before localtime
          $yday > 1) {     # UTC passed new year before localtime
         $t -= 24 * 60 * 60
      }
      else {
         $t += 24 * 60 * 60
      }
   }

  return $t
}

# converts struct tm to time_t, but does not take the local timezone into
# account unless ``local'' is nonzero
sub mutt_mktime {
  my $local = pop(@_);
  my(@t) = @_;
  my $g;

  my @AccumDaysPerMonth = (
    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  );

  my($tm_sec, $tm_min, $tm_hour, $tm_mday, $tm_mon,
     $tm_year, $tm_wday, $tm_yday, $tm_isdst) = (0 .. 8); # from "man perlfunc"
  # Compute the number of days since January 1 in the same year
  $g = $AccumDaysPerMonth[$t[$tm_mon] % 12];

  # The leap years are 1972 and every 4. year until 2096,
  # but this algoritm will fail after year 2099
  $g += $t[$tm_mday];
  if (($t[$tm_year] % 4) || $t[$tm_mon] < 2) {
    $g--
  }
  $t[$tm_yday] = $g;

  # Compute the number of days since January 1, 1970
  $g += ($t[$tm_year] - 70) * 365;
  $g += int(($t[$tm_year] - 69) / 4);

  # Compute the number of hours
  $g *= 24;
  $g += $t[$tm_hour];

  # Compute the number of minutes
  $g *= 60;
  $g += $t[$tm_min];

  # Compute the number of seconds
  $g *= 60;
  $g += $t[$tm_sec];

  if ($local) {
    $g -= compute_tz($g, @t);
  }

  return($g)
}

sub mutt_normalize_time {
  my @DaysPerMonth = (
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  );

  my($tm_sec, $tm_min, $tm_hour, $tm_mday, $tm_mon,
     $tm_year, $tm_wday, $tm_yday, $tm_isdst) = (0 .. 8); # from "man perlfunc"
  while ($_[$tm_sec] < 0)
  {
    $_[$tm_sec] += 60;
    $_[$tm_min]--
  }
  while ($_[$tm_min] < 0)
  {
    $_[$tm_min] += 60;
    $_[$tm_hour]--
  }
  while ($_[$tm_hour] < 0)
  {
    $_[$tm_hour] += 24;
    $_[$tm_mday]--
  }
  while ($_[$tm_mon] < 0)
  {
    $_[$tm_mon] += 12;
    $_[$tm_year]--
  }
  while ($_[$tm_mday] < 0)
  {
    if ($_[$tm_mon]) {
      $_[$tm_mon]--
    } else {
      $_[$tm_mon] = 11;
      $_[$tm_year]--
    }
    $_[$tm_mday] += $DaysPerMonth[$_[$tm_mon]]
  }
}

1