Initial commit: Imported yaala 0.7.3.
[yaala.git] / lib / Yaala / Parser / Netacct.pm
1 package Yaala::Parser;
2 # FIXME
3
4 use strict;
5 use warnings;
6 use vars qw(%names %datafields);
7
8 use Exporter;
9 use Config qw#get_config read_config#;
10
11 die;
12
13 @Yaala::Parser::EXPORT_OK = qw#parse extra %datafields#;
14
15 @Yaala::Parser::ISA = ('Exporter');
16
17 print STDERR "\nparser/netacct: Using NET-ACCT format" if $::DEBUG;
18 # FIXME: pass month, date and hour in seconds to properly format and sort.
19
20 read_config ('netacct.config');
21 for (get_config ('alias'))
22 {
23         s/\s//g;
24         my ($name, $ips) = split (m/:/, $_);
25         my @ips = split (m/,/, $ips);
26
27         for (grep { m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ } @ips)
28         {
29                 $names{$_} = $name;
30         }
31 }
32
33 %datafields= (  
34     protocol        => 'key',
35     source          => 'key:host',
36     sourceport      => 'key',
37     destination     => 'key:host',
38     destinationport => 'key',
39     interface       => 'key',
40     user            => 'key',
41     month           => 'key',
42     date            => 'key',
43     hour            => 'key',
44     packetcount     => 'amount:number',
45     bytes           => 'amount:bytes',
46     connections     => 'amount:number'
47     );
48
49 # This needs to be done at runtime, since Data uses Setup which relies on
50 # %datafields to be defined  -octo
51 require Yaala::Data;
52 import Yaala::Data qw#store#;
53
54 return (1);
55
56 sub parse
57 {
58         my $line = shift or return undef;
59
60         my @data = split (/[\t\s]+/, $line, 10);
61
62 # Initialize the variables that we can get out of
63 # each line first..
64                 my ($epoch, $protocol, $source_ip, $source_port, $dest_ip,
65                         $dest_port, $packet_count, $data_size, $interface,
66                         $user) = @data;
67
68                 my ($hour, $day, $month, $year) = (localtime ($epoch))[2,3,4,5];
69                 ++$month; $year += 1900;
70                 my $date = sprintf ("%04u-%02u-%02u", $year, $month, $day);
71                 $hour = sprintf ("%02u", $hour);
72                 $month = sprintf ("%02u", $month);
73
74 # And now initialize all the variables we will use
75 # to get more information out of each field..
76
77                 if ($protocol == 1) { $protocol = 'ICMP'; }
78                 elsif ($protocol == 6) { $protocol = 'TCP'; }
79                 elsif ($protocol == 17) { $protocol = 'UDP'; }
80
81                 if (defined $names{$source_ip}) { $source_ip = $names{$source_ip}; }
82                 elsif ($source_ip eq '127.0.0.1') { $source_ip = 'localhost'; }
83                 elsif ($source_ip =~ /^192\.168\./) { $source_ip = 'lan'; }
84                 else { $source_ip = 'extern'; }
85                 
86                 if (defined $names{$dest_ip}) { $dest_ip = $names{$dest_ip}; }
87                 elsif ($dest_ip eq '127.0.0.1') { $dest_ip = 'localhost'; }
88                 elsif ($dest_ip =~ /^192\.168\./) { $dest_ip = 'lan'; }
89                 else { $dest_ip = 'extern'; }
90
91                 my %combined = (
92                                         'protocol'      =>      $protocol,
93                                         'source'        =>      $source_ip,
94                                         'sourceport'    =>      $source_port,
95                                         'destination'   =>      $dest_ip,
96                                         'destinationport'=>     $dest_port,
97                                         'packetcount'   =>      $packet_count,
98                                         'interface'     =>      $interface,
99                                         'user'          =>      $user,
100                                         'bytes'         =>      $data_size,
101                                         'hour'          =>      $hour,
102                                         'date'          =>      $date,
103                                         'month'         =>      $month,
104                                         'connections'   =>      1
105                                                                                 );
106                 store (\%combined);
107 }
108
109 sub extra
110 {
111         # foo
112 }
113
114 1;