2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
4 * Copyright (C) 2014-2015 Marc Fournier
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 * Kimo Rosenbaum <kimor79 at yahoo.com>
26 * Marc Fournier <marc.fournier at camptocamp.com>
33 #include "configfile.h"
35 #if defined(__APPLE__)
36 #pragma clang diagnostic push
37 #pragma clang diagnostic warning "-Wdeprecated-declarations"
43 struct cldap_s /* {{{ */
60 typedef struct cldap_s cldap_t; /* }}} */
62 static cldap_t **databases = NULL;
63 static size_t databases_num = 0;
65 static void cldap_free (cldap_t *st) /* {{{ */
77 ldap_memfree (st->ld);
79 } /* }}} void cldap_free */
81 /* initialize ldap for each host */
82 static int cldap_init_host (cldap_t *st) /* {{{ */
87 if (st->state && st->ld)
89 DEBUG ("openldap plugin: Already connected to %s", st->url);
93 rc = ldap_initialize (&ld, st->url);
94 if (rc != LDAP_SUCCESS)
96 ERROR ("openldap plugin: ldap_initialize failed: %s",
97 ldap_err2string (rc));
99 ldap_unbind_ext_s (ld, NULL, NULL);
105 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
107 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
108 &(const struct timeval){st->timeout, 0});
110 ldap_set_option (st->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
112 if (st->cacert != NULL)
113 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
115 if (st->verifyhost == 0)
117 int never = LDAP_OPT_X_TLS_NEVER;
118 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
121 if (st->starttls != 0)
123 rc = ldap_start_tls_s (ld, NULL, NULL);
124 if (rc != LDAP_SUCCESS)
126 ERROR ("openldap plugin: Failed to start tls on %s: %s",
127 st->url, ldap_err2string (rc));
129 ldap_unbind_ext_s (st->ld, NULL, NULL);
135 if (st->password != NULL)
137 cred.bv_val = st->password;
138 cred.bv_len = strlen (st->password);
146 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
148 if (rc != LDAP_SUCCESS)
150 ERROR ("openldap plugin: Failed to bind to %s: %s",
151 st->url, ldap_err2string (rc));
153 ldap_unbind_ext_s (st->ld, NULL, NULL);
158 DEBUG ("openldap plugin: Successfully connected to %s",
163 } /* }}} static cldap_init_host */
165 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
166 value_t value, cldap_t *st)
168 value_list_t vl = VALUE_LIST_INIT;
173 if ((st->host == NULL)
174 || (strcmp ("", st->host) == 0)
175 || (strcmp ("localhost", st->host) == 0))
176 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
178 sstrncpy (vl.host, st->host, sizeof (vl.host));
180 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
181 if (st->name != NULL)
182 sstrncpy (vl.plugin_instance, st->name,
183 sizeof (vl.plugin_instance));
185 sstrncpy (vl.type, type, sizeof (vl.type));
186 if (type_instance != NULL)
187 sstrncpy (vl.type_instance, type_instance,
188 sizeof (vl.type_instance));
190 plugin_dispatch_values (&vl);
191 } /* }}} void cldap_submit_value */
193 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
194 derive_t d, cldap_t *st)
198 cldap_submit_value (type, type_instance, v, st);
199 } /* }}} void cldap_submit_derive */
201 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
202 gauge_t g, cldap_t *st)
206 cldap_submit_value (type, type_instance, v, st);
207 } /* }}} void cldap_submit_gauge */
209 static int cldap_read_host (user_data_t *ud) /* {{{ */
217 char *attrs[9] = { "monitorCounter",
218 "monitorOpCompleted",
219 "monitorOpInitiated",
227 if ((ud == NULL) || (ud->data == NULL))
229 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
233 st = (cldap_t *) ud->data;
235 status = cldap_init_host (st);
239 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
240 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
241 NULL, NULL, NULL, 0, &result);
243 if (rc != LDAP_SUCCESS)
245 ERROR ("openldap plugin: Failed to execute search: %s",
246 ldap_err2string (rc));
247 ldap_msgfree (result);
249 ldap_unbind_ext_s (st->ld, NULL, NULL);
253 for (LDAPMessage *e = ldap_first_entry (st->ld, result); e != NULL;
254 e = ldap_next_entry (st->ld, e))
256 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
258 unsigned long long counter = 0;
259 unsigned long long opc = 0;
260 unsigned long long opi = 0;
261 unsigned long long info = 0;
263 struct berval counter_data;
264 struct berval opc_data;
265 struct berval opi_data;
266 struct berval info_data;
267 struct berval olmbdb_data;
268 struct berval nc_data;
270 struct berval **counter_list;
271 struct berval **opc_list;
272 struct berval **opi_list;
273 struct berval **info_list;
274 struct berval **olmbdb_list;
275 struct berval **nc_list;
277 if ((counter_list = ldap_get_values_len (st->ld, e,
278 "monitorCounter")) != NULL)
280 counter_data = *counter_list[0];
281 counter = atoll (counter_data.bv_val);
284 if ((opc_list = ldap_get_values_len (st->ld, e,
285 "monitorOpCompleted")) != NULL)
287 opc_data = *opc_list[0];
288 opc = atoll (opc_data.bv_val);
291 if ((opi_list = ldap_get_values_len (st->ld, e,
292 "monitorOpInitiated")) != NULL)
294 opi_data = *opi_list[0];
295 opi = atoll (opi_data.bv_val);
298 if ((info_list = ldap_get_values_len (st->ld, e,
299 "monitoredInfo")) != NULL)
301 info_data = *info_list[0];
302 info = atoll (info_data.bv_val);
305 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
308 cldap_submit_derive ("total_connections", NULL,
312 "cn=Current,cn=Connections,cn=Monitor")
315 cldap_submit_gauge ("current_connections", NULL,
319 "cn=Operations,cn=Monitor") == 0)
321 cldap_submit_derive ("operations",
322 "completed", opc, st);
323 cldap_submit_derive ("operations",
324 "initiated", opi, st);
327 "cn=Bind,cn=Operations,cn=Monitor")
330 cldap_submit_derive ("operations",
331 "bind-completed", opc, st);
332 cldap_submit_derive ("operations",
333 "bind-initiated", opi, st);
336 "cn=UnBind,cn=Operations,cn=Monitor")
339 cldap_submit_derive ("operations",
340 "unbind-completed", opc, st);
341 cldap_submit_derive ("operations",
342 "unbind-initiated", opi, st);
345 "cn=Search,cn=Operations,cn=Monitor")
348 cldap_submit_derive ("operations",
349 "search-completed", opc, st);
350 cldap_submit_derive ("operations",
351 "search-initiated", opi, st);
354 "cn=Compare,cn=Operations,cn=Monitor")
357 cldap_submit_derive ("operations",
358 "compare-completed", opc, st);
359 cldap_submit_derive ("operations",
360 "compare-initiated", opi, st);
363 "cn=Modify,cn=Operations,cn=Monitor")
366 cldap_submit_derive ("operations",
367 "modify-completed", opc, st);
368 cldap_submit_derive ("operations",
369 "modify-initiated", opi, st);
372 "cn=Modrdn,cn=Operations,cn=Monitor")
375 cldap_submit_derive ("operations",
376 "modrdn-completed", opc, st);
377 cldap_submit_derive ("operations",
378 "modrdn-initiated", opi, st);
381 "cn=Add,cn=Operations,cn=Monitor")
384 cldap_submit_derive ("operations",
385 "add-completed", opc, st);
386 cldap_submit_derive ("operations",
387 "add-initiated", opi, st);
390 "cn=Delete,cn=Operations,cn=Monitor")
393 cldap_submit_derive ("operations",
394 "delete-completed", opc, st);
395 cldap_submit_derive ("operations",
396 "delete-initiated", opi, st);
399 "cn=Abandon,cn=Operations,cn=Monitor")
402 cldap_submit_derive ("operations",
403 "abandon-completed", opc, st);
404 cldap_submit_derive ("operations",
405 "abandon-initiated", opi, st);
408 "cn=Extended,cn=Operations,cn=Monitor")
411 cldap_submit_derive ("operations",
412 "extended-completed", opc, st);
413 cldap_submit_derive ("operations",
414 "extended-initiated", opi, st);
416 else if ((strncmp (dn, "cn=Database", 11) == 0)
417 && ((nc_list = ldap_get_values_len
418 (st->ld, e, "namingContexts")) != NULL))
420 nc_data = *nc_list[0];
421 char typeinst[DATA_MAX_NAME_LEN];
423 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
424 "olmBDBEntryCache")) != NULL)
426 olmbdb_data = *olmbdb_list[0];
427 ssnprintf (typeinst, sizeof (typeinst),
428 "bdbentrycache-%s", nc_data.bv_val);
429 cldap_submit_gauge ("cache_size", typeinst,
430 atoll (olmbdb_data.bv_val), st);
431 ldap_value_free_len (olmbdb_list);
434 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
435 "olmBDBDNCache")) != NULL)
437 olmbdb_data = *olmbdb_list[0];
438 ssnprintf (typeinst, sizeof (typeinst),
439 "bdbdncache-%s", nc_data.bv_val);
440 cldap_submit_gauge ("cache_size", typeinst,
441 atoll (olmbdb_data.bv_val), st);
442 ldap_value_free_len (olmbdb_list);
445 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
446 "olmBDBIDLCache")) != NULL)
448 olmbdb_data = *olmbdb_list[0];
449 ssnprintf (typeinst, sizeof (typeinst),
450 "bdbidlcache-%s", nc_data.bv_val);
451 cldap_submit_gauge ("cache_size", typeinst,
452 atoll (olmbdb_data.bv_val), st);
453 ldap_value_free_len (olmbdb_list);
456 ldap_value_free_len (nc_list);
459 "cn=Bytes,cn=Statistics,cn=Monitor")
462 cldap_submit_derive ("derive", "statistics-bytes",
466 "cn=PDU,cn=Statistics,cn=Monitor")
469 cldap_submit_derive ("derive", "statistics-pdu",
473 "cn=Entries,cn=Statistics,cn=Monitor")
476 cldap_submit_derive ("derive", "statistics-entries",
480 "cn=Referrals,cn=Statistics,cn=Monitor")
483 cldap_submit_derive ("derive", "statistics-referrals",
487 "cn=Open,cn=Threads,cn=Monitor")
490 cldap_submit_gauge ("threads", "threads-open",
494 "cn=Starting,cn=Threads,cn=Monitor")
497 cldap_submit_gauge ("threads", "threads-starting",
501 "cn=Active,cn=Threads,cn=Monitor")
504 cldap_submit_gauge ("threads", "threads-active",
508 "cn=Pending,cn=Threads,cn=Monitor")
511 cldap_submit_gauge ("threads", "threads-pending",
515 "cn=Backload,cn=Threads,cn=Monitor")
518 cldap_submit_gauge ("threads", "threads-backload",
522 "cn=Read,cn=Waiters,cn=Monitor")
525 cldap_submit_derive ("derive", "waiters-read",
529 "cn=Write,cn=Waiters,cn=Monitor")
532 cldap_submit_derive ("derive", "waiters-write",
536 ldap_value_free_len (counter_list);
537 ldap_value_free_len (opc_list);
538 ldap_value_free_len (opi_list);
539 ldap_value_free_len (info_list);
545 ldap_msgfree (result);
547 } /* }}} int cldap_read_host */
549 /* Configuration handling functions {{{
552 * <Instance "plugin_instance1">
553 * URL "ldap://localhost"
559 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
564 st = calloc (1, sizeof (*st));
567 ERROR ("openldap plugin: calloc failed.");
571 status = cf_util_get_string (ci, &st->name);
579 st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
581 st->version = LDAP_VERSION3;
583 for (int i = 0; i < ci->children_num; i++)
585 oconfig_item_t *child = ci->children + i;
587 if (strcasecmp ("BindDN", child->key) == 0)
588 status = cf_util_get_string (child, &st->binddn);
589 else if (strcasecmp ("Password", child->key) == 0)
590 status = cf_util_get_string (child, &st->password);
591 else if (strcasecmp ("CACert", child->key) == 0)
592 status = cf_util_get_string (child, &st->cacert);
593 else if (strcasecmp ("StartTLS", child->key) == 0)
594 status = cf_util_get_boolean (child, &st->starttls);
595 else if (strcasecmp ("Timeout", child->key) == 0)
596 status = cf_util_get_int (child, &st->timeout);
597 else if (strcasecmp ("URL", child->key) == 0)
598 status = cf_util_get_string (child, &st->url);
599 else if (strcasecmp ("VerifyHost", child->key) == 0)
600 status = cf_util_get_boolean (child, &st->verifyhost);
601 else if (strcasecmp ("Version", child->key) == 0)
602 status = cf_util_get_int (child, &st->version);
605 WARNING ("openldap plugin: Option `%s' not allowed here.",
614 /* Check if struct is complete.. */
615 if ((status == 0) && (st->url == NULL))
617 ERROR ("openldap plugin: Instance `%s': "
618 "No URL has been configured.",
623 /* Check if URL is valid */
624 if ((status == 0) && (st->url != NULL))
628 if (ldap_url_parse (st->url, &ludpp) != 0)
630 ERROR ("openldap plugin: Instance `%s': "
636 if ((status == 0) && (ludpp->lud_host != NULL))
637 st->host = strdup (ludpp->lud_host);
639 ldap_free_urldesc (ludpp);
646 temp = (cldap_t **) realloc (databases,
647 sizeof (*databases) * (databases_num + 1));
651 ERROR ("openldap plugin: realloc failed");
656 user_data_t ud = { 0 };
657 char callback_name[3*DATA_MAX_NAME_LEN] = { 0 };
660 databases[databases_num] = st;
665 ssnprintf (callback_name, sizeof (callback_name),
667 (st->host != NULL) ? st->host : hostname_g,
668 (st->name != NULL) ? st->name : "default"),
670 status = plugin_register_complex_read (/* group = */ NULL,
671 /* name = */ callback_name,
672 /* callback = */ cldap_read_host,
674 /* user_data = */ &ud);
685 } /* }}} int cldap_config_add */
687 static int cldap_config (oconfig_item_t *ci) /* {{{ */
691 for (int i = 0; i < ci->children_num; i++)
693 oconfig_item_t *child = ci->children + i;
695 if (strcasecmp ("Instance", child->key) == 0)
696 cldap_config_add (child);
698 WARNING ("openldap plugin: The configuration option "
699 "\"%s\" is not allowed here. Did you "
700 "forget to add an <Instance /> block "
701 "around the configuration?",
703 } /* for (ci->children) */
706 } /* }}} int cldap_config */
708 /* }}} End of configuration handling functions */
710 static int cldap_init (void) /* {{{ */
712 /* Initialize LDAP library while still single-threaded as recommended in
713 * ldap_initialize(3) */
715 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
717 } /* }}} int cldap_init */
719 static int cldap_shutdown (void) /* {{{ */
721 for (size_t i = 0; i < databases_num; i++)
722 if (databases[i]->ld != NULL)
723 ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
728 } /* }}} int cldap_shutdown */
730 void module_register (void) /* {{{ */
732 plugin_register_complex_config ("openldap", cldap_config);
733 plugin_register_init ("openldap", cldap_init);
734 plugin_register_shutdown ("openldap", cldap_shutdown);
735 } /* }}} void module_register */
737 #if defined(__APPLE__)
738 #pragma clang diagnostic pop