ASNMTAP

 view release on metacpan or  search on metacpan

plugins/nagios/templates/check_template-ldap.pl  view on Meta::CPAN

$objectNagios->printUsage ('Missing command line argument filter') unless (defined $FILTER);

my $USER = $objectNagios->getOptionsArgv ('username') ? $objectNagios->getOptionsArgv ('username') : undef;

my $debug = $objectNagios->getOptionsValue ('debug');

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

use Net::LDAP;
use Net::LDAP::Util qw( ldap_error_name ldap_error_text );

my ($returnValue, $authenticated) = (1, 0);
my $ldap = Net::LDAP->new ($ldapserver, port => $ldapport, version => 2) or $returnValue = 0;

if ($returnValue) {
  my $messageLDAP;

  if ($DN ne '' && $DN_PASS ne '') {
    $messageLDAP = $ldap->bind($DN, password => $DN_PASS);
    $authenticated = ldapStatusOk ('Wrong username or password, dude..', $messageLDAP, $debug);
  } else {
    $messageLDAP = $ldap->bind();
    $authenticated = ldapStatusOk ('Annonymous', $messageLDAP, $debug);
  }

  if ($authenticated) {
    if ($debug) {
      print "WhooHoo, authentication is good!\n";
      my ($namingContexts, $supportedLDAPVersions);

      my $dse = $ldap->root_dse();

      my @contexts = $dse->get_value('namingContexts');
      $namingContexts = join (', ', @contexts);
      print "namingContexts: $namingContexts\n" if (defined $namingContexts);

      my @supportedLDAPVersion = $dse->get_value('supportedLDAPVersion');
      $supportedLDAPVersions = join (', ', @supportedLDAPVersion);
      print "supportedLDAPVersions: $supportedLDAPVersions\n" if (defined $supportedLDAPVersions);
    }
  
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    $messageLDAP = $ldap->search ( base => $BASE, scope => $SCOPE, filter => $FILTER );

    if (ldapStatusOk ("Ooooopes, can't search", $messageLDAP, $debug)) {
      $objectNagios->pluginValues ( { stateValue => $ERRORS{OK}, alert => 'Search is good' }, $TYPE{APPEND} ); 

      if ($messageLDAP->count() != 0) {
        my $dn = ($messageLDAP->entry(0))->dn();
        print "\nDN: $dn\n" if ($debug);

        if ($debug >= 2) {
          my $entry = $messageLDAP->entry(0);
          foreach my $attribute ($entry->attributes) { print $attribute, ": ", $entry->get_value($attribute), "\n"; }
        }

        $ldap->unbind();

        if ($dn ne '' && defined $PASS && $PASS ne '') {
          # Now let's verify the authentication credentials, by rebinding with the users DN and password.
          my $ldap = Net::LDAP->new ($ldapserver, port => $ldapport, version => 2) or $returnValue = 0;

          if ($returnValue) {
            $messageLDAP = $ldap->bind($dn, password => $PASS);

            if (ldapStatusOk ('Wrong username or password', $messageLDAP, $debug)) {
              $ldap->unbind();
              $objectNagios->pluginValues ( { stateValue => $ERRORS{OK}, alert => "Search and Authentication is good" }, $TYPE{APPEND} );
            }
          } else {
            $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Can't get a connection to ldapserver '$ldapserver:$ldapport'" }, $TYPE{APPEND} );
          }
        } else {
          $objectNagios->pluginValues ( { stateValue => $ERRORS{OK}, alert => "Search is good" }, $TYPE{APPEND} ); 
        }
      } else {
        $ldap->unbind();
        $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Nothing found for 'base: $BASE - scope: $SCOPE - filter: $FILTER'" }, $TYPE{APPEND} ); 
      }
    }
  } else {
    $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Can't bind to ldapserver '$ldapserver:$ldapport' for DN '$DN'" }, $TYPE{APPEND} ); 
  }
} else {
  $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Can't get a connection to ldapserver '$ldapserver:$ldapport'" }, $TYPE{APPEND} ); 
}

$objectNagios->exit (7);

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub ldapStatusOk {
  my ($error_message, $messageLDAP, $debug) = @_;

  if ( $messageLDAP->code) {
    print "-> $error_message\n" if ($debug);

    if ($debug >= 2) {
      print "Return code : ", $messageLDAP->code, "\n";
      print "Message     : ", ldap_error_name ($messageLDAP->code), ": ", ldap_error_text ($messageLDAP->error);
      print "MessageID   : ", $messageLDAP->mesg_id, "\n"      if (defined $messageLDAP->mesg_id);
      print "DN          : ", $messageLDAP->dn, "\n"           if (defined $messageLDAP->dn);
      print "Server error:",  $messageLDAP->server_error, "\n" if (defined $messageLDAP->server_error);
    }

    $objectNagios->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => $error_message .': '. $messageLDAP->code .' - '. $messageLDAP->error, result => '' }, $TYPE{APPEND} ); 
    return (0);
  } else {
    return (1);
  }
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

__END__

=head1 NAME

ASNMTAP::Asnmtap::Plugins::Nagios

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.458 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )