check_ldap.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. ******************************************************************************/
  15. const char *progname = "check_ldap";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2000-2003";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "netutils.h"
  21. #include "utils.h"
  22. #include <lber.h>
  23. #include <ldap.h>
  24. enum {
  25. UNDEFINED = 0,
  26. #ifdef HAVE_LDAP_SET_OPTION
  27. DEFAULT_PROTOCOL = 2,
  28. #endif
  29. DEFAULT_PORT = 389
  30. };
  31. int process_arguments (int, char **);
  32. int validate_arguments (void);
  33. void print_help (void);
  34. void print_usage (void);
  35. char ld_defattr[] = "(objectclass=*)";
  36. char *ld_attr = ld_defattr;
  37. char *ld_host = NULL;
  38. char *ld_base = NULL;
  39. char *ld_passwd = NULL;
  40. char *ld_binddn = NULL;
  41. int ld_port = DEFAULT_PORT;
  42. #ifdef HAVE_LDAP_SET_OPTION
  43. int ld_protocol = DEFAULT_PROTOCOL;
  44. #endif
  45. double warn_time = UNDEFINED;
  46. double crit_time = UNDEFINED;
  47. struct timeval tv;
  48. int
  49. main (int argc, char *argv[])
  50. {
  51. LDAP *ld;
  52. LDAPMessage *result;
  53. int status;
  54. long microsec;
  55. double elapsed_time;
  56. setlocale (LC_ALL, "");
  57. bindtextdomain (PACKAGE, LOCALEDIR);
  58. textdomain (PACKAGE);
  59. if (process_arguments (argc, argv) == ERROR)
  60. usage (_("check_ldap: could not parse arguments\n"));
  61. /* initialize alarm signal handling */
  62. signal (SIGALRM, socket_timeout_alarm_handler);
  63. /* set socket timeout */
  64. alarm (socket_timeout);
  65. /* get the start time */
  66. gettimeofday (&tv, NULL);
  67. /* initialize ldap */
  68. if (!(ld = ldap_open (ld_host, ld_port))) {
  69. /*ldap_perror(ld, "ldap_open"); */
  70. printf (_("Could not connect to the server at port %i\n"), ld_port);
  71. return STATE_CRITICAL;
  72. }
  73. #ifdef HAVE_LDAP_SET_OPTION
  74. /* set ldap options */
  75. if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
  76. LDAP_OPT_SUCCESS ) {
  77. printf(_("Could not set protocol version %d\n"), ld_protocol);
  78. return STATE_CRITICAL;
  79. }
  80. #endif
  81. /* bind to the ldap server */
  82. if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
  83. LDAP_SUCCESS) {
  84. /*ldap_perror(ld, "ldap_bind"); */
  85. printf (_("Could not bind to the ldap-server\n"));
  86. return STATE_CRITICAL;
  87. }
  88. /* do a search of all objectclasses in the base dn */
  89. if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
  90. != LDAP_SUCCESS) {
  91. /*ldap_perror(ld, "ldap_search"); */
  92. printf (_("Could not search/find objectclasses in %s\n"), ld_base);
  93. return STATE_CRITICAL;
  94. }
  95. /* unbind from the ldap server */
  96. ldap_unbind (ld);
  97. /* reset the alarm handler */
  98. alarm (0);
  99. /* calcutate the elapsed time and compare to thresholds */
  100. microsec = deltime (tv);
  101. elapsed_time = (double)microsec / 1.0e6;
  102. if (crit_time!=UNDEFINED && elapsed_time>crit_time)
  103. status = STATE_CRITICAL;
  104. else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
  105. status = STATE_WARNING;
  106. else
  107. status = STATE_OK;
  108. /* print out the result */
  109. printf (_("LDAP %s - %.3f seconds response time|%s\n"),
  110. state_text (status),
  111. elapsed_time,
  112. fperfdata ("time", elapsed_time, "s",
  113. (int)warn_time, warn_time,
  114. (int)crit_time, crit_time,
  115. TRUE, 0, FALSE, 0));
  116. return status;
  117. }
  118. /* process command-line arguments */
  119. int
  120. process_arguments (int argc, char **argv)
  121. {
  122. int c;
  123. int option = 0;
  124. /* initialize the long option struct */
  125. static struct option longopts[] = {
  126. {"help", no_argument, 0, 'h'},
  127. {"version", no_argument, 0, 'V'},
  128. {"timeout", required_argument, 0, 't'},
  129. {"host", required_argument, 0, 'H'},
  130. {"base", required_argument, 0, 'b'},
  131. {"attr", required_argument, 0, 'a'},
  132. {"bind", required_argument, 0, 'D'},
  133. {"pass", required_argument, 0, 'P'},
  134. #ifdef HAVE_LDAP_SET_OPTION
  135. {"ver2", no_argument, 0, '2'},
  136. {"ver3", no_argument, 0, '3'},
  137. #endif
  138. {"use-ipv4", no_argument, 0, '4'},
  139. {"use-ipv6", no_argument, 0, '6'},
  140. {"port", required_argument, 0, 'p'},
  141. {"warn", required_argument, 0, 'w'},
  142. {"crit", required_argument, 0, 'c'},
  143. {0, 0, 0, 0}
  144. };
  145. if (argc < 2)
  146. return ERROR;
  147. for (c = 1; c < argc; c++) {
  148. if (strcmp ("-to", argv[c]) == 0)
  149. strcpy (argv[c], "-t");
  150. }
  151. while (1) {
  152. c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
  153. if (c == -1 || c == EOF)
  154. break;
  155. switch (c) {
  156. case 'h': /* help */
  157. print_help ();
  158. exit (STATE_OK);
  159. case 'V': /* version */
  160. print_revision (progname, revision);
  161. exit (STATE_OK);
  162. case 't': /* timeout period */
  163. if (!is_intnonneg (optarg))
  164. usage2 (_("Timeout interval must be a positive integer"), optarg);
  165. else
  166. socket_timeout = atoi (optarg);
  167. break;
  168. case 'H':
  169. ld_host = optarg;
  170. break;
  171. case 'b':
  172. ld_base = optarg;
  173. break;
  174. case 'p':
  175. ld_port = atoi (optarg);
  176. break;
  177. case 'a':
  178. ld_attr = optarg;
  179. break;
  180. case 'D':
  181. ld_binddn = optarg;
  182. break;
  183. case 'P':
  184. ld_passwd = optarg;
  185. break;
  186. case 'w':
  187. warn_time = strtod (optarg, NULL);
  188. break;
  189. case 'c':
  190. crit_time = strtod (optarg, NULL);
  191. break;
  192. #ifdef HAVE_LDAP_SET_OPTION
  193. case '2':
  194. ld_protocol = 2;
  195. break;
  196. case '3':
  197. ld_protocol = 3;
  198. break;
  199. #endif
  200. case '4':
  201. address_family = AF_INET;
  202. break;
  203. case '6':
  204. #ifdef USE_IPV6
  205. address_family = AF_INET6;
  206. #else
  207. usage (_("IPv6 support not available\n"));
  208. #endif
  209. break;
  210. default:
  211. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  212. print_usage ();
  213. exit (STATE_UNKNOWN);
  214. }
  215. }
  216. c = optind;
  217. if (ld_host == NULL && is_host(argv[c]))
  218. ld_host = strdup (argv[c++]);
  219. if (ld_base == NULL && argv[c])
  220. ld_base = strdup (argv[c++]);
  221. return validate_arguments ();
  222. }
  223. int
  224. validate_arguments ()
  225. {
  226. if (ld_host==NULL || strlen(ld_host)==0)
  227. usage (_("please specify the host name\n"));
  228. if (ld_base==NULL || strlen(ld_base)==0)
  229. usage (_("please specify the LDAP base\n"));
  230. return OK;
  231. }
  232. void
  233. print_help (void)
  234. {
  235. char *myport;
  236. asprintf (&myport, "%d", DEFAULT_PORT);
  237. print_revision (progname, revision);
  238. printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
  239. printf (COPYRIGHT, copyright, email);
  240. print_usage ();
  241. printf (_(UT_HELP_VRSN));
  242. printf (_(UT_HOST_PORT), 'p', myport);
  243. printf (_(UT_IPv46));
  244. printf (_("\
  245. -a [--attr]\n\
  246. ldap attribute to search (default: \"(objectclass=*)\"\n\
  247. -b [--base]\n\
  248. ldap base (eg. ou=my unit, o=my org, c=at)\n\
  249. -D [--bind]\n\
  250. ldap bind DN (if required)\n\
  251. -P [--pass]\n\
  252. ldap password (if required)\n"));
  253. #ifdef HAVE_LDAP_SET_OPTION
  254. printf (_("\
  255. -2 [--ver2]\n\
  256. use ldap protocol version 2\n\
  257. -3 [--ver3]\n\
  258. use ldap protocol version 3\n\
  259. (default protocol version: %d)\n"),
  260. DEFAULT_PROTOCOL);
  261. #endif
  262. printf (_(UT_WARN_CRIT));
  263. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  264. printf (_(UT_VERBOSE));
  265. printf (_(UT_SUPPORT));
  266. }
  267. void
  268. print_usage (void)
  269. {
  270. printf (_("\
  271. Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
  272. [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
  273. (Note: all times are in seconds.)\n"),
  274. progname, (HAVE_LDAP_SET_OPTION ? "[-2|-3] [-4|-6]" : ""));
  275. printf (_(UT_HLP_VRS), progname, progname);
  276. }