[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 | |
---|
| 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 $base = "cn=config"; |
---|
| 16 | my $server="nsDS5ReplicaHost"; |
---|
| 17 | my $status="nsds5replicaLastUpdateStatus"; |
---|
| 18 | my $laststart="nsds5replicaLastUpdateStart"; |
---|
| 19 | my $lastend="nsds5replicaLastUpdateEnd"; |
---|
| 20 | |
---|
| 21 | my $ldap=ConnectLdap(); |
---|
| 22 | my $result=LDAPSearch($ldap,"objectClass=nsDS5ReplicationAgreement","",$base); |
---|
| 23 | my @entries = $result->entries; |
---|
| 24 | foreach my $entr ( @entries ) { |
---|
| 25 | my $servername=$entr->get_value($server); |
---|
| 26 | my $serverstatus=$entr->get_value($status); |
---|
| 27 | my $serverlaststart=$entr->get_value($laststart); |
---|
| 28 | my $serverlastend=$entr->get_value($lastend); |
---|
| 29 | my $statuscode = $serverstatus; |
---|
| 30 | $statuscode =~ s/(^[-0123456789]+) (.*$)/$1/; |
---|
| 31 | $serverlaststart =~ s/(....)(..)(..)(..)(..)(..)./$1-$2-$3\ $4:$5:$6/; |
---|
| 32 | $serverlastend =~ s/(....)(..)(..)(..)(..)(..)./$1-$2-$3\ $4:$5:$6/; |
---|
| 33 | print "Replication to $servername last operation $serverlaststart "; |
---|
| 34 | print "Status: $serverstatus.\n"; |
---|
| 35 | if ($statuscode) { |
---|
| 36 | &nagios_return("ERROR", "Replication error: " . $serverstatus); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | &nagios_return("OK", "All replicas are OK"); |
---|
| 40 | |
---|
| 41 | sub ConnectLdap { |
---|
| 42 | my $ldap = Net::LDAP->new ( $ldapserver ) or die "$@"; |
---|
| 43 | open (PASSWD, $passwdfile) || &nagios_return("CRITICAL", "Could not read credentials"); |
---|
| 44 | my $passwd = <PASSWD>; |
---|
| 45 | close (PASSWD); |
---|
| 46 | my $mesg = $ldap->bind ( "$user", password => "$passwd" , version => 3 ); |
---|
| 47 | if ($mesg->code) { |
---|
| 48 | &nagios_return("CRITICAL", "Failed to bind to LDAP: " . $mesg->error); |
---|
| 49 | } |
---|
| 50 | return $ldap; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | sub LDAPSearch { |
---|
| 54 | my ($ldap,$searchString,$attrs,$base) = @_; |
---|
| 55 | my $result = $ldap->search ( base => "$base", |
---|
| 56 | scope => "sub", |
---|
| 57 | filter => "$searchString", |
---|
| 58 | attrs => $attrs |
---|
| 59 | ); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | sub nagios_return($$) { |
---|
| 63 | my ($ret, $message) = @_; |
---|
| 64 | my ($retval, $retstr); |
---|
| 65 | if (defined($ERRORS{$ret})) { |
---|
| 66 | $retval = $ERRORS{$ret}; |
---|
| 67 | $retstr = $ret; |
---|
| 68 | } else { |
---|
| 69 | $retstr = 'UNKNOWN'; |
---|
| 70 | $retval = $ERRORS{$retstr}; |
---|
| 71 | } |
---|
| 72 | $message = "$retstr - $message\n"; |
---|
| 73 | print $message; |
---|
| 74 | exit $retval; |
---|
| 75 | } |
---|
| 76 | |
---|