Skip to content

Commit

Permalink
Make log monitor work with new setup - and use upstart to manage it
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Feb 22, 2012
1 parent cdc7319 commit 47c4dcc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 65 deletions.
112 changes: 49 additions & 63 deletions bin/vg-log-monitor
Expand Up @@ -14,10 +14,15 @@ use File::Tail;
use VegGuide::Config; use VegGuide::Config;
use VegGuide::JSON; use VegGuide::JSON;


my $file = '/var/log/apache2-backend/error.log'; use Getopt::Long;

my $debug;
GetOptions( 'debug' => \$debug );

my $file = '/var/log/vegguide/error.log';


my $interval = 5 * 60; my $interval = 5 * 60;
my $sleep = 60; my $sleep = ($debug ? 1 : 60);


my $hostname = VegGuide::Config->Hostname(); my $hostname = VegGuide::Config->Hostname();
my $from = 'log-monitor@' . $hostname; my $from = 'log-monitor@' . $hostname;
Expand All @@ -29,97 +34,77 @@ my $local_tz = DateTime::TimeZone->new( name => 'local' );


my $tail = _make_tail(); my $tail = _make_tail();


my $record = q{};
while ( defined( my $line = $tail->read() ) ) { while ( defined( my $line = $tail->read() ) ) {
if ( length $line ) { if ( length $line ) {
local $@; $record .= $line;
if ( eval { process_line($line) } ) { if ( $line =~ /^}/ ) {
next; warn $record if $debug;
} eval { process_record($record) };
else {
warn $@ if $@; warn $@ if $@;
$record = q{};
} }
} }
else {
{ {
local $@; local $@;
eval { send_if_pending(); }; eval { send_if_pending(); };
warn $@ if $@; warn $@ if $@;
}
} }


sleep $sleep; sleep $sleep;
} }


sub process_line { sub process_record {
my $line = shift; my $record = shift;

return if $line eq '';


my %message; my $error = VegGuide::JSON->Decode($record);


if ( $line =~ /^([^[].*)/ ) { my $text = 'URI: ' . $error->{uri};
$message{date} = DateTime->now( time_zone => $local_tz ); if ( $error->{user} ) {
$message{level} = 'warning'; $text .= "\n";
$message{text} = $1; $text .= 'User: ' . $error->{user};
} }
elsif ( $line =~ /^\[([^]]+)\] \[(error|warning)] vegguide: (.+)$/ ) {
$message{date}
= DateTime::Format::HTTP->parse_datetime( $1, $local_tz );
$message{level} = $2;

my $text = $3;
return if $text =~ /Software caused connection abort/;

$text =~ s/(?<!\\)\\n/\n/g;
$text =~ s/\\\\/\\/g;

$text =~ s/^\s+|\s+$//g;


if ( $text =~ /^{.+}$/s ) { if ( $error->{referer} ) {
my $error = VegGuide::JSON->Decode($text); $text .= "\n";

$text .= "Referer: $error->{referer}";
$text = 'URI: ' . $error->{uri};
if ( $error->{user} ) {
$text .= "\n";
$text .= 'User: ' . $error->{user};
}

if ( $error->{referer} ) {
$text .= "\n";
$text .= "Referer: $error->{referer}";
}

$text .= "\n";
$text .= $error->{error};

}

$message{text} = $text;
} }


return unless $message{text}; $text .= "\n";
$text .= $error->{error};


$message{text} =~ s/^\s*/ /gm; $text =~ s/^\s*/ /gm;


push @messages, \%message; my $dt = DateTime->from_epoch(
epoch => $error->{epoch},
time_zone => 'America/Denver',
);

push @messages, { date => $dt, text => $text };


return 1; return 1;
} }


sub send_if_pending { sub send_if_pending {
return unless @messages; return unless @messages;


warn 'We have ' . ( scalar @messages ) . " to send\n" if $debug;

my $now = time; my $now = time;


return if $now - $last_sent < $interval; my $elapsed = $now - $last_sent;

return if $elapsed < $interval;

warn "Sending messages - $elapsed seconds since last sent\n";


my $body = ''; my $body = '';


for my $msg (@messages) { for my $msg (@messages) {
$body .= "\n\n" if $body; $body .= "\n\n" if $body;


$body .= $msg->{date}->strftime('%Y-%m-%d %H:%M:%S') . q{ - } $body .= $msg->{date}->strftime('%Y-%m-%d %H:%M:%S');
. $msg->{level};
$body .= "\n"; $body .= "\n";
$body .= $msg->{text}; $body .= $msg->{text};
} }
Expand All @@ -145,8 +130,9 @@ sub send_if_pending {


sub _make_tail { sub _make_tail {
return File::Tail->new( return File::Tail->new(
name => $file, name => $file,
resetafter => 3600 * 8, maxinterval => $sleep,
nowait => 1, resetafter => 3600 * 8,
nowait => 1,
); );
} }
6 changes: 4 additions & 2 deletions lib/VegGuide/Plugin/ErrorHandling.pm
Expand Up @@ -70,10 +70,12 @@ sub _log_error {


return if $error =~ /Software caused connection abort/; return if $error =~ /Software caused connection abort/;


# XXX - change this later to log to the apache log?
# if ( $error =~ /unknown resource/ ) # if ( $error =~ /unknown resource/ )


my %error = ( uri => $self->request()->uri() . '' ); my %error = (
uri => $self->request()->uri() . '',
epoch => time(),
);


if ( my $user = $self->vg_user() ) { if ( my $user = $self->vg_user() ) {
$error{user} = $user->real_name(); $error{user} = $user->real_name();
Expand Down
12 changes: 12 additions & 0 deletions system/etc/init/vg-log-monitor.conf
@@ -0,0 +1,12 @@
description "VegGuide log monitor"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [016]

respawn
limit as 10485760 10485760

setuid www-data
setgid adm

/opt/perl5.14.2-no-threads/bin/perl /opt/perl5.14.2-no-threads/bin/vg-log-monitor

0 comments on commit 47c4dcc

Please sign in to comment.