check_mailq.pl 22 KB

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