check_ldap.c 7.6 KB

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