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

filter: Statementstmt 100% 72 / 72 Branchbran 95.8% 46 / 48 Conditioncond 72.0% 18 / 25 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 89.8% 160 / 178 CC 35 SCAR 27.2
CC
35
Cov
94%
check
24.1
validate
11.4
__ANON__
2.0
CRAP 15.3
« blib/lib/Monitoring/Sneck/Checks/containers.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/bond.pm »
1 package Monitoring::Sneck::Checks::ldap;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Monitoring::Sneck::Checks::ldap - checks a LDAP server binds and searches, timing it
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 3 our $VERSION = '0.0.1';
21  
22 # Result codes that are not failures for a search. 4 is the size limit
23 # being hit, which still returns entries.
24 3 our %SEARCH_FINE = ( 0 => 1, 4 => 1 );
25  
26 =head1 SYNOPSIS
27  
28 ldap|perl:ldap -H ldap.example.org
29 ldap_tls|perl:ldap -H ldap.example.org -s -w 0.5 -c 2
30 ldaps|perl:ldap -H ldap.example.org -S
31 ldap_users|perl:ldap -H ldap.example.org -s -D cn=monitor,dc=example,dc=org -P /etc/sneck/ldap.pw -b ou=people,dc=example,dc=org --scope one -f '(uid=*)' -E 10:
32  
33 =head1 DESCRIPTION
34  
35 Connects, binds, searches, and unbinds, through L<Net::LDAP>, timing
36 it all. Net::LDAP is not required to install this, but this check
37 needs it.
38  
39 The bind is anonymous unless --bind-dn is given. Without --base, it
40 reads the root DSE, the empty base with a base scope, which most
41 servers answer anonymously.
42  
43 - CRITICAL :: Not connecting. StartTLS failing. The bind or search
44 failing. The entries found outside of --entries. The time over
45 --critical.
46  
47 - WARNING :: The time over --warning.
48  
49 - UNKNOWN :: Net::LDAP not being installed, or --password-file not
50 being readable.
51  
52 With --starttls, StartTLS is done before binding. With --ssl, it
53 connects over TLS from the start, as for port 636. The certificate is
54 checked unless --insecure is given. See
55 L<Monitoring::Sneck::Checks::https_cert> with --starttls ldap for when
56 it expires.
57  
58 A search hitting the server's size limit is not a failure, as it still
59 returns entries.
60  
61 =head1 OPTIONS
62  
63 - -H, --hostname :: The host. Required.
64  
65 - -p, --port :: The port.
66 Default :: 389, or 636 with --ssl
67  
68 - -S, --ssl :: Connect over TLS. Can not be used with --starttls.
69  
70 - -s, --starttls :: Do StartTLS before binding. Can not be used with
71 --ssl.
72  
73 - -D, --bind-dn :: The DN to bind as.
74  
75 - --password :: The password to bind with. --password-file is better,
76 as this can show up in ps when run through sneck_check.
77  
78 - -P, --password-file :: A file with the password, its first line
79 being it.
80  
81 - -b, --base :: The base to search.
82 Default :: the empty base, the root DSE
83  
84 - --scope :: base, one, or sub.
85 Default :: base
86  
87 - -f, --filter :: The filter to search with.
88 Default :: (objectClass=*)
89  
90 - -E, --entries :: Range for how many entries the search finds.
91 Default :: 1:
92  
93 - -w, --warning :: Range of seconds for the whole of it.
94  
95 - -c, --critical :: Range of seconds for the whole of it.
96  
97 - --insecure :: Do not check the certificate.
98  
99 - --ca-file :: The CA file to check the certificate against.
100  
101 =cut
102  
103 17 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
104 return (
105 17 'hostname|H=s', 'port|p=i', 'ssl|S', 'starttls|s', 'bind-dn|D=s',
106 'password=s', 'password-file|P=s', 'base|b=s', 'scope=s', 'filter|f=s',
107 'entries|E=s', 'warning|w=s', 'critical|c=s', 'insecure', 'ca-file=s',
108 );
109 }
110  
111 17 ▶sub defaults { return { base => '', scope => 'base', filter => '(objectClass=*)', entries => '1:' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
112  
113 17 ▶sub required { return ('hostname') }
Subroutine required: called CC=1 SCAR=0.0
Pod documented
114  
115 17 ▶sub ranges { return ( 'entries', 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
116  
117 17 ▶sub validate {
Subroutine validate: called CC=11 SCAR=24.4
Pod documented
118 17 my ( $self, $options ) = @_;
119  
120 17 my @problems;
121 push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' )
122 17 ▶ if defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"hostname"} and not $self->is_host($options->{"hostname"}) 0 17 17
Condition33%1/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
!ll&&!rl&&r
0170
MC/DC0%0/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"} not $self->is_host($options->{"hostname"})
Truth table33%1/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
ABresult
0X0
100
111
123 17 ▶ push( @problems, '--ssl and --starttls can not both be given' ) if $options->{ssl} && $options->{starttls};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"ssl"} and $options->{"starttls"} 1 16 17
Condition100%3/3
$options->{"ssl"} and $options->{"starttls"}
!ll&&!rl&&r
1421
MC/DC100%2/2
$options->{"ssl"} and $options->{"starttls"}
$options->{"ssl"} $options->{"starttls"}
Truth table100%3/3
A and B
A= $options->{"ssl"}B= $options->{"starttls"}
ABresult
0X0
100
111
124 push( @problems, '--password and --password-file can not both be given' )
125 17 ▶ if defined( $options->{password} ) && defined( $options->{'password-file'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"password"} and defined $options->{"password-file"} 0 17 17
Condition66%2/3
defined $options->{"password"} and defined $options->{"password-file"}
!ll&&!rl&&r
1430
MC/DC0%0/2
defined $options->{"password"} and defined $options->{"password-file"}
defined $options->{"password"} defined $options->{"password-file"}
Truth table66%2/3
A and B
A= defined $options->{"password"}B= defined $options->{"password-file"}
ABresult
0X0
100
111
126 push( @problems, '--password or --password-file needs --bind-dn' )
127 if ( defined( $options->{password} ) || defined( $options->{'password-file'} ) )
128 17 ▶ && !defined( $options->{'bind-dn'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"password"} || defined $options->{"password-file"} and not defined $options->{"bind-dn"} 1 16 17
Condition100%6/6
defined $options->{"password"} || defined $options->{"password-file"} and not defined $options->{"bind-dn"}
!ll&&!rl&&r
1241
defined $options->{"password"} or defined $options->{"password-file"}
l!l&&r!l&&!r
3212
MC/DC66%2/3
defined $options->{"password"} || defined $options->{"password-file"} and not defined $options->{"bind-dn"}
defined $options->{"password"} defined $options->{"password-file"} not defined $options->{"bind-dn"}
Truth table80%4/5
A or B and not(C)
A= defined $options->{"password"}B= defined $options->{"password-file"}C= not defined $options->{"bind-dn"}
ABCresult
00X0
1X00
0100
1X11
0111
129 17 ▶ push( @problems, '--scope must be base, one, or sub' ) if $options->{scope} !~ /\A(?:base|one|sub)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"scope"} =~ /\A(?:base|one|sub)\z/u 1 16 17
130  
131 17 return @problems;
132 } ## end sub validate
133  
134 =head1 METHODS
135  
136 =head2 check
137  
138 Does the check.
139  
140 =cut
141  
142 14 ▶sub check {
Subroutine check: called CC=24 SCAR=31.8
Pod documented
143 14 my ($self) = @_;
144  
145 14 ▶ return $self->finish( $self->UNKNOWN, 'Net::LDAP is not installed' ) if !eval { require Net::LDAP; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Net::LDAP; 1 } } 1 13 14
146  
147 13 my $password = $self->opt('password');
148 13 ▶ if ( defined( $self->opt('password-file') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("password-file")) 2 11 13
149 2 my $content = read_file( $self->opt('password-file'), err_mode => 'quiet' );
150 2 ▶ return $self->finish( $self->UNKNOWN, 'can not read ' . $self->opt('password-file') . ', ' . $! )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 1 1 2
151 if !defined($content);
152 1 ($password) = split( /\r?\n/, $content );
153 1 ▶ $password //= '';
Condition50%1/2
$password //= ""
l!l
10
MC/DC0%0/1
$password //= ""
$password
Truth table50%1/2
A
A= $password
Aresult
11
00
154 }
155  
156 12 my $host = $self->opt('hostname');
157 12 ▶ my $port = $self->opt('port') // ( $self->opt('ssl') ? 636 : 389 );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('ssl') ? : 1 10 11
Condition100%2/2
$self->opt("port") // ($self->opt("ssl") ? 636 : 389)
l!l
111
MC/DC100%1/1
$self->opt("port") // ($self->opt("ssl") ? 636 : 389)
$self->opt("port")
Truth table100%2/2
A
A= $self->opt("port")
Aresult
11
00
158 12 my $where = $host . ':' . $port;
159 12 ▶ my %tls = ( verify => $self->opt('insecure') ? 'none' : 'require' );
Branch: 1 branch
BranchTrueFalseTotal
$self->opt('insecure') ? : 1 11 12
160 12 ▶ $tls{cafile} = $self->opt('ca-file') if defined( $self->opt('ca-file') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("ca-file") 1 11 12
161  
162 12 my $start = Time::HiRes::time;
163 12 ▶ my $ldap = Net::LDAP->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->opt('ssl') ? : 2 10 12
$self->opt('ssl') ? : 2 10 12
164 $host,
165 port => $port,
166 scheme => $self->opt('ssl') ? 'ldaps' : 'ldap',
167 timeout => $self->timeout,
168 version => 3,
169 ( $self->opt('ssl') ? %tls : () ),
170 );
171 12 ▶ return $self->finish( $self->CRITICAL, $where . ', ' . ( $@ || 'can not connect' ) ) if !$ldap;
Branch: 1 branch
BranchTrueFalseTotal
unless $ldap 1 11 12
Condition50%1/2
$@ || 'can not connect'
l!l
10
MC/DC0%0/1
$@ || 'can not connect'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
172  
173 # each step returns a Net::LDAP::Message, its code 0 being success
174 3 ▶ my $failed = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
175 3 my ( $step, $message ) = @_;
176 3 $ldap->disconnect;
177 3 ▶ return $self->finish( $self->CRITICAL,
Condition50%1/2
$message->error // ""
l!l
30
MC/DC0%0/1
$message->error // ""
$message->error
Truth table50%1/2
A
A= $message->error
Aresult
11
00
178 $where . ' ' . $step . ' failed, ' . $message->code . ' ' . ( $message->error // '' ) );
179 11 };
180 11 ▶ if ( $self->opt('starttls') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt("starttls")) 2 9 11
181 2 my $tls_message = $ldap->start_tls(%tls);
182 2 ▶ return $failed->( 'StartTLS', $tls_message ) if $tls_message->code;
Branch: 1 branch
BranchTrueFalseTotal
if $tls_message->code 1 1 2
183 }
184  
185 10 ▶ my $bind_message
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('bind-dn') ? : 3 7 10
Condition50%1/2
$password // ""
l!l
30
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $password
Aresult
11
00
186 = defined( $self->opt('bind-dn') )
187 ? $ldap->bind( $self->opt('bind-dn'), password => $password // '' )
188 : $ldap->bind;
189 10 ▶ return $failed->( 'bind', $bind_message ) if $bind_message->code;
Branch: 1 branch
BranchTrueFalseTotal
if $bind_message->code 1 9 10
190  
191 # 1.1 asks for no attributes, as only the count is wanted
192 9 my $search = $ldap->search(
193 base => $self->opt('base'),
194 scope => $self->opt('scope'),
195 filter => $self->opt('filter'),
196 attrs => ['1.1'],
197 );
198 9 ▶ return $failed->( 'search', $search ) if !$SEARCH_FINE{ $search->code };
Branch: 1 branch
BranchTrueFalseTotal
unless $Monitoring::Sneck::Checks::ldap::SEARCH_FINE{$search->code} 1 8 9
199 8 my $entries = $search->count;
200 8 $ldap->unbind;
201 8 $ldap->disconnect;
202 8 my $seconds = sprintf( '%.3f', Time::HiRes::time - $start );
203  
204 8 ▶ my $entries_code = $self->in_alert( $entries, $self->opt('entries') ) ? $self->CRITICAL : $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
$self->in_alert($entries, $self->opt('entries')) ? : 1 7 8
205 8 my $time_code = $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') );
206 8 $self->perfdata(
207 label => 'time',
208 value => $seconds,
209 uom => 's',
210 warning => $self->opt('warning'),
211 critical => $self->opt('critical'),
212 min => 0,
213 );
214 8 $self->perfdata( label => 'entries', value => $entries, critical => $self->opt('entries'), min => 0 );
215  
216 8 ▶ my @parts = (
Branch: 3 branches
BranchTrueFalseTotal
defined $self->opt('bind-dn') ? : 2 6 8
$entries == 1 ? : 5 3 8
$search->code == 4 ? : 1 7 8
217 $where,
218 'bound ' . ( defined( $self->opt('bind-dn') ) ? 'as ' . $self->opt('bind-dn') : 'anonymously' ),
219 $entries . ' entr'
220 . ( $entries == 1 ? 'y' : 'ies' )
221 . ' found'
222 . ( $search->code == 4 ? ', size limit hit' : '' ),
223 $seconds . ' seconds',
224 );
225 8 ▶ push( @parts, 'wanted ' . $self->opt('entries') . ' entries' ) if $entries_code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $entries_code != $self->OK 1 7 8
226  
227 8 return $self->finish( $self->worst( $entries_code, $time_code ), join( ', ', @parts ) );
228 } ## end sub check
229  
230 3 1;
« blib/lib/Monitoring/Sneck/Checks/containers.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/bond.pm »