check_mailq.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/local/bin/perl -w
  2. # check_mailq - check to see how many messages are in the smtp queue awating
  3. # transmittal.
  4. #
  5. # Initial version support sendmail's mailq command
  6. # License Information:
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. #
  21. ############################################################################
  22. use POSIX;
  23. use strict;
  24. use Getopt::Long;
  25. use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t $status $state $msg $msg_q );
  26. use lib utils.pm;
  27. use utils qw(%ERRORS &print_revision &support &usage );
  28. #my $MAILQ = "/usr/bin/mailq"; # need to migrate support to utils.pm and autoconf
  29. sub print_help ();
  30. sub print_usage ();
  31. sub process_arguments ();
  32. $ENV{'PATH'}='';
  33. $ENV{'BASH_ENV'}='';
  34. $ENV{'ENV'}='';
  35. $PROGNAME = "check_mailq";
  36. Getopt::Long::Configure('bundling');
  37. $status = process_arguments();
  38. if ($status){
  39. print "ERROR: processing arguments\n";
  40. exit $ERRORS{"UNKNOWN"};
  41. }
  42. $SIG{'ALRM'} = sub {
  43. print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
  44. exit $ERRORS{"WARNING"};
  45. };
  46. alarm($opt_t);
  47. ## open mailq
  48. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  49. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  50. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  51. exit $ERRORS{'UNKNOWN'};
  52. }
  53. }else{
  54. print "ERROR: Could not find mailq executable!\n";
  55. exit $ERRORS{'UNKNOWN'};
  56. }
  57. # only first line is relevant in this iteration.
  58. while (<MAILQ>) {
  59. if (/mqueue/) {
  60. print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
  61. if (/empty/ ) {
  62. $msg = "OK: mailq is empty";
  63. $msg_q = 0;
  64. $state = $ERRORS{'OK'};
  65. }elsif ( /(\d+)/ ) {
  66. $msg_q = $1 ;
  67. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  68. if ($msg_q < $opt_w) {
  69. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  70. $state = $ERRORS{'OK'};
  71. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  72. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  73. $state = $ERRORS{'WARNING'};
  74. }else {
  75. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  76. $state = $ERRORS{'CRITICAL'};
  77. }
  78. }
  79. last;
  80. }
  81. }
  82. close (MAILQ);
  83. # declare an error if we also get a non-zero return code from mailq
  84. # unless already set to critical
  85. if ( $? ) {
  86. print "stderr = $? : $! \n" if $verbose;
  87. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
  88. print "MAILQ error: $!\n" if $verbose;
  89. }
  90. ## close mailq
  91. # Perfdata support
  92. print "$msg | mailq = $msg_q\n";
  93. exit $state;
  94. #####################################
  95. #### subs
  96. sub process_arguments(){
  97. GetOptions
  98. ("V" => \$opt_V, "version" => \$opt_V,
  99. "v" => \$opt_v, "verbose" => \$opt_v,
  100. "h" => \$opt_h, "help" => \$opt_h,
  101. "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
  102. "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
  103. "t=i" => \$opt_t, "timeout=i" => \$opt_t
  104. );
  105. if ($opt_V) {
  106. print_revision($PROGNAME,'$Revision$ ');
  107. exit $ERRORS{'OK'};
  108. }
  109. if ($opt_h) {
  110. print_help();
  111. exit $ERRORS{'OK'};
  112. }
  113. if (defined $opt_v ){
  114. $verbose = $opt_v;
  115. }
  116. unless (defined $opt_t) {
  117. $opt_t = $utils::TIMEOUT ; # default timeout
  118. }
  119. unless ( defined $opt_w && defined $opt_c ) {
  120. print_usage();
  121. exit $ERRORS{'UNKNOWN'};
  122. }
  123. if ( $opt_w >= $opt_c) {
  124. print "Warning cannot be greater than Critical!\n";
  125. exit $ERRORS{'UNKNOWN'};
  126. }
  127. return $ERRORS{'OK'};
  128. }
  129. sub print_usage () {
  130. print "Usage: $PROGNAME [-w <warn>] [-c <crit>] [-t <timeout>] [-v verbose]\n";
  131. }
  132. sub print_help () {
  133. print_revision($PROGNAME,'$Revision$');
  134. print "Copyright (c) 2002 Subhendu Ghosh\n";
  135. print "\n";
  136. print_usage();
  137. print "\n";
  138. print " Checks the number of messages in the mail queue\n";
  139. print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  140. print "-w (--warning) = Min. number of messages in queue to generate warning\n";
  141. print "-c (--critical) = Min. number of messages in queu to generate critical alert ( w < c )\n";
  142. print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  143. print "-h (--help)\n";
  144. print "-V (--version)\n";
  145. print "-v (--verbose) = deebugging output\n";
  146. print "\n\n";
  147. support();
  148. }