source: trunk/server/fedora/config/etc/nagios/check_ldap_mmr.real @ 2192

Last change on this file since 2192 was 1670, checked in by ezyang, 14 years ago
Pretty format check_ldap_mmr with USE_NEWLINES.
  • Property svn:executable set to *
File size: 3.4 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
9my $nl  = $ENV{'USE_NEWLINES'} ? "\n" : "";
10my $tab = $ENV{'USE_NEWLINES'} ? "  " : "";
11
12# Nagios codes
13my %ERRORS=('OK'=>0, 'WARNING'=>1, 'CRITICAL'=>2, 'UNKNOWN'=>3, 'DEPENDENT'=>4);
14
15my $ldapserver = 'localhost';
16my $user = 'cn=Directory Manager';
17my $passwdfile = '/etc/signup-ldap-pw';
18my $configBase = "cn=config";
19my $replicatedBase = "dc=scripts,dc=mit,dc=edu";
20my $server="nsDS5ReplicaHost";
21my $status="nsds5replicaLastUpdateStatus";
22my $laststart="nsds5replicaLastUpdateStart";
23my $lastend="nsds5replicaLastUpdateEnd";
24 
25my $ldap=ConnectLdap();
26my $result=LDAPSearch($ldap,"objectClass=nsDS5ReplicationAgreement","",$configBase);
27my @entries = $result->entries;
28my $replicaErrors = 0;
29my $conflictErrors = 0;
30my $errorstring = "Replication error(s): $nl";
31foreach 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}
47print "$nl";
48
49$result=LDAPSearch($ldap,"nsds5ReplConflict=*",["nsds5ReplConflict"],$replicatedBase);
50@entries = $result->entries;
51foreach 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}
59print "$nl";
60
61if ($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
69sub 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
81sub 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
90sub 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
Note: See TracBrowser for help on using the repository browser.