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