Code

bump copyright year
[nagiosplug.git] / plugins / check_ldap.c
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$
18  
19 ******************************************************************************/
21 const char *progname = "check_ldap";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2004";
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         /* should be    int result = STATE_UNKNOWN; */
68         
69         int status = STATE_UNKNOW;
70         long microsec;
71         double elapsed_time;
73         setlocale (LC_ALL, "");
74         bindtextdomain (PACKAGE, LOCALEDIR);
75         textdomain (PACKAGE);
77         if (process_arguments (argc, argv) != TRUE)
78                 usage4 (_("Could not parse arguments"));
80         /* initialize alarm signal handling */
81         signal (SIGALRM, socket_timeout_alarm_handler);
83         /* set socket timeout */
84         alarm (socket_timeout);
86         /* get the start time */
87         gettimeofday (&tv, NULL);
89         /* initialize ldap */
90         if (!(ld = ldap_open (ld_host, ld_port))) {
91                 /*ldap_perror(ld, "ldap_open"); */
92                 printf (_("Could not connect to the server at port %i\n"), ld_port);
93                 return STATE_CRITICAL;
94         }
96 #ifdef HAVE_LDAP_SET_OPTION
97         /* set ldap options */
98         if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &ld_protocol) !=
99                         LDAP_OPT_SUCCESS ) {
100                 printf(_("Could not set protocol version %d\n"), ld_protocol);
101                 return STATE_CRITICAL;
102         }
103 #endif
104         /* bind to the ldap server */
105         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
106                         LDAP_SUCCESS) {
107                 /*ldap_perror(ld, "ldap_bind"); */
108                 printf (_("Could not bind to the ldap-server\n"));
109                 return STATE_CRITICAL;
110         }
112         /* do a search of all objectclasses in the base dn */
113         if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
114                         != LDAP_SUCCESS) {
115                 /*ldap_perror(ld, "ldap_search"); */
116                 printf (_("Could not search/find objectclasses in %s\n"), ld_base);
117                 return STATE_CRITICAL;
118         }
120         /* unbind from the ldap server */
121         ldap_unbind (ld);
123         /* reset the alarm handler */
124         alarm (0);
126         /* calcutate the elapsed time and compare to thresholds */
128         microsec = deltime (tv);
129         elapsed_time = (double)microsec / 1.0e6;
131         if (crit_time!=UNDEFINED && elapsed_time>crit_time)
132                 status = STATE_CRITICAL;
133         else if (warn_time!=UNDEFINED && elapsed_time>warn_time)
134                 status = STATE_WARNING;
135         else
136                 status = STATE_OK;
138         /* print out the result */
139         printf (_("LDAP %s - %.3f seconds response time|%s\n"),
140                 state_text (status),
141                 elapsed_time,
142                 fperfdata ("time", elapsed_time, "s",
143                           (int)warn_time, warn_time,
144                           (int)crit_time, crit_time,
145                           TRUE, 0, FALSE, 0));
147         return status;
150 /* process command-line arguments */
151 int
152 process_arguments (int argc, char **argv)
154         int c;
156         int option = 0;
157         /* initialize the long option struct */
158         static struct option longopts[] = {
159                 {"help", no_argument, 0, 'h'},
160                 {"version", no_argument, 0, 'V'},
161                 {"timeout", required_argument, 0, 't'},
162                 {"host", required_argument, 0, 'H'},
163                 {"base", required_argument, 0, 'b'},
164                 {"attr", required_argument, 0, 'a'},
165                 {"bind", required_argument, 0, 'D'},
166                 {"pass", required_argument, 0, 'P'},
167 #ifdef HAVE_LDAP_SET_OPTION
168                 {"ver2", no_argument, 0, '2'},
169                 {"ver3", no_argument, 0, '3'},
170 #endif
171                 {"use-ipv4", no_argument, 0, '4'},
172                 {"use-ipv6", no_argument, 0, '6'},
173                 {"port", required_argument, 0, 'p'},
174                 {"warn", required_argument, 0, 'w'},
175                 {"crit", required_argument, 0, 'c'},
176                 {0, 0, 0, 0}
177         };
179         if (argc < 2)
180                 return ERROR;
182         for (c = 1; c < argc; c++) {
183                 if (strcmp ("-to", argv[c]) == 0)
184                         strcpy (argv[c], "-t");
185         }
187         while (1) {
188                 c = getopt_long (argc, argv, "hV2346t:c:w:H:b:p:a:D:P:", longopts, &option);
190                 if (c == -1 || c == EOF)
191                         break;
193                 switch (c) {
194                 case 'h':                                                                       /* help */
195                         print_help ();
196                         exit (STATE_OK);
197                 case 'V':                                                                       /* version */
198                         print_revision (progname, revision);
199                         exit (STATE_OK);
200                 case 't':                                                                       /* timeout period */
201                         if (!is_intnonneg (optarg))
202                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
203                         else
204                                 socket_timeout = atoi (optarg);
205                         break;
206                 case 'H':
207                         ld_host = optarg;
208                         break;
209                 case 'b':
210                         ld_base = optarg;
211                         break;
212                 case 'p':
213                         ld_port = atoi (optarg);
214                         break;
215                 case 'a':
216                         ld_attr = optarg;
217                         break;
218                 case 'D':
219                         ld_binddn = optarg;
220                         break;
221                 case 'P':
222                         ld_passwd = optarg;
223                         break;
224                 case 'w':
225                         warn_time = strtod (optarg, NULL);
226                         break;
227                 case 'c':
228                         crit_time = strtod (optarg, NULL);
229                         break;
230 #ifdef HAVE_LDAP_SET_OPTION
231                 case '2':
232                         ld_protocol = 2;
233                         break;
234                 case '3':
235                         ld_protocol = 3;
236                         break;
237 #endif
238                 case '4':
239                         address_family = AF_INET;
240                         break;
241                 case '6':
242 #ifdef USE_IPV6
243                         address_family = AF_INET6;
244 #else
245                         usage (_("IPv6 support not available\n"));
246 #endif
247                         break;
248                 default:
249                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
250                         print_usage ();
251                         exit (STATE_UNKNOWN);
252                 }
253         }
255         c = optind;
256         if (ld_host == NULL && is_host(argv[c]))
257                 ld_host = strdup (argv[c++]);
259         if (ld_base == NULL && argv[c])
260                 ld_base = strdup (argv[c++]);
262         return validate_arguments ();
265 int
266 validate_arguments ()
268         if (ld_host==NULL || strlen(ld_host)==0)
269                 usage (_("please specify the host name\n"));
271         if (ld_base==NULL || strlen(ld_base)==0)
272                 usage (_("please specify the LDAP base\n"));
274         return OK;
282 void
283 print_help (void)
285         char *myport;
286         asprintf (&myport, "%d", DEFAULT_PORT);
288         print_revision (progname, revision);
290         printf ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n");
291         printf (COPYRIGHT, copyright, email);
293         print_usage ();
295         printf (_(UT_HELP_VRSN));
297         printf (_(UT_HOST_PORT), 'p', myport);
299         printf (_(UT_IPv46));
301         printf (_("\
302  -a [--attr]\n\
303     ldap attribute to search (default: \"(objectclass=*)\"\n\
304  -b [--base]\n\
305     ldap base (eg. ou=my unit, o=my org, c=at)\n\
306  -D [--bind]\n\
307     ldap bind DN (if required)\n\
308  -P [--pass]\n\
309     ldap password (if required)\n"));
311 #ifdef HAVE_LDAP_SET_OPTION
312         printf (_("\
313  -2 [--ver2]\n\
314      use ldap protocol version 2\n\
315  -3 [--ver3]\n\
316     use ldap protocol version 3\n\
317     (default protocol version: %d)\n"),
318                 DEFAULT_PROTOCOL);
319 #endif
321         printf (_(UT_WARN_CRIT));
323         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
325         printf (_(UT_VERBOSE));
327         printf (_(UT_SUPPORT));
333 void
334 print_usage (void)
336         printf (_("\
337 Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n\
338   [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]%s\n\
339 (Note: all times are in seconds.)\n"),
340                 progname,
341 #ifdef HAVE_LDAP_SET_OPTION
342                         " [-2|-3] [-4|-6]"
343 #else
344                         ""
345 #endif
346                         );
348         printf (_(UT_HLP_VRS), progname, progname);