summaryrefslogtreecommitdiffstats
path: root/web/attachments/74206-check_mailq.diffs
blob: 67fa08612c3675d32750fa9234d151d706adc663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
--- plugins-scripts/utils.pm.in.orig	2004-01-13 12:45:04.000000000 -0500
+++ plugins-scripts/utils.pm.in	2004-01-13 12:45:39.000000000 -0500
@@ -43,6 +43,7 @@
 $PATH_TO_SMBCLIENT = "@PATH_TO_SMBCLIENT@" ;
 $PATH_TO_MAILQ   = "@PATH_TO_MAILQ@";
 $PATH_TO_QMAIL_QSTAT = "@PATH_TO_QMAIL_QSTAT@";
+$PATH_TO_SENDMAIL_CF = "@PATH_TO_SENDMAIL_CF@";
 
 ## common variables
 $TIMEOUT = 15;
--- plugins-scripts/check_mailq.pl.orig	2004-01-13 12:45:47.000000000 -0500
+++ plugins-scripts/check_mailq.pl	2004-01-20 14:53:05.000000000 -0500
@@ -28,15 +28,16 @@
 use strict;
 use Getopt::Long;
 use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c $opt_t
-					$opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
-					%srcdomains %dstdomains);
+					$opt_M $mailq $status $state $msg $msg_q $msg_p $opt_W $opt_C $opt_f
+					$mailq @lines %srcdomains %dstdomains);
 use lib  utils.pm;
 use utils qw(%ERRORS &print_revision &support &usage );
-
+use File::Glob ':glob';
 
 sub print_help ();
 sub print_usage ();
 sub process_arguments ();
+sub get_queue_directories (;$);
 
 $ENV{'PATH'}='';
 $ENV{'BASH_ENV'}=''; 
@@ -47,7 +48,7 @@
 $msg_p = 0 ;
 $state = $ERRORS{'UNKNOWN'};
 
-Getopt::Long::Configure('bundling');
+Getopt::Long::Configure('bundling','no_ignore_case');
 $status = process_arguments();
 if ($status){
 	print "ERROR: processing arguments\n";
@@ -64,6 +65,22 @@
 
 if ($mailq eq "sendmail") {
 
+## get configuration file, and from that, the queue directories
+	my @queuedirs=();
+	my $configfile="";
+	if ( defined($opt_f) && -r $opt_f ) {
+	   $configfile="-C$opt_f";
+	   @queuedirs=get_queue_directories($opt_f);
+	}else {
+	   @queuedirs=get_queue_directories();
+	}
+
+	if (!defined($queuedirs[0])) {
+	   print "UNKNOWN: check_mailq: no queue directories | msq_q = 0\n";
+	   exit $ERRORS{"UNKNOWN"};
+	}
+	my $qpattern=join("|",@queuedirs);
+
 	## open mailq 
 	if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
 		if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
@@ -163,21 +180,16 @@
 
 		# Finally look at the overall queue length
 		#
-		if (/mqueue/) {
+		# the pattern helps us get one or more queue directories, and adds up
+		# all the requests
+		if (/\b$qpattern\b/) {
 			print "$utils::PATH_TO_MAILQ = $_ "if $verbose ;
-			if (/ \((\d+) request/) {
-	    	#
-		    # single queue: first line
-		    # multi queue: one for each queue. overwrite on multi queue below
-	  	  $msg_q = $1 ;
+			if (/empty/) {
+			}elsif (/\((\d+)\s*requests\)/i) {
+				$msg_q+=$1;
+				print "size = $1, msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $verbose;
 			}
-		} elsif (/^\s+Total\sRequests:\s(\d+)$/) {
-			print "$utils::PATH_TO_MAILQ = $_ \n" if $verbose ;
-			#
-			# multi queue: last line
-			$msg_q = $1 ;
 		}
-	
 	}
 	
 
@@ -519,7 +531,8 @@
 		 "c=i" => \$opt_c, "critical=i" => \$opt_c,	  # critical if above this number
 		 "W=i" => \$opt_W, "Warning=i"  => \$opt_W,   # warning if above this number
 		 "C=i" => \$opt_C, "Critical=i" => \$opt_C,	  # critical if above this number
-		 "t=i" => \$opt_t, "timeout=i"  => \$opt_t 
+		 "t=i" => \$opt_t, "timeout=i"  => \$opt_t,
+		 "f=s" => \$opt_f, "configfile"	=> \$opt_f
 		 );
 
 	if ($opt_V) {
@@ -572,12 +585,19 @@
 	}else{
 		$mailq = 'sendmail' ;
 	}
+
+	if (defined $opt_f) {
+		if (! -r $opt_f) {
+			print "Can't read config file (-f): $opt_f!W\n";
+			exit $ERRORS{'UNKNOWN'};
+		}
+	}
 		
 	return $ERRORS{'OK'};
 }
 
 sub print_usage () {
-	print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose]\n";
+	print "Usage: $PROGNAME -w <warn> -c <crit> [-W <warn>] [-C <crit>] [-M <MTA>] [-t <timeout>] [-v verbose] [-f configfile]\n";
 }
 
 sub print_help () {
@@ -594,6 +614,7 @@
 	print "-C (--Critical)  = Min. number of messages for same domain in queue to generate critical alert ( W < C )\n";
 	print "-t (--timeout)   = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
 	print "-M (--mailserver) = [ sendmail | qmail | postfix | exim ] (default = sendmail)\n";
+	print "-f (--configfile) = configuration file for sendmail (default = $utils::PATH_TO_SENDMAIL_CF)\n";
 	print "-h (--help)\n";
 	print "-V (--version)\n";
 	print "-v (--verbose)   = debugging output\n";
@@ -608,3 +629,45 @@
 	print "\n\n";
 	support();
 }
+
+sub get_queue_directories (;$)
+{
+	my $cfin=shift;
+	my @cfs=();
+	my @qdirs=();
+
+	if ($cfin) {
+		@cfs=($cfin);
+	}else {
+		if (-r $utils::PATH_TO_SENDMAIL_CF) {
+			@cfs=($utils::PATH_TO_SENDMAIL_CF);
+		}else {
+			@cfs=qw(/etc/mail/sendmail.cf);
+		}
+	}
+
+	my @cffiles=grep { -r $_} @cfs;
+	if (scalar(@cffiles)==0) {
+		warn "unable to find a sendmail configuration file";
+		return (undef);
+	}
+
+	my $qdir;
+	foreach my $cf (@cffiles) {
+		if (open(CF,$cf)) {
+			while(<CF>) {
+				chomp;
+				next unless (/^O\s*QueueDirectory=(.*)$/ && ($qdir=$1));
+				push(@qdirs,bsd_glob($qdir));
+				last;
+			}
+		}
+		close(CF);
+	}
+
+	if (@qdirs==0) {
+		warn "no QueueDirectory variable found in sendmail configuration file";
+		return (undef);
+	};
+	return @qdirs;
+}