/[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

Contents of /sysplogd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sat Apr 11 11:45:36 2009 UTC (12 years, 3 months ago) by dpavlin
File size: 2507 byte(s)
extract configuration into conf.pl

1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use IO::Socket;
7 use Data::Dump qw/dump/;
8 use DBI;
9 use Getopt::Long;
10
11 our $port = 514;
12 our $MAXLEN = 1524;
13
14 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
94
95 my $sock = IO::Socket::INET->new(
96 LocalPort => $port,
97 Proto => 'udp'
98 # ReuseAddr => 1,
99 ) || die "can't listen to $port: $!";
100
101 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 = '';
111 my $buf;
112 while(1) {
113 $sock->recv($buf, $MAXLEN);
114 my ($port, $ipaddr) = sockaddr_in($sock->peername);
115 my $hostname = gethostbyaddr($ipaddr, AF_INET);
116 my $ip = join('.', unpack('C4',$ipaddr));
117 my @values = ( $ip, $hostname, $buf );
118
119 if ( $buf =~ /<(\d+)>\s*(\S*)\s*:\s*(.*)/ ) {
120 $values[2] = $3;
121 my $level = $1 % 8;
122 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 }

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26