Code

convert PROGANE from a define to a const char
[nagiosplug.git] / plugins / check_ldap.c
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 #define 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 #define UNKNOWN -1
36 int process_arguments (int, char **);
37 int validate_arguments (void);
38 void print_help (void);
39 void print_usage (void);
41 char ld_defattr[] = "(objectclass=*)";
42 char *ld_attr = ld_defattr;
43 char *ld_host = NULL, *ld_base = NULL, *ld_passwd = NULL, *ld_binddn = NULL;
44 unsigned int ld_port = 389;
45 int warn_time = UNKNOWN, crit_time = UNKNOWN;
47 int
48 main (int argc, char *argv[])
49 {
51         LDAP *ld;
52         LDAPMessage *result;
54         int t_diff;
55         time_t time0, time1;
57         if (process_arguments (argc, argv) == ERROR)
58                 usage ("check_ldap: could not parse arguments\n");
60         /* initialize alarm signal handling */
61         signal (SIGALRM, socket_timeout_alarm_handler);
63         /* set socket timeout */
64         alarm (socket_timeout);
66         /* get the start time */
67         time (&time0);
69         /* initialize ldap */
70         if (!(ld = ldap_open (ld_host, ld_port))) {
71                 /*ldap_perror(ld, "ldap_open"); */
72                 printf ("Could not connect to the server at port %i\n", ld_port);
73                 return STATE_CRITICAL;
74         }
76         /* bind to the ldap server */
77         if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
78                         LDAP_SUCCESS) {
79                 /*ldap_perror(ld, "ldap_bind"); */
80                 printf ("Could not bind to the ldap-server\n");
81                 return STATE_CRITICAL;
82         }
84         /* do a search of all objectclasses in the base dn */
85         if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
86                         != LDAP_SUCCESS) {
87                 /*ldap_perror(ld, "ldap_search"); */
88                 printf ("Could not search/find objectclasses in %s\n", ld_base);
89                 return STATE_CRITICAL;
90         }
92         /* unbind from the ldap server */
93         ldap_unbind (ld);
95         /* reset the alarm handler */
96         alarm (0);
98         /* get the finish time */
99         time (&time1);
101         /* calcutate the elapsed time */
102         t_diff = time1 - time0;
104         /* check if warn_time or crit_time was exceeded */
105         if ((t_diff >= warn_time) && (t_diff < crit_time)) {
106                 printf ("LDAP warning - %i seconds response time\n", t_diff);
107                 return STATE_WARNING;
108         }
109         if (t_diff >= crit_time) {
110                 printf ("LDAP critical - %i seconds response time\n", t_diff);
111                 return STATE_CRITICAL;
112         }
114         /* print out the result */
115         printf ("LDAP ok - %i seconds response time\n", t_diff);
117         return STATE_OK;
120 /* process command-line arguments */
121 int
122 process_arguments (int argc, char **argv)
124         int c;
126 #ifdef HAVE_GETOPT_H
127         int option_index = 0;
128         /* initialize the long option struct */
129         static struct option longopts[] = {
130                 {"help", no_argument, 0, 'h'},
131                 {"version", no_argument, 0, 'V'},
132                 {"timeout", required_argument, 0, 't'},
133                 {"host", required_argument, 0, 'H'},
134                 {"base", required_argument, 0, 'b'},
135                 {"attr", required_argument, 0, 'a'},
136                 {"bind", required_argument, 0, 'D'},
137                 {"pass", required_argument, 0, 'P'},
138                 {"port", required_argument, 0, 'p'},
139                 {"warn", required_argument, 0, 'w'},
140                 {"crit", required_argument, 0, 'c'},
141                 {0, 0, 0, 0}
142         };
143 #endif
145         if (argc < 2)
146                 return ERROR;
148         for (c = 1; c < argc; c++) {
149                 if (strcmp ("-to", argv[c]) == 0)
150                         strcpy (argv[c], "-t");
151         }
153         while (1) {
154 #ifdef HAVE_GETOPT_H
155                 c =     getopt_long (argc, argv, "hVt:c:w:H:b:p:a:D:P:", longopts, &option_index);
156 #else
157                 c = getopt (argc, argv, "+?hVt:c:w:H:b:p:a:D:P:");
158 #endif
160                 if (c == -1 || c == EOF)
161                         break;
163                 switch (c) {
164                 case 'h':                                                                       /* help */
165                         print_help ();
166                         exit (STATE_OK);
167                 case 'V':                                                                       /* version */
168                         print_revision (progname, REVISION);
169                         exit (STATE_OK);
170                 case 't':                                                                       /* timeout period */
171                         if (!is_intnonneg (optarg))
172                                 usage2 ("timeout interval must be an integer", optarg);
173                         socket_timeout = atoi (optarg);
174                         break;
175                 case 'H':
176                         ld_host = optarg;
177                         break;
178                 case 'b':
179                         ld_base = optarg;
180                         break;
181                 case 'p':
182                         ld_port = atoi (optarg);
183                         break;
184                 case 'a':
185                         ld_attr = optarg;
186                         break;
187                 case 'D':
188                         ld_binddn = optarg;
189                         break;
190                 case 'P':
191                         ld_passwd = optarg;
192                         break;
193                 case 'w':
194                         warn_time = atoi (optarg);
195                         break;
196                 case 'c':
197                         crit_time = atoi (optarg);
198                         break;
199                 default:
200                         usage ("check_ldap: could not parse arguments\n");
201                         break;
202                 }
203         }
205         if (ld_host[0] == 0) {
206                 asprintf (&ld_host, "%s", argv[c]);
207         }
209         return validate_arguments ();
212 int
213 validate_arguments ()
215         if (ld_host[0] == 0 ||
216                         ld_base[0] == 0 ||
217                         ld_port == UNKNOWN || warn_time == UNKNOWN || crit_time == UNKNOWN) {
218                 return ERROR;
219         }
220         else {
221                 return OK;
222         }
224 \f
227 /* function print_help */
228 void
229 print_help ()
231         print_revision (progname, REVISION);
232         printf
233                 ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"
234                  "License: GPL\n" "\n");
235         print_usage ();
236         printf
237                 ("\n"
238                  "Options:\n"
239                  "\t-H [--host] ... host\n"
240                  "\t-a [--attr] ... ldap attribute to search (default: \"(objectclass=*)\"\n"
241                  "\t-b [--base] ... ldap base (eg. ou=my unit, o=my org, c=at)\n"
242                  "\t-D [--bind] ... ldap bind DN (if required)\n"
243                  "\t-P [--pass] ... ldap password (if required)\n"
244                  "\t-p [--port] ... ldap port (normaly 389)\n"
245                  "\t-w [--warn] ... time in secs. - if the exceeds <warn> the STATE_WARNING will be returned\n"
246                  "\t-c [--crit] ... time in secs. - if the exceeds <crit> the STATE_CRITICAL will be returned\n"
247                  "\n");
251 void
252 print_usage ()
254         printf
255                 ("Usage: %s -H <host> -b <base_dn> -p <port> [-a <attr>] [-D <binddn>]\n"
256                  "         [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]\n"
257                  "(Note: all times are in seconds.)\n", progname);