check_ldap.c 7.2 KB

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