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 int ldap_read_host (user_data_t *ud);
50 static void ldap_free (ldap_t *st) /* {{{ */
62 } /* }}} void ldap_free */
64 /* Configuration handling functions {{{
67 * <Instance "plugin_instance1">
68 * URL "ldap://localhost"
74 static int ldap_config_set_string (char **ret_string, /* {{{ */
79 if ((ci->values_num != 1)
80 || (ci->values[0].type != OCONFIG_TYPE_STRING))
82 WARNING ("openldap plugin: The `%s' config option "
83 "needs exactly one string argument.", ci->key);
87 string = strdup (ci->values[0].value.string);
90 ERROR ("openldap plugin: strdup failed.");
94 if (*ret_string != NULL)
99 } /* }}} int ldap_config_set_string */
101 static int ldap_config_set_int (int *ret_int, /* {{{ */
104 if ((ci->values_num != 1)
105 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
107 WARNING ("openldap plugin: The `%s' config option "
108 "needs exactly one string argument.", ci->key);
112 *ret_int = ci->values[0].value.number;
115 } /* }}} int ldap_config_set_int */
117 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
122 if (ci->values_num != 1)
127 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
128 *ret_boolean = ci->values[0].value.boolean;
129 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
131 if (IS_TRUE (ci->values[0].value.string))
133 else if (IS_FALSE (ci->values[0].value.string))
144 WARNING ("openldap plugin: The `%s' config option "
145 "needs exactly one boolean argument.", ci->key);
149 } /* }}} ldap_config_set_bool */
151 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
157 if ((ci->values_num != 1)
158 || (ci->values[0].type != OCONFIG_TYPE_STRING))
160 WARNING ("openldap plugin: The `%s' config option "
161 "needs exactly one string argument.", ci->key);
165 st = (ldap_t *) malloc (sizeof (*st));
168 ERROR ("openldap plugin: malloc failed.");
171 memset (st, 0, sizeof (*st));
173 status = ldap_config_set_string (&st->name, ci);
181 st->version = LDAP_VERSION3;
183 for (i = 0; i < ci->children_num; i++)
185 oconfig_item_t *child = ci->children + i;
187 if (strcasecmp ("CACert", child->key) == 0)
188 status = ldap_config_set_string (&st->cacert, child);
189 else if (strcasecmp ("StartTLS", child->key) == 0)
190 status = ldap_config_set_bool (&st->starttls, child);
191 else if (strcasecmp ("Timeout", child->key) == 0)
192 status = ldap_config_set_int (&st->timeout, child);
193 else if (strcasecmp ("URL", child->key) == 0)
194 status = ldap_config_set_string (&st->url, child);
195 else if (strcasecmp ("VerifyHost", child->key) == 0)
196 status = ldap_config_set_bool (&st->verifyhost, child);
197 else if (strcasecmp ("Version", child->key) == 0)
198 status = ldap_config_set_int (&st->version, child);
201 WARNING ("openldap plugin: Option `%s' not allowed here.",
210 /* Check if struct is complete.. */
211 if ((status == 0) && (st->url == NULL))
213 ERROR ("openldap plugin: Instance `%s': "
214 "No URL has been configured.",
219 /* Check if URL is valid */
220 if ((status == 0) && (st->url != NULL))
225 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
227 ERROR ("openldap plugin: Instance `%s': "
234 st->host = strdup (ludpp->lud_host);
237 ldap_free_urldesc(ludpp);
243 char callback_name[3*DATA_MAX_NAME_LEN];
245 memset (&ud, 0, sizeof (ud));
248 memset (callback_name, 0, sizeof (callback_name));
249 ssnprintf (callback_name, sizeof (callback_name),
251 (st->host != NULL) ? st->host : hostname_g,
252 (st->name != NULL) ? st->name : "default"),
254 status = plugin_register_complex_read (/* group = */ NULL,
255 /* name = */ callback_name,
256 /* callback = */ ldap_read_host,
257 /* interval = */ NULL,
258 /* user_data = */ &ud);
268 } /* }}} int ldap_config_add */
270 static int ldap_config (oconfig_item_t *ci)
275 for (i = 0; i < ci->children_num; i++)
277 oconfig_item_t *child = ci->children + i;
279 if (strcasecmp ("Instance", child->key) == 0)
280 ldap_config_add (child);
282 WARNING ("openldap plugin: The configuration option "
283 "\"%s\" is not allowed here. Did you "
284 "forget to add an <Instance /> block "
285 "around the configuration?",
287 } /* for (ci->children) */
292 /* }}} End of configuration handling functions */
294 /* initialize ldap for each host */
295 static int ldap_init_host (ldap_t *st)
299 rc = ldap_initialize (&ld, st->url);
300 if (rc != LDAP_SUCCESS)
303 sstrerror (errno, errbuf, sizeof (errbuf));
304 ERROR ("ldap_initialize failed: %s", errbuf);
311 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
313 if(st->cacert != NULL)
314 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
316 if(st->verifyhost == 0)
318 int never = LDAP_OPT_X_TLS_NEVER;
319 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
322 if(st->starttls != 0)
324 rc = ldap_start_tls_s(ld, NULL, NULL);
325 if (rc != LDAP_SUCCESS)
327 ERROR ("openldap plugin: Failed to start tls on %s: %s",
328 st->url, ldap_err2string (rc));
330 ldap_destroy(st->ld);
339 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
340 if (rc != LDAP_SUCCESS)
342 ERROR ("openldap plugin: Failed to bind to %s: %s",
343 st->url, ldap_err2string (rc));
345 ldap_destroy(st->ld);
350 DEBUG ("openldap plugin: Successfully connected to %s",
355 } /* static init_host (ldap_t *st) */
357 static void ldap_submit_value (const char *type, const char *type_instance,
358 value_t value, ldap_t *st)
360 value_list_t vl = VALUE_LIST_INIT;
365 if ((st->host == NULL)
366 || (strcmp ("", st->host) == 0)
367 || (strcmp ("localhost", st->host) == 0))
369 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
373 sstrncpy (vl.host, st->host, sizeof (vl.host));
376 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
377 if (st->name != NULL)
378 sstrncpy (vl.plugin_instance, st->name,
379 sizeof (vl.plugin_instance));
381 sstrncpy (vl.type, type, sizeof (vl.type));
382 if (type_instance != NULL)
383 sstrncpy (vl.type_instance, type_instance,
384 sizeof (vl.type_instance));
386 plugin_dispatch_values (&vl);
387 } /* ldap_submit_value */
389 static void ldap_submit_derive (const char *type, const char *type_instance,
390 derive_t d, ldap_t *st)
394 ldap_submit_value (type, type_instance, v, st);
395 } /* void ldap_submit_derive */
397 static void ldap_submit_gauge (const char *type, const char *type_instance,
398 gauge_t g, ldap_t *st)
402 ldap_submit_value (type, type_instance, v, st);
403 } /* void ldap_submit_gauge */
405 static int ldap_read_host (user_data_t *ud)
408 LDAPMessage *e, *result;
413 char *attrs[8] = { "monitorCounter",
414 "monitorOpCompleted",
415 "monitorOpInitiated",
422 if ((ud == NULL) || (ud->data == NULL))
424 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
428 st = (ldap_t *) ud->data;
430 status = ldap_init_host (st);
434 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
435 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
436 NULL, NULL, NULL, 0, &result);
438 if (rc != LDAP_SUCCESS)
440 ERROR ("openldap plugin: Failed to execute search: %s",
441 ldap_err2string (rc));
442 ldap_msgfree (result);
446 for (e = ldap_first_entry (st->ld, result); e != NULL;
447 e = ldap_next_entry (st->ld, e))
449 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
451 unsigned long long counter = 0;
452 unsigned long long opc = 0;
453 unsigned long long opi = 0;
455 struct berval counter_data;
456 struct berval opc_data;
457 struct berval opi_data;
458 struct berval info_data;
459 struct berval olmbdb_data;
460 struct berval nc_data;
462 struct berval **counter_list;
463 struct berval **opc_list;
464 struct berval **opi_list;
465 struct berval **info_list;
466 struct berval **olmbdb_list;
467 struct berval **nc_list;
469 if ((counter_list = ldap_get_values_len (st->ld, e,
470 "monitorCounter")) != NULL)
472 counter_data = *counter_list[0];
473 counter = atoll (counter_data.bv_val);
476 if ((opc_list = ldap_get_values_len (st->ld, e,
477 "monitorOpCompleted")) != NULL)
479 opc_data = *opc_list[0];
480 opc = atoll (opc_data.bv_val);
483 if ((opi_list = ldap_get_values_len (st->ld, e,
484 "monitorOpInitiated")) != NULL)
486 opi_data = *opi_list[0];
487 opi = atoll (opi_data.bv_val);
490 if ((info_list = ldap_get_values_len (st->ld, e,
491 "monitoredInfo")) != NULL)
493 info_data = *info_list[0];
494 // don't convert search result to long long at this point,
495 // because this field is often populated with non-numerical data.
498 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
501 ldap_submit_derive ("total_connections", NULL,
505 "cn=Current,cn=Connections,cn=Monitor")
508 ldap_submit_gauge ("current_connections", NULL,
512 "cn=Operations,cn=Monitor") == 0)
514 ldap_submit_derive ("operations",
515 "completed", opc, st);
516 ldap_submit_derive ("operations",
517 "initiated", opi, st);
520 "cn=Bind,cn=Operations,cn=Monitor")
523 ldap_submit_derive ("operations",
524 "bind-completed", opc, st);
525 ldap_submit_derive ("operations",
526 "bind-initiated", opi, st);
529 "cn=UnBind,cn=Operations,cn=Monitor")
532 ldap_submit_derive ("operations",
533 "unbind-completed", opc, st);
534 ldap_submit_derive ("operations",
535 "unbind-initiated", opi, st);
538 "cn=Search,cn=Operations,cn=Monitor")
541 ldap_submit_derive ("operations",
542 "search-completed", opc, st);
543 ldap_submit_derive ("operations",
544 "search-initiated", opi, st);
547 "cn=Compare,cn=Operations,cn=Monitor")
550 ldap_submit_derive ("operations",
551 "compare-completed", opc, st);
552 ldap_submit_derive ("operations",
553 "compare-initiated", opi, st);
556 "cn=Modify,cn=Operations,cn=Monitor")
559 ldap_submit_derive ("operations",
560 "modify-completed", opc, st);
561 ldap_submit_derive ("operations",
562 "modify-initiated", opi, st);
565 "cn=Modrdn,cn=Operations,cn=Monitor")
568 ldap_submit_derive ("operations",
569 "modrdn-completed", opc, st);
570 ldap_submit_derive ("operations",
571 "modrdn-initiated", opi, st);
574 "cn=Add,cn=Operations,cn=Monitor")
577 ldap_submit_derive ("operations",
578 "add-completed", opc, st);
579 ldap_submit_derive ("operations",
580 "add-initiated", opi, st);
583 "cn=Delete,cn=Operations,cn=Monitor")
586 ldap_submit_derive ("operations",
587 "delete-completed", opc, st);
588 ldap_submit_derive ("operations",
589 "delete-initiated", opi, st);
592 "cn=Abandon,cn=Operations,cn=Monitor")
595 ldap_submit_derive ("operations",
596 "abandon-completed", opc, st);
597 ldap_submit_derive ("operations",
598 "abandon-initiated", opi, st);
601 "cn=Extended,cn=Operations,cn=Monitor")
604 ldap_submit_derive ("operations",
605 "extended-completed", opc, st);
606 ldap_submit_derive ("operations",
607 "extended-initiated", opi, st);
609 else if ((strncmp (dn, "cn=Database", 11) == 0)
610 && ((nc_list = ldap_get_values_len
611 (st->ld, e, "namingContexts")) != NULL))
613 nc_data = *nc_list[0];
614 char typeinst[DATA_MAX_NAME_LEN];
616 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
617 "olmBDBEntryCache")) != NULL)
619 olmbdb_data = *olmbdb_list[0];
620 ssnprintf (typeinst, sizeof (typeinst),
621 "bdbentrycache-%s", nc_data.bv_val);
622 ldap_submit_gauge ("cache_size", typeinst,
623 atoll (olmbdb_data.bv_val), st);
624 ldap_value_free_len (olmbdb_list);
627 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
628 "olmBDBDNCache")) != NULL)
630 olmbdb_data = *olmbdb_list[0];
631 ssnprintf (typeinst, sizeof (typeinst),
632 "bdbdncache-%s", nc_data.bv_val);
633 ldap_submit_gauge ("cache_size", typeinst,
634 atoll (olmbdb_data.bv_val), st);
635 ldap_value_free_len (olmbdb_list);
638 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
639 "olmBDBIDLCache")) != NULL)
641 olmbdb_data = *olmbdb_list[0];
642 ssnprintf (typeinst, sizeof (typeinst),
643 "bdbidlcache-%s", nc_data.bv_val);
644 ldap_submit_gauge ("cache_size", typeinst,
645 atoll (olmbdb_data.bv_val), st);
646 ldap_value_free_len (olmbdb_list);
649 ldap_value_free_len (nc_list);
652 "cn=Bytes,cn=Statistics,cn=Monitor")
655 ldap_submit_derive ("derive", "statistics-bytes",
659 "cn=PDU,cn=Statistics,cn=Monitor")
662 ldap_submit_derive ("derive", "statistics-pdu",
666 "cn=Entries,cn=Statistics,cn=Monitor")
669 ldap_submit_derive ("derive", "statistics-entries",
673 "cn=Referrals,cn=Statistics,cn=Monitor")
676 ldap_submit_derive ("derive", "statistics-referrals",
680 "cn=Open,cn=Threads,cn=Monitor")
683 ldap_submit_gauge ("threads", "threads-open",
684 atoll (info_data.bv_val), st);
687 "cn=Starting,cn=Threads,cn=Monitor")
690 ldap_submit_gauge ("threads", "threads-starting",
691 atoll (info_data.bv_val), st);
694 "cn=Active,cn=Threads,cn=Monitor")
697 ldap_submit_gauge ("threads", "threads-active",
698 atoll (info_data.bv_val), st);
701 "cn=Pending,cn=Threads,cn=Monitor")
704 ldap_submit_gauge ("threads", "threads-pending",
705 atoll (info_data.bv_val), st);
708 "cn=Backload,cn=Threads,cn=Monitor")
711 ldap_submit_gauge ("threads", "threads-backload",
712 atoll (info_data.bv_val), st);
715 "cn=Read,cn=Waiters,cn=Monitor")
718 ldap_submit_derive ("derive", "waiters-read",
722 "cn=Write,cn=Waiters,cn=Monitor")
725 ldap_submit_derive ("derive", "waiters-write",
729 ldap_value_free_len (counter_list);
730 ldap_value_free_len (opc_list);
731 ldap_value_free_len (opi_list);
732 ldap_value_free_len (info_list);
738 ldap_msgfree (result);
739 ldap_unbind_ext_s (st->ld, NULL, NULL);
741 } /* int ldap_read_host */
743 void module_register (void)
745 plugin_register_complex_config ("openldap", ldap_config);
746 } /* void module_register */