check_mailq.pl 21 KB

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