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) /* {{{ */
60 } /* }}} void ldap_free */
61 /* initialize ldap for each host */
62 static int ldap_init_host (ldap_t *st) /* {{{ */
66 rc = ldap_initialize (&ld, st->url);
67 if (rc != LDAP_SUCCESS)
70 sstrerror (errno, errbuf, sizeof (errbuf));
71 ERROR ("ldap_initialize failed: %s", errbuf);
78 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
80 if(st->cacert != NULL)
81 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
83 if(st->verifyhost == 0)
85 int never = LDAP_OPT_X_TLS_NEVER;
86 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
91 rc = ldap_start_tls_s(ld, NULL, NULL);
92 if (rc != LDAP_SUCCESS)
94 ERROR ("openldap plugin: Failed to start tls on %s: %s",
95 st->url, ldap_err2string (rc));
97 ldap_unbind_ext_s(st->ld, NULL, NULL);
106 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
107 if (rc != LDAP_SUCCESS)
109 ERROR ("openldap plugin: Failed to bind to %s: %s",
110 st->url, ldap_err2string (rc));
112 ldap_unbind_ext_s(st->ld, NULL, NULL);
117 DEBUG ("openldap plugin: Successfully connected to %s",
122 } /* }}} static ldap_init_host */
124 static void ldap_submit_value (const char *type, const char *type_instance, /* {{{ */
125 value_t value, ldap_t *st)
127 value_list_t vl = VALUE_LIST_INIT;
132 if ((st->host == NULL)
133 || (strcmp ("", st->host) == 0)
134 || (strcmp ("localhost", st->host) == 0))
136 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
140 sstrncpy (vl.host, st->host, sizeof (vl.host));
143 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
144 if (st->name != NULL)
145 sstrncpy (vl.plugin_instance, st->name,
146 sizeof (vl.plugin_instance));
148 sstrncpy (vl.type, type, sizeof (vl.type));
149 if (type_instance != NULL)
150 sstrncpy (vl.type_instance, type_instance,
151 sizeof (vl.type_instance));
153 plugin_dispatch_values (&vl);
154 } /* }}} void ldap_submit_value */
156 static void ldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
157 derive_t d, ldap_t *st)
161 ldap_submit_value (type, type_instance, v, st);
162 } /* }}} void ldap_submit_derive */
164 static void ldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
165 gauge_t g, ldap_t *st)
169 ldap_submit_value (type, type_instance, v, st);
170 } /* }}} void ldap_submit_gauge */
172 static int ldap_read_host (user_data_t *ud) /* {{{ */
175 LDAPMessage *e, *result;
180 char *attrs[9] = { "monitorCounter",
181 "monitorOpCompleted",
182 "monitorOpInitiated",
190 if ((ud == NULL) || (ud->data == NULL))
192 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
196 st = (ldap_t *) ud->data;
198 status = ldap_init_host (st);
202 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
203 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
204 NULL, NULL, NULL, 0, &result);
206 if (rc != LDAP_SUCCESS)
208 ERROR ("openldap plugin: Failed to execute search: %s",
209 ldap_err2string (rc));
210 ldap_msgfree (result);
214 for (e = ldap_first_entry (st->ld, result); e != NULL;
215 e = ldap_next_entry (st->ld, e))
217 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
219 unsigned long long counter = 0;
220 unsigned long long opc = 0;
221 unsigned long long opi = 0;
222 unsigned long long info = 0;
224 struct berval counter_data;
225 struct berval opc_data;
226 struct berval opi_data;
227 struct berval info_data;
228 struct berval olmbdb_data;
229 struct berval nc_data;
231 struct berval **counter_list;
232 struct berval **opc_list;
233 struct berval **opi_list;
234 struct berval **info_list;
235 struct berval **olmbdb_list;
236 struct berval **nc_list;
238 if ((counter_list = ldap_get_values_len (st->ld, e,
239 "monitorCounter")) != NULL)
241 counter_data = *counter_list[0];
242 counter = atoll (counter_data.bv_val);
245 if ((opc_list = ldap_get_values_len (st->ld, e,
246 "monitorOpCompleted")) != NULL)
248 opc_data = *opc_list[0];
249 opc = atoll (opc_data.bv_val);
252 if ((opi_list = ldap_get_values_len (st->ld, e,
253 "monitorOpInitiated")) != NULL)
255 opi_data = *opi_list[0];
256 opi = atoll (opi_data.bv_val);
259 if ((info_list = ldap_get_values_len (st->ld, e,
260 "monitoredInfo")) != NULL)
262 info_data = *info_list[0];
263 info = atoll (info_data.bv_val);
266 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
269 ldap_submit_derive ("total_connections", NULL,
273 "cn=Current,cn=Connections,cn=Monitor")
276 ldap_submit_gauge ("current_connections", NULL,
280 "cn=Operations,cn=Monitor") == 0)
282 ldap_submit_derive ("operations",
283 "completed", opc, st);
284 ldap_submit_derive ("operations",
285 "initiated", opi, st);
288 "cn=Bind,cn=Operations,cn=Monitor")
291 ldap_submit_derive ("operations",
292 "bind-completed", opc, st);
293 ldap_submit_derive ("operations",
294 "bind-initiated", opi, st);
297 "cn=UnBind,cn=Operations,cn=Monitor")
300 ldap_submit_derive ("operations",
301 "unbind-completed", opc, st);
302 ldap_submit_derive ("operations",
303 "unbind-initiated", opi, st);
306 "cn=Search,cn=Operations,cn=Monitor")
309 ldap_submit_derive ("operations",
310 "search-completed", opc, st);
311 ldap_submit_derive ("operations",
312 "search-initiated", opi, st);
315 "cn=Compare,cn=Operations,cn=Monitor")
318 ldap_submit_derive ("operations",
319 "compare-completed", opc, st);
320 ldap_submit_derive ("operations",
321 "compare-initiated", opi, st);
324 "cn=Modify,cn=Operations,cn=Monitor")
327 ldap_submit_derive ("operations",
328 "modify-completed", opc, st);
329 ldap_submit_derive ("operations",
330 "modify-initiated", opi, st);
333 "cn=Modrdn,cn=Operations,cn=Monitor")
336 ldap_submit_derive ("operations",
337 "modrdn-completed", opc, st);
338 ldap_submit_derive ("operations",
339 "modrdn-initiated", opi, st);
342 "cn=Add,cn=Operations,cn=Monitor")
345 ldap_submit_derive ("operations",
346 "add-completed", opc, st);
347 ldap_submit_derive ("operations",
348 "add-initiated", opi, st);
351 "cn=Delete,cn=Operations,cn=Monitor")
354 ldap_submit_derive ("operations",
355 "delete-completed", opc, st);
356 ldap_submit_derive ("operations",
357 "delete-initiated", opi, st);
360 "cn=Abandon,cn=Operations,cn=Monitor")
363 ldap_submit_derive ("operations",
364 "abandon-completed", opc, st);
365 ldap_submit_derive ("operations",
366 "abandon-initiated", opi, st);
369 "cn=Extended,cn=Operations,cn=Monitor")
372 ldap_submit_derive ("operations",
373 "extended-completed", opc, st);
374 ldap_submit_derive ("operations",
375 "extended-initiated", opi, st);
377 else if ((strncmp (dn, "cn=Database", 11) == 0)
378 && ((nc_list = ldap_get_values_len
379 (st->ld, e, "namingContexts")) != NULL))
381 nc_data = *nc_list[0];
382 char typeinst[DATA_MAX_NAME_LEN];
384 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
385 "olmBDBEntryCache")) != NULL)
387 olmbdb_data = *olmbdb_list[0];
388 ssnprintf (typeinst, sizeof (typeinst),
389 "bdbentrycache-%s", nc_data.bv_val);
390 ldap_submit_gauge ("cache_size", typeinst,
391 atoll (olmbdb_data.bv_val), st);
392 ldap_value_free_len (olmbdb_list);
395 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
396 "olmBDBDNCache")) != NULL)
398 olmbdb_data = *olmbdb_list[0];
399 ssnprintf (typeinst, sizeof (typeinst),
400 "bdbdncache-%s", nc_data.bv_val);
401 ldap_submit_gauge ("cache_size", typeinst,
402 atoll (olmbdb_data.bv_val), st);
403 ldap_value_free_len (olmbdb_list);
406 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
407 "olmBDBIDLCache")) != NULL)
409 olmbdb_data = *olmbdb_list[0];
410 ssnprintf (typeinst, sizeof (typeinst),
411 "bdbidlcache-%s", nc_data.bv_val);
412 ldap_submit_gauge ("cache_size", typeinst,
413 atoll (olmbdb_data.bv_val), st);
414 ldap_value_free_len (olmbdb_list);
417 ldap_value_free_len (nc_list);
420 "cn=Bytes,cn=Statistics,cn=Monitor")
423 ldap_submit_derive ("derive", "statistics-bytes",
427 "cn=PDU,cn=Statistics,cn=Monitor")
430 ldap_submit_derive ("derive", "statistics-pdu",
434 "cn=Entries,cn=Statistics,cn=Monitor")
437 ldap_submit_derive ("derive", "statistics-entries",
441 "cn=Referrals,cn=Statistics,cn=Monitor")
444 ldap_submit_derive ("derive", "statistics-referrals",
448 "cn=Open,cn=Threads,cn=Monitor")
451 ldap_submit_gauge ("threads", "threads-open",
455 "cn=Starting,cn=Threads,cn=Monitor")
458 ldap_submit_gauge ("threads", "threads-starting",
462 "cn=Active,cn=Threads,cn=Monitor")
465 ldap_submit_gauge ("threads", "threads-active",
469 "cn=Pending,cn=Threads,cn=Monitor")
472 ldap_submit_gauge ("threads", "threads-pending",
476 "cn=Backload,cn=Threads,cn=Monitor")
479 ldap_submit_gauge ("threads", "threads-backload",
483 "cn=Read,cn=Waiters,cn=Monitor")
486 ldap_submit_derive ("derive", "waiters-read",
490 "cn=Write,cn=Waiters,cn=Monitor")
493 ldap_submit_derive ("derive", "waiters-write",
497 ldap_value_free_len (counter_list);
498 ldap_value_free_len (opc_list);
499 ldap_value_free_len (opi_list);
500 ldap_value_free_len (info_list);
506 ldap_msgfree (result);
507 ldap_unbind_ext_s (st->ld, NULL, NULL);
509 } /* }}} int ldap_read_host */
511 /* Configuration handling functions {{{
514 * <Instance "plugin_instance1">
515 * URL "ldap://localhost"
521 static int ldap_config_set_string (char **ret_string, /* {{{ */
526 if ((ci->values_num != 1)
527 || (ci->values[0].type != OCONFIG_TYPE_STRING))
529 WARNING ("openldap plugin: The `%s' config option "
530 "needs exactly one string argument.", ci->key);
534 string = strdup (ci->values[0].value.string);
537 ERROR ("openldap plugin: strdup failed.");
541 if (*ret_string != NULL)
543 *ret_string = string;
546 } /* }}} int ldap_config_set_string */
548 static int ldap_config_set_int (int *ret_int, /* {{{ */
551 if ((ci->values_num != 1)
552 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
554 WARNING ("openldap plugin: The `%s' config option "
555 "needs exactly one string argument.", ci->key);
559 *ret_int = ci->values[0].value.number;
562 } /* }}} int ldap_config_set_int */
564 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
569 if (ci->values_num != 1)
574 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
575 *ret_boolean = ci->values[0].value.boolean;
576 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
578 if (IS_TRUE (ci->values[0].value.string))
580 else if (IS_FALSE (ci->values[0].value.string))
591 WARNING ("openldap plugin: The `%s' config option "
592 "needs exactly one boolean argument.", ci->key);
596 } /* }}} int ldap_config_set_bool */
598 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
604 if ((ci->values_num != 1)
605 || (ci->values[0].type != OCONFIG_TYPE_STRING))
607 WARNING ("openldap plugin: The `%s' config option "
608 "needs exactly one string argument.", ci->key);
612 st = (ldap_t *) malloc (sizeof (*st));
615 ERROR ("openldap plugin: malloc failed.");
618 memset (st, 0, sizeof (*st));
620 status = ldap_config_set_string (&st->name, ci);
628 st->version = LDAP_VERSION3;
630 for (i = 0; i < ci->children_num; i++)
632 oconfig_item_t *child = ci->children + i;
634 if (strcasecmp ("CACert", child->key) == 0)
635 status = ldap_config_set_string (&st->cacert, child);
636 else if (strcasecmp ("StartTLS", child->key) == 0)
637 status = ldap_config_set_bool (&st->starttls, child);
638 else if (strcasecmp ("Timeout", child->key) == 0)
639 status = ldap_config_set_int (&st->timeout, child);
640 else if (strcasecmp ("URL", child->key) == 0)
641 status = ldap_config_set_string (&st->url, child);
642 else if (strcasecmp ("VerifyHost", child->key) == 0)
643 status = ldap_config_set_bool (&st->verifyhost, child);
644 else if (strcasecmp ("Version", child->key) == 0)
645 status = ldap_config_set_int (&st->version, child);
648 WARNING ("openldap plugin: Option `%s' not allowed here.",
657 /* Check if struct is complete.. */
658 if ((status == 0) && (st->url == NULL))
660 ERROR ("openldap plugin: Instance `%s': "
661 "No URL has been configured.",
666 /* Check if URL is valid */
667 if ((status == 0) && (st->url != NULL))
672 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
674 ERROR ("openldap plugin: Instance `%s': "
681 st->host = strdup (ludpp->lud_host);
684 ldap_free_urldesc(ludpp);
690 char callback_name[3*DATA_MAX_NAME_LEN];
692 memset (&ud, 0, sizeof (ud));
695 memset (callback_name, 0, sizeof (callback_name));
696 ssnprintf (callback_name, sizeof (callback_name),
698 (st->host != NULL) ? st->host : hostname_g,
699 (st->name != NULL) ? st->name : "default"),
701 status = plugin_register_complex_read (/* group = */ NULL,
702 /* name = */ callback_name,
703 /* callback = */ ldap_read_host,
704 /* interval = */ NULL,
705 /* user_data = */ &ud);
715 } /* }}} int ldap_config_add */
717 static int ldap_config (oconfig_item_t *ci) /* {{{ */
722 for (i = 0; i < ci->children_num; i++)
724 oconfig_item_t *child = ci->children + i;
726 if (strcasecmp ("Instance", child->key) == 0)
727 ldap_config_add (child);
729 WARNING ("openldap plugin: The configuration option "
730 "\"%s\" is not allowed here. Did you "
731 "forget to add an <Instance /> block "
732 "around the configuration?",
734 } /* for (ci->children) */
737 } /* }}} int ldap_config */
739 /* }}} End of configuration handling functions */
741 void module_register (void) /* {{{ */
743 plugin_register_complex_config ("openldap", ldap_config);
744 } /* }}} void module_register */