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 int option_index = 0;
134 /* initialize the long option struct */
135 static struct option longopts[] = {
136 {"help", no_argument, 0, 'h'},
137 {"version", no_argument, 0, 'V'},
138 {"timeout", required_argument, 0, 't'},
139 {"host", required_argument, 0, 'H'},
140 {"base", required_argument, 0, 'b'},
141 {"attr", required_argument, 0, 'a'},
142 {"bind", required_argument, 0, 'D'},
143 {"pass", required_argument, 0, 'P'},
144 {"port", required_argument, 0, 'p'},
145 {"warn", required_argument, 0, 'w'},
146 {"crit", required_argument, 0, 'c'},
147 {0, 0, 0, 0}
148 };
150 if (argc < 2)
151 return ERROR;
153 for (c = 1; c < argc; c++) {
154 if (strcmp ("-to", argv[c]) == 0)
155 strcpy (argv[c], "-t");
156 }
158 while (1) {
159 c = getopt_long (argc, argv, "hVt:c:w:H:b:p:a:D:P:", longopts, &option_index);
161 if (c == -1 || c == EOF)
162 break;
164 switch (c) {
165 case 'h': /* help */
166 print_help ();
167 exit (STATE_OK);
168 case 'V': /* version */
169 print_revision (progname, revision);
170 exit (STATE_OK);
171 case 't': /* timeout period */
172 if (!is_intnonneg (optarg))
173 usage2 ("timeout interval must be a positive integer", optarg);
174 socket_timeout = atoi (optarg);
175 break;
176 case 'H':
177 ld_host = optarg;
178 break;
179 case 'b':
180 ld_base = optarg;
181 break;
182 case 'p':
183 ld_port = atoi (optarg);
184 break;
185 case 'a':
186 ld_attr = optarg;
187 break;
188 case 'D':
189 ld_binddn = optarg;
190 break;
191 case 'P':
192 ld_passwd = optarg;
193 break;
194 case 'w':
195 warn_time = atoi (optarg);
196 break;
197 case 'c':
198 crit_time = atoi (optarg);
199 break;
200 default:
201 usage ("check_ldap: could not parse arguments\n");
202 break;
203 }
204 }
206 if (ld_host[0] == 0) {
207 asprintf (&ld_host, "%s", argv[c]);
208 }
210 return validate_arguments ();
211 }
213 int
214 validate_arguments ()
215 {
216 if (strlen(ld_host) == 0)
217 usage ("please specify the host name\n");
219 if (strlen(ld_base) == 0)
220 usage ("please specify the LDAP base\n");
222 else
223 return OK;
225 }
226 \f
229 /* function print_help */
230 void
231 print_help ()
232 {
233 print_revision (progname, revision);
234 printf
235 ("Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)\n"
236 "License: GPL\n" "\n");
237 print_usage ();
238 printf
239 ("\n"
240 "Options:\n"
241 "\t-H [--host] ... host\n"
242 "\t-a [--attr] ... ldap attribute to search (default: \"(objectclass=*)\"\n"
243 "\t-b [--base] ... ldap base (eg. ou=my unit, o=my org, c=at)\n"
244 "\t-D [--bind] ... ldap bind DN (if required)\n"
245 "\t-P [--pass] ... ldap password (if required)\n"
246 "\t-p [--port] ... ldap port (default: %d)\n"
247 "\t-w [--warn] ... time in secs. - if the exceeds <warn> the STATE_WARNING will be returned\n"
248 "\t-c [--crit] ... time in secs. - if the exceeds <crit> the STATE_CRITICAL will be returned\n"
249 "\n", DEFAULT_PORT);
250 }
253 void
254 print_usage ()
255 {
256 printf
257 ("Usage: %s -H <host> -b <base_dn> [-p <port>] [-a <attr>] [-D <binddn>]\n"
258 " [-P <password>] [-w <warn_time>] [-c <crit_time>] [-t timeout]\n"
259 "(Note: all times are in seconds.)\n", progname);
260 }