Back to index

nagios-plugins  1.4.16
Defines | Enumerations | Functions | Variables
check_ldap.c File Reference
#include "common.h"
#include "netutils.h"
#include "utils.h"
#include <lber.h>
#include <ldap.h>

Go to the source code of this file.

Defines

#define LDAP_DEPRECATED   1
#define LDAP_OPT_SUCCESS   LDAP_SUCCESS

Enumerations

enum  { UNDEFINED = 0, DEFAULT_PORT = 389 }

Functions

int process_arguments (int, char **)
int validate_arguments (void)
void print_help (void)
void print_usage (void)
int main (int argc, char *argv[])

Variables

char * progname = "check_ldap"
const char * copyright = "2000-2008"
const char * email = "nagiosplug-devel@lists.sourceforge.net"
char ld_defattr [] = "(objectclass=*)"
char * ld_attr = ld_defattr
char * ld_host = NULL
char * ld_base = NULL
char * ld_passwd = NULL
char * ld_binddn = NULL
int ld_port = -1
double warn_time = UNDEFINED
double crit_time = UNDEFINED
int starttls = FALSE
int ssl_on_connect = FALSE
int verbose = 0
char * SERVICE = "LDAP"

Define Documentation

#define LDAP_DEPRECATED   1

Definition at line 39 of file check_ldap.c.

#define LDAP_OPT_SUCCESS   LDAP_SUCCESS

Definition at line 66 of file check_ldap.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
UNDEFINED 
DEFAULT_PORT 

Definition at line 42 of file check_ldap.c.

     {
       UNDEFINED = 0,
#ifdef HAVE_LDAP_SET_OPTION
       DEFAULT_PROTOCOL = 2,
#endif
       DEFAULT_PORT = 389
};

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 80 of file check_ldap.c.

{

       LDAP *ld;
       LDAPMessage *result;

       /* should be  int result = STATE_UNKNOWN; */

       int status = STATE_UNKNOWN;
       long microsec;
       double elapsed_time;

       /* for ldap tls */

       int tls;
       int version=3;

       setlocale (LC_ALL, "");
       bindtextdomain (PACKAGE, LOCALEDIR);
       textdomain (PACKAGE);

       if (strstr(argv[0],"check_ldaps")) {
              asprintf (&progname, "check_ldaps");
       }

       /* Parse extra opts if any */
       argv=np_extra_opts (&argc, argv, progname);

       if (process_arguments (argc, argv) == ERROR)
              usage4 (_("Could not parse arguments"));

       if (strstr(argv[0],"check_ldaps") && ! starttls && ! ssl_on_connect)
              starttls = TRUE;

       /* initialize alarm signal handling */
       signal (SIGALRM, socket_timeout_alarm_handler);

       /* set socket timeout */
       alarm (socket_timeout);

       /* get the start time */
       gettimeofday (&tv, NULL);

       /* initialize ldap */
#ifdef HAVE_LDAP_INIT
       if (!(ld = ldap_init (ld_host, ld_port))) {
              printf ("Could not connect to the server at port %i\n", ld_port);
              return STATE_CRITICAL;
       }
#else
       if (!(ld = ldap_open (ld_host, ld_port))) {
              if (verbose)
                     ldap_perror(ld, "ldap_open");
              printf (_("Could not connect to the server at port %i\n"), ld_port);
              return STATE_CRITICAL;
       }
#endif /* HAVE_LDAP_INIT */

#ifdef HAVE_LDAP_SET_OPTION
       /* set ldap options */
       if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
                     LDAP_OPT_SUCCESS ) {
              printf(_("Could not set protocol version %d\n"), ld_protocol);
              return STATE_CRITICAL;
       }
