check_ldap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. int option_index = 0;
  108. /* initialize the long option struct */
  109. static struct option longopts[] = {
  110. {"help", no_argument, 0, 'h'},
  111. {"version", no_argument, 0, 'V'},
  112. {"timeout", required_argument, 0, 't'},
  113. {"host", required_argument, 0, 'H'},
  114. {"base", required_argument, 0, 'b'},
  115. {"attr", required_argument, 0, 'a'},
  116. {"bind", required_argument, 0, 'D'},
  117. {"pass", required_argument, 0, 'P'},
  118. {"port", required_argument, 0, 'p'},
  119. {"warn", required_argument, 0, 'w'},
  120. {"crit", required_argument, 0, 'c'},
  121. {0, 0, 0, 0}
  122. };
  123. if (argc < 2)
  124. return ERROR;
  125. for (c = 1; c < argc; c++) {
  126. if (strcmp ("-to", argv[c]) == 0)
  127. strcpy (argv[c], "-t");
  128. }
  129. while (1) {
  130. c = getopt_long (argc, argv, "hVt:c:w:H:b:p:a:D:P:", longopts, &option_index);
  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 a positive 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 unknown 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 (strlen(ld_host) == 0)
  183. usage ("please specify the host name\n");
  184. if (strlen(ld_base) == 0)
  185. usage ("please specify the LDAP base\n");
  186. else
  187. return OK;
  188. }
  189. /* function print_help */
  190. void
  191. print_help ()
  192. {
  193. print_revision (progname, revision);
  194. printf
  195. ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"
  196. "License: GPL\n" "\n");
  197. print_usage ();
  198. printf
  199. ("\n"
  200. "Options:\n"
  201. "\t-H [--host] ... host\n"
  202. "\t-a [--attr] ... ldap attribute to search (default: \"(objectclass=*)\"\n"
  203. "\t-b [--base] ... ldap base (eg. ou=my unit, o=my org, c=at)\n"
  204. "\t-D [--bind] ... ldap bind DN (if required)\n"
  205. "\t-P [--pass] ... ldap password (if required)\n"
  206. "\t-p [--port] ... ldap port (default: %d)\n"
  207. "\t-w [--warn] ... time in secs. - if the exceeds <warn> the STATE_WARNING will be returned\n"
  208. "\t-c [--crit] ... time in secs. - if the exceeds <crit> the STATE_CRITICAL will be returned\n"
  209. "\n", DEFAULT_PORT);
  210. }
  211. void
  212. print_usage ()
  213. {
  214. printf
  215. ("Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n"
  216. " [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]\n"
  217. "(Note: all times are in seconds.)\n", progname);
  218. }