Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make log monitor work with new setup - and use upstart to manage it

  • Loading branch information...
commit 47c4dcc417275f5b97c9ff1fc061b63f750d7123 1 parent cdc7319
@autarch autarch authored
View
112 bin/vg-log-monitor
@@ -14,10 +14,15 @@ use File::Tail;
use VegGuide::Config;
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 $sleep = 60;
+my $sleep = ($debug ? 1 : 60);
my $hostname = VegGuide::Config->Hostname();
my $from = 'log-monitor@' . $hostname;
@@ -29,79 +34,54 @@ my $local_tz = DateTime::TimeZone->new( name => 'local' );
my $tail = _make_tail();
+my $record = q{};
while ( defined( my $line = $tail->read() ) ) {
if ( length $line ) {
- local $@;
- if ( eval { process_line($line) } ) {
- next;
- }
- else {
+ $record .= $line;
+ if ( $line =~ /^}/ ) {
+ warn $record if $debug;
+ eval { process_record($record) };
warn $@ if $@;
+ $record = q{};
}
}
- else {
- {
- local $@;
- eval { send_if_pending(); };
- warn $@ if $@;
- }
+
+ {
+ local $@;
+ eval { send_if_pending(); };
+ warn $@ if $@;
}
sleep $sleep;
}
-sub process_line {
- my $line = shift;
-
- return if $line eq '';
+sub process_record {
+ my $record = shift;
- my %message;
+ my $error = VegGuide::JSON->Decode($record);
- if ( $line =~ /^([^[].*)/ ) {
- $message{date} = DateTime->now( time_zone => $local_tz );
- $message{level} = 'warning';
- $message{text} = $1;
+ my $text = 'URI: ' . $error->{uri};
+ if ( $error->{user} ) {
+ $text .= "\n";
+ $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 ) {
- my $error = VegGuide::JSON->Decode($text);
-
- $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;
+ if ( $error->{referer} ) {
+ $text .= "\n";
+ $text .= "Referer: $error->{referer}";
}
- 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;
}
@@ -109,17 +89,22 @@ sub process_line {
sub send_if_pending {
return unless @messages;
+ warn 'We have ' . ( scalar @messages ) . " to send\n" if $debug;
+
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 = '';
for my $msg (@messages) {
$body .= "\n\n" if $body;
- $body .= $msg->{date}->strftime('%Y-%m-%d %H:%M:%S') . q{ - }
- . $msg->{level};
+ $body .= $msg->{date}->strftime('%Y-%m-%d %H:%M:%S');
$body .= "\n";
$body .= $msg->{text};
}
@@ -145,8 +130,9 @@ sub send_if_pending {
sub _make_tail {
return File::Tail->new(
- name => $file,
- resetafter => 3600 * 8,
- nowait => 1,
+ name => $file,
+ maxinterval => $sleep,
+ resetafter => 3600 * 8,
+ nowait => 1,
);
}
View
6 lib/VegGuide/Plugin/ErrorHandling.pm
@@ -70,10 +70,12 @@ sub _log_error {
return if $error =~ /Software caused connection abort/;
- # XXX - change this later to log to the apache log?
# if ( $error =~ /unknown resource/ )
- my %error = ( uri => $self->request()->uri() . '' );
+ my %error = (
+ uri => $self->request()->uri() . '',
+ epoch => time(),
+ );
if ( my $user = $self->vg_user() ) {
$error{user} = $user->real_name();
View
12 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
Please sign in to comment.
Something went wrong with that request. Please try again.