[1270] | 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 | |
---|
[1675] | 9 | my $nl = $ENV{'USE_NEWLINES'} ? "\n" : ""; |
---|
| 10 | my $tab = $ENV{'USE_NEWLINES'} ? " " : ""; |
---|
| 11 | |
---|
[1270] | 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'; |
---|
[1579] | 18 | my $configBase = "cn=config"; |
---|
| 19 | my $replicatedBase = "dc=scripts,dc=mit,dc=edu"; |
---|
[1270] | 20 | my $server="nsDS5ReplicaHost"; |
---|
| 21 | my $status="nsds5replicaLastUpdateStatus"; |
---|
| 22 | my $laststart="nsds5replicaLastUpdateStart"; |
---|
| 23 | my $lastend="nsds5replicaLastUpdateEnd"; |
---|
| 24 | |
---|
| 25 | my $ldap=ConnectLdap(); |
---|
[1579] | 26 | my $result=LDAPSearch($ldap,"objectClass=nsDS5ReplicationAgreement","",$configBase); |
---|
[1270] | 27 | my @entries = $result->entries; |
---|
[1579] | 28 | my $replicaErrors = 0; |
---|
| 29 | my $conflictErrors = 0; |
---|
[1675] | 30 | my $errorstring = "Replication error(s): $nl"; |
---|
[1270] | 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/; |
---|
[1675] | 40 | print "Replication to $servername last operation $serverlaststart $nl"; |
---|
| 41 | print $tab . "Status: $serverstatus. $nl"; |
---|
[1270] | 42 | if ($statuscode) { |
---|
[1579] | 43 | $replicaErrors++; |
---|
[1342] | 44 | $errorstring = $errorstring . $serverstatus . ", "; |
---|
[1270] | 45 | } |
---|
| 46 | } |
---|
[1675] | 47 | print "$nl"; |
---|
[1579] | 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"); |
---|
[1675] | 54 | print "Conflict found for DN $conflictingDN $nl"; |
---|
| 55 | print $tab . "Reason: $conflictDesc. $nl"; |
---|
[1579] | 56 | $conflictErrors++; |
---|
| 57 | $errorstring = $errorstring . $conflictDesc . ", "; |
---|
| 58 | } |
---|
[1675] | 59 | print "$nl"; |
---|
[1579] | 60 | |
---|
| 61 | if ($conflictErrors > 0) { |
---|
| 62 | &nagios_return("CRITICAL", $errorstring); |
---|
| 63 | } elsif ($replicaErrors > 0) { |
---|
[1342] | 64 | &nagios_return("WARNING", $errorstring); |
---|
| 65 | } else { |
---|
[1579] | 66 | &nagios_return("OK", "All replicas are OK and no conflicts are present"); |
---|
[1342] | 67 | } |
---|
[1270] | 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 | |
---|