4
0

check_mailq.pl 25 KB

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