4
0

check_mailq.pl 22 KB

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