File: flowscan.in

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 (174 lines) | stat: -rwxr-xr-x 5,156 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
#! @PERL_PATH@

#  flowscan - a utility to analyze and report on Cflowd flow files
#  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.in,v 1.20 2001/02/16 21:17:26 dplonka Exp $
# Dave Plonka <plonka@doit.wisc.edu>

require 5.004; # for UNIVERSAL::can method
use FindBin;
use Cflow qw(:flowvars 1.017);
use Benchmark;
use Getopt::Std;
use POSIX; # for strftime
use File::Basename;
use ConfigReader::DirectiveStyle;
use lib $FindBin::Bin;
use FlowScan; # for mutt_mktime, etc.

'$Revision: 1.20 $' =~ m/(\d+)\.(\d+)/ && (( $VERSION ) = sprintf("%d.%03d", $1, $2));

# Set the default options from the configuration file:
$c = new ConfigReader::DirectiveStyle;
$c->directive('Verbose');
$c->directive('WaitSeconds');
$c->required('FlowFileGlob');
$c->required('ReportClasses');
#$c->load("${FindBin::Bin}/${FindBin::Script}.cf");
$c->load("/etc/${FindBin::Script}/${FindBin::Script}.cf");
$flowfileglob = $c->value('FlowFileGlob');
$opt_w = $c->value('WaitSeconds');
$opt_v = $c->value('Verbose');
@classes = split(m/\s*,\s*/, $c->value('ReportClasses'));

if (!getopts('hvw:g:s:') || $opt_h) {
   print STDERR <<_EOF_
usage: $FindBin::Script [-hv] [-w secs] [-s bytes] FlowScanClass [...]
       -g - use this glob (file pattern match) when looking for raw
	    flow files to be processed.  Defaults to: '$flowfileglob'
	    (mnemonic: 'g'lob)
       -h - shows this usage information (mnemonic: 'h'elp)
       -v - verbose - show warnings (mnemonic: 'v'erbose)
       -w secs - process the flow files, and wait secs seconds for new ones
		 to appear.  Flow file will be globbed using
		 "$flowfileglob".  (Don't pass flow file names as arguments
		 when using this option.)
       -s bytes - skip processing of files of size greater than bytes
		  (mnemonic: 's'kip 's'ize)
_EOF_
   ;
   exit($opt_h? 0 : 2)
}

if ($opt_g) {
   $flowfileglob = $opt_g
}

if (@ARGV) {
   @classes = @ARGV
}

foreach my $class (@classes) {
   eval "use $class";
   die "$@" if $@
}

Cflow::verbose($opt_v);

   while (1) {
      my @files = sort by_timestamp <${flowfileglob}>;
      if (@files) {
         my $file;
	 foreach $file (@files) {
	    my @s = stat($file);
	    my $dirname = dirname $file;
	    my $basename = basename $file;

	    if (!$opt_s || $s[7] <= $opt_s) {

            foreach (@classes) {
               push(@objects, $_->new || die "$_->new failed\n")
	    }
	    # note that "perfile" sets $router which is used by "wanted"...
	    my $t0 = new Benchmark;
	    my $size = 0;
	    foreach ($file) {
	       if (@_ = stat) {
	          $size += $_[7]
	       }
	    }
            my $result = Cflow::find(\&wanted, \&perfile, $file);
	    my $t1 = new Benchmark;
	    warn(strftime("%Y/%m/%d %H:%M:%S", localtime),
		 " $FindBin::Script-$VERSION @classes: Cflow::find took ",
		 timestr(timediff($t1, $t0)),
		 " for $size flow file bytes, flow hit ratio: ${result}\n")
		    if $opt_v;
            &report;
	    my $t2 = new Benchmark;
	    warn(strftime("%Y/%m/%d %H:%M:%S", localtime),
		 " $FindBin::Script-$VERSION @classes: report took ",
		 timestr(timediff($t2, $t1)), "\n") if $opt_v;
	    } else {
               warn(strftime("%Y/%m/%d %H:%M:%S", localtime),
	            " skipping file ${file} of size $s[7] bytes.\n") if $opt_v;
	    }

	    if (-d "$dirname/saved") {
	       goto ok_label if rename($file, "$dirname/saved/$basename");
               warn strftime("%Y/%m/%d %H:%M:%S", localtime),
		  " rename \"$file\", \"$dirname/saved/$basename\": $!\n";
	    }
	    if (!unlink($file)) {
               warn strftime("%Y/%m/%d %H:%M:%S", localtime),
		  " unlink \"$file\": $!\n";
	    }
ok_label:
	    # &zero # clear out the totals
	    @objects = () # DESTROY all objects
	 }
      } else {
	 last unless $opt_w;
	 warn("sleep ${opt_w}...\n") if ($opt_v);
         sleep $opt_w;
      }
   }

exit 0;

sub perfile {
   my $file = shift;
   warn(strftime("%Y/%m/%d %H:%M:%S", localtime),
	" working on file ${file}...\n") if $opt_v;
   foreach (@objects) {
      next unless ($_->can('perfile'));
      $_->perfile($file)
   }
}

sub wanted {
   my $rv = 0; # boolean return value (for "hit ratio" feature of Cflow)
   foreach (@objects) {
      next unless ($_->can('wanted'));
      if ($_->wanted) {
	 $rv = 1
      }
   }
   return $rv
}

sub report {
   foreach (@objects) {
      next unless ($_->can('report'));
      $_->report
   }
}

sub by_timestamp {
   FlowScan::file2time_t($a) <=> FlowScan::file2time_t($b)
}