check_mailq.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #!/usr/local/bin/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., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. #
  23. ############################################################################
  24. use POSIX;
  25. use strict;
  26. use Getopt::Long;
  27. use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
  28. $opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
  29. %srcdomains %dstdomains);
  30. use lib utils.pm;
  31. use utils qw(%ERRORS &print_revision &support &usage );
  32. sub print_help ();
  33. sub print_usage ();
  34. sub process_arguments ();
  35. $ENV{'PATH'}='';
  36. $ENV{'BASH_ENV'}='';
  37. $ENV{'ENV'}='';
  38. $PROGNAME = "check_mailq";
  39. $mailq = 'sendmail'; # default
  40. $msg_q = 0 ;
  41. $msg_p = 0 ;
  42. $state = $ERRORS{'UNKNOWN'};
  43. Getopt::Long::Configure('bundling');
  44. $status = process_arguments();
  45. if ($status){
  46. print "ERROR: processing arguments\n";
  47. exit $ERRORS{"UNKNOWN"};
  48. }
  49. $SIG{'ALRM'} = sub {
  50. print ("ERROR: timed out waiting for $utils::PATH_TO_MAILQ \n");
  51. exit $ERRORS{"WARNING"};
  52. };
  53. alarm($opt_t);
  54. # switch based on MTA
  55. if ($mailq eq "sendmail") {
  56. ## open mailq
  57. if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
  58. if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
  59. print "ERROR: could not open $utils::PATH_TO_MAILQ \n";
  60. exit $ERRORS{'UNKNOWN'};
  61. }
  62. }elsif( defined $utils::PATH_TO_MAILQ){
  63. unless (-x $utils::PATH_TO_MAILQ) {
  64. print "ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
  65. exit $ERRORS{'UNKNOWN'};
  66. }
  67. } else {
  68. print "ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
  69. exit $ERRORS{'UNKNOWN'};
  70. }
  71. # single queue empty
  72. ##/var/spool/mqueue is empty
  73. # single queue: 1
  74. ## /var/spool/mqueue (1 request)
  75. ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
  76. ##h32E30p01763 2782 Wed Apr 2 15:03 <silvaATkpnqwest.pt>
  77. ## 8BITMIME
  78. ## <silvaATeunet.pt>
  79. # multi queue empty
  80. ##/var/spool/mqueue/q0/df is empty
  81. ##/var/spool/mqueue/q1/df is empty
  82. ##/var/spool/mqueue/q2/df is empty
  83. ##/var/spool/mqueue/q3/df is empty
  84. ##/var/spool/mqueue/q4/df is empty
  85. ##/var/spool/mqueue/q5/df is empty
  86. ##/var/spool/mqueue/q6/df is empty
  87. ##/var/spool/mqueue/q7/df is empty
  88. ##/var/spool/mqueue/q8/df is empty
  89. ##/var/spool/mqueue/q9/df is empty
  90. ##/var/spool/mqueue/qA/df is empty
  91. ##/var/spool/mqueue/qB/df is empty
  92. ##/var/spool/mqueue/qC/df is empty
  93. ##/var/spool/mqueue/qD/df is empty
  94. ##/var/spool/mqueue/qE/df is empty
  95. ##/var/spool/mqueue/qF/df is empty
  96. ## Total Requests: 0
  97. # multi queue: 1
  98. ##/var/spool/mqueue/q0/df is empty
  99. ##/var/spool/mqueue/q1/df is empty
  100. ##/var/spool/mqueue/q2/df is empty
  101. ## /var/spool/mqueue/q3/df (1 request)
  102. ##----Q-ID---- --Size-- -----Q-Time----- ------------Sender/Recipient------------
  103. ##h32De2f23534* 48 Wed Apr 2 14:40 nocol
  104. ## nouserATEUnet.pt
  105. ## canau
  106. ##/var/spool/mqueue/q4/df is empty
  107. ##/var/spool/mqueue/q5/df is empty
  108. ##/var/spool/mqueue/q6/df is empty
  109. ##/var/spool/mqueue/q7/df is empty
  110. ##/var/spool/mqueue/q8/df is empty
  111. ##/var/spool/mqueue/q9/df is empty
  112. ##/var/spool/mqueue/qA/df is empty
  113. ##/var/spool/mqueue/qB/df is empty
  114. ##/var/spool/mqueue/qC/df is empty
  115. ##/var/spool/mqueue/qD/df is empty
  116. ##/var/spool/mqueue/qE/df is empty
  117. ##/var/spool/mqueue/qF/df is empty
  118. ## Total Requests: 1
  119. while (<MAILQ>) {
  120. # match email addr on queue listing
  121. if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
  122. my $domain = $1;
  123. if (/^\w+/) {
  124. print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $verbose ;
  125. $srcdomains{$domain} ++;
  126. }
  127. next;
  128. }
  129. #
  130. # ...
  131. # sendmail considers a message with more than one destiny, say N, to the same MX
  132. # to have N messages in queue.
  133. # we will only consider one in this code
  134. if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
  135. ( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
  136. ( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) ||
  137. ( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
  138. print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $verbose ;
  139. $dstdomains{$1} ++;
  140. }
  141. if (/\s+\(I\/O\serror\)/) {
  142. print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $verbose ;
  143. $dstdomains{'UNKNOWN'} ++;
  144. }
  145. # Finally look at the overall queue length
  146. #
  147. if (/mqueue/) {
  148. print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
  149. if (/ \((\d+) request/) {
  150. #
  151. # single queue: first line
  152. # multi queue: one for each queue. overwrite on multi queue below
  153. $msg_q = $1 ;
  154. }
  155. } elsif (/^\s+Total\sRequests:\s(\d+)$/) {
  156. print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
  157. #
  158. # multi queue: last line
  159. $msg_q = $1 ;
  160. }
  161. }
  162. ## close mailq
  163. close (MAILQ);
  164. # declare an error if we also get a non-zero return code from mailq
  165. # unless already set to critical
  166. if ( $? ) {
  167. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  168. print "STDERR $?: $!\n" if $verbose;
  169. $msg = "$state: (stderr)\n";
  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 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 ($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 is $msg_q (threshold w = $opt_w)";
  185. $state = $ERRORS{'WARNING'};
  186. }else {
  187. $msg = "CRITICAL: 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. # declare an error if we also get a non-zero return code from mailq
  279. # unless already set to critical
  280. if ( $? ) {
  281. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  282. print "STDERR $?: $!\n" if $verbose;
  283. $msg = "$state: (stderr)\n";
  284. }
  285. ## shut off the alarm
  286. alarm(0);
  287. # check queue length
  288. if ($lines[0]=~/Kbytes in (\d+)/) {
  289. $msg_q = $1 ;
  290. }elsif ($lines[0]=~/Mail queue is empty/) {
  291. $msg_q = 0;
  292. }else{
  293. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  294. exit $ERRORS{'UNKNOWN'};
  295. }
  296. # check messages not processed
  297. #if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  298. # my $msg_p = $1;
  299. #}else{
  300. # print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  301. # exit $ERRORS{'UNKNOWN'};
  302. #}
  303. # check queue length(s)
  304. if ($msg_q == 0){
  305. $msg = "OK: mailq reports queue is empty";
  306. $state = $ERRORS{'OK'};
  307. } else {
  308. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  309. # overall queue length
  310. if ($msg_q < $opt_w) {
  311. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  312. $state = $ERRORS{'OK'};
  313. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  314. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  315. $state = $ERRORS{'WARNING'};
  316. }else {
  317. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  318. $state = $ERRORS{'CRITICAL'};
  319. }
  320. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  321. # are defined)
  322. #if (defined $opt_W) {
  323. # $msg .= "[Preprocessed = $msg_p]";
  324. # if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  325. # $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  326. # }elsif ($msg_p >= $opt_C ) {
  327. # $state = $ERRORS{"CRITICAL"} ;
  328. # }
  329. #}
  330. }
  331. } # end of ($mailq eq "postfixl")
  332. elsif ( $mailq eq "qmail" ) {
  333. # open qmail-qstat
  334. if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
  335. if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
  336. print "ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
  337. exit $ERRORS{'UNKNOWN'};
  338. }
  339. }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
  340. unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
  341. print "ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
  342. exit $ERRORS{'UNKNOWN'};
  343. }
  344. } else {
  345. print "ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
  346. exit $ERRORS{'UNKNOWN'};
  347. }
  348. @lines = <MAILQ>;
  349. # close qmail-qstat
  350. close MAILQ;
  351. # declare an error if we also get a non-zero return code from mailq
  352. # unless already set to critical
  353. if ( $? ) {
  354. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  355. print "STDERR $?: $!\n" if $verbose;
  356. $msg = "$state: (stderr)\n";
  357. }
  358. ## shut off the alarm
  359. alarm(0);
  360. # check queue length
  361. if ($lines[0]=~/^messages in queue: (\d+)/) {
  362. $msg_q = $1 ;
  363. }else{
  364. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  365. exit $ERRORS{'UNKNOWN'};
  366. }
  367. # check messages not processed
  368. if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
  369. my $msg_p = $1;
  370. }else{
  371. print "Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
  372. exit $ERRORS{'UNKNOWN'};
  373. }
  374. # check queue length(s)
  375. if ($msg_q == 0){
  376. $msg = "OK: qmail-qstat reports queue is empty";
  377. $state = $ERRORS{'OK'};
  378. } else {
  379. print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
  380. # overall queue length
  381. if ($msg_q < $opt_w) {
  382. $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
  383. $state = $ERRORS{'OK'};
  384. }elsif ($msg_q >= $opt_w && $msg_q < $opt_c) {
  385. $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
  386. $state = $ERRORS{'WARNING'};
  387. }else {
  388. $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
  389. $state = $ERRORS{'CRITICAL'};
  390. }
  391. # check messages not yet preprocessed (only compare is $opt_W and $opt_C
  392. # are defined)
  393. if (defined $opt_W) {
  394. $msg .= "[Preprocessed = $msg_p]";
  395. if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
  396. $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"} ;
  397. }elsif ($msg_p >= $opt_C ) {
  398. $state = $ERRORS{"CRITICAL"} ;
  399. }
  400. }
  401. }
  402. } # end of ($mailq eq "qmail")
  403. # Perfdata support
  404. print "$msg |mailq=$msg_q\n";
  405. exit $state;
  406. #####################################
  407. #### subs
  408. sub process_arguments(){
  409. GetOptions
  410. ("V" => \$opt_V, "version" => \$opt_V,
  411. "v" => \$opt_v, "verbose" => \$opt_v,
  412. "h" => \$opt_h, "help" => \$opt_h,
  413. "M:s" => \$opt_M, "mailserver:s" => \$opt_M, # mailserver (default sendmail)
  414. "w=i" => \$opt_w, "warning=i" => \$opt_w, # warning if above this number
  415. "c=i" => \$opt_c, "critical=i" => \$opt_c, # critical if above this number
  416. "W=i" => \$opt_W, "Warning=i" => \$opt_W, # warning if above this number
  417. "C=i" => \$opt_C, "Critical=i" => \$opt_C, # critical if above this number
  418. "t=i" => \$opt_t, "timeout=i" => \$opt_t
  419. );
  420. if ($opt_V) {
  421. print_revision($PROGNAME,'$Revision$ ');
  422. exit $ERRORS{'OK'};
  423. }
  424. if ($opt_h) {
  425. print_help();
  426. exit $ERRORS{'OK'};
  427. }
  428. if (defined $opt_v ){
  429. $verbose = $opt_v;
  430. }
  431. unless (defined $opt_t) {
  432. $opt_t = $utils::TIMEOUT ; # default timeout
  433. }
  434. unless ( defined $opt_w && defined $opt_c ) {
  435. print_usage();
  436. exit $ERRORS{'UNKNOWN'};
  437. }
  438. if ( $opt_w >= $opt_c) {
  439. print "Warning (-w) cannot be greater than Critical (-c)!\n";
  440. exit $ERRORS{'UNKNOWN'};
  441. }
  442. if (defined $opt_W && ! defined !$opt_C) {
  443. print "Need -C if using -W\n";
  444. exit $ERRORS{'UNKNOWN'};
  445. }elsif(defined $opt_W && defined $opt_C) {
  446. if ($opt_W >= $opt_C) {
  447. print "Warning (-W) cannot be greater than Critical (-C)!\n";
  448. exit $ERRORS{'UNKNOWN'};
  449. }
  450. }
  451. if (defined $opt_M) {
  452. if ($opt_M =~ /sendmail/ || $opt_M =~ /qmail/ || $opt_M =~ /postfix/ ) {
  453. $mailq = $opt_M ;
  454. }elsif( $opt_M eq ''){
  455. $mailq = 'sendmail';
  456. }else{
  457. print "-M: $opt_M is not supported\n";
  458. exit $ERRORS{'UNKNOWN'};
  459. }
  460. }else{
  461. $mailq = 'sendmail' ;
  462. }
  463. return $ERRORS{'OK'};
  464. }
  465. sub print_usage () {
  466. print "Usage: $PROGNAME [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
  467. }
  468. sub print_help () {
  469. print_revision($PROGNAME,'$Revision$');
  470. print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
  471. print "\n";
  472. print_usage();
  473. print "\n";
  474. print " Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail)\n";
  475. print " Feedback/patches to support non-sendmail mailqueue welcome\n\n";
  476. print "-w (--warning) = Min. number of messages in queue to generate warning\n";
  477. print "-c (--critical) = Min. number of messages in queu to generate critical alert ( w < c )\n";
  478. print "-W (--Warning) = Min. number of messages for same domain in queue to generate warning\n";
  479. print "-C (--Critical) = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
  480. print "-t (--timeout) = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
  481. print "-M (--mailserver) = [ sendmail | qmail | postfix ] (default = sendmail)\n";
  482. print "-h (--help)\n";
  483. print "-V (--version)\n";
  484. print "-v (--verbose) = debugging output\n";
  485. print "\n\n";
  486. print "Note: -w and -c are required arguments. -W and -C are optional.\n";
  487. print " -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail)\n";
  488. print " -W and -C are applied message not yet preproccessed. (qmail)\n";
  489. print " This plugin uses the system mailq command (sendmail) or qmail-stat (qmail)\n";
  490. print " to look at the queues. Mailq can usually only be accessed by root or \n";
  491. print " a TrustedUser. You will have to set appropriate permissions for the plugin to work.\n";
  492. print "";
  493. print "\n\n";
  494. support();
  495. }