Description: |
When running MRTG from cron, if a device fails to reply within the allotted time, iit spits out a voluminous error message. If many devices are down (because a nearby gateway has fallen off the net), the exact nature of the problem can be difficult to see, as there is too much output to wade through.
This script takes the output, reformats and summarises it and sends it to the relevant authorities.
|
#! /usr/bin/perl -w
use strict;
use Getopt::Std;
use Mail::Sendmail;
use constant FROM => 'mrtg@example.com';
use constant TO => 'operator@example.com';
my %args;
getopt('t', \%args);
# t -- test: do not send mail
my( $date, $time, $no_response, $host, %error );
my %unmatched;
while( <> ) {
chomp;
if( /^((\d{4}-\d{2}-\d{2} \d{2}:\d{2}):\d{2}) -- SNMP Error:$/ ) {
($time, $date, $no_response, $host) = ($1, $2, undef, undef);
}
elsif( $date and $_ eq 'no response received' ) {
$no_response = 1;
}
elsif( $date and $no_response and /^SNMPv1_Session \(remote host:
+"([^"]+)" \[([^\]]+)/ ) {
push @{$error{$date}}, [$1,$2,$time];
$_ = <> for (1..6); # throw away next six lines
}
elsif( /-- SNMPGET Problem for/
or /-- WARNING: Expected a number/
or m{at /usr/local/mrtg-2/bin/mrtg line \d+}
) {
next;
}
else {
$unmatched{$.} = $_;
}
}
if( defined $args{t} ) {
print( "$_ ", join( ", " => @{$error{$_}} ), "\n" ) for sort keys
+%error;
exit;
}
exit 0 unless scalar keys %error or scalar keys %unmatched;
my $body = "The following gateways did not reply within 2 seconds\n\n"
+;
foreach my $date( sort keys %error ) {
$body .= "$date\n";
foreach my $r( @{$error{$date}} ) {
$body .= " $r->[0] ($r->[1]) at $r->[2]\n";
}
$body .= "\n";
}
if( scalar keys %unmatched ) {
$body .= "The following error messages were ignored\n";
foreach( sort {$a<=>$b} keys %unmatched ) {
$body .= "$_:$unmatched{$_}\n";
}
$body .= "\n";
}
$body .= "--end--";
my %mail;
@mail{ qw/From To Subject Body/ } = (
FROM,
TO,
'Network Errors ' . join( ',' => sort keys %error ),
$body,
);
sendmail(%mail) or die "$0: mail send failure:\n\t$Mail::Sendmail::err
+or\n";
print "OK. Log says:\n", $Mail::Sendmail::log, "\n" if -t STDIN;
=head1 NAME
mrtg.errorcap - Capture and reformat MRTG errors, and send them via em
+ail
=head1 SYNOPSIS
B<mrtg.errorcap> [B<-t>]
=head1 DESCRIPTION
Capture and reformat the error messages that MRTG may emit when monito
+ring equiment
=head1 OPTIONS
=over 5
=item B<-t>
Test. Do not send email.
=head1 EXAMPLES
Usually run from cron. An example crontab entry may like the following
C<*/5 * * * * /usr/local/bin/mrtg /etc/mrtg.cfg 2E<gt>&1 | /usr/local/
+bin/mrtg.errorcap>
If a host does not reply to mrtg in the allotted time, a verbose error
+ message is produced.
See the _END_ section at the end of this file for an example. This scr
+ipt cleans the messages
up and produces a report like
=over 4
The following gateways did not reply within 2 seconds
2002-01-25 11:10
gw-ab (10.0.46.254) at 2002-01-25 11:10:14
gw-jk (10.0.46.254) at 2002-01-25 11:10:26
--end--
=back
=head1 BUGS
None yet.
=head1 SEE ALSO
L<http://people.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg.html|The MRTG w
+eb site>
=head1 COPYRIGHT
Copyright (c) 2002 David Landgren.
This script is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 AUTHOR
David "grinder" Landgren
eval {join chr(64) => qw[landgren bpinet.com]}
=cut
__END__
2001-10-02 09:15:16 -- SNMP Error:
no response received
SNMPv1_Session (remote host: "gw-ab" [10.0.34.254].161)
community: "foo_bar"
request ID: 1857483728
PDU bufsize: 8000 bytes
timeout: 2s
retries: 5
backoff: 1)
2001-10-02 09:15:16 -- SNMPGET Problem for ifInOctets.8 ifOutOctets.8
+sysUptime sysName on foo_bar@gw-ab:
at /usr/local/mrtg-2/bin/mrtg line 1488
2001-10-02 09:15:16 -- WARNING: Expected a number but got ''
2001-10-02 09:15:16 -- WARNING: Expected a number but got ''
2001-10-02 09:15:26 -- SNMP Error:
no response received
SNMPv1_Session (remote host: "gw-cd" [10.0.37.254].161)
community: "foo_bar"
request ID: 1805349969
PDU bufsize: 8000 bytes
timeout: 2s
retries: 5
backoff: 1)
2001-10-02 09:15:26 -- SNMPGET Problem for ifInOctets.7 ifOutOctets.7
+sysUptime sysName on foo_bar@gw-cd:
at /usr/local/mrtg-2/bin/mrtg line 1488
2001-10-02 09:15:26 -- WARNING: Expected a number but got ''
2001-10-02 09:15:26 -- WARNING: Expected a number but got ''
2001-10-02 09:15:36 -- SNMP Error:
no response received
SNMPv1_Session (remote host: "gw-ef" [10.0.38.254].161)
community: "foo_bar"
request ID: 1479025078
PDU bufsize: 8000 bytes
timeout: 2s
retries: 5
backoff: 1)
2001-10-02 09:15:36 -- SNMPGET Problem for ifInOctets.7 ifOutOctets.7
+sysUptime sysName on foo_bar@gw-ef:
at /usr/local/mrtg-2/bin/mrtg line 1488
2001-10-02 09:15:36 -- WARNING: Expected a number but got ''
2001-10-02 09:15:36 -- WARNING: Expected a number but got ''
|