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

Legend:
Removed from v.5  
changed lines
  Added in v.11

  ViewVC Help
Powered by ViewVC 1.1.26