blib/lib/Monitoring/Sneck/Checks/mailq.pm

filter: Statementstmt 98.1% 108 / 110 Branchbran 94.2% 49 / 52 Conditioncond 63.8% 30 / 47 MC/DCmcdc 37.0% 10 / 27 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 84.7% 216 / 255 CC 49 SCAR 25.5
CC
49
Cov
89%
check
20.5
postfix_queue
14.3
exim_queue
11.1
CRAP 12.8
« blib/lib/Monitoring/Sneck/Checks/tls_protocols.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/pkg_audit.pm »
1 package Monitoring::Sneck::Checks::mailq;
2  
3 14 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::mailq - checks the mail queue of Postfix or Exim
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 14 our $VERSION = '0.0.1';
19  
20 # Where postqueue and exim are. Set by tests. Undef means the first of
21 # their PATHS that is there.
22 14 our $POSTQUEUE;
23 14 our @POSTQUEUE_PATHS = ( '/usr/sbin/postqueue', '/usr/local/sbin/postqueue' );
24 14 our $EXIM;
25 14 our @EXIM_PATHS = ( '/usr/sbin/exim4', '/usr/sbin/exim', '/usr/local/sbin/exim' );
26  
27 # Seconds in each unit exim -bp gives ages in.
28 14 our %EXIM_UNITS = ( s => 1, m => 60, h => 3600, d => 86400 );
29  
30 =head1 SYNOPSIS
31  
32 mailq|perl:mailq
33 mailq_busy|perl:mailq -w 500 -c 2000 -W 86400
34 mailq_exim|perl:mailq -m exim
35  
36 =head1 DESCRIPTION
37  
38 Checks how many messages are queued and how old the oldest is, through
39 postqueue -j for Postfix, 3.1 or newer, or exim -bp for Exim. Without
40 --mta, Postfix is used if postqueue is there, else Exim.
41  
42 - CRITICAL :: The count or oldest over its critical.
43  
44 - WARNING :: The count or oldest over its warning.
45  
46 - UNKNOWN :: Neither being there, or the command failing, such as
47 Postfix not running or Exim not run as root or a admin user.
48  
49 - OK :: Everything else.
50  
51 The status line says how many are in each Postfix queue, such as
52 deferred and hold, or how many are frozen for Exim. The oldest
53 messages follow it, with the reason they are delayed for Postfix.
54  
55 Exim only gives ages to the minute, hour, or day, depending on how
56 old.
57  
58 =head1 OPTIONS
59  
60 - -m, --mta :: postfix or exim, instead of looking for them.
61  
62 - -w, --warning :: Range for the count.
63 Default :: 50
64  
65 - -c, --critical :: Range for the count.
66 Default :: 200
67  
68 - -W, --age-warning :: Range for the age of the oldest, in seconds.
69 Default :: 14400
70  
71 - -C, --age-critical :: Range for the age of the oldest, in seconds.
72  
73 - -l, --list :: How many of the oldest to list after the status line.
74 Default :: 5
75  
76 =cut
77  
78 80 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
79 80 return ( 'mta|m=s', 'warning|w=s', 'critical|c=s', 'age-warning|W=s', 'age-critical|C=s', 'list|l=i' );
80 }
81  
82 80 ▶sub defaults { return { warning => '50', critical => '200', 'age-warning' => '14400', list => 5 } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
83  
84 80 ▶sub ranges { return ( 'warning', 'critical', 'age-warning', 'age-critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
85  
86 80 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
87 80 my ( $self, $options ) = @_;
88  
89 80 my @problems;
90 push( @problems, '--mta must be postfix or exim' )
91 80 ▶ if defined( $options->{mta} ) && $options->{mta} ne 'postfix' && $options->{mta} ne 'exim';
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"mta"} and $options->{"mta"} ne "postfix" and $options->{"mta"} ne "exim" 1 79 80
Condition100%6/6
defined $options->{"mta"} and $options->{"mta"} ne "postfix" and $options->{"mta"} ne "exim"
!ll&&!rl&&r
7271
defined $options->{"mta"} and $options->{"mta"} ne "postfix"
!ll&&!rl&&r
7118
MC/DC100%3/3
defined $options->{"mta"} and $options->{"mta"} ne "postfix" and $options->{"mta"} ne "exim"
defined $options->{"mta"} $options->{"mta"} ne "postfix" $options->{"mta"} ne "exim"
Truth table100%4/4
A and B and C
A= defined $options->{"mta"}B= $options->{"mta"} ne "postfix"C= $options->{"mta"} ne "exim"
ABCresult
0XX0
10X0
1100
1111
92 80 ▶ push( @problems, '--list must be 0 or more' ) if $options->{list} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"list"} < 0 0 80 80
93  
94 80 return @problems;
95 } ## end sub validate
96  
97 =head1 METHODS
98  
99 =head2 check
100  
101 Does the check.
102  
103 =cut
104  
105 79 ▶sub check {
Subroutine check: called CC=20 SCAR=30.2
Pod documented
106 79 my ($self) = @_;
107  
108 0 ▶ my $postqueue = $POSTQUEUE // ( grep { -x $_ } @POSTQUEUE_PATHS )[0];
Condition66%2/3
$Monitoring::Sneck::Checks::mailq::POSTQUEUE // (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::POSTQUEUE_PATHS)[0]
l!l&&r!l&&!r
7504
MC/DC50%1/2
$Monitoring::Sneck::Checks::mailq::POSTQUEUE // (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::POSTQUEUE_PATHS)[0]
$Monitoring::Sneck::Checks::mailq::POSTQUEUE (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::POSTQUEUE_PATHS)[0]
Truth table66%2/3
A // B
A= $Monitoring::Sneck::Checks::mailq::POSTQUEUEB= (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::POSTQUEUE_PATHS)[0]
ABresult
1X1
011
000
109 0 ▶ my $exim = $EXIM // ( grep { -x $_ } @EXIM_PATHS )[0];
Condition66%2/3
$Monitoring::Sneck::Checks::mailq::EXIM // (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::EXIM_PATHS)[0]
l!l&&r!l&&!r
7702
MC/DC50%1/2
$Monitoring::Sneck::Checks::mailq::EXIM // (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::EXIM_PATHS)[0]
$Monitoring::Sneck::Checks::mailq::EXIM (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::EXIM_PATHS)[0]
Truth table66%2/3
A // B
A= $Monitoring::Sneck::Checks::mailq::EXIMB= (grep {-x $_;} @Monitoring::Sneck::Checks::mailq::EXIM_PATHS)[0]
ABresult
1X1
011
000
110 79 my $mta = $self->opt('mta');
111 79 ▶ if ( !defined($mta) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mta) 71 8 79
112 71 ▶ $mta = 'exim' if defined($exim);
Branch: 1 branch
BranchTrueFalseTotal
if defined $exim 70 1 71
113 71 ▶ $mta = 'postfix' if defined($postqueue);
Branch: 1 branch
BranchTrueFalseTotal
if defined $postqueue 68 3 71
114 }
115 79 ▶ return $self->finish( $self->UNKNOWN, 'neither Postfix nor Exim found' ) if !defined($mta);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mta 1 78 79
116  
117 78 my $messages;
118 78 ▶ if ( $mta eq 'postfix' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mta eq 'postfix') { } 69 9 78
119 69 ▶ return $self->finish( $self->UNKNOWN, 'postqueue is not there' ) if !defined($postqueue) || !-x $postqueue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $postqueue and -x $postqueue 1 68 69
Condition66%2/3
defined $postqueue and -x $postqueue
!ll&&!rl&&r
1068
MC/DC50%1/2
defined $postqueue and -x $postqueue
defined $postqueue -x $postqueue
Truth table66%2/3
A and B
A= defined $postqueueB= -x $postqueue
ABresult
0X0
100
111
120 68 $messages = $self->postfix_queue($postqueue);
121 } else {
122 9 ▶ return $self->finish( $self->UNKNOWN, 'exim is not there' ) if !defined($exim) || !-x $exim;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exim and -x $exim 0 9 9
Condition33%1/3
defined $exim and -x $exim
!ll&&!rl&&r
009
MC/DC0%0/2
defined $exim and -x $exim
defined $exim -x $exim
Truth table33%1/3
A and B
A= defined $eximB= -x $exim
ABresult
0X0
100
111
123 9 $messages = $self->exim_queue($exim);
124 }
125  
126 55 my $count = scalar( @{$messages} );
127 55 my @oldest = sort { $b->{age} <=> $a->{age} } @{$messages};
128 55 ▶ my $age = $count ? $oldest[0]{age} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$count ? : 44 11 55
129 55 my %states;
130 55 $states{ $_->{state} }++ foreach @{$messages};
131  
132 55 my $code = $self->worst(
133 $self->threshold_status( $count, $self->opt('warning'), $self->opt('critical') ),
134 $self->threshold_status( $age, $self->opt('age-warning'), $self->opt('age-critical') )
135 );
136 55 $self->perfdata(
137 label => 'messages',
138 value => $count,
139 warning => $self->opt('warning'),
140 critical => $self->opt('critical'),
141 min => 0,
142 );
143 55 $self->perfdata(
144 label => 'oldest',
145 value => $age,
146 uom => 's',
147 warning => $self->opt('age-warning'),
148 critical => $self->opt('age-critical'),
149 min => 0,
150 );
151  
152 55 ▶ my @parts = ( $count . ' message' . ( $count == 1 ? '' : 's' ) . ' in the ' . $mta . ' queue' );
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 13 42 55
153 55 ▶ push( @parts, 'oldest ' . _age($age) ) if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 44 11 55
154 55 push( @parts, map { $states{$_} . ' ' . $_ } grep { $_ ne 'queued' } sort( keys(%states) ) );
155  
156 55 my @details;
157 55 foreach my $message ( splice( @oldest, 0, $self->opt('list') ) ) {
158 88 my @recipients = @{ $message->{recipients} };
159 88 ▶ my $to = $recipients[0] // 'no one';
Condition50%1/2
$recipients[0] // "no one"
l!l
880
MC/DC0%0/1
$recipients[0] // "no one"
$recipients[0]
Truth table50%1/2
A
A= $recipients[0]
Aresult
11
00
160 88 ▶ $to = $to . ' and ' . ( @recipients - 1 ) . ' more' if @recipients > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @recipients > 1 18 70 88
161 88 my $detail = $message->{id} . ' ' . _age( $message->{age} ) . ' ' . $message->{sender} . ' to ' . $to;
162 88 ▶ $detail = $detail . ', ' . $message->{reason} if defined( $message->{reason} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $message->{"reason"} 40 48 88
163 88 push( @details, $detail );
164 }
165  
166 55 return $self->finish( $code, join( ', ', @parts ), @details );
167 } ## end sub check
168  
169 =head2 postfix_queue
170  
171 Reads the Postfix queue through postqueue -j. Dies if it fails.
172  
173 - postqueue :: The path to postqueue.
174  
175 Returns a array ref of hash refs of id, age, in seconds, state, the
176 queue it is in, such as deferred or hold, sender, <> for a bounce,
177 recipients, a array ref, and reason, why the first recipient with one
178 is delayed, or undef.
179  
180 my $messages = $check->postfix_queue('/usr/sbin/postqueue');
181 # [ { id => '4B7F9A1234', age => 7200, state => 'deferred', sender => 'alice@example.org',
182 # recipients => ['bob@example.net'], reason => 'connect to mx.example.net[192.0.2.25]:25: Connection refused' } ]
183  
184 =cut
185  
186 68 ▶sub postfix_queue {
Subroutine postfix_queue: called CC=13 SCAR=26.6
Pod documented
187 68 my ( $self, $postqueue ) = @_;
188  
189 68 my ( $exit, $output ) = $self->run_command( $postqueue, '-j' );
190 60 ▶ die( 'postqueue -j exited ' . $exit . ', ' . ( ( split( /\n/, $output ) )[0] // 'saying nothing' ) . "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $exit 5 55 60
Condition50%1/2
(split(/\n/u, $output, 0))[0] // "saying nothing"
l!l
50
MC/DC0%0/1
(split(/\n/u, $output, 0))[0] // "saying nothing"
(split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (split(/\n/u, $output, 0))[0]
Aresult
11
00
191 if $exit;
192  
193 55 require JSON;
194 55 my $json = JSON->new;
195 55 my $now = time;
196 55 my @messages;
197 55 foreach my $line ( grep { /\S/ } split( /\n/, $output ) ) {
198 98 my $message = eval { $json->decode($line) };
199 98 ▶ die( 'postqueue -j printed something not JSON, "' . $line . '"' . "\n" ) if ref($message) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $message ne "HASH" 4 94 98
200 94 ▶ my @recipients = @{ $message->{recipients} // [] };
Condition50%1/2
$message->{"recipients"} // []
l!l
940
MC/DC0%0/1
$message->{"recipients"} // []
$message->{"recipients"}
Truth table50%1/2
A
A= $message->{"recipients"}
Aresult
11
00
201 94 ▶ my $sender = $message->{sender} // '';
Condition50%1/2
$message->{"sender"} // ""
l!l
940
MC/DC0%0/1
$message->{"sender"} // ""
$message->{"sender"}
Truth table50%1/2
A
A= $message->{"sender"}
Aresult
11
00
202 94 ▶ my $age = $now - ( $message->{arrival_time} // $now );
Condition33%1/3
$message->{"arrival_time"} // $now
l!l&&r!l&&!r
9400
MC/DC0%0/2
$message->{"arrival_time"} // $now
$message->{"arrival_time"} $now
Truth table33%1/3
A // B
A= $message->{"arrival_time"}B= $now
ABresult
1X1
011
000
203 94 ▶ my ($reason) = grep { defined($_) && $_ ne '' } map { $_->{delay_reason} } @recipients;
Condition66%2/3
defined $_ && $_ ne ''
!ll&&!rl&&r
108040
MC/DC50%1/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table66%2/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
204 push(
205 @messages,
206 {
207 id => $message->{queue_id} // '?',
208 age => $age < 0 ? 0 : $age,
209 state => $message->{queue_name} // '?',
210 sender => $sender eq '' ? '<>' : $sender,
211 94 ▶ recipients => [ map { $_->{address} } @recipients ],
Branch: 2 branches
BranchTrueFalseTotal
$age < 0 ? : 0 94 94
$sender eq '' ? : 27 67 94
Condition50%2/4
$message->{"queue_id"} // "?"
l!l
940
$message->{"queue_name"} // "?"
l!l
940
MC/DC0%0/1
$message->{"queue_id"} // "?"
$message->{"queue_id"}
MC/DC0%0/1
$message->{"queue_name"} // "?"
$message->{"queue_name"}
Truth table50%1/2
A
A= $message->{"queue_id"}
Aresult
11
00
Truth table50%1/2
A
A= $message->{"queue_name"}
Aresult
11
00
212 reason => $reason,
213 }
214 );
215 } ## end foreach my $line ( grep { /\S/ } split( /\n/, $output...))
216  
217 51 return \@messages;
218 } ## end sub postfix_queue
219  
220 =head2 exim_queue
221  
222 Reads the Exim queue through exim -bp. Dies if it fails.
223  
224 - exim :: The path to exim.
225  
226 Returns the same as L</postfix_queue>, with a state of frozen or
227 queued and no reason. Recipients already delivered to are left out.
228  
229 my $messages = $check->exim_queue('/usr/sbin/exim4');
230 # [ { id => '1i7k9a-0003Zs-Qe', age => 14400, state => 'frozen', sender => '<>',
231 # recipients => ['carol@example.com'], reason => undef } ]
232  
233 =cut
234  
235 9 ▶sub exim_queue {
Subroutine exim_queue: called CC=11 SCAR=24.0
Pod documented
236 9 my ( $self, $exim ) = @_;
237  
238 9 my ( $exit, $output ) = $self->run_command( $exim, '-bp' );
239 # not being a admin is said with a exit of 0 by some versions
240 6 ▶ if ( $exit || $output =~ /\Aexim: / ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit or $output =~ /\Aexim: /u) 2 4 6
Condition66%2/3
$exit or $output =~ /\Aexim: /u
l!l&&r!l&&!r
204
MC/DC50%1/2
$exit or $output =~ /\Aexim: /u
$exit $output =~ /\Aexim: /u
Truth table66%2/3
A or B
A= $exitB= $output =~ /\Aexim: /u
ABresult
1X1
011
000
241 2 ▶ die( 'exim -bp exited ' . $exit . ', ' . ( ( split( /\n/, $output ) )[0] // 'saying nothing' ) . "\n" );
Condition50%1/2
(split(/\n/u, $output, 0))[0] // "saying nothing"
l!l
20
MC/DC0%0/1
(split(/\n/u, $output, 0))[0] // "saying nothing"
(split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (split(/\n/u, $output, 0))[0]
Aresult
11
00
242 }
243  
244 4 my @messages;
245 4 foreach my $line ( split( /\n/, $output ) ) {
246 # a message, such as " 4h 12K 1i7k9a-0003Zs-Qe <> *** frozen ***", age, size, id, sender, and flags
247 36 my ( $number, $unit, $id, $sender, $flags ) = $line =~ /\A\s*([0-9]+)([smhd]) +\S+ +(\S+) +<(.*?)>(.*)\z/;
248 36 ▶ if ( defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 12 24 36
249 push(
250 @messages,
251 {
252 id => $id,
253 12 ▶ age => $number * $EXIM_UNITS{$unit},
Branch: 2 branches
BranchTrueFalseTotal
$flags =~ /\*\*\* frozen \*\*\*/u ? : 4 8 12
$sender eq '' ? : 4 8 12
254 state => $flags =~ /\*\*\* frozen \*\*\*/ ? 'frozen' : 'queued',
255 sender => $sender eq '' ? '<>' : $sender,
256 recipients => [],
257 reason => undef,
258 }
259 );
260 12 next;
261 } ## end if ( defined($id) )
262  
263 # a recipient of the last, D before it if delivered to, +D if to one it was redirected to
264 24 my ( $delivered, $recipient ) = $line =~ /\A\s+(\+?D\s+)?(\S+)\s*\z/;
265 24 ▶ push( @{ $messages[-1]{recipients} }, $recipient )
Branch: 1 branch
BranchTrueFalseTotal
if defined $recipient and not defined $delivered and defined $messages[0] 12 12 24
Condition83%5/6
defined $recipient and not defined $delivered and defined $messages[0]
!ll&&!rl&&r
12012
defined $recipient and not defined $delivered
!ll&&!rl&&r
8412
MC/DC66%2/3
defined $recipient and not defined $delivered and defined $messages[0]
defined $recipient not defined $delivered defined $messages[0]
Truth table75%3/4
A and not(B) and C
A= defined $recipientB= not defined $deliveredC= defined $messages[0]
ABCresult
0XX0
10X0
1100
1111
266 if defined($recipient) && !defined($delivered) && defined( $messages[0] );
267 } ## end foreach my $line ( split( /\n/, $output ) )
268  
269 4 return \@messages;
270 } ## end sub exim_queue
271  
272 # Turns seconds into the largest whole unit it fits, as exim -bp does.
273 #
274 # Args...
275 #
276 # - seconds :: The seconds.
277 #
278 # Returns the text, such as '2d', '4h', '25m', or '40s'.
279 #
280 # Example...
281 #
282 # _age(9000); # '2h'
283 132 ▶sub _age {
Subroutine _age: called CC=4 SCAR=13.9
284 132 my ($seconds) = @_;
285  
286 132 ▶ return int( $seconds / 86400 ) . 'd' if $seconds >= 86400;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds >= 86400 34 98 132
287 98 ▶ return int( $seconds / 3600 ) . 'h' if $seconds >= 3600;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds >= 3600 58 40 98
288 40 ▶ return int( $seconds / 60 ) . 'm' if $seconds >= 60;
Branch: 1 branch
BranchTrueFalseTotal
if $seconds >= 60 22 18 40
289  
290 18 return int($seconds) . 's';
291 }
292  
293 14 1;
« blib/lib/Monitoring/Sneck/Checks/tls_protocols.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/pkg_audit.pm »