/[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 1 by dpavlin, Fri Apr 10 17:49:49 2009 UTC revision 12 by dpavlin, Sat Apr 11 11:45:36 2009 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use IO::Socket;  use IO::Socket;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8    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    require 'conf.pl' if -e 'conf.pl';
19    
20    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  print "INFO: listen on $port",$/;  open(my $log_fh, '>>', $log) || die "can't open log $log: $!";
102    $log_fh->autoflush(1);
103    sub _log {
104            warn 'LOG ',dump( @_ ), $/ if $debug;
105            print $log_fh time() . '|' . join('|', @_), $/;
106    }
107    
108    _log "INFO: listen on $port";
109    
110  my $rin = '';  my $rin = '';
111  my $buf;  my $buf;
# Line 31  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          if ( $buf=~/<(\d+)>(.*?):(.*)/ ) {  
119                  my $pri=$1;          if ( $buf =~ /<(\d+)>\s*(\S*)\s*:\s*(.*)/ ) {
120                  my $head=$2;                  $values[2] = $3;
121                  my $msg=$3;                  my $level    = $1 % 8;
122                  my $sev=$pri % 8;                  my $facility = ( $1-$level ) / 8;
123                  my $fac=($pri-$sev) / 8;                  my $program  = $2;
124                  print dump( $ip, $port, $hostname, $pri, $head, $msg, $sev, $fac, $facilities[$fac] ),$/;                  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.1  
changed lines
  Added in v.12

  ViewVC Help
Powered by ViewVC 1.1.26