check_ldap.c 6.0 KB

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