1 /**
2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
4 * Copyright (C) 2014 Marc Fournier
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kimo Rosenbaum <kimor79 at yahoo.com>
26 * Marc Fournier <marc.fournier at camptocamp.com>
27 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
34 #include <lber.h>
35 #include <ldap.h>
37 struct cldap_s /* {{{ */
38 {
39 char *name;
41 char *cacert;
42 char *host;
43 int state;
44 _Bool starttls;
45 int timeout;
46 char *url;
47 _Bool verifyhost;
48 int version;
50 LDAP *ld;
51 };
52 typedef struct cldap_s cldap_t; /* }}} */
54 static void cldap_free (cldap_t *st) /* {{{ */
55 {
56 if (st == NULL)
57 return;
59 sfree (st->cacert);
60 sfree (st->host);
61 sfree (st->name);
62 sfree (st->url);
63 if (st->ld)
64 ldap_memfree (st->ld);
65 sfree (st);
66 } /* }}} void cldap_free */
68 /* initialize ldap for each host */
69 static int cldap_init_host (cldap_t *st) /* {{{ */
70 {
71 LDAP *ld;
72 int rc;
73 rc = ldap_initialize (&ld, st->url);
74 if (rc != LDAP_SUCCESS)
75 {
76 ERROR ("openldap plugin: ldap_initialize failed: %s",
77 ldap_err2string (rc));
78 st->state = 0;
79 ldap_unbind_ext_s (ld, NULL, NULL);
80 return (-1);
81 }
83 st->ld = ld;
85 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
87 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
88 &(const struct timeval){st->timeout, 0});
90 if (st->cacert != NULL)
91 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
93 if (st->verifyhost == 0)
94 {
95 int never = LDAP_OPT_X_TLS_NEVER;
96 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
97 }
99 if (st->starttls != 0)
100 {
101 rc = ldap_start_tls_s (ld, NULL, NULL);
102 if (rc != LDAP_SUCCESS)
103 {
104 ERROR ("openldap plugin: Failed to start tls on %s: %s",
105 st->url, ldap_err2string (rc));
106 st->state = 0;
107 ldap_unbind_ext_s (st->ld, NULL, NULL);
108 return (-1);
109 }
110 }
112 struct berval cred;
113 cred.bv_val = "";
114 cred.bv_len = 0;
116 rc = ldap_sasl_bind_s (st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
117 if (rc != LDAP_SUCCESS)
118 {
119 ERROR ("openldap plugin: Failed to bind to %s: %s",
120 st->url, ldap_err2string (rc));
121 st->state = 0;
122 ldap_unbind_ext_s (st->ld, NULL, NULL);
123 return (-1);
124 }
125 else
126 {
127 DEBUG ("openldap plugin: Successfully connected to %s",
128 st->url);
129 st->state = 1;
130 return (0);
131 }
132 } /* }}} static cldap_init_host */
134 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
135 value_t value, cldap_t *st)
136 {
137 value_list_t vl = VALUE_LIST_INIT;
139 vl.values = &value;
140 vl.values_len = 1;
142 if ((st->host == NULL)
143 || (strcmp ("", st->host) == 0)
144 || (strcmp ("localhost", st->host) == 0))
145 {
146 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
147 }
148 else
149 {
150 sstrncpy (vl.host, st->host, sizeof (vl.host));
151 }
153 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
154 if (st->name != NULL)
155 sstrncpy (vl.plugin_instance, st->name,
156 sizeof (vl.plugin_instance));
158 sstrncpy (vl.type, type, sizeof (vl.type));
159 if (type_instance != NULL)
160 sstrncpy (vl.type_instance, type_instance,
161 sizeof (vl.type_instance));
163 plugin_dispatch_values (&vl);
164 } /* }}} void cldap_submit_value */
166 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
167 derive_t d, cldap_t *st)
168 {
169 value_t v;
170 v.derive = d;
171 cldap_submit_value (type, type_instance, v, st);
172 } /* }}} void cldap_submit_derive */
174 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
175 gauge_t g, cldap_t *st)
176 {
177 value_t v;
178 v.gauge = g;
179 cldap_submit_value (type, type_instance, v, st);
180 } /* }}} void cldap_submit_gauge */
182 static int cldap_read_host (user_data_t *ud) /* {{{ */
183 {
184 cldap_t *st;
185 LDAPMessage *e, *result;
186 char *dn;
187 int rc;
188 int status;
190 char *attrs[9] = { "monitorCounter",
191 "monitorOpCompleted",
192 "monitorOpInitiated",
193 "monitoredInfo",
194 "olmBDBEntryCache",
195 "olmBDBDNCache",
196 "olmBDBIDLCache",
197 "namingContexts",
198 NULL };
200 if ((ud == NULL) || (ud->data == NULL))
201 {
202 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
203 return (-1);
204 }
206 st = (cldap_t *) ud->data;
208 status = cldap_init_host (st);
209 if (status != 0)
210 return (-1);
212 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
213 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
214 NULL, NULL, NULL, 0, &result);
216 if (rc != LDAP_SUCCESS)
217 {
218 ERROR ("openldap plugin: Failed to execute search: %s",
219 ldap_err2string (rc));
220 ldap_msgfree (result);
221 ldap_unbind_ext_s (st->ld, NULL, NULL);
222 return (-1);
223 }
225 for (e = ldap_first_entry (st->ld, result); e != NULL;
226 e = ldap_next_entry (st->ld, e))
227 {
228 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
229 {
230 unsigned long long counter = 0;
231 unsigned long long opc = 0;
232 unsigned long long opi = 0;
233 unsigned long long info = 0;
235 struct berval counter_data;
236 struct berval opc_data;
237 struct berval opi_data;
238 struct berval info_data;
239 struct berval olmbdb_data;
240 struct berval nc_data;
242 struct berval **counter_list;
243 struct berval **opc_list;
244 struct berval **opi_list;
245 struct berval **info_list;
246 struct berval **olmbdb_list;
247 struct berval **nc_list;
249 if ((counter_list = ldap_get_values_len (st->ld, e,
250 "monitorCounter")) != NULL)
251 {
252 counter_data = *counter_list[0];
253 counter = atoll (counter_data.bv_val);
254 }
256 if ((opc_list = ldap_get_values_len (st->ld, e,
257 "monitorOpCompleted")) != NULL)
258 {
259 opc_data = *opc_list[0];
260 opc = atoll (opc_data.bv_val);
261 }
263 if ((opi_list = ldap_get_values_len (st->ld, e,
264 "monitorOpInitiated")) != NULL)
265 {
266 opi_data = *opi_list[0];
267 opi = atoll (opi_data.bv_val);
268 }
270 if ((info_list = ldap_get_values_len (st->ld, e,
271 "monitoredInfo")) != NULL)
272 {
273 info_data = *info_list[0];
274 info = atoll (info_data.bv_val);
275 }
277 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
278 == 0)
279 {
280 cldap_submit_derive ("total_connections", NULL,
281 counter, st);
282 }
283 else if (strcmp (dn,
284 "cn=Current,cn=Connections,cn=Monitor")
285 == 0)
286 {
287 cldap_submit_gauge ("current_connections", NULL,
288 counter, st);
289 }
290 else if (strcmp (dn,
291 "cn=Operations,cn=Monitor") == 0)
292 {
293 cldap_submit_derive ("operations",
294 "completed", opc, st);
295 cldap_submit_derive ("operations",
296 "initiated", opi, st);
297 }
298 else if (strcmp (dn,
299 "cn=Bind,cn=Operations,cn=Monitor")
300 == 0)
301 {
302 cldap_submit_derive ("operations",
303 "bind-completed", opc, st);
304 cldap_submit_derive ("operations",
305 "bind-initiated", opi, st);
306 }
307 else if (strcmp (dn,
308 "cn=UnBind,cn=Operations,cn=Monitor")
309 == 0)
310 {
311 cldap_submit_derive ("operations",
312 "unbind-completed", opc, st);
313 cldap_submit_derive ("operations",
314 "unbind-initiated", opi, st);
315 }
316 else if (strcmp (dn,
317 "cn=Search,cn=Operations,cn=Monitor")
318 == 0)
319 {
320 cldap_submit_derive ("operations",
321 "search-completed", opc, st);
322 cldap_submit_derive ("operations",
323 "search-initiated", opi, st);
324 }
325 else if (strcmp (dn,
326 "cn=Compare,cn=Operations,cn=Monitor")
327 == 0)
328 {
329 cldap_submit_derive ("operations",
330 "compare-completed", opc, st);
331 cldap_submit_derive ("operations",
332 "compare-initiated", opi, st);
333 }
334 else if (strcmp (dn,
335 "cn=Modify,cn=Operations,cn=Monitor")
336 == 0)
337 {
338 cldap_submit_derive ("operations",
339 "modify-completed", opc, st);
340 cldap_submit_derive ("operations",
341 "modify-initiated", opi, st);
342 }
343 else if (strcmp (dn,
344 "cn=Modrdn,cn=Operations,cn=Monitor")
345 == 0)
346 {
347 cldap_submit_derive ("operations",
348 "modrdn-completed", opc, st);
349 cldap_submit_derive ("operations",
350 "modrdn-initiated", opi, st);
351 }
352 else if (strcmp (dn,
353 "cn=Add,cn=Operations,cn=Monitor")
354 == 0)
355 {
356 cldap_submit_derive ("operations",
357 "add-completed", opc, st);
358 cldap_submit_derive ("operations",
359 "add-initiated", opi, st);
360 }
361 else if (strcmp (dn,
362 "cn=Delete,cn=Operations,cn=Monitor")
363 == 0)
364 {
365 cldap_submit_derive ("operations",
366 "delete-completed", opc, st);
367 cldap_submit_derive ("operations",
368 "delete-initiated", opi, st);
369 }
370 else if (strcmp (dn,
371 "cn=Abandon,cn=Operations,cn=Monitor")
372 == 0)
373 {
374 cldap_submit_derive ("operations",
375 "abandon-completed", opc, st);
376 cldap_submit_derive ("operations",
377 "abandon-initiated", opi, st);
378 }
379 else if (strcmp (dn,
380 "cn=Extended,cn=Operations,cn=Monitor")
381 == 0)
382 {
383 cldap_submit_derive ("operations",
384 "extended-completed", opc, st);
385 cldap_submit_derive ("operations",
386 "extended-initiated", opi, st);
387 }
388 else if ((strncmp (dn, "cn=Database", 11) == 0)
389 && ((nc_list = ldap_get_values_len
390 (st->ld, e, "namingContexts")) != NULL))
391 {
392 nc_data = *nc_list[0];
393 char typeinst[DATA_MAX_NAME_LEN];
395 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
396 "olmBDBEntryCache")) != NULL)
397 {
398 olmbdb_data = *olmbdb_list[0];
399 ssnprintf (typeinst, sizeof (typeinst),
400 "bdbentrycache-%s", nc_data.bv_val);
401 cldap_submit_gauge ("cache_size", typeinst,
402 atoll (olmbdb_data.bv_val), st);
403 ldap_value_free_len (olmbdb_list);
404 }
406 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
407 "olmBDBDNCache")) != NULL)
408 {
409 olmbdb_data = *olmbdb_list[0];
410 ssnprintf (typeinst, sizeof (typeinst),
411 "bdbdncache-%s", nc_data.bv_val);
412 cldap_submit_gauge ("cache_size", typeinst,
413 atoll (olmbdb_data.bv_val), st);
414 ldap_value_free_len (olmbdb_list);
415 }
417 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
418 "olmBDBIDLCache")) != NULL)
419 {
420 olmbdb_data = *olmbdb_list[0];
421 ssnprintf (typeinst, sizeof (typeinst),
422 "bdbidlcache-%s", nc_data.bv_val);
423 cldap_submit_gauge ("cache_size", typeinst,
424 atoll (olmbdb_data.bv_val), st);
425 ldap_value_free_len (olmbdb_list);
426 }
428 ldap_value_free_len (nc_list);
429 }
430 else if (strcmp (dn,
431 "cn=Bytes,cn=Statistics,cn=Monitor")
432 == 0)
433 {
434 cldap_submit_derive ("derive", "statistics-bytes",
435 counter, st);
436 }
437 else if (strcmp (dn,
438 "cn=PDU,cn=Statistics,cn=Monitor")
439 == 0)
440 {
441 cldap_submit_derive ("derive", "statistics-pdu",
442 counter, st);
443 }
444 else if (strcmp (dn,
445 "cn=Entries,cn=Statistics,cn=Monitor")
446 == 0)
447 {
448 cldap_submit_derive ("derive", "statistics-entries",
449 counter, st);
450 }
451 else if (strcmp (dn,
452 "cn=Referrals,cn=Statistics,cn=Monitor")
453 == 0)
454 {
455 cldap_submit_derive ("derive", "statistics-referrals",
456 counter, st);
457 }
458 else if (strcmp (dn,
459 "cn=Open,cn=Threads,cn=Monitor")
460 == 0)
461 {
462 cldap_submit_gauge ("threads", "threads-open",
463 info, st);
464 }
465 else if (strcmp (dn,
466 "cn=Starting,cn=Threads,cn=Monitor")
467 == 0)
468 {
469 cldap_submit_gauge ("threads", "threads-starting",
470 info, st);
471 }
472 else if (strcmp (dn,
473 "cn=Active,cn=Threads,cn=Monitor")
474 == 0)
475 {
476 cldap_submit_gauge ("threads", "threads-active",
477 info, st);
478 }
479 else if (strcmp (dn,
480 "cn=Pending,cn=Threads,cn=Monitor")
481 == 0)
482 {
483 cldap_submit_gauge ("threads", "threads-pending",
484 info, st);
485 }
486 else if (strcmp (dn,
487 "cn=Backload,cn=Threads,cn=Monitor")
488 == 0)
489 {
490 cldap_submit_gauge ("threads", "threads-backload",
491 info, st);
492 }
493 else if (strcmp (dn,
494 "cn=Read,cn=Waiters,cn=Monitor")
495 == 0)
496 {
497 cldap_submit_derive ("derive", "waiters-read",
498 counter, st);
499 }
500 else if (strcmp (dn,
501 "cn=Write,cn=Waiters,cn=Monitor")
502 == 0)
503 {
504 cldap_submit_derive ("derive", "waiters-write",
505 counter, st);
506 }
508 ldap_value_free_len (counter_list);
509 ldap_value_free_len (opc_list);
510 ldap_value_free_len (opi_list);
511 ldap_value_free_len (info_list);
512 }
514 ldap_memfree (dn);
515 }
517 ldap_msgfree (result);
518 ldap_unbind_ext_s (st->ld, NULL, NULL);
519 return (0);
520 } /* }}} int cldap_read_host */
522 /* Configuration handling functions {{{
523 *
524 * <Plugin ldap>
525 * <Instance "plugin_instance1">
526 * URL "ldap://localhost"
527 * ...
528 * </Instance>
529 * </Plugin>
530 */
532 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
533 {
534 cldap_t *st;
535 int i;
536 int status;
538 st = malloc (sizeof (*st));
539 if (st == NULL)
540 {
541 ERROR ("openldap plugin: malloc failed.");
542 return (-1);
543 }
544 memset (st, 0, sizeof (*st));
546 status = cf_util_get_string (ci, &st->name);
547 if (status != 0)
548 {
549 sfree (st);
550 return (status);
551 }
553 st->starttls = 0;
554 st->timeout = -1;
555 st->verifyhost = 1;
556 st->version = LDAP_VERSION3;
558 for (i = 0; i < ci->children_num; i++)
559 {
560 oconfig_item_t *child = ci->children + i;
562 if (strcasecmp ("CACert", child->key) == 0)
563 status = cf_util_get_string (child, &st->cacert);
564 else if (strcasecmp ("StartTLS", child->key) == 0)
565 status = cf_util_get_boolean (child, &st->starttls);
566 else if (strcasecmp ("Timeout", child->key) == 0)
567 status = cf_util_get_int (child, &st->timeout);
568 else if (strcasecmp ("URL", child->key) == 0)
569 status = cf_util_get_string (child, &st->url);
570 else if (strcasecmp ("VerifyHost", child->key) == 0)
571 status = cf_util_get_boolean (child, &st->verifyhost);
572 else if (strcasecmp ("Version", child->key) == 0)
573 status = cf_util_get_int (child, &st->version);
574 else
575 {
576 WARNING ("openldap plugin: Option `%s' not allowed here.",
577 child->key);
578 status = -1;
579 }
581 if (status != 0)
582 break;
583 }
585 /* Check if struct is complete.. */
586 if ((status == 0) && (st->url == NULL))
587 {
588 ERROR ("openldap plugin: Instance `%s': "
589 "No URL has been configured.",
590 st->name);
591 status = -1;
592 }
594 /* Check if URL is valid */
595 if ((status == 0) && (st->url != NULL))
596 {
597 LDAPURLDesc *ludpp;
598 int rc;
600 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
601 {
602 ERROR ("openldap plugin: Instance `%s': "
603 "Invalid URL: `%s'",
604 st->name, st->url);
605 status = -1;
606 }
608 if ((status == 0) && (ludpp->lud_host != NULL))
609 {
610 st->host = strdup (ludpp->lud_host);
611 }
613 ldap_free_urldesc (ludpp);
614 }
616 if (status == 0)
617 {
618 user_data_t ud;
619 char callback_name[3*DATA_MAX_NAME_LEN];
621 memset (&ud, 0, sizeof (ud));
622 ud.data = st;
624 memset (callback_name, 0, sizeof (callback_name));
625 ssnprintf (callback_name, sizeof (callback_name),
626 "openldap/%s/%s",
627 (st->host != NULL) ? st->host : hostname_g,
628 (st->name != NULL) ? st->name : "default"),
630 status = plugin_register_complex_read (/* group = */ NULL,
631 /* name = */ callback_name,
632 /* callback = */ cldap_read_host,
633 /* interval = */ NULL,
634 /* user_data = */ &ud);
635 }
637 if (status != 0)
638 {
639 cldap_free (st);
640 return (-1);
641 }
643 return (0);
644 } /* }}} int cldap_config_add */
646 static int cldap_config (oconfig_item_t *ci) /* {{{ */
647 {
648 int i;
649 int status = 0;
651 for (i = 0; i < ci->children_num; i++)
652 {
653 oconfig_item_t *child = ci->children + i;
655 if (strcasecmp ("Instance", child->key) == 0)
656 cldap_config_add (child);
657 else
658 WARNING ("openldap plugin: The configuration option "
659 "\"%s\" is not allowed here. Did you "
660 "forget to add an <Instance /> block "
661 "around the configuration?",
662 child->key);
663 } /* for (ci->children) */
665 return (status);
666 } /* }}} int cldap_config */
668 /* }}} End of configuration handling functions */
670 static int cldap_init (void) /* {{{ */
671 {
672 /* Initialize LDAP library while still single-threaded as recommended in
673 * ldap_initialize(3) */
674 int debug_level;
675 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
676 return (0);
677 } /* }}} int cldap_init */
679 void module_register (void) /* {{{ */
680 {
681 plugin_register_complex_config ("openldap", cldap_config);
682 plugin_register_init ("openldap", cldap_init);
683 } /* }}} void module_register */