check_ldap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /***************************************************************************** *
  2. * CHECK_LDAP.C
  3. *
  4. * Program: Ldap plugin for Nagios
  5. * License: GPL
  6. * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Command line: check_ldap -h <host> -b <base_dn> -p <port> -w <warn_time> -w <crit_time>
  11. *
  12. * Description:
  13. *
  14. * This plugin is for testing a ldap server.
  15. *
  16. * Modifications:
  17. *
  18. * 08-25-1999 Ethan Galstad (nagios@nagios.org)
  19. * Modified to use common plugin include file
  20. *
  21. *****************************************************************************/
  22. #define PROGNAME "check_ldap"
  23. #define REVISION "$Revision$"
  24. #include "config.h"
  25. #include "common.h"
  26. #include "netutils.h"
  27. #include "utils.h"
  28. #include <lber.h>
  29. #include <ldap.h>
  30. #define UNKNOWN -1
  31. int process_arguments (int, char **);
  32. int validate_arguments (void);
  33. static void print_help (void);
  34. static void print_usage (void);
  35. char ld_defattr[] = "(objectclass=*)";
  36. char *ld_attr = ld_defattr;
  37. char *ld_host = NULL, *ld_base = NULL, *ld_passwd = NULL, *ld_binddn = NULL;
  38. unsigned int ld_port = 389;
  39. int warn_time = UNKNOWN, crit_time = UNKNOWN;
  40. int
  41. main (int argc, char *argv[])
  42. {
  43. LDAP *ld;
  44. LDAPMessage *result;
  45. int t_diff;
  46. time_t time0, time1;
  47. if (process_arguments (argc, argv) == ERROR)
  48. usage ("check_ldap: could not parse arguments\n");
  49. /* initialize alarm signal handling */
  50. signal (SIGALRM, socket_timeout_alarm_handler);
  51. /* set socket timeout */
  52. alarm (socket_timeout);
  53. /* get the start time */
  54. time (&time0);
  55. /* initialize ldap */
  56. if (!(ld = ldap_open (ld_host, ld_port))) {
  57. /*ldap_perror(ld, "ldap_open"); */
  58. printf ("Could not connect to the server at port %i\n", ld_port);
  59. return STATE_CRITICAL;
  60. }
  61. /* bind to the ldap server */
  62. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  63. LDAP_SUCCESS) {
  64. /*ldap_perror(ld, "ldap_bind"); */
  65. printf ("Could not bind to the ldap-server\n");
  66. return STATE_CRITICAL;
  67. }
  68. /* do a search of all objectclasses in the base dn */
  69. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  70. != LDAP_SUCCESS) {
  71. /*ldap_perror(ld, "ldap_search"); */
  72. printf ("Could not search/find objectclasses in %s\n", ld_base);
  73. return STATE_CRITICAL;
  74. }
  75. /* unbind from the ldap server */
  76. ldap_unbind (ld);
  77. /* reset the alarm handler */
  78. alarm (0);
  79. /* get the finish time */
  80. time (&time1);
  81. /* calcutate the elapsed time */
  82. t_diff = time1 - time0;
  83. /* check if warn_time or crit_time was exceeded */
  84. if ((t_diff >= warn_time) && (t_diff < crit_time)) {
  85. printf ("LDAP warning - %i seconds response time\n", t_diff);
  86. return STATE_WARNING;
  87. }
  88. if (t_diff >= crit_time) {
  89. printf ("LDAP critical - %i seconds response time\n", t_diff);
  90. return STATE_CRITICAL;
  91. }
  92. /* print out the result */
  93. printf ("LDAP ok - %i seconds response time\n", t_diff);
  94. return STATE_OK;
  95. }
  96. /* process command-line arguments */
  97. int
  98. process_arguments (int argc, char **argv)
  99. {
  100. int c;
  101. #ifdef HAVE_GETOPT_H
  102. int option_index = 0;
  103. /* initialize the long option struct */
  104. static struct option longopts[] = {
  105. {"help", no_argument, 0, 'h'},
  106. {"version", no_argument, 0, 'V'},
  107. {"timeout", required_argument, 0, 't'},
  108. {"host", required_argument, 0, 'H'},
  109. {"base", required_argument, 0, 'b'},
  110. {"attr", required_argument, 0, 'a'},
  111. {"bind", required_argument, 0, 'D'},
  112. {"pass", required_argument, 0, 'P'},
  113. {"port", required_argument, 0, 'p'},
  114. {"warn", required_argument, 0, 'w'},
  115. {"crit", required_argument, 0, 'c'},
  116. {0, 0, 0, 0}
  117. };
  118. #endif
  119. if (argc < 2)
  120. return ERROR;
  121. for (c = 1; c < argc; c++) {
  122. if (strcmp ("-to", argv[c]) == 0)
  123. strcpy (argv[c], "-t");
  124. }
  125. while (1) {
  126. #ifdef HAVE_GETOPT_H
  127. c = getopt_long (argc, argv, "hVt:c:w:H:b:p:a:D:P:", longopts, &option_index);
  128. #else
  129. c = getopt (argc, argv, "+?hVt:c:w:H:b:p:a:D:P:");
  130. #endif
  131. if (c == -1 || c == EOF)
  132. break;
  133. switch (c) {
  134. case 'h': /* help */
  135. print_help ();
  136. exit (STATE_OK);
  137. case 'V': /* version */
  138. print_revision (PROGNAME, REVISION);
  139. exit (STATE_OK);
  140. case 't': /* timeout period */
  141. if (!is_intnonneg (optarg))
  142. usage2 ("timeout interval must be an integer", optarg);
  143. socket_timeout = atoi (optarg);
  144. break;
  145. case 'H':
  146. ld_host = optarg;
  147. break;
  148. case 'b':
  149. ld_base = optarg;
  150. break;
  151. case 'p':
  152. ld_port = atoi (optarg);
  153. break;
  154. case 'a':
  155. ld_attr = optarg;
  156. break;
  157. case 'D':
  158. ld_binddn = optarg;
  159. break;
  160. case 'P':
  161. ld_passwd = optarg;
  162. break;
  163. case 'w':
  164. warn_time = atoi (optarg);
  165. break;
  166. case 'c':
  167. crit_time = atoi (optarg);
  168. break;
  169. default:
  170. usage ("check_ldap: could not parse arguments\n");
  171. break;
  172. }
  173. }
  174. if (ld_host[0] == 0) {
  175. asprintf (&ld_host, "%s", argv[c]);
  176. }
  177. return validate_arguments ();
  178. }
  179. int
  180. validate_arguments ()
  181. {
  182. if (ld_host[0] == 0 ||
  183. ld_base[0] == 0 ||
  184. ld_port == UNKNOWN || warn_time == UNKNOWN || crit_time == UNKNOWN) {
  185. return ERROR;
  186. }
  187. else {
  188. return OK;
  189. }
  190. }
  191. /* function print_help */
  192. static void
  193. print_help ()
  194. {
  195. print_revision (PROGNAME, REVISION);
  196. printf
  197. ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"
  198. "License: GPL\n" "\n");
  199. print_usage ();
  200. printf
  201. ("\n"
  202. "Options:\n"
  203. "\t-H [--host] ... host\n"
  204. "\t-a [--attr] ... ldap attribute to search (default: \"(objectclass=*)\"\n"
  205. "\t-b [--base] ... ldap base (eg. ou=my unit, o=my org, c=at)\n"
  206. "\t-D [--bind] ... ldap bind DN (if required)\n"
  207. "\t-P [--pass] ... ldap password (if required)\n"
  208. "\t-p [--port] ... ldap port (normaly 389)\n"
  209. "\t-w [--warn] ... time in secs. - if the exceeds <warn> the STATE_WARNING will be returned\n"
  210. "\t-c [--crit] ... time in secs. - if the exceeds <crit> the STATE_CRITICAL will be returned\n"
  211. "\n");
  212. }
  213. static void
  214. print_usage ()
  215. {
  216. printf
  217. ("Usage: %s -H <host> -b <base_dn> -p <port> [-a <attr>] [-D <binddn>]\n"
  218. " [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]\n"
  219. "(Note: all times are in seconds.)\n", PROGNAME);
  220. }