blib/lib/Nagios/Plugin/LDAP.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 246 CC 21 SCAR 33.1
CC
21
Cov
0%
_ldap_connect
72.0
_ldap_do_search
42.0
_ldap_check_repl
20.0
CRAP 27.4
1 package Nagios::Plugin::LDAP;
2  
3 use strict;
4 use warnings;
5  
6 use base qw(Nagios::Plugin);
7  
8 use Net::LDAP;
9 use Net::LDAP::Util;
10 use Nagios::Plugin;
11 use Time::HiRes qw(time);
12 use DateTime;
13  
14 our $VERSION = '0.04';
15 our $TIMEOUT = 4;
16  
17  
18 sub new {
19 my $class = shift;
20  
21 my $usage = <<'USAGE';
22 Usage: check_ldap -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]
23 [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]
24 [-2|-3] [-4|-6]
25 USAGE
26  
27 my $self = $class->SUPER::new(
28 shortname => 'LDAP',
29 usage => $usage,
30 version => $VERSION,
31 url => 'http://search.cpan.org/dist/Nagios-Plugin-LDAP/bin/check_ldap_repl',
32 license =>
33 qq|This library is free software, you can redistribute it and/or modify\nit under the same terms as Perl itself.|,
34 );
35  
36 $self->_add_ldap_options;
37  
38 return $self;
39 }
40  
41  
42 sub _add_ldap_options {
43 my ($self) = @_;
44  
45 my @args = (
46 { spec => 'master|M=s',
47 help =>
48 qq|-M, --master=ADDRESS\n Host name or IP Address of master LDAP server to check replication|,
49 required => 1,
50 },
51 { spec => 'hostname|H=s',
52 help =>
53 qq|-H, --hostname=ADDRESS\n Host name, IP Address, or unix socket (must be an absolute path)|,
54 required => 1,
55 },
56 { spec => 'port|p=i',
57 help => qq|-p, --port=INTEGER\n Port number (default: 389)|,
58 },
59 { spec => 'use-ipv4|4!',
60 help => qq|-4, --use-ipv4\n Use IPv4 connection|,
61 },
62 { spec => 'use-ipv6|6!',
63 help => qq|-6, --use-ipv6\n Use IPv6 connection|,
64 },
65 { spec => 'attr|a=s',
66 help => qq|-a [--attr]\n ldap attribute to search (default: "(objectclass=*)")|,
67 },
68 { spec => 'base|b=s',
69 help => qq|-b [--base]\n ldap base (eg. ou=my unit, o=my org, c=at)|,
70 },
71 { spec => 'bind|D=s',
72 help => qq|-D [--bind]\n ldap bind DN (if required)|,
73 },
74 { spec => 'pass|P=s',
75 help => qq|-P [--pass]\n ldap password (if required)|,
76 },
77 { spec => 'starttls|T!',
78 help => qq|-T [--starttls]\n use starttls mechanism introduced in protocol version 3|,
79 },
80 { spec => 'ssl|S!',
81 help =>
82 qq|-S [--ssl]\n use ldaps (ldap v2 ssl method). this also sets the default port to %s|,
83 },
84 { spec => 'ver2|2!',
85 help => qq|-2 [--ver2]\n use ldap protocol version 2|,
86 },
87 { spec => 'ver3|3!',
88 help => qq|-3 [--ver3]\n use ldap protocol version 3\n (default protocol version: 2)|,
89 },
90 { spec => 'repl-warning=i',
91 help =>
92 qq|--repl-warning=INTEGER\n Replication time delta to result in warning status (seconds)|,
93 },
94 { spec => 'repl-critical=i',
95 help =>
96 qq|-c, --critical=DOUBLE\n Replication time delta to result in critical status (seconds)|,
97 },
98 { spec => 'warning|w=f',
99 help => qq|-w, --warning=DOUBLE\n Response time to result in warning status (seconds)|,
100 },
101 { spec => 'critical|c=f',
102 help => qq|-c, --critical=DOUBLE\n Response time to result in critical status (seconds)|,
103 },
104 );
105  
106 $self->add_arg(%$_) for (@args);
107 }
108  
109  
110 sub run {
111 my ($self) = @_;
112  
113 $self->getopts;
114  
115 my $opts = $self->opts;
116  
117 my $hostname = $opts->get('hostname') || 'localhost';
118  
119 if (my $ldap = $self->_ldap_connect($hostname)) {
120 if ($self->_ldap_bind($ldap)) {
121 $self->_ldap_search($ldap);
122 }
123 $self->_ldap_check_repl($ldap);
124 }
125  
126 $self->nagios_exit($self->check_messages(join => ", "));
127 return;
128 }
129  
130 sub _ldap_connect {
131 my ($self, $hostname) = @_;
132 my $opts = $self->opts;
133  
134 my $timeout = $opts->get('timeout') || 4;
135 my $ssl = $opts->get('ssl');
136 my $port = $opts->get('port') || ($ssl ? 636 : 389);
137 my $starttls = $opts->get('starttls') && !$ssl;
138 my $version = $opts->get('ver3') ? 3 : 2;
139 my $ipv6 = $opts->get('use-ipv6');
140  
141 my $class = $ssl ? 'Net::LDAPS' : 'Net::LDAP';
142  
143 if ($ssl and !eval { require Net::LDAPS }) {
144 my $err = $@;
145 $self->add_message(WARNING, $err);
146 return;
147 }
148  
149 my $ldap = $class->new(
150 $hostname,
151 timeout => $timeout,
152 version => $version,
153 inet6 => $ipv6,
154 );
155  
156 unless ($ldap) {
157 my $err = $@;
158 $self->add_message(CRITICAL, "$hostname: " . $err);
159 return;
160 }
161  
162 if ($starttls) {
163 my $mesg = $ldap->start_tls;
164 if ($mesg->code) {
165 $self->add_message(WARNING, "starttls: [$hostname] " . $mesg->error);
166 return;
167 }
168 }
169  
170 return $ldap;
171 }
172  
173  
174 sub _ldap_bind {
175 my ($self, $ldap) = @_;
176 my $opts = $self->opts;
177 my $bind = $opts->get('bind') or return 1;
178  
179 my $pass = $opts->get('pass');
180 my @auth = $pass ? (password => $pass) : (noauth => 1);
181 my $mesg = $ldap->bind($bind, @auth);
182  
183 if ($mesg->code) {
184 $self->add_message(WARNING, "bind: " . $mesg->error_desc);
185 return 0;
186 }
187  
188 return 1;
189 }
190  
191 sub _ldap_do_search {
192 my ($self, $ldap, $filter, @attrs) = @_;
193 my $opts = $self->opts;
194  
195 my $base = $opts->get('base');
196 unless ($base) {
197 $self->add_message(WARNING, "No search base");
198 return 0;
199 }
200  
201 my $mesg = $ldap->search(
202 scope => (@attrs ? 'base' : 'subtree'),
203 base => $base,
204 filter => $filter,
205 (@attrs ? (attrs => \@attrs) : ()),
206 );
207 if ($mesg->code) {
208 $self->add_message(WARNING, "search: " . $mesg->error_desc);
209 return;
210 }
211  
212 unless ($mesg->count) {
213 $self->add_message(WARNING, "search: No entries found @attrs");
214 return;
215 }
216  
217 return $mesg->pop_entry;
218 }
219  
220 sub _ldap_search {
221 my ($self, $ldap) = @_;
222 my $opts = $self->opts;
223  
224 my $warning = $opts->get('warning');
225 my $critical = $opts->get('critical');
226  
227 return 1 unless $warning or $critical;
228  
229 my $attr = $opts->get('attr') || '(objectClass=*)';
230  
231 my $start = time;
232 $self->_ldap_do_search($ldap, $attr);
233 my $delta = time - $start;
234  
235 $self->add_message($self->check_threshold($delta), sprintf("%.3f seconds response time", $delta));
236  
237 $self->add_perfdata(
238 label => 'time',
239 value => sprintf("%.4f", $delta),
240 uom => 's',
241 threshold => $self->threshold
242 );
243  
244 return 1;
245 }
246  
247 sub _ldap_check_repl {
248 my ($self, $dst_ldap) = @_;
249 my $opts = $self->opts;
250  
251 my $master = $opts->get('master') or return 1;
252  
253 my $warning = $opts->get('repl-warning');
254 my $critical = $opts->get('repl-critical');
255 my $verbose = $opts->get('verbose');
256  
257 return 1 unless $warning or $critical;
258  
259 my $src_ldap = $self->_ldap_connect($master) or return;
260  
261 my $src_entry =
262 $self->_ldap_do_search($src_ldap, '(&(objectClass=*)(contextCSN=*))', 'contextCSN')
263 or return;
264 my $dst_entry =
265 $self->_ldap_do_search($dst_ldap, '(&(objectClass=*)(contextCSN=*))', 'contextCSN')
266 or return;
267  
268 my $src_csn = $src_entry->get_value('contextCSN');
269 my $dst_csn = $dst_entry->get_value('contextCSN');
270  
271 print "Master CSN = $src_csn\n" if $verbose;
272 print "Slave CSN = $dst_csn\n" if $verbose;
273  
274 my ($YYYY, $MM, $DD, $hh, $mm, $ss);
275 ($YYYY, $MM, $DD, $hh, $mm, $ss) = $src_csn =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
276 my $src_dt = DateTime->new(
277 year => $YYYY,
278 month => $MM,
279 day => $DD,
280 hour => $hh,
281 minute => $mm,
282 second => $ss
283 );
284 ($YYYY, $MM, $DD, $hh, $mm, $ss) = $dst_csn =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
285 my $dst_dt = DateTime->new(
286 year => $YYYY,
287 month => $MM,
288 day => $DD,
289 hour => $hh,
290 minute => $mm,
291 second => $ss
292 );
293  
294 my $delta = abs($src_dt->epoch - $dst_dt->epoch);
295  
296 $self->add_message(
297 $self->check_threshold(check => $delta, warning => $warning, critical => $critical),
298 sprintf("%d seconds replication delta", $delta));
299  
300 $self->add_perfdata(
301 label => 'repl',
302 value => $delta,
303 uom => 's',
304 threshold => $self->threshold
305 );
306  
307 }
308  
309 __END__