check_mailq.pl 22 KB

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