check_mailq.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. # Support for mutiple sendmail queues (Carlos Canau)
  7. # Support for qmail (Benjamin Schmid)
  8. # License Information:
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. #
  23. ############################################################################
  24. use POSIX;
  25. use strict;
  26. use Getopt::Long;
  27. use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
  28. $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
  29. %srcdomains %dstdomains);
  30. use lib utils.pm;
  31. use utils qw(%ERRORS &print_revision &support &usage );
  32. sub print_help ();
  33. sub print_usage ();
  34. sub process_arguments ();
  35. $ENV{'PATH'}='';
  36. $ENV{'BASH_ENV'}='';
  37. $ENV{'ENV'}='';
  38. $PROGNAME = "check_mailq";
  39. $mailq = 'sendmail'; # default
  40. $msg_q = 0 ;
  41. $msg_p = 0 ;
  42. $state = $ERRORS{'UNKNOWN'};
  43. Getopt::Long::Configure('bundling');
  44. $status = process_arguments();
  45. if ($status){
  46. print "ERROR: processing arguments\n";
  47. exit $ERRORS{"UNKNOWN"};
  48. }
  49. $SIG{'ALRM'} = sub {
  50. print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
  51. exit $ERRORS{"WARNING"};
  52. };
  53. alarm($opt_t);
  54. # switch based on MTA
  55. if ($mailq eq "sendmail") {
  56. ## open mailq
  57. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  58. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  59. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  60. exit $ERRORS{'UNKNOWN'};
  61. }
  62. }elsif( defined $utils::PATH_TO_MAILQ){
  63. unless (-x $utils::PATH_TO_MAILQ) {
  64. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  65. exit $ERRORS{'UNKNOWN'};
  66. }
  67. } else {
  68. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  69. exit $ERRORS{'UNKNOWN'};
  70. }
  71. # single queue empty
  72. ##/var/spool/mqueue is empty
  73. # single queue: 1
  74. ## /var/spool/mqueue (1 request)
  75. ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
  76. ##h32E30p01763 2782 Wed Apr 2 15:03 <silvaATkpnqwest.pt>
  77. ## 8BITMIME
  78. ## <silvaATeunet.pt>
  79. # multi queue empty
  80. ##/var/spool/mqueue/q0/df is empty
  81. ##/var/spool/mqueue/q1/df is empty
  82. ##/var/spool/mqueue/q2/df is empty
  83. ##/var/spool/mqueue/q3/df is empty
  84. ##/var/spool/mqueue/q4/df is empty
  85. ##/var/spool/mqueue/q5/df is empty
  86. ##/var/spool/mqueue/q6/df is empty
  87. ##/var/spool/mqueue/q7/df is empty
  88. ##/var/spool/mqueue/q8/df is empty
  89. ##/var/spool/mqueue/q9/df is empty
  90. ##/var/spool/mqueue/qA/df is empty
  91. ##/var/spool/mqueue/qB/df is empty
  92. ##/var/spool/mqueue/qC/df is empty
  93. ##/var/spool/mqueue/qD/df is empty
  94. ##/var/spool/mqueue/qE/df is empty
  95. ##/var/spool/mqueue/qF/df is empty
  96. ## Total Requests: 0
  97. # multi queue: 1
  98. ##/var/spool/mqueue/q0/df is empty
  99. ##/var/spool/mqueue/q1/df is empty
  100. ##/var/spool/mqueue/q2/df is empty
  101. ## /var/spool/mqueue/q3/df (1 request)
  102. ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
  103. ##h32De2f23534* 48 Wed Apr 2 14:40 nocol
  104. ## nouserATEUnet.pt
  105. ## canau
  106. ##/var/spool/mqueue/q4/df is empty
  107. ##/var/spool/mqueue/q5/df is empty
  108. ##/var/spool/mqueue/q6/df is empty
  109. ##/var/spool/mqueue/q7/df is empty
  110. ##/var/spool/mqueue/q8/df is empty
  111. ##/var/spool/mqueue/q9/df is empty
  112. ##/var/spool/mqueue/qA/df is empty
  113. ##/var/spool/mqueue/qB/df is empty
  114. ##/var/spool/mqueue/qC/df is empty
  115. ##/var/spool/mqueue/qD/df is empty
  116. ##/var/spool/mqueue/qE/df is empty
  117. ##/var/spool/mqueue/qF/df is empty
  118. ## Total Requests: 1
  119. while (<MAILQ>) {
  120. # match email addr on queue listing
  121. if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
  122. my $domain = $1;
  123. if (/^\w+/) {
  124. print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
  125. $srcdomains{$domain} ++;
  126. }
  127. next;
  128. }
  129. #
  130. # ...
  131. # sendmail considers a message with more than one destiny, say N, to the same MX
  132. # to have N messages in queue.
  133. # we will only consider one in this code
  134. if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
  135. ( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
  136. ( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) ||
  137. ( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
  138. print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
  139. $dstdomains{$1} ++;
  140. }
  141. if (/\s+\(I\/O\serror\)/) {
  142. print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
  143. $dstdomains{'UNKNOWN'} ++;
  144. }
  145. # Finally look at the overall queue length
  146. #
  147. if (/mqueue/) {
  148. print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
  149. if (/ \((\d+) request/) {
  150. #
  151. # single queue: first line
  152. # multi queue: one for each queue. overwrite on multi queue below
  153. $msg_q = $1 ;
  154. }
  155. } elsif (/^\s+Total\sRequests:\s(\d+)$/) {
  156. print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
  157. #
  158. # multi queue: last line
  159. $msg_q = $1 ;
  160. }
  161. }
  162. ## close mailq
  163. close (MAILQ);
  164. # declare an error if we also get a non-zero return code from mailq
  165. # unless already set to critical
  166. if ( $? ) {
  167. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  168. print "STDERR $?: $!\n" if $verbose;
  169. $msg = "$state: (stderr)\n";
  170. }
  171. ## shut off the alarm
  172. alarm(0);
  173. ## now check the queue length(s)
  174. if ($msg_q == 0) {
  175. $msg = "OK: mailq is empty";
  176. $state = $ERRORS{'OK'};
  177. } else {
  178. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  179. # overall queue length
  180. if ($msg_q < $opt_w) {
  181. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  182. $state = $ERRORS{'OK'};
  183. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  184. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  185. $state = $ERRORS{'WARNING'};
  186. }else {
  187. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  188. $state = $ERRORS{'CRITICAL'};
  189. }
  190. # check for domain specific queue lengths if requested
  191. if (defined $opt_W) {
  192. # Apply threshold to queue lengths FROM domain
  193. my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
  194. my $srcmaxkey = $srckeys[0];
  195. print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
  196. if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
  197. if ($state == $ERRORS{'OK'}) {
  198. $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  199. $state = $ERRORS{'WARNING'};
  200. } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
  201. $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  202. } else {
  203. $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  204. $state = $ERRORS{'WARNING'};
  205. }
  206. } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
  207. if ($state == $ERRORS{'OK'}) {
  208. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
  209. $state = $ERRORS{'CRITICAL'};
  210. } elsif ($state == $ERRORS{'WARNING'}) {
  211. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
  212. $msg =~ s/WARNING: //;
  213. } elsif ($state == $ERRORS{'CRITICAL'}) {
  214. $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  215. } else {
  216. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  217. $state = $ERRORS{'CRITICAL'};
  218. }
  219. } else {
  220. if ($srcdomains{$srcmaxkey} > 0) {
  221. $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
  222. }
  223. }
  224. # Apply threshold to queue lengths TO domain
  225. my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
  226. my $dstmaxkey = $dstkeys[0];
  227. print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
  228. if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
  229. if ($state == $ERRORS{'OK'}) {
  230. $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  231. $state = $ERRORS{'WARNING'};
  232. } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
  233. $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  234. } else {
  235. $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  236. $state = $ERRORS{'WARNING'};
  237. }
  238. } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
  239. if ($state == $ERRORS{'OK'}) {
  240. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
  241. $state = $ERRORS{'CRITICAL'};
  242. } elsif ($state == $ERRORS{'WARNING'}) {
  243. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
  244. $msg =~ s/WARNING: //;
  245. } elsif ($state == $ERRORS{'CRITICAL'}) {
  246. $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  247. } else {
  248. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  249. $state = $ERRORS{'CRITICAL'};
  250. }
  251. } else {
  252. if ($dstdomains{$dstmaxkey} > 0) {
  253. $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
  254. }
  255. }
  256. } # End of queue length thresholds
  257. }
  258. } # end of ($mailq eq "sendmail")
  259. elsif ( $mailq eq "qmail" ) {
  260. # open qmail-qstat
  261. if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
  262. if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
  263. print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
  264. exit $ERRORS{'UNKNOWN'};
  265. }
  266. }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
  267. unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
  268. print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
  269. exit $ERRORS{'UNKNOWN'};
  270. }
  271. } else {
  272. print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
  273. exit $ERRORS{'UNKNOWN'};
  274. }
  275. @lines = <MAILQ>;
  276. # close qmail-qstat
  277. close MAILQ;
  278. # declare an error if we also get a non-zero return code from mailq
  279. # unless already set to critical
  280. if ( $? ) {
  281. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  282. print "STDERR $?: $!\n" if $verbose;
  283. $msg = "$state: (stderr)\n";
  284. }
  285. ## shut off the alarm
  286. alarm(0);
  287. # check queue length
  288. if ($lines[0]=~/^messages in queue: (\d+)/) {
  289. $msg_q = $1 ;
  290. }else{
  291. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  292. exit $ERRORS{'UNKNOWN'};
  293. }
  294. # check messages not processed
  295. if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  296. my $msg_p = $1;
  297. }else{
  298. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  299. exit $ERRORS{'UNKNOWN'};
  300. }
  301. # check queue length(s)
  302. if ($msg_q == 0){
  303. $msg = "OK: qmail-qstat reports queue is empty";
  304. $state = $ERRORS{'OK'};
  305. } else {
  306. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  307. # overall queue length
  308. if ($msg_q < $opt_w) {
  309. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  310. $state = $ERRORS{'OK'};
  311. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  312. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  313. $state = $ERRORS{'WARNING'};
  314. }else {
  315. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  316. $state = $ERRORS{'CRITICAL'};
  317. }
  318. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  319. # are defined)
  320. if (defined $opt_W) {
  321. $msg .= "[Preprocessed = $msg_p]";
  322. if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  323. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  324. }elsif ($msg_p >= $opt_C ) {
  325. $state = $ERRORS{"CRITICAL"} ;
  326. }
  327. }
  328. }
  329. } # end of ($mailq eq "qmail")
  330. # Perfdata support
  331. print "$msg |mailq=$msg_q\n";
  332. exit $state;
  333. #####################################
  334. #### subs
  335. sub process_arguments(){
  336. GetOptions
  337. ("V" => \$opt_V, "version" => \$opt_V,
  338. "v" => \$opt_v, "verbose" => \$opt_v,
  339. "h" => \$opt_h, "help" => \$opt_h,
  340. "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
  341. "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
  342. "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
  343. "W=i" => \$opt_W, "Warning=i" => \$opt_W, # warning if above this number
  344. "C=i" => \$opt_C, "Critical=i" => \$opt_C, # critical if above this number
  345. "t=i" => \$opt_t, "timeout=i" => \$opt_t
  346. );
  347. if ($opt_V) {
  348. print_revision($PROGNAME,'$Revision$ ');
  349. exit $ERRORS{'OK'};
  350. }
  351. if ($opt_h) {
  352. print_help();
  353. exit $ERRORS{'OK'};
  354. }
  355. if (defined $opt_v ){
  356. $verbose = $opt_v;
  357. }
  358. unless (defined $opt_t) {
  359. $opt_t = $utils::TIMEOUT ; # default timeout
  360. }
  361. unless ( defined $opt_w && defined $opt_c ) {
  362. print_usage();
  363. exit $ERRORS{'UNKNOWN'};
  364. }
  365. if ( $opt_w >= $opt_c) {
  366. print "Warning (-w) cannot be greater than Critical (-c)!\n";
  367. exit $ERRORS{'UNKNOWN'};
  368. }
  369. if (defined $opt_W && ! defined !$opt_C) {
  370. print "Need -C if using -W\n";
  371. exit $ERRORS{'UNKNOWN'};
  372. }elsif(defined $opt_W && defined $opt_C) {
  373. if ($opt_W >= $opt_C) {
  374. print "Warning (-W) cannot be greater than Critical (-C)!\n";
  375. exit $ERRORS{'UNKNOWN'};
  376. }
  377. }
  378. if (defined $opt_M) {
  379. if ($opt_M =~ /sendmail/ || $opt_M =~ /qmail/ ) {
  380. $mailq = $opt_M ;
  381. }elsif( $opt_M eq ''){
  382. $mailq = 'sendmail';
  383. }else{
  384. print "-M: $opt_M is not supported\n";
  385. exit $ERRORS{'UNKNOWN'};
  386. }
  387. }else{
  388. $mailq = 'sendmail' ;
  389. }
  390. return $ERRORS{'OK'};
  391. }
  392. sub print_usage () {
  393. print "Usage: $PROGNAME [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
  394. }
  395. sub print_help () {
  396. print_revision($PROGNAME,'$Revision$');
  397. print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
  398. print "\n";
  399. print_usage();
  400. print "\n";
  401. print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
  402. print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  403. print "-w (--warning) = Min. number of messages in queue to generate warning\n";
  404. print "-c (--critical) = Min. number of messages in queu to generate critical alert ( w < c )\n";
  405. print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n";
  406. print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
  407. print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  408. print "-M (--mailserver) = [ sendmail | qmail ] (default = sendmail)\n";
  409. print "-h (--help)\n";
  410. print "-V (--version)\n";
  411. print "-v (--verbose) = debugging output\n";
  412. print "\n\n";
  413. print "Note: -w and -c are required arguments. -W and -C are optional.\n";
  414. print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
  415. print " -W and -C are applied message not yet preproccessed. (qmail)\n";
  416. print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
  417. print " to look at the queues. Mailq can usually only be accessed by root or \n";
  418. print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
  419. print "";
  420. print "\n\n";
  421. support();
  422. }