-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-389dir-replication.pl
131 lines (105 loc) · 3.62 KB
/
check-389dir-replication.pl
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
#!/usr/bin/perl -w
# Original author: –Neutrino38 18:07, 12 September 2008 (EDT),
# Copied from http://directory.fedoraproject.org/docs/389ds/howto/howto-replicationmonitoring.html
# Modified by Michal Medvecky, Deutsche telekom Pan-Net s.r.o.
use Net::LDAP;
use strict;
use Getopt::Long;
# Nagios codes
my %ERRORS=('OK'=>0, 'WARNING'=>1, 'CRITICAL'=>2, 'UNKNOWN'=>3, 'DEPENDENT'=>4);
my $ldapserver;
my $user;
my $passwd;
my $replicas=0;
my $statusmessage;
GetOptions(
'host=s' => \$ldapserver,
'user=s' => \$user,
'password=s' => \$passwd,
'help' => sub { &usage(); },
);
&nagios_return("UNKNOWN", "[1] --host not specified") if (!$ldapserver);
&nagios_return("UNKNOWN", "[1] --user not specified") if (!$user);
#
#BIND INFORMATION, and SEARCH BASE
my $base = "cn=config";
#Attributes
my $server="nsDS5ReplicaHost";
my $status="nsds5replicaLastUpdateStatus";
my $laststart="nsds5replicaLastUpdateStart";
my $lastend="nsds5replicaLastUpdateEnd";
#connect to ldap server
my $ldap=ConnectLdap();
my $result=LDAPSearch($ldap,"objectClass=nsDS5ReplicationAgreement","",$base);
my @entries = $result->entries;
my $entr;
my $maxstatcode = 0;
my $agreements = 0;
foreach $entr ( @entries ) {
my $servername=$entr->get_value($server);
my $serverstatus=$entr->get_value($status);
my $serverlaststart=$entr->get_value($laststart);
my $serverlastend=$entr->get_value($lastend);
my $statuscode = $entr->get_value($status);
my $agreement = $entr->get_value("cn");
$serverlaststart =~ s/(....)(..)(..)(..)(..)(..)./$1-$2-$3\ $4:$5:$6/;
$serverlastend =~ s/(....)(..)(..)(..)(..)(..)./$1-$2-$3\ $4:$5:$6/;
$statuscode =~ s/(?:^Error \()?([-0123456789]+)(?:\)?) (.*$)/$1/;
$serverstatus =~ s/^Error //;
$agreements++;
$statusmessage .= " $servername: $serverlaststart";
if ($statuscode>1)
{
&nagios_return("CRITICAL", "Replication error on ".$agreement.": " . $serverstatus);
}
}
if ($agreements<1) {
&nagios_return("WARNING", "No agreements found. Are you sure you want to monitor them?");
}
&nagios_return("OK", "All $agreements agreements are OK: $statusmessage");
exit;
sub ConnectLdap {
my $ldap = Net::LDAP->new ( $ldapserver ) or die "$@";
# my $s = $ldap->start_tls(verify => 'none', keydecrypt => sub { 'secret'; }, capath => '/etc/ssl/certs/');
# print $s."\n";
my $mesg = $ldap->bind ( "$user", password => "$passwd" , version => 3 );
# $mesg->code && warn "error: ", $mesg->error;
if ($mesg->code)
{
&nagios_return("CRITICAL", "Failed to connect to LDAP: " . $mesg->error . " with user $user.");
}
return $ldap;
}
sub LDAPSearch
{
my ($ldap,$searchString,$attrs,$base) = @_;
my $result = $ldap->search ( base => "$base",
scope => "sub",
filter => "$searchString",
attrs => $attrs
);
}
sub nagios_return($$) {
my ($ret, $message) = @_;
my ($retval, $retstr);
if (defined($ERRORS{$ret})) {
$retval = $ERRORS{$ret};
$retstr = $ret;
} else {
$retstr = 'UNKNOWN';
$retval = $ERRORS{$retstr};
$message = "WTF is return code '$ret'??? ($message)";
}
$message = "$retstr - $message\n";
print $message;
exit $retval;
}
sub usage() {
print("Emmanuel BUU <emmanuel.buu\@ives.fr> (c) IVèS 2008
http://www.ives.fr/
--host=<host> Hostname or IP address to connect to.
--user=<user>
--password=<password>
--help Guess what ;-)
");
}