5 |
|
|
6 |
use IO::Socket; |
use IO::Socket; |
7 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
8 |
#use StoreToMongoDB; |
use DBI; |
9 |
|
|
10 |
my $port = 514; |
my $port = 514; |
11 |
|
|
12 |
my $MAXLEN = 1524; |
my $MAXLEN = 1524; |
13 |
|
|
14 |
|
my $dsn = 'DBI:Pg:dbname=syslog'; |
15 |
|
my $user = 'dpavlin'; |
16 |
|
my $debug = 0; |
17 |
|
|
18 |
my @facilities = ( qw/ |
my @facilities = ( qw/ |
19 |
kernel user mail system security internal printer news uucp clock security2 |
kernel user mail system security internal printer news uucp clock security2 |
20 |
FTP NTP audit alert clock2 local0 local1 local2 local3 local4 local5 local6 local7 |
FTP NTP audit alert clock2 local0 local1 local2 local3 local4 local5 local6 local7 |
27 |
# ReuseAddr => 1, |
# ReuseAddr => 1, |
28 |
) || die "can't listen to $port: $!"; |
) || die "can't listen to $port: $!"; |
29 |
|
|
30 |
|
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
31 |
|
|
32 |
|
my $sth = $dbh->prepare(qq{ |
33 |
|
insert into log |
34 |
|
(ip,hostname,facility,priority,level,program,message) values |
35 |
|
(? ,? ,? ,? ,? ,? ,? ) |
36 |
|
}); |
37 |
|
|
38 |
print "INFO: listen on $port",$/; |
print "INFO: listen on $port",$/; |
39 |
|
|
40 |
my $rin = ''; |
my $rin = ''; |
47 |
warn "# ",dump( $port, $ipaddr, $hostname, $buf ); |
warn "# ",dump( $port, $ipaddr, $hostname, $buf ); |
48 |
|
|
49 |
if ( $buf=~/<(\d+)>(.*?):(.*)/ ) { |
if ( $buf=~/<(\d+)>(.*?):(.*)/ ) { |
50 |
my $sev=$1 % 8; |
my $level=$1 % 8; |
51 |
my $fac=($1-$sev) / 8; |
my $fac=($1-$level) / 8; |
52 |
|
$sth->execute( $ip, $hostname, $fac, $1, $level, $2, $3 ); |
|
my $log = { |
|
|
ip => $ip, |
|
|
port => $port, |
|
|
hostname => $hostname, |
|
|
|
|
|
priority => $1, |
|
|
severity => $sev, |
|
|
facility => $fac, |
|
|
header => $2, |
|
|
message => $3, |
|
|
}; |
|
|
print dump( $log ),$/; |
|
|
#StoreToMongoDB->insert( $log ); |
|
53 |
} |
} |
54 |
} |
} |