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>
34 #if defined(__APPLE__)
35 #pragma clang diagnostic push
36 #pragma clang diagnostic warning "-Wdeprecated-declarations"
42 struct cldap_s /* {{{ */
59 typedef struct cldap_s cldap_t; /* }}} */
61 static cldap_t **databases = NULL;
62 static size_t databases_num = 0;
64 static void cldap_free (cldap_t *st) /* {{{ */
76 ldap_memfree (st->ld);
78 } /* }}} void cldap_free */
80 /* initialize ldap for each host */
81 static int cldap_init_host (cldap_t *st) /* {{{ */
86 if (st->state && st->ld)
88 DEBUG ("openldap plugin: Already connected to %s", st->url);
92 rc = ldap_initialize (&ld, st->url);
93 if (rc != LDAP_SUCCESS)
95 ERROR ("openldap plugin: ldap_initialize failed: %s",
96 ldap_err2string (rc));
98 ldap_unbind_ext_s (ld, NULL, NULL);
104 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
106 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
107 &(const struct timeval){st->timeout, 0});
109 ldap_set_option (st->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
111 if (st->cacert != NULL)
112 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
114 if (st->verifyhost == 0)
116 int never = LDAP_OPT_X_TLS_NEVER;
117 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
120 if (st->starttls != 0)
122 rc = ldap_start_tls_s (ld, NULL, NULL);
123 if (rc != LDAP_SUCCESS)
125 ERROR ("openldap plugin: Failed to start tls on %s: %s",
126 st->url, ldap_err2string (rc));
128 ldap_unbind_ext_s (st->ld, NULL, NULL);
134 if (st->password != NULL)
136 cred.bv_val = st->password;
137 cred.bv_len = strlen (st->password);
145 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
147 if (rc != LDAP_SUCCESS)
149 ERROR ("openldap plugin: Failed to bind to %s: %s",
150 st->url, ldap_err2string (rc));
152 ldap_unbind_ext_s (st->ld, NULL, NULL);
157 DEBUG ("openldap plugin: Successfully connected to %s",
162 } /* }}} static cldap_init_host */
164 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
165 value_t value, cldap_t *st)
167 value_list_t vl = VALUE_LIST_INIT;
172 if ((st->host == NULL)
173 || (strcmp ("", st->host) == 0)
174 || (strcmp ("localhost", st->host) == 0))
175 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
177 sstrncpy (vl.host, st->host, sizeof (vl.host));
179 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
180 if (st->name != NULL)
181 sstrncpy (vl.plugin_instance, st->name,
182 sizeof (vl.plugin_instance));
184 sstrncpy (vl.type, type, sizeof (vl.type));
185 if (type_instance != NULL)
186 sstrncpy (vl.type_instance, type_instance,
187 sizeof (vl.type_instance));
189 plugin_dispatch_values (&vl);
190 } /* }}} void cldap_submit_value */
192 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
193 derive_t d, cldap_t *st)
195 cldap_submit_value (type, type_instance, (value_t) { .derive = d }, st);
196 } /* }}} void cldap_submit_derive */
198 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
199 gauge_t g, cldap_t *st)
201 cldap_submit_value (type, type_instance, (value_t) { .gauge = g }, st);
202 } /* }}} void cldap_submit_gauge */
204 static int cldap_read_host (user_data_t *ud) /* {{{ */
212 char *attrs[9] = { "monitorCounter",
213 "monitorOpCompleted",
214 "monitorOpInitiated",
222 if ((ud == NULL) || (ud->data == NULL))
224 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
228 st = (cldap_t *) ud->data;
230 status = cldap_init_host (st);
234 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
235 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
236 NULL, NULL, NULL, 0, &result);
238 if (rc != LDAP_SUCCESS)
240 ERROR ("openldap plugin: Failed to execute search: %s",
241 ldap_err2string (rc));
242 ldap_msgfree (result);
244 ldap_unbind_ext_s (st->ld, NULL, NULL);
248 for (LDAPMessage *e = ldap_first_entry (st->ld, result); e != NULL;
249 e = ldap_next_entry (st->ld, e))
251 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
253 unsigned long long counter = 0;
254 unsigned long long opc = 0;
255 unsigned long long opi = 0;
256 unsigned long long info = 0;
258 struct berval counter_data;
259 struct berval opc_data;
260 struct berval opi_data;
261 struct berval info_data;
262 struct berval olmbdb_data;
263 struct berval nc_data;
265 struct berval **counter_list;
266 struct berval **opc_list;
267 struct berval **opi_list;
268 struct berval **info_list;
269 struct berval **olmbdb_list;
270 struct berval **nc_list;
272 if ((counter_list = ldap_get_values_len (st->ld, e,
273 "monitorCounter")) != NULL)
275 counter_data = *counter_list[0];
276 counter = atoll (counter_data.bv_val);
279 if ((opc_list = ldap_get_values_len (st->ld, e,
280 "monitorOpCompleted")) != NULL)
282 opc_data = *opc_list[0];
283 opc = atoll (opc_data.bv_val);
286 if ((opi_list = ldap_get_values_len (st->ld, e,
287 "monitorOpInitiated")) != NULL)
289 opi_data = *opi_list[0];
290 opi = atoll (opi_data.bv_val);
293 if ((info_list = ldap_get_values_len (st->ld, e,
294 "monitoredInfo")) != NULL)
296 info_data = *info_list[0];
297 info = atoll (info_data.bv_val);
300 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
303 cldap_submit_derive ("total_connections", NULL,
307 "cn=Current,cn=Connections,cn=Monitor")
310 cldap_submit_gauge ("current_connections", NULL,
314 "cn=Operations,cn=Monitor") == 0)
316 cldap_submit_derive ("operations",
317 "completed", opc, st);
318 cldap_submit_derive ("operations",
319 "initiated", opi, st);
322 "cn=Bind,cn=Operations,cn=Monitor")
325 cldap_submit_derive ("operations",
326 "bind-completed", opc, st);
327 cldap_submit_derive ("operations",
328 "bind-initiated", opi, st);
331 "cn=UnBind,cn=Operations,cn=Monitor")
334 cldap_submit_derive ("operations",
335 "unbind-completed", opc, st);
336 cldap_submit_derive ("operations",
337 "unbind-initiated", opi, st);
340 "cn=Search,cn=Operations,cn=Monitor")
343 cldap_submit_derive ("operations",
344 "search-completed", opc, st);
345 cldap_submit_derive ("operations",
346 "search-initiated", opi, st);
349 "cn=Compare,cn=Operations,cn=Monitor")
352 cldap_submit_derive ("operations",
353 "compare-completed", opc, st);
354 cldap_submit_derive ("operations",
355 "compare-initiated", opi, st);
358 "cn=Modify,cn=Operations,cn=Monitor")
361 cldap_submit_derive ("operations",
362 "modify-completed", opc, st);
363 cldap_submit_derive ("operations",
364 "modify-initiated", opi, st);
367 "cn=Modrdn,cn=Operations,cn=Monitor")
370 cldap_submit_derive ("operations",
371 "modrdn-completed", opc, st);
372 cldap_submit_derive ("operations",
373 "modrdn-initiated", opi, st);
376 "cn=Add,cn=Operations,cn=Monitor")
379 cldap_submit_derive ("operations",
380 "add-completed", opc, st);
381 cldap_submit_derive ("operations",
382 "add-initiated", opi, st);
385 "cn=Delete,cn=Operations,cn=Monitor")
388 cldap_submit_derive ("operations",
389 "delete-completed", opc, st);
390 cldap_submit_derive ("operations",
391 "delete-initiated", opi, st);
394 "cn=Abandon,cn=Operations,cn=Monitor")
397 cldap_submit_derive ("operations",
398 "abandon-completed", opc, st);
399 cldap_submit_derive ("operations",
400 "abandon-initiated", opi, st);
403 "cn=Extended,cn=Operations,cn=Monitor")
406 cldap_submit_derive ("operations",
407 "extended-completed", opc, st);
408 cldap_submit_derive ("operations",
409 "extended-initiated", opi, st);
411 else if ((strncmp (dn, "cn=Database", 11) == 0)
412 && ((nc_list = ldap_get_values_len
413 (st->ld, e, "namingContexts")) != NULL))
415 nc_data = *nc_list[0];
416 char typeinst[DATA_MAX_NAME_LEN];
418 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
419 "olmBDBEntryCache")) != NULL)
421 olmbdb_data = *olmbdb_list[0];
422 ssnprintf (typeinst, sizeof (typeinst),
423 "bdbentrycache-%s", nc_data.bv_val);
424 cldap_submit_gauge ("cache_size", typeinst,
425 atoll (olmbdb_data.bv_val), st);
426 ldap_value_free_len (olmbdb_list);
429 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
430 "olmBDBDNCache")) != NULL)
432 olmbdb_data = *olmbdb_list[0];
433 ssnprintf (typeinst, sizeof (typeinst),
434 "bdbdncache-%s", nc_data.bv_val);
435 cldap_submit_gauge ("cache_size", typeinst,
436 atoll (olmbdb_data.bv_val), st);
437 ldap_value_free_len (olmbdb_list);
440 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
441 "olmBDBIDLCache")) != NULL)
443 olmbdb_data = *olmbdb_list[0];
444 ssnprintf (typeinst, sizeof (typeinst),
445 "bdbidlcache-%s", nc_data.bv_val);
446 cldap_submit_gauge ("cache_size", typeinst,
447 atoll (olmbdb_data.bv_val), st);
448 ldap_value_free_len (olmbdb_list);
451 ldap_value_free_len (nc_list);
454 "cn=Bytes,cn=Statistics,cn=Monitor")
457 cldap_submit_derive ("derive", "statistics-bytes",
461 "cn=PDU,cn=Statistics,cn=Monitor")
464 cldap_submit_derive ("derive", "statistics-pdu",
468 "cn=Entries,cn=Statistics,cn=Monitor")
471 cldap_submit_derive ("derive", "statistics-entries",
475 "cn=Referrals,cn=Statistics,cn=Monitor")
478 cldap_submit_derive ("derive", "statistics-referrals",
482 "cn=Open,cn=Threads,cn=Monitor")
485 cldap_submit_gauge ("threads", "threads-open",
489 "cn=Starting,cn=Threads,cn=Monitor")
492 cldap_submit_gauge ("threads", "threads-starting",
496 "cn=Active,cn=Threads,cn=Monitor")
499 cldap_submit_gauge ("threads", "threads-active",
503 "cn=Pending,cn=Threads,cn=Monitor")
506 cldap_submit_gauge ("threads", "threads-pending",
510 "cn=Backload,cn=Threads,cn=Monitor")
513 cldap_submit_gauge ("threads", "threads-backload",
517 "cn=Read,cn=Waiters,cn=Monitor")
520 cldap_submit_derive ("derive", "waiters-read",
524 "cn=Write,cn=Waiters,cn=Monitor")
527 cldap_submit_derive ("derive", "waiters-write",
531 ldap_value_free_len (counter_list);
532 ldap_value_free_len (opc_list);
533 ldap_value_free_len (opi_list);
534 ldap_value_free_len (info_list);
540 ldap_msgfree (result);
542 } /* }}} int cldap_read_host */
544 /* Configuration handling functions {{{
547 * <Instance "plugin_instance1">
548 * URL "ldap://localhost"
554 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
559 st = calloc (1, sizeof (*st));
562 ERROR ("openldap plugin: calloc failed.");
566 status = cf_util_get_string (ci, &st->name);
574 st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
576 st->version = LDAP_VERSION3;
578 for (int i = 0; i < ci->children_num; i++)
580 oconfig_item_t *child = ci->children + i;
582 if (strcasecmp ("BindDN", child->key) == 0)
583 status = cf_util_get_string (child, &st->binddn);
584 else if (strcasecmp ("Password", child->key) == 0)
585 status = cf_util_get_string (child, &st->password);
586 else if (strcasecmp ("CACert", child->key) == 0)
587 status = cf_util_get_string (child, &st->cacert);
588 else if (strcasecmp ("StartTLS", child->key) == 0)
589 status = cf_util_get_boolean (child, &st->starttls);
590 else if (strcasecmp ("Timeout", child->key) == 0)
591 status = cf_util_get_int (child, &st->timeout);
592 else if (strcasecmp ("URL", child->key) == 0)
593 status = cf_util_get_string (child, &st->url);
594 else if (strcasecmp ("VerifyHost", child->key) == 0)
595 status = cf_util_get_boolean (child, &st->verifyhost);
596 else if (strcasecmp ("Version", child->key) == 0)
597 status = cf_util_get_int (child, &st->version);
600 WARNING ("openldap plugin: Option `%s' not allowed here.",
609 /* Check if struct is complete.. */
610 if ((status == 0) && (st->url == NULL))
612 ERROR ("openldap plugin: Instance `%s': "
613 "No URL has been configured.",
618 /* Check if URL is valid */
619 if ((status == 0) && (st->url != NULL))
623 if (ldap_url_parse (st->url, &ludpp) != 0)
625 ERROR ("openldap plugin: Instance `%s': "
631 if ((status == 0) && (ludpp->lud_host != NULL))
632 st->host = strdup (ludpp->lud_host);
634 ldap_free_urldesc (ludpp);
641 temp = (cldap_t **) realloc (databases,
642 sizeof (*databases) * (databases_num + 1));
646 ERROR ("openldap plugin: realloc failed");
651 char callback_name[3*DATA_MAX_NAME_LEN] = { 0 };
654 databases[databases_num] = st;
657 ssnprintf (callback_name, sizeof (callback_name),
659 (st->host != NULL) ? st->host : hostname_g,
660 (st->name != NULL) ? st->name : "default");
662 status = plugin_register_complex_read (/* group = */ NULL,
663 /* name = */ callback_name,
664 /* callback = */ cldap_read_host,
679 } /* }}} int cldap_config_add */
681 static int cldap_config (oconfig_item_t *ci) /* {{{ */
685 for (int i = 0; i < ci->children_num; i++)
687 oconfig_item_t *child = ci->children + i;
689 if (strcasecmp ("Instance", child->key) == 0)
690 cldap_config_add (child);
692 WARNING ("openldap plugin: The configuration option "
693 "\"%s\" is not allowed here. Did you "
694 "forget to add an <Instance /> block "
695 "around the configuration?",
697 } /* for (ci->children) */
700 } /* }}} int cldap_config */
702 /* }}} End of configuration handling functions */
704 static int cldap_init (void) /* {{{ */
706 /* Initialize LDAP library while still single-threaded as recommended in
707 * ldap_initialize(3) */
709 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
711 } /* }}} int cldap_init */
713 static int cldap_shutdown (void) /* {{{ */
715 for (size_t i = 0; i < databases_num; i++)
716 if (databases[i]->ld != NULL)
717 ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
722 } /* }}} int cldap_shutdown */
724 void module_register (void) /* {{{ */
726 plugin_register_complex_config ("openldap", cldap_config);
727 plugin_register_init ("openldap", cldap_init);
728 plugin_register_shutdown ("openldap", cldap_shutdown);
729 } /* }}} void module_register */
731 #if defined(__APPLE__)
732 #pragma clang diagnostic pop