Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#! /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 ''

In reply to mrtg.errorcap - reformat MRTG errors by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2015-07-05 09:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (61 votes), past polls