source: branches/fc13-dev/server/fedora/config/etc/nagios/check_ldap_mmr.real @ 1674

Last change on this file since 1674 was 1674, checked in by ezyang, 14 years ago
Undo merge.
  • Property svn:executable set to *
File size: 3.2 KB
Line 
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
6use Net::LDAP;
7use strict;
8
9# Nagios codes
10my %ERRORS=('OK'=>0, 'WARNING'=>1, 'CRITICAL'=>2, 'UNKNOWN'=>3, 'DEPENDENT'=>4);
11
12my $ldapserver = 'localhost';
13my $user = 'cn=Directory Manager';
14my $passwdfile = '/etc/signup-ldap-pw';
15my $configBase = "cn=config";
16my $replicatedBase = "dc=scripts,dc=mit,dc=edu";
17my $server="nsDS5ReplicaHost";
18my $status="nsds5replicaLastUpdateStatus";
19my $laststart="nsds5replicaLastUpdateStart";
20my $lastend="nsds5replicaLastUpdateEnd";
21 
22my $ldap=ConnectLdap();
23my $result=LDAPSearch($ldap,"objectClass=nsDS5ReplicationAgreement","",$configBase);
24my @entries = $result->entries;
25my $replicaErrors = 0;
26my $conflictErrors = 0;
27my $errorstring = "Replication error(s): ";
28foreach 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;
47foreach 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
56if ($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
64sub 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
76sub 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
85sub 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
Note: See TracBrowser for help on using the repository browser.