/[pxelator]/lib/PXElator/syslogd.pm
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 /lib/PXElator/syslogd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 247 - (show annotations)
Tue Aug 18 08:57:47 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 1524 byte(s)
added grouping category to syslog
1 package syslogd;
2
3 use warnings;
4 use strict;
5
6 use IO::Socket;
7 use Data::Dump qw/dump/;
8 use CouchDB;
9
10 use server;
11
12 our $port = 514;
13 our $MAXLEN = 1524;
14
15 sub message {
16 my $sock = shift;
17
18 my $buf;
19 $sock->recv($buf, $MAXLEN);
20
21 next unless $buf;
22
23 my ($port, $ipaddr) = sockaddr_in($sock->peername);
24 my $log = {
25 ip => join('.', unpack('C4',$ipaddr)),
26 buf => $buf,
27 };
28
29 if ( $buf =~ s/<(\d+)>// ) {
30 $log->{pri} = $1 % 8;
31 $log->{facility} = ( $1 - $log->{pri} ) / 8;
32
33 $log->{timestamp} = $1 if $buf =~ s/^(\w\w\w\s+\d+\s+\d\d:\d\d:\d\d)\s*//; # strip timestamp which some syslog servers insert here
34
35 if ( $buf =~ s/^([^:]+)\s*:\s*// ) {
36 my $tag = $1;
37 if ( $tag =~ m{^(\S+)\s(\S+)} ) {
38 $log->{tag} = $2;
39 $log->{hostname} = $1;
40 } else {
41 $log->{tag} = $tag;
42 }
43
44 if ( $log->{tag} =~ s/\[(\d+)\]$// ) {
45 $log->{pid} = $1;
46 } elsif ( $buf =~ s/^(\d+):\s*// ) {
47 $log->{pid} = $1;
48 }
49 }
50
51 if ( $log->{tag} =~ m{CRON}i && $buf =~ m{^\((\w+)\) (.+) \((.+)\)$} ) {
52 $log->{cron} = {
53 user => $1,
54 command => $2,
55 argument => $3,
56 };
57 }
58
59 if ( $buf =~ m{(init|error|mount|smart|usb|fs)}i ) {
60 $log->{category} = $1;
61 }
62
63 $log->{message} = $buf;
64 }
65
66 warn "log ",dump( $log );
67 CouchDB::audit( 'syslog', $log );
68 }
69
70 sub start {
71
72 my $sock = IO::Socket::INET->new(
73 LocalPort => $port,
74 Proto => 'udp',
75 ReuseAddr => 1,
76 ) || die "can't listen to $port: $!";
77
78 CouchDB::audit('start', { port => $port });
79
80 while(1) {
81 message($sock);
82 server->refresh;
83 }
84 }
85
86 1;

  ViewVC Help
Powered by ViewVC 1.1.26