check_mailq.pl
changeset 18 0a525189554d
parent 17 a0ce6426f63b
equal deleted inserted replaced
17:a0ce6426f63b 18:0a525189554d
     1 #! /usr/bin/perl -w
       
     2 # $Id$
       
     3 # $URL$
       
     4 
       
     5 use strict;
       
     6 use warnings;
       
     7 
       
     8 use File::Basename;
       
     9 use Getopt::Long;
       
    10 use POSIX qw(:sys_wait_h);
       
    11 use lib "/usr/lib/nagios/plugins";
       
    12 use utils qw (%ERRORS &print_revision &support);
       
    13 
       
    14 sub print_help();
       
    15 sub print_usage();
       
    16 
       
    17 my $ME = basename $0;
       
    18 my ( $opt_w, $opt_c, $opt_s, $opt_V, $opt_h, $opt_b );
       
    19 my ( $result, $mailq );
       
    20 
       
    21 $opt_s = "86400";
       
    22 $opt_w = "10";
       
    23 $opt_c = "20";
       
    24 $opt_b = "/usr/sbin/exipick";
       
    25 
       
    26 Getopt::Long::Configure('bundling');
       
    27 GetOptions(
       
    28     "V"          => \$opt_V,
       
    29     "version"    => \$opt_V,
       
    30     "h"          => \$opt_h,
       
    31     "help"       => \$opt_h,
       
    32     "b=s"        => \$opt_b,
       
    33     "binary"     => \$opt_b,
       
    34     "s=i"        => \$opt_s,
       
    35     "seconds"    => \$opt_s,
       
    36     "w=i"        => \$opt_w,
       
    37     "warning=i"  => \$opt_w,
       
    38     "c=i"        => \$opt_c,
       
    39     "critical=i" => \$opt_c
       
    40 );
       
    41 
       
    42 if ($opt_V) {
       
    43     print_revision( $ME, "1.1" );
       
    44     exit $ERRORS{"OK"};
       
    45 }
       
    46 
       
    47 if ($opt_h) {
       
    48     print_help();
       
    49     exit $ERRORS{"OK"};
       
    50 }
       
    51 
       
    52 if ( $opt_w > $opt_c ) {
       
    53     print
       
    54       "MAILQ CRITICAL: Warning (-w) cannot be greater than Critical (-c)!\n";
       
    55     exit $ERRORS{"CRITICAL"};
       
    56 }
       
    57 
       
    58 # check exipick binary
       
    59 unless ( -x $opt_b ) {
       
    60     print "MAILQ CRITICAL: exipick not found or not executable - $opt_b\n";
       
    61     exit $ERRORS{"CRITICAL"};
       
    62 }
       
    63 
       
    64 $SIG{CHLD} = sub {
       
    65     while (waitpid(-1, WNOHANG) > 0) {};
       
    66 };
       
    67 
       
    68 # check the mailq
       
    69 die "Can't fork: $!" unless defined (my $pid = open(F, "-|"));
       
    70 if ($pid) {
       
    71     <F> =~ /^(\d+).*/;
       
    72     $mailq = $1;
       
    73     close F;
       
    74 } else {
       
    75     exec $opt_b, '-o', $opt_s, '-c', @ARGV
       
    76         or die "can't exec $opt_b: $!";
       
    77 }
       
    78 
       
    79 SWITCH: {
       
    80     $mailq < $opt_w  and $result = "OK",       last;
       
    81     $mailq >= $opt_c and $result = "CRITICAL", last;
       
    82     $mailq >= $opt_w and $result = "WARNING",  last;
       
    83 }
       
    84 
       
    85 print
       
    86 "MAILQ $result: ($mailq) queued mails older than $opt_s sec.|unsent=$mailq;$opt_w;$opt_c;0;0\n";
       
    87 exit $ERRORS{$result};
       
    88 
       
    89 sub print_usage() {
       
    90     print "Usage:\n";
       
    91     print "  $ME [-b <binary>] [-s <sec>] [-w <warn>] [-c <crit>] [<exipick criterion> [<exipick criterion> ... ]]\n";
       
    92     print "  $ME [-h | --help]\n";
       
    93     print "  $ME [-V | --version]\n";
       
    94 }
       
    95 
       
    96 sub print_help() {
       
    97     print_revision( $ME, "1.1" );
       
    98     print "Copyright (c) 2008 Christian Arnold\n";
       
    99     print "Copyright (c) 2015 Matthias Förste\n\n";
       
   100     print "This plugin checks the number of messages, which are longer than\n";
       
   101     print "an specified time holding in the mail queue.\n\n";
       
   102     print_usage();
       
   103     print "\n";
       
   104     print "  -b, --binary <binary>\n";
       
   105     print "     Path of exipick binary (default: /usr/sbin/exipick)\n";
       
   106     print "  -s, --seconds <sec>\n";
       
   107     print "     Age of messages in seconds in the mail queue (default: 86400) one day\n";
       
   108     print "  -w, --warning <warn>\n";
       
   109     print "     Min. number of messages in the mail queue to generate warning alert (default: 10)\n";
       
   110     print "  -c, --critical <crit>\n";
       
   111     print "     Min. number of messages in the mail queue to generate critical alert (default: 20)\n";
       
   112     print "\n";
       
   113     print "Note: This plugin only works with exim4.\n\n";
       
   114     support();
       
   115 }