check_mailq.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #!@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 FindBin;
  32. use lib "$FindBin::Bin";
  33. use utils qw(%ERRORS &print_revision &support &usage );
  34. sub print_help ();
  35. sub print_usage ();
  36. sub process_arguments ();
  37. $ENV{'PATH'}='@TRUSTED_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. if ( $? ) {
  167. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
  168. exit $ERRORS{CRITICAL};
  169. }
  170. ## shut off the alarm
  171. alarm(0);
  172. ## now check the queue length(s)
  173. if ($msg_q == 0) {
  174. $msg = "OK: $mailq mailq is empty";
  175. $state = $ERRORS{'OK'};
  176. } else {
  177. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  178. # overall queue length
  179. if ($msg_q < $opt_w) {
  180. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  181. $state = $ERRORS{'OK'};
  182. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  183. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  184. $state = $ERRORS{'WARNING'};
  185. }else {
  186. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  187. $state = $ERRORS{'CRITICAL'};
  188. }
  189. # check for domain specific queue lengths if requested
  190. if (defined $opt_W) {
  191. # Apply threshold to queue lengths FROM domain
  192. my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
  193. my $srcmaxkey = $srckeys[0];
  194. print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $verbose;
  195. if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
  196. if ($state == $ERRORS{'OK'}) {
  197. $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  198. $state = $ERRORS{'WARNING'};
  199. } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
  200. $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  201. } else {
  202. $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  203. $state = $ERRORS{'WARNING'};
  204. }
  205. } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
  206. if ($state == $ERRORS{'OK'}) {
  207. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
  208. $state = $ERRORS{'CRITICAL'};
  209. } elsif ($state == $ERRORS{'WARNING'}) {
  210. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
  211. $msg =~ s/WARNING: //;
  212. } elsif ($state == $ERRORS{'CRITICAL'}) {
  213. $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  214. } else {
  215. $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
  216. $state = $ERRORS{'CRITICAL'};
  217. }
  218. } else {
  219. if ($srcdomains{$srcmaxkey} > 0) {
  220. $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
  221. }
  222. }
  223. # Apply threshold to queue lengths TO domain
  224. my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
  225. my $dstmaxkey = $dstkeys[0];
  226. print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $verbose;
  227. if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
  228. if ($state == $ERRORS{'OK'}) {
  229. $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  230. $state = $ERRORS{'WARNING'};
  231. } elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
  232. $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  233. } else {
  234. $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  235. $state = $ERRORS{'WARNING'};
  236. }
  237. } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
  238. if ($state == $ERRORS{'OK'}) {
  239. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
  240. $state = $ERRORS{'CRITICAL'};
  241. } elsif ($state == $ERRORS{'WARNING'}) {
  242. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
  243. $msg =~ s/WARNING: //;
  244. } elsif ($state == $ERRORS{'CRITICAL'}) {
  245. $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  246. } else {
  247. $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
  248. $state = $ERRORS{'CRITICAL'};
  249. }
  250. } else {
  251. if ($dstdomains{$dstmaxkey} > 0) {
  252. $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
  253. }
  254. }
  255. } # End of queue length thresholds
  256. }
  257. } # end of ($mailq eq "sendmail")
  258. elsif ( $mailq eq "postfix" ) {
  259. ## open mailq
  260. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  261. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  262. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  263. exit $ERRORS{'UNKNOWN'};
  264. }
  265. }elsif( defined $utils::PATH_TO_MAILQ){
  266. unless (-x $utils::PATH_TO_MAILQ) {
  267. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  268. exit $ERRORS{'UNKNOWN'};
  269. }
  270. } else {
  271. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  272. exit $ERRORS{'UNKNOWN'};
  273. }
  274. @lines = reverse <MAILQ>;
  275. # close qmail-qstat
  276. close MAILQ;
  277. if ( $? ) {
  278. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
  279. exit $ERRORS{CRITICAL};
  280. }
  281. ## shut off the alarm
  282. alarm(0);
  283. # check queue length
  284. if ($lines[0]=~/Kbytes in (\d+)/) {
  285. $msg_q = $1 ;
  286. }elsif ($lines[0]=~/Mail queue is empty/) {
  287. $msg_q = 0;
  288. }else{
  289. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  290. exit $ERRORS{'UNKNOWN'};
  291. }
  292. # check messages not processed
  293. #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  294. # my $msg_p = $1;
  295. #}else{
  296. # print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  297. # exit $ERRORS{'UNKNOWN'};
  298. #}
  299. # check queue length(s)
  300. if ($msg_q == 0){
  301. $msg = "OK: $mailq mailq reports queue is empty";
  302. $state = $ERRORS{'OK'};
  303. } else {
  304. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  305. # overall queue length
  306. if ($msg_q < $opt_w) {
  307. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  308. $state = $ERRORS{'OK'};
  309. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  310. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  311. $state = $ERRORS{'WARNING'};
  312. }else {
  313. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  314. $state = $ERRORS{'CRITICAL'};
  315. }
  316. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  317. # are defined)
  318. #if (defined $opt_W) {
  319. # $msg .= "[Preprocessed = $msg_p]";
  320. # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  321. # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  322. # }elsif ($msg_p >= $opt_C ) {
  323. # $state = $ERRORS{"CRITICAL"} ;
  324. # }
  325. #}
  326. }
  327. } # end of ($mailq eq "postfix")
  328. elsif ( $mailq eq "qmail" ) {
  329. # open qmail-qstat
  330. if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
  331. if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
  332. print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
  333. exit $ERRORS{'UNKNOWN'};
  334. }
  335. }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
  336. unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
  337. print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
  338. exit $ERRORS{'UNKNOWN'};
  339. }
  340. } else {
  341. print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
  342. exit $ERRORS{'UNKNOWN'};
  343. }
  344. @lines = <MAILQ>;
  345. # close qmail-qstat
  346. close MAILQ;
  347. if ( $? ) {
  348. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
  349. exit $ERRORS{CRITICAL};
  350. }
  351. ## shut off the alarm
  352. alarm(0);
  353. # check queue length
  354. if ($lines[0]=~/^messages in queue: (\d+)/) {
  355. $msg_q = $1 ;
  356. }else{
  357. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  358. exit $ERRORS{'UNKNOWN'};
  359. }
  360. # check messages not processed
  361. if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  362. my $msg_p = $1;
  363. }else{
  364. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  365. exit $ERRORS{'UNKNOWN'};
  366. }
  367. # check queue length(s)
  368. if ($msg_q == 0){
  369. $msg = "OK: qmail-qstat reports queue is empty";
  370. $state = $ERRORS{'OK'};
  371. } else {
  372. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  373. # overall queue length
  374. if ($msg_q < $opt_w) {
  375. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  376. $state = $ERRORS{'OK'};
  377. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  378. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  379. $state = $ERRORS{'WARNING'};
  380. }else {
  381. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  382. $state = $ERRORS{'CRITICAL'};
  383. }
  384. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  385. # are defined)
  386. if (defined $opt_W) {
  387. $msg .= "[Preprocessed = $msg_p]";
  388. if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  389. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  390. }elsif ($msg_p >= $opt_C ) {
  391. $state = $ERRORS{"CRITICAL"} ;
  392. }
  393. }
  394. }
  395. } # end of ($mailq eq "qmail")
  396. elsif ( $mailq eq "exim" ) {
  397. ## open mailq
  398. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  399. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  400. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  401. exit $ERRORS{'UNKNOWN'};
  402. }
  403. }elsif( defined $utils::PATH_TO_MAILQ){
  404. unless (-x $utils::PATH_TO_MAILQ) {
  405. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  406. exit $ERRORS{'UNKNOWN'};
  407. }
  408. } else {
  409. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  410. exit $ERRORS{'UNKNOWN'};
  411. }
  412. while (<MAILQ>) {
  413. #22m 1.7K 19aEEr-0007hx-Dy <> *** frozen ***
  414. #root@exlixams.glups.fr
  415. if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
  416. $msg_q++ ;
  417. }
  418. }
  419. close(MAILQ) ;
  420. if ( $? ) {
  421. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
  422. exit $ERRORS{CRITICAL};
  423. }
  424. if ($msg_q < $opt_w) {
  425. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  426. $state = $ERRORS{'OK'};
  427. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  428. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  429. $state = $ERRORS{'WARNING'};
  430. }else {
  431. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  432. $state = $ERRORS{'CRITICAL'};
  433. }
  434. } # end of ($mailq eq "exim")
  435. elsif ( $mailq eq "nullmailer" ) {
  436. ## open mailq
  437. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  438. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  439. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  440. exit $ERRORS{'UNKNOWN'};
  441. }
  442. }elsif( defined $utils::PATH_TO_MAILQ){
  443. unless (-x $utils::PATH_TO_MAILQ) {
  444. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  445. exit $ERRORS{'UNKNOWN'};
  446. }
  447. } else {
  448. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  449. exit $ERRORS{'UNKNOWN'};
  450. }
  451. while (<MAILQ>) {
  452. #2006-06-22 16:00:00 282 bytes
  453. 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$/) {
  454. $msg_q++ ;
  455. }
  456. }
  457. close(MAILQ) ;
  458. if ($msg_q < $opt_w) {
  459. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  460. $state = $ERRORS{'OK'};
  461. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  462. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  463. $state = $ERRORS{'WARNING'};
  464. }else {
  465. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  466. $state = $ERRORS{'CRITICAL'};
  467. }
  468. } # end of ($mailq eq "nullmailer")
  469. # Perfdata support
  470. print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
  471. exit $state;
  472. #####################################
  473. #### subs
  474. sub process_arguments(){
  475. GetOptions
  476. ("V" => \$opt_V, "version" => \$opt_V,
  477. "v" => \$opt_v, "verbose" => \$opt_v,
  478. "h" => \$opt_h, "help" => \$opt_h,
  479. "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
  480. "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
  481. "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
  482. "t=i" => \$opt_t, "timeout=i" => \$opt_t
  483. );
  484. if ($opt_V) {
  485. print_revision($PROGNAME,'@NP_VERSION@');
  486. exit $ERRORS{'OK'};
  487. }
  488. if ($opt_h) {
  489. print_help();
  490. exit $ERRORS{'OK'};
  491. }
  492. if (defined $opt_v ){
  493. $verbose = $opt_v;
  494. }
  495. unless (defined $opt_t) {
  496. $opt_t = $utils::TIMEOUT ; # default timeout
  497. }
  498. unless ( defined $opt_w && defined $opt_c ) {
  499. print_usage();
  500. exit $ERRORS{'UNKNOWN'};
  501. }
  502. if ( $opt_w >= $opt_c) {
  503. print "Warning (-w) cannot be greater than Critical (-c)!\n";
  504. exit $ERRORS{'UNKNOWN'};
  505. }
  506. if (defined $opt_W && ! defined !$opt_C) {
  507. print "Need -C if using -W\n";
  508. exit $ERRORS{'UNKNOWN'};
  509. }elsif(defined $opt_W && defined $opt_C) {
  510. if ($opt_W >= $opt_C) {
  511. print "Warning (-W) cannot be greater than Critical (-C)!\n";
  512. exit $ERRORS{'UNKNOWN'};
  513. }
  514. }
  515. if (defined $opt_M) {
  516. if ($opt_M =~ /^(sendmail|qmail|postfix|exim|nullmailer)$/) {
  517. $mailq = $opt_M ;
  518. }elsif( $opt_M eq ''){
  519. $mailq = 'sendmail';
  520. }else{
  521. print "-M: $opt_M is not supported\n";
  522. exit $ERRORS{'UNKNOWN'};
  523. }
  524. }else{
  525. if (defined $utils::PATH_TO_QMAIL_QSTAT
  526. && -x $utils::PATH_TO_QMAIL_QSTAT)
  527. {
  528. $mailq = 'qmail';
  529. }
  530. elsif (-d '/var/lib/postfix' || -d '/var/local/lib/postfix'
  531. || -e '/usr/sbin/postfix' || -e '/usr/local/sbin/postfix')
  532. {
  533. $mailq = 'postfix';
  534. }
  535. elsif (-d '/usr/lib/exim4' || -d '/usr/local/lib/exim4'
  536. || -e '/usr/sbin/exim' || -e '/usr/local/sbin/exim')
  537. {
  538. $mailq = 'exim';
  539. }
  540. elsif (-d '/usr/lib/nullmailer' || -d '/usr/local/lib/nullmailer'
  541. || -e '/usr/sbin/nullmailer-send'
  542. || -e '/usr/local/sbin/nullmailer-send')
  543. {
  544. $mailq = 'nullmailer';
  545. }
  546. else {
  547. $mailq = 'sendmail';
  548. }
  549. }
  550. return $ERRORS{'OK'};
  551. }
  552. sub print_usage () {
  553. print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
  554. }
  555. sub print_help () {
  556. print_revision($PROGNAME,'@NP_VERSION@');
  557. print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
  558. print "\n";
  559. print_usage();
  560. print "\n";
  561. print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
  562. print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  563. print "-w (--warning) = Min. number of messages in queue to generate warning\n";
  564. print "-c (--critical) = Min. number of messages in queue to generate critical alert ( w < c )\n";
  565. print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n";
  566. print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
  567. print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  568. print "-M (--mailserver) = [ sendmail | qmail | postfix | exim | nullmailer ] (default = autodetect)\n";
  569. print "-h (--help)\n";
  570. print "-V (--version)\n";
  571. print "-v (--verbose) = debugging output\n";
  572. print "\n\n";
  573. print "Note: -w and -c are required arguments. -W and -C are optional.\n";
  574. print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
  575. print " -W and -C are applied message not yet preproccessed. (qmail)\n";
  576. print " This plugin tries to autodetect which mailserver you are running,\n";
  577. print " you can override the autodetection with -M.\n";
  578. print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
  579. print " to look at the queues. Mailq can usually only be accessed by root or \n";
  580. print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
  581. print "";
  582. print "\n\n";
  583. support();
  584. }