4
0

check_mailq.pl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. # $Id$
  24. #
  25. ############################################################################
  26. use POSIX;
  27. use strict;
  28. use Getopt::Long;
  29. use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
  30. $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
  31. %srcdomains %dstdomains);
  32. use lib utils.pm;
  33. use utils qw(%ERRORS &print_revision &support &usage );
  34. sub print_help ();
  35. sub print_usage ();
  36. sub process_arguments ();
  37. $ENV{'PATH'}='';
  38. $ENV{'BASH_ENV'}='';
  39. $ENV{'ENV'}='';
  40. $PROGNAME = "check_mailq";
  41. $mailq = 'sendmail'; # default
  42. $msg_q = 0 ;
  43. $msg_p = 0 ;
  44. $state = $ERRORS{'UNKNOWN'};
  45. Getopt::Long::Configure('bundling');
  46. $status = process_arguments();
  47. if ($status){
  48. print "ERROR: processing arguments\n";
  49. exit $ERRORS{"UNKNOWN"};
  50. }
  51. $SIG{'ALRM'} = sub {
  52. print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
  53. exit $ERRORS{"WARNING"};
  54. };
  55. alarm($opt_t);
  56. # switch based on MTA
  57. if ($mailq eq "sendmail") {
  58. ## open mailq
  59. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  60. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  61. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  62. exit $ERRORS{'UNKNOWN'};
  63. }
  64. }elsif( defined $utils::PATH_TO_MAILQ){
  65. unless (-x $utils::PATH_TO_MAILQ) {
  66. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  67. exit $ERRORS{'UNKNOWN'};
  68. }
  69. } else {
  70. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  71. exit $ERRORS{'UNKNOWN'};
  72. }
  73. # single queue empty
  74. ##/var/spool/mqueue is empty
  75. # single queue: 1
  76. ## /var/spool/mqueue (1 request)
  77. ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
  78. ##h32E30p01763 2782 Wed Apr 2 15:03 <silvaATkpnqwest.pt>
  79. ## 8BITMIME
  80. ## <silvaATeunet.pt>
  81. # multi queue empty
  82. ##/var/spool/mqueue/q0/df is empty
  83. ##/var/spool/mqueue/q1/df is empty
  84. ##/var/spool/mqueue/q2/df is empty
  85. ##/var/spool/mqueue/q3/df is empty
  86. ##/var/spool/mqueue/q4/df is empty
  87. ##/var/spool/mqueue/q5/df is empty
  88. ##/var/spool/mqueue/q6/df is empty
  89. ##/var/spool/mqueue/q7/df is empty
  90. ##/var/spool/mqueue/q8/df is empty
  91. ##/var/spool/mqueue/q9/df is empty
  92. ##/var/spool/mqueue/qA/df is empty
  93. ##/var/spool/mqueue/qB/df is empty
  94. ##/var/spool/mqueue/qC/df is empty
  95. ##/var/spool/mqueue/qD/df is empty
  96. ##/var/spool/mqueue/qE/df is empty
  97. ##/var/spool/mqueue/qF/df is empty
  98. ## Total Requests: 0
  99. # multi queue: 1
  100. ##/var/spool/mqueue/q0/df is empty
  101. ##/var/spool/mqueue/q1/df is empty
  102. ##/var/spool/mqueue/q2/df is empty
  103. ## /var/spool/mqueue/q3/df (1 request)
  104. ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
  105. ##h32De2f23534* 48 Wed Apr 2 14:40 nocol
  106. ## nouserATEUnet.pt
  107. ## canau
  108. ##/var/spool/mqueue/q4/df is empty
  109. ##/var/spool/mqueue/q5/df is empty
  110. ##/var/spool/mqueue/q6/df is empty
  111. ##/var/spool/mqueue/q7/df is empty
  112. ##/var/spool/mqueue/q8/df is empty
  113. ##/var/spool/mqueue/q9/df is empty
  114. ##/var/spool/mqueue/qA/df is empty
  115. ##/var/spool/mqueue/qB/df is empty
  116. ##/var/spool/mqueue/qC/df is empty
  117. ##/var/spool/mqueue/qD/df is empty
  118. ##/var/spool/mqueue/qE/df is empty
  119. ##/var/spool/mqueue/qF/df is empty
  120. ## Total Requests: 1
  121. while (<MAILQ>) {
  122. # match email addr on queue listing
  123. if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
  124. my $domain = $1;
  125. if (/^\w+/) {
  126. print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
  127. $srcdomains{$domain} ++;
  128. }
  129. next;
  130. }
  131. #
  132. # ...
  133. # sendmail considers a message with more than one destiny, say N, to the same MX
  134. # to have N messages in queue.
  135. # we will only consider one in this code
  136. if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
  137. ( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
  138. ( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) ||
  139. ( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
  140. print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
  141. $dstdomains{$1} ++;
  142. }
  143. if (/\s+\(I\/O\serror\)/) {
  144. print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
  145. $dstdomains{'UNKNOWN'} ++;
  146. }
  147. # Finally look at the overall queue length
  148. #
  149. if (/mqueue/) {
  150. print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
  151. if (/ \((\d+) request/) {
  152. #
  153. # single queue: first line
  154. # multi queue: one for each queue. overwrite on multi queue below
  155. $msg_q = $1 ;
  156. }
  157. } elsif (/^\s+Total\sRequests:\s(\d+)$/i) {
  158. print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
  159. #
  160. # multi queue: last line
  161. $msg_q = $1 ;
  162. }
  163. }
  164. ## close mailq
  165. close (MAILQ);
  166. # declare an error if we also get a non-zero return code from mailq
  167. # unless already set to critical
  168. if ( $? ) {
  169. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  170. print "STDERR $?: $!\n" if $verbose;
  171. $msg = "$state: (stderr)\n";
  172. }
  173. ## shut off the alarm
  174. alarm(0);
  175. ## now check the queue length(s)
  176. if ($msg_q == 0) {
  177. $msg = "OK: mailq is empty";
  178. $state = $ERRORS{'OK'};
  179. } else {
  180. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  181. # overall queue length
  182. if ($msg_q < $opt_w) {
  183. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  184. $state = $ERRORS{'OK'};
  185. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  186. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  187. $state = $ERRORS{'WARNING'};
  188. }else {
  189. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  190. $state = $ERRORS{'CRITICAL'};
  191. }
  192. # check for domain specific queue lengths if requested
  193. if (defined $opt_W) {
  194. # Apply threshold to queue lengths FROM domain
  195. my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
  196. my $srcmaxkey = $srckeys[0];
  197. print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
  198. if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
  199. if ($state == $ERRORS{'OK'}) {
  200. $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  201. $state = $ERRORS{'WARNING'};
  202. } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
  203. $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  204. } else {
  205. $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  206. $state = $ERRORS{'WARNING'};
  207. }
  208. } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
  209. if ($state == $ERRORS{'OK'}) {
  210. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
  211. $state = $ERRORS{'CRITICAL'};
  212. } elsif ($state == $ERRORS{'WARNING'}) {
  213. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
  214. $msg =~ s/WARNING: //;
  215. } elsif ($state == $ERRORS{'CRITICAL'}) {
  216. $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  217. } else {
  218. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  219. $state = $ERRORS{'CRITICAL'};
  220. }
  221. } else {
  222. if ($srcdomains{$srcmaxkey} > 0) {
  223. $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
  224. }
  225. }
  226. # Apply threshold to queue lengths TO domain
  227. my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
  228. my $dstmaxkey = $dstkeys[0];
  229. print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
  230. if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
  231. if ($state == $ERRORS{'OK'}) {
  232. $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  233. $state = $ERRORS{'WARNING'};
  234. } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
  235. $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  236. } else {
  237. $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  238. $state = $ERRORS{'WARNING'};
  239. }
  240. } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
  241. if ($state == $ERRORS{'OK'}) {
  242. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
  243. $state = $ERRORS{'CRITICAL'};
  244. } elsif ($state == $ERRORS{'WARNING'}) {
  245. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
  246. $msg =~ s/WARNING: //;
  247. } elsif ($state == $ERRORS{'CRITICAL'}) {
  248. $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  249. } else {
  250. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  251. $state = $ERRORS{'CRITICAL'};
  252. }
  253. } else {
  254. if ($dstdomains{$dstmaxkey} > 0) {
  255. $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
  256. }
  257. }
  258. } # End of queue length thresholds
  259. }
  260. } # end of ($mailq eq "sendmail")
  261. elsif ( $mailq eq "postfix" ) {
  262. ## open mailq
  263. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  264. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  265. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  266. exit $ERRORS{'UNKNOWN'};
  267. }
  268. }elsif( defined $utils::PATH_TO_MAILQ){
  269. unless (-x $utils::PATH_TO_MAILQ) {
  270. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  271. exit $ERRORS{'UNKNOWN'};
  272. }
  273. } else {
  274. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  275. exit $ERRORS{'UNKNOWN'};
  276. }
  277. @lines = reverse <MAILQ>;
  278. # close qmail-qstat
  279. close MAILQ;
  280. # declare an error if we also get a non-zero return code from mailq
  281. # unless already set to critical
  282. if ( $? ) {
  283. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  284. print "STDERR $?: $!\n" if $verbose;
  285. $msg = "$state: (stderr)\n";
  286. }
  287. ## shut off the alarm
  288. alarm(0);
  289. # check queue length
  290. if ($lines[0]=~/Kbytes in (\d+)/) {
  291. $msg_q = $1 ;
  292. }elsif ($lines[0]=~/Mail queue is empty/) {
  293. $msg_q = 0;
  294. }else{
  295. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  296. exit $ERRORS{'UNKNOWN'};
  297. }
  298. # check messages not processed
  299. #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  300. # my $msg_p = $1;
  301. #}else{
  302. # print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  303. # exit $ERRORS{'UNKNOWN'};
  304. #}
  305. # check queue length(s)
  306. if ($msg_q == 0){
  307. $msg = "OK: mailq reports queue is empty";
  308. $state = $ERRORS{'OK'};
  309. } else {
  310. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  311. # overall queue length
  312. if ($msg_q < $opt_w) {
  313. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  314. $state = $ERRORS{'OK'};
  315. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  316. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  317. $state = $ERRORS{'WARNING'};
  318. }else {
  319. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  320. $state = $ERRORS{'CRITICAL'};
  321. }
  322. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  323. # are defined)
  324. #if (defined $opt_W) {
  325. # $msg .= "[Preprocessed = $msg_p]";
  326. # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  327. # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  328. # }elsif ($msg_p >= $opt_C ) {
  329. # $state = $ERRORS{"CRITICAL"} ;
  330. # }
  331. #}
  332. }
  333. } # end of ($mailq eq "postfixl")
  334. elsif ( $mailq eq "qmail" ) {
  335. # open qmail-qstat
  336. if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
  337. if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
  338. print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
  339. exit $ERRORS{'UNKNOWN'};
  340. }
  341. }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
  342. unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
  343. print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
  344. exit $ERRORS{'UNKNOWN'};
  345. }
  346. } else {
  347. print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
  348. exit $ERRORS{'UNKNOWN'};
  349. }
  350. @lines = <MAILQ>;
  351. # close qmail-qstat
  352. close MAILQ;
  353. # declare an error if we also get a non-zero return code from mailq
  354. # unless already set to critical
  355. if ( $? ) {
  356. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  357. print "STDERR $?: $!\n" if $verbose;
  358. $msg = "$state: (stderr)\n";
  359. }
  360. ## shut off the alarm
  361. alarm(0);
  362. # check queue length
  363. if ($lines[0]=~/^messages in queue: (\d+)/) {
  364. $msg_q = $1 ;
  365. }else{
  366. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  367. exit $ERRORS{'UNKNOWN'};
  368. }
  369. # check messages not processed
  370. if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  371. my $msg_p = $1;
  372. }else{
  373. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  374. exit $ERRORS{'UNKNOWN'};
  375. }
  376. # check queue length(s)
  377. if ($msg_q == 0){
  378. $msg = "OK: qmail-qstat reports queue is empty";
  379. $state = $ERRORS{'OK'};
  380. } else {
  381. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  382. # overall queue length
  383. if ($msg_q < $opt_w) {
  384. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  385. $state = $ERRORS{'OK'};
  386. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  387. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  388. $state = $ERRORS{'WARNING'};
  389. }else {
  390. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  391. $state = $ERRORS{'CRITICAL'};
  392. }
  393. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  394. # are defined)
  395. if (defined $opt_W) {
  396. $msg .= "[Preprocessed = $msg_p]";
  397. if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  398. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  399. }elsif ($msg_p >= $opt_C ) {
  400. $state = $ERRORS{"CRITICAL"} ;
  401. }
  402. }
  403. }
  404. } # end of ($mailq eq "qmail")
  405. elsif ( $mailq eq "exim" ) {
  406. ## open mailq
  407. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  408. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  409. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  410. exit $ERRORS{'UNKNOWN'};
  411. }
  412. }elsif( defined $utils::PATH_TO_MAILQ){
  413. unless (-x $utils::PATH_TO_MAILQ) {
  414. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  415. exit $ERRORS{'UNKNOWN'};
  416. }
  417. } else {
  418. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  419. exit $ERRORS{'UNKNOWN'};
  420. }
  421. while (<MAILQ>) {
  422. #22m 1.7K 19aEEr-0007hx-Dy <> *** frozen ***
  423. #root@exlixams.glups.fr
  424. if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
  425. $msg_q++ ;
  426. }
  427. }
  428. close(MAILQ) ;
  429. if ($msg_q < $opt_w) {
  430. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  431. $state = $ERRORS{'OK'};
  432. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  433. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  434. $state = $ERRORS{'WARNING'};
  435. }else {
  436. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  437. $state = $ERRORS{'CRITICAL'};
  438. }
  439. } # end of ($mailq eq "exim")
  440. # Perfdata support
  441. print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
  442. exit $state;
  443. #####################################
  444. #### subs
  445. sub process_arguments(){
  446. GetOptions
  447. ("V" => \$opt_V, "version" => \$opt_V,
  448. "v" => \$opt_v, "verbose" => \$opt_v,
  449. "h" => \$opt_h, "help" => \$opt_h,
  450. "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
  451. "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
  452. "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
  453. "t=i" => \$opt_t, "timeout=i" => \$opt_t
  454. );
  455. if ($opt_V) {
  456. print_revision($PROGNAME,'$Revision$ ');
  457. exit $ERRORS{'OK'};
  458. }
  459. if ($opt_h) {
  460. print_help();
  461. exit $ERRORS{'OK'};
  462. }
  463. if (defined $opt_v ){
  464. $verbose = $opt_v;
  465. }
  466. unless (defined $opt_t) {
  467. $opt_t = $utils::TIMEOUT ; # default timeout
  468. }
  469. unless ( defined $opt_w && defined $opt_c ) {
  470. print_usage();
  471. exit $ERRORS{'UNKNOWN'};
  472. }
  473. if ( $opt_w >= $opt_c) {
  474. print "Warning (-w) cannot be greater than Critical (-c)!\n";
  475. exit $ERRORS{'UNKNOWN'};
  476. }
  477. if (defined $opt_W && ! defined !$opt_C) {
  478. print "Need -C if using -W\n";
  479. exit $ERRORS{'UNKNOWN'};
  480. }elsif(defined $opt_W && defined $opt_C) {
  481. if ($opt_W >= $opt_C) {
  482. print "Warning (-W) cannot be greater than Critical (-C)!\n";
  483. exit $ERRORS{'UNKNOWN'};
  484. }
  485. }
  486. if (defined $opt_M) {
  487. if ($opt_M =~ /^(sendmail|qmail|postfix|exim)$/) {
  488. $mailq = $opt_M ;
  489. }elsif( $opt_M eq ''){
  490. $mailq = 'sendmail';
  491. }else{
  492. print "-M: $opt_M is not supported\n";
  493. exit $ERRORS{'UNKNOWN'};
  494. }
  495. }else{
  496. $mailq = 'sendmail' ;
  497. }
  498. return $ERRORS{'OK'};
  499. }
  500. sub print_usage () {
  501. print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
  502. }
  503. sub print_help () {
  504. print_revision($PROGNAME,'$Revision$');
  505. print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
  506. print "\n";
  507. print_usage();
  508. print "\n";
  509. print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
  510. print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  511. print "-w (--warning) = Min. number of messages in queue to generate warning\n";
  512. print "-c (--critical) = Min. number of messages in queu to generate critical alert ( w < c )\n";
  513. print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n";
  514. print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
  515. print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  516. print "-M (--mailserver) = [ sendmail | qmail | postfix | exim ] (default = sendmail)\n";
  517. print "-h (--help)\n";
  518. print "-V (--version)\n";
  519. print "-v (--verbose) = debugging output\n";
  520. print "\n\n";
  521. print "Note: -w and -c are required arguments. -W and -C are optional.\n";
  522. print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
  523. print " -W and -C are applied message not yet preproccessed. (qmail)\n";
  524. print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
  525. print " to look at the queues. Mailq can usually only be accessed by root or \n";
  526. print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
  527. print "";
  528. print "\n\n";
  529. support();
  530. }