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 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 = "";
47 char *ld_base = "";
48 char *ld_passwd = NULL;
49 char *ld_binddn = NULL;
50 unsigned int ld_port = DEFAULT_PORT;
51 int warn_time = UNDEFINED;
52 int crit_time = UNDEFINED;
54 int
55 main (int argc, char *argv[])
56 {
58 LDAP *ld;
59 LDAPMessage *result;
61 int t_diff;
62 time_t time0, time1;
64 if (process_arguments (argc, argv) == ERROR)
65 usage ("check_ldap: could not parse arguments\n");
67 /* initialize alarm signal handling */
68 signal (SIGALRM, socket_timeout_alarm_handler);
70 /* set socket timeout */
71 alarm (socket_timeout);
73 /* get the start time */
74 time (&time0);
76 /* initialize ldap */
77 if (!(ld = ldap_open (ld_host, ld_port))) {
78 /*ldap_perror(ld, "ldap_open"); */
79 printf ("Could not connect to the server at port %i\n", ld_port);
80 return STATE_CRITICAL;
81 }
83 /* bind to the ldap server */
84 if (ldap_bind_s (ld, ld_binddn, ld_passwd, LDAP_AUTH_SIMPLE) !=
85 LDAP_SUCCESS) {
86 /*ldap_perror(ld, "ldap_bind"); */
87 printf ("Could not bind to the ldap-server\n");
88 return STATE_CRITICAL;
89 }
91 /* do a search of all objectclasses in the base dn */
92 if (ldap_search_s (ld, ld_base, LDAP_SCOPE_BASE, ld_attr, NULL, 0, &result)
93 != LDAP_SUCCESS) {
94 /*ldap_perror(ld, "ldap_search"); */
95 printf ("Could not search/find objectclasses in %s\n", ld_base);
96 return STATE_CRITICAL;
97 }
99 /* unbind from the ldap server */
100 ldap_unbind (ld);
102 /* reset the alarm handler */
103 alarm (0);
105 /* get the finish time */
106 time (&time1);
108 /* calcutate the elapsed time and compare to thresholds */
109 t_diff = time1 - time0;
111 if (crit_time!=UNDEFINED && t_diff>=crit_time) {
112 printf ("LDAP critical - %i seconds response time\n", t_diff);
113 return STATE_CRITICAL;
114 }
116 if (warn_time!=UNDEFINED && t_diff>=warn_time) {
117 printf ("LDAP warning - %i seconds response time\n", t_diff);
118 return STATE_WARNING;
119 }
121 /* print out the result */
122 printf ("LDAP ok - %i seconds response time\n", t_diff);
124 return STATE_OK;
125 }
127 /* process command-line arguments */
128 int
129 process_arguments (int argc, char **argv)
130 {
131 int c;
133 #ifdef HAVE_GETOPT_H
134 int option_index = 0;
135 /* initialize the long option struct */
136 static struct option longopts[] = {
137 {"help", no_argument, 0, 'h'},
138 {"version", no_argument, 0, 'V'},
139 {"timeout", required_argument, 0, 't'},
140 {"host", required_argument, 0, 'H'},
141 {"base", required_argument, 0, 'b'},
142 {"attr", required_argument, 0, 'a'},
143 {"bind", required_argument, 0, 'D'},
144 {"pass", required_argument, 0, 'P'},
145 {"port", required_argument, 0, 'p'},
146 {"warn", required_argument, 0, 'w'},
147 {"crit", required_argument, 0, 'c'},
148 {0, 0, 0, 0}
149 };
150 #endif
152 if (argc < 2)
153 return ERROR;
155 for (c = 1; c < argc; c++) {
156 if (strcmp ("-to", argv[c]) == 0)
157 strcpy (argv[c], "-t");
158 }
160 while (1) {
161 #ifdef HAVE_GETOPT_H
162 c = getopt_long (argc, argv, "hVt:c:w:H:b:p:a:D:P:", longopts, &option_index);
163 #else
164 c = getopt (argc, argv, "+?hVt:c:w:H:b:p:a:D:P:");
165 #endif
167 if (c == -1 || c == EOF)
168 break;
170 switch (c) {
171 case 'h': /* help */
172 print_help ();
173 exit (STATE_OK);
174 case 'V': /* version */
175 print_revision (progname, revision);
176 exit (STATE_OK);
177 case 't': /* timeout period */
178 if (!is_intnonneg (optarg))
179 usage2 ("timeout interval must be a positive integer", optarg);
180 socket_timeout = atoi (optarg);
181 break;
182 case 'H':
183 ld_host = optarg;
184 break;
185 case 'b':
186 ld_base = optarg;
187 break;
188 case 'p':
189 ld_port = atoi (optarg);
190 break;
191 case 'a':
192 ld_attr = optarg;
193 break;
194 case 'D':
195 ld_binddn = optarg;
196 break;
197 case 'P':
198 ld_passwd = optarg;
199 break;
200 case 'w':
201 warn_time = atoi (optarg);
202 break;
203 case 'c':
204 crit_time = atoi (optarg);
205 break;
206 default:
207 usage ("check_ldap: could not parse arguments\n");
208 break;
209 }
210 }
212 if (ld_host[0] == 0) {
213 asprintf (&ld_host, "%s", argv[c]);
214 }
216 return validate_arguments ();
217 }
219 int
220 validate_arguments ()
221 {
222 if (strlen(ld_host) == 0)
223 usage ("please specify the host name\n");
225 if (strlen(ld_base) == 0)
226 usage ("please specify the LDAP base\n");
228 else
229 return OK;
231 }
232 \f
235 /* function print_help */
236 void
237 print_help ()
238 {
239 print_revision (progname, revision);
240 printf
241 ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"
242 "License: GPL\n" "\n");
243 print_usage ();
244 printf
245 ("\n"
246 "Options:\n"
247 "\t-H [--host] ... host\n"
248 "\t-a [--attr] ... ldap attribute to search (default: \"(objectclass=*)\"\n"
249 "\t-b [--base] ... ldap base (eg. ou=my unit, o=my org, c=at)\n"
250 "\t-D [--bind] ... ldap bind DN (if required)\n"
251 "\t-P [--pass] ... ldap password (if required)\n"
252 "\t-p [--port] ... ldap port (default: %d)\n"
253 "\t-w [--warn] ... time in secs. - if the exceeds <warn> the STATE_WARNING will be returned\n"
254 "\t-c [--crit] ... time in secs. - if the exceeds <crit> the STATE_CRITICAL will be returned\n"
255 "\n", DEFAULT_PORT);
256 }
259 void
260 print_usage ()
261 {
262 printf
263 ("Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n"
264 " [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]\n"
265 "(Note: all times are in seconds.)\n", progname);
266 }