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