check_mailq.pl 22 KB

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