You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

778 lines
25 KiB

  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. close(MAILQ);
  303. if ( $? ) {
  304. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ$mailq_args",$/;
  305. exit $ERRORS{CRITICAL};
  306. }
  307. ## shut off the alarm
  308. alarm(0);
  309. # check queue length
  310. if ($lines[0]=~/Kbytes in (\d+)/) {
  311. $msg_q = $1 ;
  312. }elsif ($lines[0]=~/Mail queue is empty/) {
  313. $msg_q = 0;
  314. }else{
  315. print "Couldn't match $utils::PATH_TO_MAILQ$mailq_args output\n";
  316. exit $ERRORS{'UNKNOWN'};
  317. }
  318. # check messages not processed
  319. #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  320. # my $msg_p = $1;
  321. #}else{
  322. # print "Couldn't match $utils::PATH_TO_MAILQ output\n";
  323. # exit $ERRORS{'UNKNOWN'};
  324. #}
  325. # check queue length(s)
  326. if ($msg_q == 0){
  327. $msg = "OK: $mailq mailq reports queue is empty";
  328. $state = $ERRORS{'OK'};
  329. } else {
  330. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  331. # overall queue length
  332. if ($msg_q < $opt_w) {
  333. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  334. $state = $ERRORS{'OK'};
  335. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  336. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  337. $state = $ERRORS{'WARNING'};
  338. }else {
  339. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  340. $state = $ERRORS{'CRITICAL'};
  341. }
  342. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  343. # are defined)
  344. #if (defined $opt_W) {
  345. # $msg .= "[Preprocessed = $msg_p]";
  346. # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  347. # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  348. # }elsif ($msg_p >= $opt_C ) {
  349. # $state = $ERRORS{"CRITICAL"} ;
  350. # }
  351. #}
  352. }
  353. } # end of ($mailq eq "postfix")
  354. elsif ( $mailq eq "qmail" ) {
  355. # open qmail-qstat
  356. if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
  357. if (! open (MAILQ, "$sudo $utils::PATH_TO_QMAIL_QSTAT | " ) ) {
  358. print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
  359. exit $ERRORS{'UNKNOWN'};
  360. }
  361. }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
  362. unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
  363. print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
  364. exit $ERRORS{'UNKNOWN'};
  365. }
  366. } else {
  367. print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
  368. exit $ERRORS{'UNKNOWN'};
  369. }
  370. @lines = <MAILQ>;
  371. # close qmail-qstat
  372. close MAILQ;
  373. if ( $? ) {
  374. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
  375. exit $ERRORS{CRITICAL};
  376. }
  377. ## shut off the alarm
  378. alarm(0);
  379. # check queue length
  380. if ($lines[0]=~/^messages in queue: (\d+)/) {
  381. $msg_q = $1 ;
  382. }else{
  383. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  384. exit $ERRORS{'UNKNOWN'};
  385. }
  386. # check messages not processed
  387. if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  388. my $msg_p = $1;
  389. }else{
  390. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  391. exit $ERRORS{'UNKNOWN'};
  392. }
  393. # check queue length(s)
  394. if ($msg_q == 0){
  395. $msg = "OK: qmail-qstat reports queue is empty";
  396. $state = $ERRORS{'OK'};
  397. } else {
  398. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  399. # overall queue length
  400. if ($msg_q < $opt_w) {
  401. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  402. $state = $ERRORS{'OK'};
  403. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  404. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  405. $state = $ERRORS{'WARNING'};
  406. }else {
  407. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  408. $state = $ERRORS{'CRITICAL'};
  409. }
  410. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  411. # are defined)
  412. if (defined $opt_W) {
  413. $msg .= "[Preprocessed = $msg_p]";
  414. if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  415. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  416. }elsif ($msg_p >= $opt_C ) {
  417. $state = $ERRORS{"CRITICAL"} ;
  418. }
  419. }
  420. }
  421. } # end of ($mailq eq "qmail")
  422. elsif ( $mailq eq "exim" ) {
  423. ## open mailq
  424. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  425. if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
  426. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  427. exit $ERRORS{'UNKNOWN'};
  428. }
  429. }elsif( defined $utils::PATH_TO_MAILQ){
  430. unless (-x $utils::PATH_TO_MAILQ) {
  431. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  432. exit $ERRORS{'UNKNOWN'};
  433. }
  434. } else {
  435. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  436. exit $ERRORS{'UNKNOWN'};
  437. }
  438. while (<MAILQ>) {
  439. #22m 1.7K 19aEEr-0007hx-Dy <> *** frozen ***
  440. #root@exlixams.glups.fr
  441. if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
  442. $msg_q++ ;
  443. }
  444. }
  445. close(MAILQ) ;
  446. if ( $? ) {
  447. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_MAILQ",$/;
  448. exit $ERRORS{CRITICAL};
  449. }
  450. if ($msg_q < $opt_w) {
  451. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  452. $state = $ERRORS{'OK'};
  453. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  454. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  455. $state = $ERRORS{'WARNING'};
  456. }else {
  457. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  458. $state = $ERRORS{'CRITICAL'};
  459. }
  460. } # end of ($mailq eq "exim")
  461. elsif ( $mailq eq "opensmtpd" ) {
  462. ## open smtpctl
  463. if ( defined $utils::PATH_TO_SMTPCTL && -x $utils::PATH_TO_SMTPCTL ) {
  464. if (! open (MAILQ, "$sudo $utils::PATH_TO_SMTPCTL show queue | " ) ) {
  465. print "ERROR: could not open $utils::PATH_TO_SMTPCTL \n";
  466. exit $ERRORS{'UNKNOWN'};
  467. }
  468. }elsif( defined $utils::PATH_TO_SMTPCTL){
  469. unless (-x $utils::PATH_TO_SMTPCTL) {
  470. print "ERROR: $utils::PATH_TO_SMTPCTL is not executable by (uid $>:gid($)))\n";
  471. exit $ERRORS{'UNKNOWN'};
  472. }
  473. } else {
  474. print "ERROR: \$utils::PATH_TO_SMTPCTL is not defined\n";
  475. exit $ERRORS{'UNKNOWN'};
  476. }
  477. while (<MAILQ>) {
  478. # 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
  479. if (/^.*|.*|.*|.*|.*|.*|.*|.*|.*|.*|.*|.*|.*|.*$/) {
  480. $msg_q++ ;
  481. }
  482. }
  483. close(MAILQ);
  484. if ( $? ) {
  485. print "CRITICAL: Error code ".($?>>8)." returned from $utils::PATH_TO_SMTPCTL",$/;
  486. exit $ERRORS{CRITICAL};
  487. }
  488. if ($msg_q < $opt_w) {
  489. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  490. $state = $ERRORS{'OK'};
  491. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  492. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  493. $state = $ERRORS{'WARNING'};
  494. }else {
  495. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  496. $state = $ERRORS{'CRITICAL'};
  497. }
  498. } # end of ($mailq eq "opensmtpd")
  499. elsif ( $mailq eq "nullmailer" ) {
  500. ## open mailq
  501. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  502. if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
  503. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  504. exit $ERRORS{'UNKNOWN'};
  505. }
  506. }elsif( defined $utils::PATH_TO_MAILQ){
  507. unless (-x $utils::PATH_TO_MAILQ) {
  508. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  509. exit $ERRORS{'UNKNOWN'};
  510. }
  511. } else {
  512. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  513. exit $ERRORS{'UNKNOWN'};
  514. }
  515. while (<MAILQ>) {
  516. #2006-06-22 16:00:00 282 bytes
  517. 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$/) {
  518. $msg_q++ ;
  519. }
  520. }
  521. close(MAILQ) ;
  522. if ($msg_q < $opt_w) {
  523. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  524. $state = $ERRORS{'OK'};
  525. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  526. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  527. $state = $ERRORS{'WARNING'};
  528. }else {
  529. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  530. $state = $ERRORS{'CRITICAL'};
  531. }
  532. } # end of ($mailq eq "nullmailer")
  533. elsif ( $mailq eq "opensmtp" ) {
  534. ## open mailq
  535. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  536. if (! open (MAILQ, "$sudo $utils::PATH_TO_MAILQ | " ) ) {
  537. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  538. exit $ERRORS{'UNKNOWN'};
  539. }
  540. }elsif( defined $utils::PATH_TO_MAILQ){
  541. unless (-x $utils::PATH_TO_MAILQ) {
  542. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  543. exit $ERRORS{'UNKNOWN'};
  544. }
  545. } else {
  546. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  547. exit $ERRORS{'UNKNOWN'};
  548. }
  549. $msg_q++ while (<MAILQ>);
  550. close(MAILQ) ;
  551. if ($msg_q < $opt_w) {
  552. $msg = "OK: $mailq mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  553. $state = $ERRORS{'OK'};
  554. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  555. $msg = "WARNING: $mailq mailq is $msg_q (threshold w = $opt_w)";
  556. $state = $ERRORS{'WARNING'};
  557. }else {
  558. $msg = "CRITICAL: $mailq mailq is $msg_q (threshold c = $opt_c)";
  559. $state = $ERRORS{'CRITICAL'};
  560. }
  561. } # end of ($mailq eq "opensmtp")
  562. # Perfdata support
  563. print "$msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
  564. exit $state;
  565. #####################################
  566. #### subs
  567. sub process_arguments(){
  568. GetOptions
  569. ("V" => \$opt_V, "version" => \$opt_V,
  570. "v" => \$opt_v, "verbose" => \$opt_v,
  571. "h" => \$opt_h, "help" => \$opt_h,
  572. "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
  573. "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
  574. "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
  575. "t=i" => \$opt_t, "timeout=i" => \$opt_t,
  576. "s" => \$opt_s, "sudo" => \$opt_s,
  577. "d:s" => \$opt_d, "configdir:s" => \$opt_d
  578. );
  579. if ($opt_V) {
  580. print_revision($PROGNAME,'@NP_VERSION@');
  581. exit $ERRORS{'OK'};
  582. }
  583. if ($opt_h) {
  584. print_help();
  585. exit $ERRORS{'OK'};
  586. }
  587. if (defined $opt_v ){
  588. $verbose = $opt_v;
  589. }
  590. unless (defined $opt_t) {
  591. $opt_t = $utils::TIMEOUT ; # default timeout
  592. }
  593. unless ( defined $opt_w && defined $opt_c ) {
  594. print_usage();
  595. exit $ERRORS{'UNKNOWN'};
  596. }
  597. if ( $opt_w >= $opt_c) {
  598. print "Warning (-w) cannot be greater than Critical (-c)!\n";
  599. exit $ERRORS{'UNKNOWN'};
  600. }
  601. if (defined $opt_W && ! defined !$opt_C) {
  602. print "Need -C if using -W\n";
  603. exit $ERRORS{'UNKNOWN'};
  604. }elsif(defined $opt_W && defined $opt_C) {
  605. if ($opt_W >= $opt_C) {
  606. print "Warning (-W) cannot be greater than Critical (-C)!\n";
  607. exit $ERRORS{'UNKNOWN'};
  608. }
  609. }
  610. if (defined $opt_M) {
  611. if ($opt_M =~ /^(sendmail|qmail|postfix|exim|nullmailer|opensmtpd)$/) {
  612. $mailq = $opt_M ;
  613. }elsif( $opt_M eq ''){
  614. $mailq = 'sendmail';
  615. }else{
  616. print "-M: $opt_M is not supported\n";
  617. exit $ERRORS{'UNKNOWN'};
  618. }
  619. }else{
  620. if (defined $utils::PATH_TO_QMAIL_QSTAT
  621. && -x $utils::PATH_TO_QMAIL_QSTAT)
  622. {
  623. $mailq = 'qmail';
  624. }
  625. elsif (-d '/var/lib/postfix' || -d '/var/local/lib/postfix'
  626. || -e '/usr/sbin/postfix' || -e '/usr/local/sbin/postfix')
  627. {
  628. $mailq = 'postfix';
  629. }
  630. elsif (-d '/usr/lib/exim4' || -d '/usr/local/lib/exim4'
  631. || -e '/usr/sbin/exim' || -e '/usr/local/sbin/exim')
  632. {
  633. $mailq = 'exim';
  634. }
  635. elsif (-d '/usr/lib/nullmailer' || -d '/usr/local/lib/nullmailer'
  636. || -e '/usr/sbin/nullmailer-send'
  637. || -e '/usr/local/sbin/nullmailer-send')
  638. {
  639. $mailq = 'nullmailer';
  640. }
  641. elsif (defined $utils::PATH_TO_SMTPCTL && -x $utils::PATH_TO_SMTPCTL)
  642. {
  643. $mailq = 'opensmtpd';
  644. }
  645. else {
  646. $mailq = 'sendmail';
  647. }
  648. }
  649. return $ERRORS{'OK'};
  650. }
  651. sub print_usage () {
  652. print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-s] [-d <CONFIGDIR>] [-v]\n";
  653. }
  654. sub print_help () {
  655. print_revision($PROGNAME,'@NP_VERSION@');
  656. print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
  657. print "\n";
  658. print_usage();
  659. print "\n";
  660. print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
  661. print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  662. print "-w (--warning) = Min. number of messages in queue to generate warning\n";
  663. print "-c (--critical) = Min. number of messages in queue to generate critical alert ( w < c )\n";
  664. print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n";
  665. print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
  666. print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  667. print "-M (--mailserver) = [ sendmail | qmail | postfix | exim | nullmailer | opensmtpd ] (default = autodetect)\n";
  668. print "-h (--help)\n";
  669. print "-V (--version)\n";
  670. print "-v (--verbose) = debugging output\n";
  671. print "\n\n";
  672. print "Note: -w and -c are required arguments. -W and -C are optional.\n";
  673. print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
  674. print " -W and -C are applied message not yet preproccessed. (qmail)\n";
  675. print " This plugin tries to autodetect which mailserver you are running,\n";
  676. print " you can override the autodetection with -M.\n";
  677. print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
  678. print " to look at the queues. Mailq can usually only be accessed by root or \n";
  679. print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
  680. print "";
  681. print "\n\n";
  682. support();
  683. }