#endif

       if (ld_port == LDAPS_PORT || ssl_on_connect) {
              asprintf (&SERVICE, "LDAPS");
#if defined(HAVE_LDAP_SET_OPTION) && defined(LDAP_OPT_X_TLS)
              /* ldaps: set option tls */
              tls = LDAP_OPT_X_TLS_HARD;

              if (ldap_set_option (ld, LDAP_OPT_X_TLS, &tls) != LDAP_SUCCESS)
              {
                     if (verbose)
                            ldap_perror(ld, "ldaps_option");
                     printf (_("Could not init TLS at port %i!\n"), ld_port);
                     return STATE_CRITICAL;
              }
#else
              printf (_("TLS not supported by the libraries!\n"));
              return STATE_CRITICAL;
#endif /* LDAP_OPT_X_TLS */
       } else if (starttls) {
              asprintf (&SERVICE, "LDAP-TLS");
#if defined(HAVE_LDAP_SET_OPTION) && defined(HAVE_LDAP_START_TLS_S)
              /* ldap with startTLS: set option version */
              if (ldap_get_option(ld,LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS )
              {
                     if (version < LDAP_VERSION3)
                     {
                            version = LDAP_VERSION3;
                            ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
                     }
              }
              /* call start_tls */
              if (ldap_start_tls_s(ld, NULL, NULL) != LDAP_SUCCESS)
              {
                     if (verbose)
                            ldap_perror(ld, "ldap_start_tls");
                     printf (_("Could not init startTLS at port %i!\n"), ld_port);
                     return STATE_CRITICAL;
              }
#else
              printf (_("startTLS not supported by the library, needs LDAPv3!\n"));
              return STATE_CRITICAL;
#endif /* HAVE_LDAP_START_TLS_S */
       }

       /* bind to the ldap server */
       if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
                     LDAP_SUCCESS) {
              if (verbose)
                     ldap_perror(ld, "ldap_bind");
              printf (_("Could not bind to the LDAP server\n"));
              return STATE_CRITICAL;
       }

       /* do a search of all objectclasses in the base dn */
       if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
                     != LDAP_SUCCESS) {
              if (verbose)
                     ldap_perror(ld, "ldap_search");
              printf (_("Could not search/find objectclasses in %s\n"), ld_base);
              return STATE_CRITICAL;
       }

       /* unbind from the ldap server */
       ldap_unbind (ld);

       /* reset the alarm handler */
       alarm (0);

       /* calcutate the elapsed time and compare to thresholds */

       microsec = deltime (tv);
       elapsed_time = (double)microsec / 1.0e6;

       if (crit_time!=UNDEFINED && elapsed_time>crit_time)
              status = STATE_CRITICAL;
       else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
              status = STATE_WARNING;
       else
              status = STATE_OK;

       /* print out the result */
       printf (_("LDAP %s - %.3f seconds response time|%s\n"),
               state_text (status),
               elapsed_time,
               fperfdata ("time", elapsed_time, "s",
                         (int)warn_time, warn_time,
                         (int)crit_time, crit_time,
                         TRUE, 0, FALSE, 0));

       return status;
}

Here is the call graph for this function:

void print_help ( void  )
void print_usage ( void  )

Definition at line 389 of file check_cpqarray.c.

{
  printf("cpqarrayd [options]\n");
  printf("   -h         prints this text\n");
  printf("   -d         enables debugging\n");
}
int process_arguments ( int  argc,
char **  argv 
)
int validate_arguments ( void  )

Variable Documentation

const char* copyright = "2000-2008"

Definition at line 31 of file check_ldap.c.

double crit_time = UNDEFINED

Definition at line 69 of file check_ldap.c.

const char* email = "nagiosplug-devel@lists.sourceforge.net"

Definition at line 32 of file check_ldap.c.

Definition at line 56 of file check_ldap.c.

char* ld_base = NULL

Definition at line 58 of file check_ldap.c.

char* ld_binddn = NULL

Definition at line 60 of file check_ldap.c.

char ld_defattr[] = "(objectclass=*)"

Definition at line 55 of file check_ldap.c.

char* ld_host = NULL

Definition at line 57 of file check_ldap.c.

char* ld_passwd = NULL

Definition at line 59 of file check_ldap.c.

int ld_port = -1

Definition at line 61 of file check_ldap.c.

char* progname = "check_ldap"

Definition at line 30 of file check_ldap.c.

char* SERVICE = "LDAP"

Definition at line 77 of file check_ldap.c.

Definition at line 72 of file check_ldap.c.

int starttls = FALSE

Definition at line 71 of file check_ldap.c.

int verbose = 0

Definition at line 73 of file check_ldap.c.

double warn_time = UNDEFINED

Definition at line 68 of file check_ldap.c.