/[sysplogd]/sysplogd
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sysplogd

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by dpavlin, Fri Apr 10 21:22:01 2009 UTC revision 12 by dpavlin, Sat Apr 11 11:45:36 2009 UTC
# Line 6  use strict; Line 6  use strict;
6  use IO::Socket;  use IO::Socket;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use DBI;  use DBI;
9    use Getopt::Long;
10    
11  my $port = 514;  our $port = 514;
12    our $MAXLEN = 1524;
13    
14  my $MAXLEN = 1524;  our $dsn    = 'DBI:Pg:dbname=syslog';
15    our $user   = 'dpavlin';
16    our $log    = '/tmp/sysplog.log';
17    
18  my $dsn = 'DBI:Pg:dbname=syslog';  require 'conf.pl' if -e 'conf.pl';
19  my $user = 'dpavlin';  
20  my $debug = 0;  my $debug  = 0;
21    my $schema = 0;
22    
23    GetOptions(
24            'debug+'  => \$debug,
25            'schema!' => \$schema,
26            'log=s'   => \$log,
27            'port=i'  => \$port,
28    ) || die "usage: $0 --debug --schema\n";
29    
30    our $VERSION = '0.00';
31    
32    my $sql_schema = q{
33    
34    CREATE TABLE facilities (
35            id              serial,
36            name            text,
37    
38            PRIMARY KEY(name)
39    );
40    
41    CREATE TABLE log (
42            id              serial,
43            timestamp       timestamp default now(),
44            ip              inet not null,
45            hostname        text not null,
46            message         text,
47            level           int,
48            facility        int,
49            program         text,
50            pid             int,
51    
52            PRIMARY KEY (id)
53    );
54    
55    };
56    
57    
58    my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
59    
60    if ( $schema ) {
61            $dbh->begin_work;
62    
63            $dbh->do( $_ ) foreach split(/;/, $sql_schema);
64    
65            my $sth = $dbh->prepare( q{
66                    insert into facilities (name) values (?)
67            });
68    
69            $sth->execute( $_ ) foreach ( qw/
70                    kernel user mail system security internal
71                    printer news uucp clock
72                    security2
73                    ftp ntp
74                    audit alert
75                    clock2
76                    local0 local1 local2 local3 local4 local5 local6 local7
77            / );
78    
79            warn "# created sql schema\n";
80    
81            $dbh->commit;
82    }
83    
84    my $sth_log_full = $dbh->prepare(qq{
85            insert into log
86            (ip,hostname,message,level,facility,program,pid)
87            values (?,?,?,?,?,?,?)
88    });
89    
90    my $sth_log_unparsed = $dbh->prepare(qq{
91            insert into log (ip,hostname,messsage) values (?,?,?)
92    });
93    
 my @facilities = ( qw/  
 kernel user mail system security internal printer news uucp clock security2  
 FTP NTP audit alert clock2 local0 local1 local2 local3 local4 local5 local6 local7  
 / );  
94    
 # Start Listening on UDP port 514  
95  my $sock = IO::Socket::INET->new(  my $sock = IO::Socket::INET->new(
96          LocalPort => $port,          LocalPort => $port,
97          Proto => 'udp'          Proto => 'udp'
98  #       ReuseAddr => 1,  #       ReuseAddr => 1,
99  ) || die "can't listen to $port: $!";  ) || die "can't listen to $port: $!";
100    
101  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;  open(my $log_fh, '>>', $log) || die "can't open log $log: $!";
102    $log_fh->autoflush(1);
103  my $sth = $dbh->prepare(qq{  sub _log {
104          insert into log          warn 'LOG ',dump( @_ ), $/ if $debug;
105          (ip,hostname,facility,priority,level,program,message) values          print $log_fh time() . '|' . join('|', @_), $/;
106          (? ,?       ,?       ,?       ,?    ,?      ,?      )  }
 });  
107    
108  print "INFO: listen on $port",$/;  _log "INFO: listen on $port";
109    
110  my $rin = '';  my $rin = '';
111  my $buf;  my $buf;
# Line 44  while(1) { Line 114  while(1) {
114          my ($port, $ipaddr) = sockaddr_in($sock->peername);          my ($port, $ipaddr) = sockaddr_in($sock->peername);
115          my $hostname = gethostbyaddr($ipaddr, AF_INET);          my $hostname = gethostbyaddr($ipaddr, AF_INET);
116          my $ip = join('.', unpack('C4',$ipaddr));          my $ip = join('.', unpack('C4',$ipaddr));
117          warn "# ",dump( $port, $ipaddr, $hostname, $buf );          my @values = ( $ip, $hostname, $buf );
118    
119          if ( $buf=~/<(\d+)>(.*?):(.*)/ ) {          if ( $buf =~ /<(\d+)>\s*(\S*)\s*:\s*(.*)/ ) {
120                  my $level=$1 % 8;                  $values[2] = $3;
121                  my $fac=($1-$level) / 8;                  my $level    = $1 % 8;
122                  $sth->execute( $ip, $hostname, $fac, $1, $level, $2, $3 );                  my $facility = ( $1-$level ) / 8;
123                    my $program  = $2;
124                    my $pid      = $1 if $program =~ s/\[(\d+)\]$//;
125                    push @values, ( $level, $facility, $program, $pid );
126                    $sth_log_full->execute( @values );
127            } else {
128                    $sth_log_unparsed->execute( @values );
129          }          }
130            _log( @values );
131  }  }

Legend:
Removed from v.3  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26