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 {"port", required_argument, 0, 'p'},
163 {"warn", required_argument, 0, 'w'},
164 {"crit", required_argument, 0, 'c'},
165 {0, 0, 0, 0}
166 };
168 if (argc < 2)
169 return ERROR;
171 for (c = 1; c < argc; c++) {
172 if (strcmp ("-to", argv[c]) == 0)
173 strcpy (argv[c], "-t");
174 }
176 while (1) {
177 c = getopt_long (argc, argv, "hV23t:c:w:H:b:p:a:D:P:", longopts, &option_index);
179 if (c == -1 || c == EOF)
180 break;
182 switch (c) {
183 case 'h': /* help */
184 print_help ();
185 exit (STATE_OK);
186 case 'V': /* version */
187 print_revision (progname, revision);
188 exit (STATE_OK);
189 case 't': /* timeout period */
190 if (!is_intnonneg (optarg))
191 usage2 ("timeout interval must be a positive integer", optarg);
192 socket_timeout = atoi (optarg);
193 break;
194 case 'H':
195 ld_host = optarg;
196 break;
197 case 'b':
198 ld_base = optarg;
199 break;
200 case 'p':
201 ld_port = atoi (optarg);
202 break;
203 case 'a':
204 ld_attr = optarg;
205 break;
206 case 'D':
207 ld_binddn = optarg;
208 break;
209 case 'P':
210 ld_passwd = optarg;
211 break;
212 case 'w':
213 warn_time = atoi (optarg);
214 break;
215 case 'c':
216 crit_time = atoi (optarg);
217 break;
218 #ifdef HAVE_LDAP_SET_OPTION
219 case '2':
220 ld_protocol = 2;
221 break;
222 case '3':
223 ld_protocol = 3;
224 break;
225 #endif
226 default:
227 usage ("check_ldap: could not parse unknown arguments\n");
228 break;
229 }
230 }
232 if (ld_host[0] == 0) {
233 asprintf (&ld_host, "%s", argv[c]);
234 }
236 return validate_arguments ();
237 }
239 int
240 validate_arguments ()
241 {
242 if (strlen(ld_host) == 0)
243 usage ("please specify the host name\n");
245 if (strlen(ld_base) == 0)
246 usage ("please specify the LDAP base\n");
248 else
249 return OK;
251 }
252 \f
255 /* function print_help */
256 void
257 print_help ()
258 {
259 print_revision (progname, revision);
260 printf
261 ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"
262 "License: GPL\n" "\n");
263 print_usage ();
264 printf
265 ("\n"
266 "Options:\n"
267 "\t-H [--host] ... host\n"
268 "\t-a [--attr] ... ldap attribute to search (default: \"(objectclass=*)\"\n"
269 "\t-b [--base] ... ldap base (eg. ou=my unit, o=my org, c=at)\n"
270 "\t-D [--bind] ... ldap bind DN (if required)\n"
271 "\t-P [--pass] ... ldap password (if required)\n"
272 "\t-p [--port] ... ldap port (default: %d)\n"
273 #ifdef HAVE_LDAP_SET_OPTION
274 "\t-2 [--ver2] ... use ldap porotocol version 2\n"
275 "\t-3 [--ver3] ... use ldap porotocol version 3\n"
276 "\t\t(default protocol version: %d)\n"
277 #endif
278 "\t-w [--warn] ... time in secs. - if the exceeds <warn> the STATE_WARNING will be returned\n"
279 "\t-c [--crit] ... time in secs. - if the exceeds <crit> the STATE_CRITICAL will be returned\n"
280 "\n", DEFAULT_PORT
281 #ifdef HAVE_LDAP_SET_OPTION
282 , DEFAULT_PROTOCOL
283 #endif
284 );
285 }
288 void
289 print_usage ()
290 {
291 printf
292 ("Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n"
293 " [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]\n"
294 #ifdef HAVE_LDAP_SET_OPTION
295 " [-2|-3]\n"
296 #endif
297 "(Note: all times are in seconds.)\n", progname);
298 }