check_ldap.c 6.4 KB

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