1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | # Originally by Emmanuel BUU <emmanuel.buu@ives.fr> (c) IVèS 2008 |
---|
4 | # Adapted for scripts.mit.edu by Mitchell Berger <mitchb@mit.edu> |
---|
5 | |
---|
6 | use Net::LDAP; |
---|
7 | use strict; |
---|
8 | |
---|
9 | # Nagios codes |
---|
10 | my %ERRORS=('OK'=>0, 'WARNING'=>1, 'CRITICAL'=>2, 'UNKNOWN'=>3, 'DEPENDENT'=>4); |
---|
11 | |
---|
12 | my $ldapserver = 'localhost'; |
---|
13 | my $user = 'cn=Directory Manager'; |
---|
14 | my $passwdfile = '/etc/signup-ldap-pw'; |
---|
15 | my $configBase = "cn=config"; |
---|
16 | my $replicatedBase = "dc=scripts,dc=mit,dc=edu"; |
---|
17 | my $server="nsDS5ReplicaHost"; |
---|
18 | my $status="nsds5replicaLastUpdateStatus"; |
---|
19 | my $laststart="nsds5replicaLastUpdateStart"; |
---|
20 | my $lastend="nsds5replicaLastUpdateEnd"; |
---|
21 | |
---|
22 | my $ldap=ConnectLdap(); |
---|
23 | my $result=LDAPSearch($ldap,"objectClass=nsDS5ReplicationAgreement","",$configBase); |
---|
24 | my @entries = $result->entries; |
---|
25 | my $replicaErrors = 0; |
---|
26 | my $conflictErrors = 0; |
---|
27 | my $errorstring = "Replication error(s): "; |
---|
28 | foreach my $entr ( @entries ) { |
---|
29 | my $servername=$entr->get_value($server); |
---|
30 | my $serverstatus=$entr->get_value($status); |
---|
31 | my $serverlaststart=$entr->get_value($laststart); |
---|
32 | my $serverlastend=$entr->get_value($lastend); |
---|
33 | my $statuscode = $serverstatus; |
---|
34 | $statuscode =~ s/(^[-0123456789]+) (.*$)/$1/; |
---|
35 | $serverlaststart =~ s/(....)(..)(..)(..)(..)(..)./$1-$2-$3\ $4:$5:$6/; |
---|
36 | $serverlastend =~ s/(....)(..)(..)(..)(..)(..)./$1-$2-$3\ $4:$5:$6/; |
---|
37 | print "Replication to $servername last operation $serverlaststart "; |
---|
38 | print "Status: $serverstatus. "; |
---|
39 | if ($statuscode) { |
---|
40 | $replicaErrors++; |
---|
41 | $errorstring = $errorstring . $serverstatus . ", "; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | $result=LDAPSearch($ldap,"nsds5ReplConflict=*",["nsds5ReplConflict"],$replicatedBase); |
---|
46 | @entries = $result->entries; |
---|
47 | foreach my $entr ( @entries ) { |
---|
48 | my $conflictingDN=$entr->dn(); |
---|
49 | my $conflictDesc=$entr->get_value("nsds5ReplConflict"); |
---|
50 | print "Conflict found for DN $conflictingDN "; |
---|
51 | print "Reason: $conflictDesc. "; |
---|
52 | $conflictErrors++; |
---|
53 | $errorstring = $errorstring . $conflictDesc . ", "; |
---|
54 | } |
---|
55 | |
---|
56 | if ($conflictErrors > 0) { |
---|
57 | &nagios_return("CRITICAL", $errorstring); |
---|
58 | } elsif ($replicaErrors > 0) { |
---|
59 | &nagios_return("WARNING", $errorstring); |
---|
60 | } else { |
---|
61 | &nagios_return("OK", "All replicas are OK and no conflicts are present"); |
---|
62 | } |
---|
63 | |
---|
64 | sub ConnectLdap { |
---|
65 | my $ldap = Net::LDAP->new ( $ldapserver ) or die "$@"; |
---|
66 | open (PASSWD, $passwdfile) || &nagios_return("CRITICAL", "Could not read credentials"); |
---|
67 | my $passwd = <PASSWD>; |
---|
68 | close (PASSWD); |
---|
69 | my $mesg = $ldap->bind ( "$user", password => "$passwd" , version => 3 ); |
---|
70 | if ($mesg->code) { |
---|
71 | &nagios_return("CRITICAL", "Failed to bind to LDAP: " . $mesg->error); |
---|
72 | } |
---|
73 | return $ldap; |
---|
74 | } |
---|
75 | |
---|
76 | sub LDAPSearch { |
---|
77 | my ($ldap,$searchString,$attrs,$base) = @_; |
---|
78 | my $result = $ldap->search ( base => "$base", |
---|
79 | scope => "sub", |
---|
80 | filter => "$searchString", |
---|
81 | attrs => $attrs |
---|
82 | ); |
---|
83 | } |
---|
84 | |
---|
85 | sub nagios_return($$) { |
---|
86 | my ($ret, $message) = @_; |
---|
87 | my ($retval, $retstr); |
---|
88 | if (defined($ERRORS{$ret})) { |
---|
89 | $retval = $ERRORS{$ret}; |
---|
90 | $retstr = $ret; |
---|
91 | } else { |
---|
92 | $retstr = 'UNKNOWN'; |
---|
93 | $retval = $ERRORS{$retstr}; |
---|
94 | } |
---|
95 | $message = "$retstr - $message\n"; |
---|
96 | print $message; |
---|
97 | exit $retval; |
---|
98 | } |
---|
99 | |
---|