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