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