2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Kimo Rosenbaum <kimor79 at yahoo.com>
25 #include "configfile.h"
30 struct ldap_s /* {{{ */
46 typedef struct ldap_s ldap_t; /* }}} */
48 static void ldap_free (ldap_t *st) /* {{{ */
58 ldap_memfree (st->ld);
60 } /* }}} void ldap_free */
62 /* initialize ldap for each host */
63 static int ldap_init_host (ldap_t *st) /* {{{ */
67 rc = ldap_initialize (&ld, st->url);
68 if (rc != LDAP_SUCCESS)
70 ERROR ("openldap plugin: ldap_initialize failed: %s",
71 ldap_err2string (rc));
78 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
80 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
81 &(const struct timeval){st->timeout, 0});
83 if (st->cacert != NULL)
84 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
86 if (st->verifyhost == 0)
88 int never = LDAP_OPT_X_TLS_NEVER;
89 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
92 if (st->starttls != 0)
94 rc = ldap_start_tls_s (ld, NULL, NULL);
95 if (rc != LDAP_SUCCESS)
97 ERROR ("openldap plugin: Failed to start tls on %s: %s",
98 st->url, ldap_err2string (rc));
100 ldap_unbind_ext_s (st->ld, NULL, NULL);
109 rc = ldap_sasl_bind_s (st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
110 if (rc != LDAP_SUCCESS)
112 ERROR ("openldap plugin: Failed to bind to %s: %s",
113 st->url, ldap_err2string (rc));
115 ldap_unbind_ext_s (st->ld, NULL, NULL);
120 DEBUG ("openldap plugin: Successfully connected to %s",
125 } /* }}} static ldap_init_host */
127 static void ldap_submit_value (const char *type, const char *type_instance, /* {{{ */
128 value_t value, ldap_t *st)
130 value_list_t vl = VALUE_LIST_INIT;
135 if ((st->host == NULL)
136 || (strcmp ("", st->host) == 0)
137 || (strcmp ("localhost", st->host) == 0))
139 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
143 sstrncpy (vl.host, st->host, sizeof (vl.host));
146 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
147 if (st->name != NULL)
148 sstrncpy (vl.plugin_instance, st->name,
149 sizeof (vl.plugin_instance));
151 sstrncpy (vl.type, type, sizeof (vl.type));
152 if (type_instance != NULL)
153 sstrncpy (vl.type_instance, type_instance,
154 sizeof (vl.type_instance));
156 plugin_dispatch_values (&vl);
157 } /* }}} void ldap_submit_value */
159 static void ldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
160 derive_t d, ldap_t *st)
164 ldap_submit_value (type, type_instance, v, st);
165 } /* }}} void ldap_submit_derive */
167 static void ldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
168 gauge_t g, ldap_t *st)
172 ldap_submit_value (type, type_instance, v, st);
173 } /* }}} void ldap_submit_gauge */
175 static int ldap_read_host (user_data_t *ud) /* {{{ */
178 LDAPMessage *e, *result;
183 char *attrs[9] = { "monitorCounter",
184 "monitorOpCompleted",
185 "monitorOpInitiated",
193 if ((ud == NULL) || (ud->data == NULL))
195 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
199 st = (ldap_t *) ud->data;
201 status = ldap_init_host (st);
205 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
206 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
207 NULL, NULL, NULL, 0, &result);
209 if (rc != LDAP_SUCCESS)
211 ERROR ("openldap plugin: Failed to execute search: %s",
212 ldap_err2string (rc));
213 ldap_msgfree (result);
217 for (e = ldap_first_entry (st->ld, result); e != NULL;
218 e = ldap_next_entry (st->ld, e))
220 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
222 unsigned long long counter = 0;
223 unsigned long long opc = 0;
224 unsigned long long opi = 0;
225 unsigned long long info = 0;
227 struct berval counter_data;
228 struct berval opc_data;
229 struct berval opi_data;
230 struct berval info_data;
231 struct berval olmbdb_data;
232 struct berval nc_data;
234 struct berval **counter_list;
235 struct berval **opc_list;
236 struct berval **opi_list;
237 struct berval **info_list;
238 struct berval **olmbdb_list;
239 struct berval **nc_list;
241 if ((counter_list = ldap_get_values_len (st->ld, e,
242 "monitorCounter")) != NULL)
244 counter_data = *counter_list[0];
245 counter = atoll (counter_data.bv_val);
248 if ((opc_list = ldap_get_values_len (st->ld, e,
249 "monitorOpCompleted")) != NULL)
251 opc_data = *opc_list[0];
252 opc = atoll (opc_data.bv_val);
255 if ((opi_list = ldap_get_values_len (st->ld, e,
256 "monitorOpInitiated")) != NULL)
258 opi_data = *opi_list[0];
259 opi = atoll (opi_data.bv_val);
262 if ((info_list = ldap_get_values_len (st->ld, e,
263 "monitoredInfo")) != NULL)
265 info_data = *info_list[0];
266 info = atoll (info_data.bv_val);
269 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
272 ldap_submit_derive ("total_connections", NULL,
276 "cn=Current,cn=Connections,cn=Monitor")
279 ldap_submit_gauge ("current_connections", NULL,
283 "cn=Operations,cn=Monitor") == 0)
285 ldap_submit_derive ("operations",
286 "completed", opc, st);
287 ldap_submit_derive ("operations",
288 "initiated", opi, st);
291 "cn=Bind,cn=Operations,cn=Monitor")
294 ldap_submit_derive ("operations",
295 "bind-completed", opc, st);
296 ldap_submit_derive ("operations",
297 "bind-initiated", opi, st);
300 "cn=UnBind,cn=Operations,cn=Monitor")
303 ldap_submit_derive ("operations",
304 "unbind-completed", opc, st);
305 ldap_submit_derive ("operations",
306 "unbind-initiated", opi, st);
309 "cn=Search,cn=Operations,cn=Monitor")
312 ldap_submit_derive ("operations",
313 "search-completed", opc, st);
314 ldap_submit_derive ("operations",
315 "search-initiated", opi, st);
318 "cn=Compare,cn=Operations,cn=Monitor")
321 ldap_submit_derive ("operations",
322 "compare-completed", opc, st);
323 ldap_submit_derive ("operations",
324 "compare-initiated", opi, st);
327 "cn=Modify,cn=Operations,cn=Monitor")
330 ldap_submit_derive ("operations",
331 "modify-completed", opc, st);
332 ldap_submit_derive ("operations",
333 "modify-initiated", opi, st);
336 "cn=Modrdn,cn=Operations,cn=Monitor")
339 ldap_submit_derive ("operations",
340 "modrdn-completed", opc, st);
341 ldap_submit_derive ("operations",
342 "modrdn-initiated", opi, st);
345 "cn=Add,cn=Operations,cn=Monitor")
348 ldap_submit_derive ("operations",
349 "add-completed", opc, st);
350 ldap_submit_derive ("operations",
351 "add-initiated", opi, st);
354 "cn=Delete,cn=Operations,cn=Monitor")
357 ldap_submit_derive ("operations",
358 "delete-completed", opc, st);
359 ldap_submit_derive ("operations",
360 "delete-initiated", opi, st);
363 "cn=Abandon,cn=Operations,cn=Monitor")
366 ldap_submit_derive ("operations",
367 "abandon-completed", opc, st);
368 ldap_submit_derive ("operations",
369 "abandon-initiated", opi, st);
372 "cn=Extended,cn=Operations,cn=Monitor")
375 ldap_submit_derive ("operations",
376 "extended-completed", opc, st);
377 ldap_submit_derive ("operations",
378 "extended-initiated", opi, st);
380 else if ((strncmp (dn, "cn=Database", 11) == 0)
381 && ((nc_list = ldap_get_values_len
382 (st->ld, e, "namingContexts")) != NULL))
384 nc_data = *nc_list[0];
385 char typeinst[DATA_MAX_NAME_LEN];
387 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
388 "olmBDBEntryCache")) != NULL)
390 olmbdb_data = *olmbdb_list[0];
391 ssnprintf (typeinst, sizeof (typeinst),
392 "bdbentrycache-%s", nc_data.bv_val);
393 ldap_submit_gauge ("cache_size", typeinst,
394 atoll (olmbdb_data.bv_val), st);
395 ldap_value_free_len (olmbdb_list);
398 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
399 "olmBDBDNCache")) != NULL)
401 olmbdb_data = *olmbdb_list[0];
402 ssnprintf (typeinst, sizeof (typeinst),
403 "bdbdncache-%s", nc_data.bv_val);
404 ldap_submit_gauge ("cache_size", typeinst,
405 atoll (olmbdb_data.bv_val), st);
406 ldap_value_free_len (olmbdb_list);
409 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
410 "olmBDBIDLCache")) != NULL)
412 olmbdb_data = *olmbdb_list[0];
413 ssnprintf (typeinst, sizeof (typeinst),
414 "bdbidlcache-%s", nc_data.bv_val);
415 ldap_submit_gauge ("cache_size", typeinst,
416 atoll (olmbdb_data.bv_val), st);
417 ldap_value_free_len (olmbdb_list);
420 ldap_value_free_len (nc_list);
423 "cn=Bytes,cn=Statistics,cn=Monitor")
426 ldap_submit_derive ("derive", "statistics-bytes",
430 "cn=PDU,cn=Statistics,cn=Monitor")
433 ldap_submit_derive ("derive", "statistics-pdu",
437 "cn=Entries,cn=Statistics,cn=Monitor")
440 ldap_submit_derive ("derive", "statistics-entries",
444 "cn=Referrals,cn=Statistics,cn=Monitor")
447 ldap_submit_derive ("derive", "statistics-referrals",
451 "cn=Open,cn=Threads,cn=Monitor")
454 ldap_submit_gauge ("threads", "threads-open",
458 "cn=Starting,cn=Threads,cn=Monitor")
461 ldap_submit_gauge ("threads", "threads-starting",
465 "cn=Active,cn=Threads,cn=Monitor")
468 ldap_submit_gauge ("threads", "threads-active",
472 "cn=Pending,cn=Threads,cn=Monitor")
475 ldap_submit_gauge ("threads", "threads-pending",
479 "cn=Backload,cn=Threads,cn=Monitor")
482 ldap_submit_gauge ("threads", "threads-backload",
486 "cn=Read,cn=Waiters,cn=Monitor")
489 ldap_submit_derive ("derive", "waiters-read",
493 "cn=Write,cn=Waiters,cn=Monitor")
496 ldap_submit_derive ("derive", "waiters-write",
500 ldap_value_free_len (counter_list);
501 ldap_value_free_len (opc_list);
502 ldap_value_free_len (opi_list);
503 ldap_value_free_len (info_list);
509 ldap_msgfree (result);
510 ldap_unbind_ext_s (st->ld, NULL, NULL);
512 } /* }}} int ldap_read_host */
514 /* Configuration handling functions {{{
517 * <Instance "plugin_instance1">
518 * URL "ldap://localhost"
524 static int ldap_config_set_string (char **ret_string, /* {{{ */
529 if ((ci->values_num != 1)
530 || (ci->values[0].type != OCONFIG_TYPE_STRING))
532 WARNING ("openldap plugin: The `%s' config option "
533 "needs exactly one string argument.", ci->key);
537 string = strdup (ci->values[0].value.string);
540 ERROR ("openldap plugin: strdup failed.");
544 if (*ret_string != NULL)
546 *ret_string = string;
549 } /* }}} int ldap_config_set_string */
551 static int ldap_config_set_int (int *ret_int, /* {{{ */
554 if ((ci->values_num != 1)
555 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
557 WARNING ("openldap plugin: The `%s' config option "
558 "needs exactly one string argument.", ci->key);
562 *ret_int = ci->values[0].value.number;
565 } /* }}} int ldap_config_set_int */
567 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
572 if (ci->values_num != 1)
577 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
578 *ret_boolean = ci->values[0].value.boolean;
579 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
581 if (IS_TRUE (ci->values[0].value.string))
583 else if (IS_FALSE (ci->values[0].value.string))
594 WARNING ("openldap plugin: The `%s' config option "
595 "needs exactly one boolean argument.", ci->key);
599 } /* }}} int ldap_config_set_bool */
601 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
607 if ((ci->values_num != 1)
608 || (ci->values[0].type != OCONFIG_TYPE_STRING))
610 WARNING ("openldap plugin: The `%s' config option "
611 "needs exactly one string argument.", ci->key);
615 st = (ldap_t *) malloc (sizeof (*st));
618 ERROR ("openldap plugin: malloc failed.");
621 memset (st, 0, sizeof (*st));
623 status = ldap_config_set_string (&st->name, ci);
632 st->version = LDAP_VERSION3;
634 for (i = 0; i < ci->children_num; i++)
636 oconfig_item_t *child = ci->children + i;
638 if (strcasecmp ("CACert", child->key) == 0)
639 status = ldap_config_set_string (&st->cacert, child);
640 else if (strcasecmp ("StartTLS", child->key) == 0)
641 status = ldap_config_set_bool (&st->starttls, child);
642 else if (strcasecmp ("Timeout", child->key) == 0)
643 status = ldap_config_set_int (&st->timeout, child);
644 else if (strcasecmp ("URL", child->key) == 0)
645 status = ldap_config_set_string (&st->url, child);
646 else if (strcasecmp ("VerifyHost", child->key) == 0)
647 status = ldap_config_set_bool (&st->verifyhost, child);
648 else if (strcasecmp ("Version", child->key) == 0)
649 status = ldap_config_set_int (&st->version, child);
652 WARNING ("openldap plugin: Option `%s' not allowed here.",
661 /* Check if struct is complete.. */
662 if ((status == 0) && (st->url == NULL))
664 ERROR ("openldap plugin: Instance `%s': "
665 "No URL has been configured.",
670 /* Check if URL is valid */
671 if ((status == 0) && (st->url != NULL))
676 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
678 ERROR ("openldap plugin: Instance `%s': "
685 st->host = strdup (ludpp->lud_host);
688 ldap_free_urldesc (ludpp);
694 char callback_name[3*DATA_MAX_NAME_LEN];
696 memset (&ud, 0, sizeof (ud));
699 memset (callback_name, 0, sizeof (callback_name));
700 ssnprintf (callback_name, sizeof (callback_name),
702 (st->host != NULL) ? st->host : hostname_g,
703 (st->name != NULL) ? st->name : "default"),
705 status = plugin_register_complex_read (/* group = */ NULL,
706 /* name = */ callback_name,
707 /* callback = */ ldap_read_host,
708 /* interval = */ NULL,
709 /* user_data = */ &ud);
719 } /* }}} int ldap_config_add */
721 static int ldap_config (oconfig_item_t *ci) /* {{{ */
726 for (i = 0; i < ci->children_num; i++)
728 oconfig_item_t *child = ci->children + i;
730 if (strcasecmp ("Instance", child->key) == 0)
731 ldap_config_add (child);
733 WARNING ("openldap plugin: The configuration option "
734 "\"%s\" is not allowed here. Did you "
735 "forget to add an <Instance /> block "
736 "around the configuration?",
738 } /* for (ci->children) */
741 } /* }}} int ldap_config */
743 /* }}} End of configuration handling functions */
745 static int ldap_init (void) /* {{{ */
747 /* Initialize LDAP library while still single-threaded as recommended in
748 * ldap_initialize(3) */
750 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
752 } /* }}} int ldap_init */
754 void module_register (void) /* {{{ */
756 plugin_register_complex_config ("openldap", ldap_config);
757 plugin_register_init ("openldap", ldap_init);
758 } /* }}} void module_register */