check_mailq.pl 22 KB

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