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));
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_destroy(st->ld);
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[8] = { "monitorCounter",
181 "monitorOpCompleted",
182 "monitorOpInitiated",
189 if ((ud == NULL) || (ud->data == NULL))
191 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
195 st = (ldap_t *) ud->data;
197 status = ldap_init_host (st);
201 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
202 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
203 NULL, NULL, NULL, 0, &result);
205 if (rc != LDAP_SUCCESS)
207 ERROR ("openldap plugin: Failed to execute search: %s",
208 ldap_err2string (rc));
209 ldap_msgfree (result);
213 for (e = ldap_first_entry (st->ld, result); e != NULL;
214 e = ldap_next_entry (st->ld, e))
216 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
218 unsigned long long counter = 0;
219 unsigned long long opc = 0;
220 unsigned long long opi = 0;
221 unsigned long long info = 0;
223 struct berval counter_data;
224 struct berval opc_data;
225 struct berval opi_data;
226 struct berval info_data;
227 struct berval olmbdb_data;
228 struct berval nc_data;
230 struct berval **counter_list;
231 struct berval **opc_list;
232 struct berval **opi_list;
233 struct berval **info_list;
234 struct berval **olmbdb_list;
235 struct berval **nc_list;
237 if ((counter_list = ldap_get_values_len (st->ld, e,
238 "monitorCounter")) != NULL)
240 counter_data = *counter_list[0];
241 counter = atoll (counter_data.bv_val);
244 if ((opc_list = ldap_get_values_len (st->ld, e,
245 "monitorOpCompleted")) != NULL)
247 opc_data = *opc_list[0];
248 opc = atoll (opc_data.bv_val);
251 if ((opi_list = ldap_get_values_len (st->ld, e,
252 "monitorOpInitiated")) != NULL)
254 opi_data = *opi_list[0];
255 opi = atoll (opi_data.bv_val);
258 if ((info_list = ldap_get_values_len (st->ld, e,
259 "monitoredInfo")) != NULL)
261 info_data = *info_list[0];
262 info = atoll (info_data.bv_val);
265 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
268 ldap_submit_derive ("total_connections", NULL,
272 "cn=Current,cn=Connections,cn=Monitor")
275 ldap_submit_gauge ("current_connections", NULL,
279 "cn=Operations,cn=Monitor") == 0)
281 ldap_submit_derive ("operations",
282 "completed", opc, st);
283 ldap_submit_derive ("operations",
284 "initiated", opi, st);
287 "cn=Bind,cn=Operations,cn=Monitor")
290 ldap_submit_derive ("operations",
291 "bind-completed", opc, st);
292 ldap_submit_derive ("operations",
293 "bind-initiated", opi, st);
296 "cn=UnBind,cn=Operations,cn=Monitor")
299 ldap_submit_derive ("operations",
300 "unbind-completed", opc, st);
301 ldap_submit_derive ("operations",
302 "unbind-initiated", opi, st);
305 "cn=Search,cn=Operations,cn=Monitor")
308 ldap_submit_derive ("operations",
309 "search-completed", opc, st);
310 ldap_submit_derive ("operations",
311 "search-initiated", opi, st);
314 "cn=Compare,cn=Operations,cn=Monitor")
317 ldap_submit_derive ("operations",
318 "compare-completed", opc, st);
319 ldap_submit_derive ("operations",
320 "compare-initiated", opi, st);
323 "cn=Modify,cn=Operations,cn=Monitor")
326 ldap_submit_derive ("operations",
327 "modify-completed", opc, st);
328 ldap_submit_derive ("operations",
329 "modify-initiated", opi, st);
332 "cn=Modrdn,cn=Operations,cn=Monitor")
335 ldap_submit_derive ("operations",
336 "modrdn-completed", opc, st);
337 ldap_submit_derive ("operations",
338 "modrdn-initiated", opi, st);
341 "cn=Add,cn=Operations,cn=Monitor")
344 ldap_submit_derive ("operations",
345 "add-completed", opc, st);
346 ldap_submit_derive ("operations",
347 "add-initiated", opi, st);
350 "cn=Delete,cn=Operations,cn=Monitor")
353 ldap_submit_derive ("operations",
354 "delete-completed", opc, st);
355 ldap_submit_derive ("operations",
356 "delete-initiated", opi, st);
359 "cn=Abandon,cn=Operations,cn=Monitor")
362 ldap_submit_derive ("operations",
363 "abandon-completed", opc, st);
364 ldap_submit_derive ("operations",
365 "abandon-initiated", opi, st);
368 "cn=Extended,cn=Operations,cn=Monitor")
371 ldap_submit_derive ("operations",
372 "extended-completed", opc, st);
373 ldap_submit_derive ("operations",
374 "extended-initiated", opi, st);
376 else if ((strncmp (dn, "cn=Database", 11) == 0)
377 && ((nc_list = ldap_get_values_len
378 (st->ld, e, "namingContexts")) != NULL))
380 nc_data = *nc_list[0];
381 char typeinst[DATA_MAX_NAME_LEN];
383 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
384 "olmBDBEntryCache")) != NULL)
386 olmbdb_data = *olmbdb_list[0];
387 ssnprintf (typeinst, sizeof (typeinst),
388 "bdbentrycache-%s", nc_data.bv_val);
389 ldap_submit_gauge ("cache_size", typeinst,
390 atoll (olmbdb_data.bv_val), st);
391 ldap_value_free_len (olmbdb_list);
394 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
395 "olmBDBDNCache")) != NULL)
397 olmbdb_data = *olmbdb_list[0];
398 ssnprintf (typeinst, sizeof (typeinst),
399 "bdbdncache-%s", nc_data.bv_val);
400 ldap_submit_gauge ("cache_size", typeinst,
401 atoll (olmbdb_data.bv_val), st);
402 ldap_value_free_len (olmbdb_list);
405 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
406 "olmBDBIDLCache")) != NULL)
408 olmbdb_data = *olmbdb_list[0];
409 ssnprintf (typeinst, sizeof (typeinst),
410 "bdbidlcache-%s", nc_data.bv_val);
411 ldap_submit_gauge ("cache_size", typeinst,
412 atoll (olmbdb_data.bv_val), st);
413 ldap_value_free_len (olmbdb_list);
416 ldap_value_free_len (nc_list);
419 "cn=Bytes,cn=Statistics,cn=Monitor")
422 ldap_submit_derive ("derive", "statistics-bytes",
426 "cn=PDU,cn=Statistics,cn=Monitor")
429 ldap_submit_derive ("derive", "statistics-pdu",
433 "cn=Entries,cn=Statistics,cn=Monitor")
436 ldap_submit_derive ("derive", "statistics-entries",
440 "cn=Referrals,cn=Statistics,cn=Monitor")
443 ldap_submit_derive ("derive", "statistics-referrals",
447 "cn=Open,cn=Threads,cn=Monitor")
450 ldap_submit_gauge ("threads", "threads-open",
454 "cn=Starting,cn=Threads,cn=Monitor")
457 ldap_submit_gauge ("threads", "threads-starting",
461 "cn=Active,cn=Threads,cn=Monitor")
464 ldap_submit_gauge ("threads", "threads-active",
468 "cn=Pending,cn=Threads,cn=Monitor")
471 ldap_submit_gauge ("threads", "threads-pending",
475 "cn=Backload,cn=Threads,cn=Monitor")
478 ldap_submit_gauge ("threads", "threads-backload",
482 "cn=Read,cn=Waiters,cn=Monitor")
485 ldap_submit_derive ("derive", "waiters-read",
489 "cn=Write,cn=Waiters,cn=Monitor")
492 ldap_submit_derive ("derive", "waiters-write",
496 ldap_value_free_len (counter_list);
497 ldap_value_free_len (opc_list);
498 ldap_value_free_len (opi_list);
499 ldap_value_free_len (info_list);
505 ldap_msgfree (result);
506 ldap_unbind_ext_s (st->ld, NULL, NULL);
508 } /* }}} int ldap_read_host */
510 /* Configuration handling functions {{{
513 * <Instance "plugin_instance1">
514 * URL "ldap://localhost"
520 static int ldap_config_set_string (char **ret_string, /* {{{ */
525 if ((ci->values_num != 1)
526 || (ci->values[0].type != OCONFIG_TYPE_STRING))
528 WARNING ("openldap plugin: The `%s' config option "
529 "needs exactly one string argument.", ci->key);
533 string = strdup (ci->values[0].value.string);
536 ERROR ("openldap plugin: strdup failed.");
540 if (*ret_string != NULL)
542 *ret_string = string;
545 } /* }}} int ldap_config_set_string */
547 static int ldap_config_set_int (int *ret_int, /* {{{ */
550 if ((ci->values_num != 1)
551 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
553 WARNING ("openldap plugin: The `%s' config option "
554 "needs exactly one string argument.", ci->key);
558 *ret_int = ci->values[0].value.number;
561 } /* }}} int ldap_config_set_int */
563 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
568 if (ci->values_num != 1)
573 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
574 *ret_boolean = ci->values[0].value.boolean;
575 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
577 if (IS_TRUE (ci->values[0].value.string))
579 else if (IS_FALSE (ci->values[0].value.string))
590 WARNING ("openldap plugin: The `%s' config option "
591 "needs exactly one boolean argument.", ci->key);
595 } /* }}} int ldap_config_set_bool */
597 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
603 if ((ci->values_num != 1)
604 || (ci->values[0].type != OCONFIG_TYPE_STRING))
606 WARNING ("openldap plugin: The `%s' config option "
607 "needs exactly one string argument.", ci->key);
611 st = (ldap_t *) malloc (sizeof (*st));
614 ERROR ("openldap plugin: malloc failed.");
617 memset (st, 0, sizeof (*st));
619 status = ldap_config_set_string (&st->name, ci);
627 st->version = LDAP_VERSION3;
629 for (i = 0; i < ci->children_num; i++)
631 oconfig_item_t *child = ci->children + i;
633 if (strcasecmp ("CACert", child->key) == 0)
634 status = ldap_config_set_string (&st->cacert, child);
635 else if (strcasecmp ("StartTLS", child->key) == 0)
636 status = ldap_config_set_bool (&st->starttls, child);
637 else if (strcasecmp ("Timeout", child->key) == 0)
638 status = ldap_config_set_int (&st->timeout, child);
639 else if (strcasecmp ("URL", child->key) == 0)
640 status = ldap_config_set_string (&st->url, child);
641 else if (strcasecmp ("VerifyHost", child->key) == 0)
642 status = ldap_config_set_bool (&st->verifyhost, child);
643 else if (strcasecmp ("Version", child->key) == 0)
644 status = ldap_config_set_int (&st->version, child);
647 WARNING ("openldap plugin: Option `%s' not allowed here.",
656 /* Check if struct is complete.. */
657 if ((status == 0) && (st->url == NULL))
659 ERROR ("openldap plugin: Instance `%s': "
660 "No URL has been configured.",
665 /* Check if URL is valid */
666 if ((status == 0) && (st->url != NULL))
671 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
673 ERROR ("openldap plugin: Instance `%s': "
680 st->host = strdup (ludpp->lud_host);
683 ldap_free_urldesc(ludpp);
689 char callback_name[3*DATA_MAX_NAME_LEN];
691 memset (&ud, 0, sizeof (ud));
694 memset (callback_name, 0, sizeof (callback_name));
695 ssnprintf (callback_name, sizeof (callback_name),
697 (st->host != NULL) ? st->host : hostname_g,
698 (st->name != NULL) ? st->name : "default"),
700 status = plugin_register_complex_read (/* group = */ NULL,
701 /* name = */ callback_name,
702 /* callback = */ ldap_read_host,
703 /* interval = */ NULL,
704 /* user_data = */ &ud);
714 } /* }}} int ldap_config_add */
716 static int ldap_config (oconfig_item_t *ci) /* {{{ */
721 for (i = 0; i < ci->children_num; i++)
723 oconfig_item_t *child = ci->children + i;
725 if (strcasecmp ("Instance", child->key) == 0)
726 ldap_config_add (child);
728 WARNING ("openldap plugin: The configuration option "
729 "\"%s\" is not allowed here. Did you "
730 "forget to add an <Instance /> block "
731 "around the configuration?",
733 } /* for (ci->children) */
736 } /* }}} int ldap_config */
738 /* }}} End of configuration handling functions */
740 void module_register (void) /* {{{ */
742 plugin_register_complex_config ("openldap", ldap_config);
743 } /* }}} void module_register */