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)
69 ERROR ("openldap plugin: ldap_initialize failed: %s",
70 ldap_err2string (rc));
77 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
79 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
80 &(const struct timeval){st->timeout, 0});
82 if(st->cacert != NULL)
83 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
85 if(st->verifyhost == 0)
87 int never = LDAP_OPT_X_TLS_NEVER;
88 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
93 rc = ldap_start_tls_s(ld, NULL, NULL);
94 if (rc != LDAP_SUCCESS)
96 ERROR ("openldap plugin: Failed to start tls on %s: %s",
97 st->url, ldap_err2string (rc));
99 ldap_unbind_ext_s(st->ld, NULL, NULL);
108 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
109 if (rc != LDAP_SUCCESS)
111 ERROR ("openldap plugin: Failed to bind to %s: %s",
112 st->url, ldap_err2string (rc));
114 ldap_unbind_ext_s(st->ld, NULL, NULL);
119 DEBUG ("openldap plugin: Successfully connected to %s",
124 } /* }}} static ldap_init_host */
126 static void ldap_submit_value (const char *type, const char *type_instance, /* {{{ */
127 value_t value, ldap_t *st)
129 value_list_t vl = VALUE_LIST_INIT;
134 if ((st->host == NULL)
135 || (strcmp ("", st->host) == 0)
136 || (strcmp ("localhost", st->host) == 0))
138 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
142 sstrncpy (vl.host, st->host, sizeof (vl.host));
145 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
146 if (st->name != NULL)
147 sstrncpy (vl.plugin_instance, st->name,
148 sizeof (vl.plugin_instance));
150 sstrncpy (vl.type, type, sizeof (vl.type));
151 if (type_instance != NULL)
152 sstrncpy (vl.type_instance, type_instance,
153 sizeof (vl.type_instance));
155 plugin_dispatch_values (&vl);
156 } /* }}} void ldap_submit_value */
158 static void ldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
159 derive_t d, ldap_t *st)
163 ldap_submit_value (type, type_instance, v, st);
164 } /* }}} void ldap_submit_derive */
166 static void ldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
167 gauge_t g, ldap_t *st)
171 ldap_submit_value (type, type_instance, v, st);
172 } /* }}} void ldap_submit_gauge */
174 static int ldap_read_host (user_data_t *ud) /* {{{ */
177 LDAPMessage *e, *result;
182 char *attrs[9] = { "monitorCounter",
183 "monitorOpCompleted",
184 "monitorOpInitiated",
192 if ((ud == NULL) || (ud->data == NULL))
194 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
198 st = (ldap_t *) ud->data;
200 status = ldap_init_host (st);
204 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
205 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
206 NULL, NULL, NULL, 0, &result);
208 if (rc != LDAP_SUCCESS)
210 ERROR ("openldap plugin: Failed to execute search: %s",
211 ldap_err2string (rc));
212 ldap_msgfree (result);
216 for (e = ldap_first_entry (st->ld, result); e != NULL;
217 e = ldap_next_entry (st->ld, e))
219 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
221 unsigned long long counter = 0;
222 unsigned long long opc = 0;
223 unsigned long long opi = 0;
224 unsigned long long info = 0;
226 struct berval counter_data;
227 struct berval opc_data;
228 struct berval opi_data;
229 struct berval info_data;
230 struct berval olmbdb_data;
231 struct berval nc_data;
233 struct berval **counter_list;
234 struct berval **opc_list;
235 struct berval **opi_list;
236 struct berval **info_list;
237 struct berval **olmbdb_list;
238 struct berval **nc_list;
240 if ((counter_list = ldap_get_values_len (st->ld, e,
241 "monitorCounter")) != NULL)
243 counter_data = *counter_list[0];
244 counter = atoll (counter_data.bv_val);
247 if ((opc_list = ldap_get_values_len (st->ld, e,
248 "monitorOpCompleted")) != NULL)
250 opc_data = *opc_list[0];
251 opc = atoll (opc_data.bv_val);
254 if ((opi_list = ldap_get_values_len (st->ld, e,
255 "monitorOpInitiated")) != NULL)
257 opi_data = *opi_list[0];
258 opi = atoll (opi_data.bv_val);
261 if ((info_list = ldap_get_values_len (st->ld, e,
262 "monitoredInfo")) != NULL)
264 info_data = *info_list[0];
265 info = atoll (info_data.bv_val);
268 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
271 ldap_submit_derive ("total_connections", NULL,
275 "cn=Current,cn=Connections,cn=Monitor")
278 ldap_submit_gauge ("current_connections", NULL,
282 "cn=Operations,cn=Monitor") == 0)
284 ldap_submit_derive ("operations",
285 "completed", opc, st);
286 ldap_submit_derive ("operations",
287 "initiated", opi, st);
290 "cn=Bind,cn=Operations,cn=Monitor")
293 ldap_submit_derive ("operations",
294 "bind-completed", opc, st);
295 ldap_submit_derive ("operations",
296 "bind-initiated", opi, st);
299 "cn=UnBind,cn=Operations,cn=Monitor")
302 ldap_submit_derive ("operations",
303 "unbind-completed", opc, st);
304 ldap_submit_derive ("operations",
305 "unbind-initiated", opi, st);
308 "cn=Search,cn=Operations,cn=Monitor")
311 ldap_submit_derive ("operations",
312 "search-completed", opc, st);
313 ldap_submit_derive ("operations",
314 "search-initiated", opi, st);
317 "cn=Compare,cn=Operations,cn=Monitor")
320 ldap_submit_derive ("operations",
321 "compare-completed", opc, st);
322 ldap_submit_derive ("operations",
323 "compare-initiated", opi, st);
326 "cn=Modify,cn=Operations,cn=Monitor")
329 ldap_submit_derive ("operations",
330 "modify-completed", opc, st);
331 ldap_submit_derive ("operations",
332 "modify-initiated", opi, st);
335 "cn=Modrdn,cn=Operations,cn=Monitor")
338 ldap_submit_derive ("operations",
339 "modrdn-completed", opc, st);
340 ldap_submit_derive ("operations",
341 "modrdn-initiated", opi, st);
344 "cn=Add,cn=Operations,cn=Monitor")
347 ldap_submit_derive ("operations",
348 "add-completed", opc, st);
349 ldap_submit_derive ("operations",
350 "add-initiated", opi, st);
353 "cn=Delete,cn=Operations,cn=Monitor")
356 ldap_submit_derive ("operations",
357 "delete-completed", opc, st);
358 ldap_submit_derive ("operations",
359 "delete-initiated", opi, st);
362 "cn=Abandon,cn=Operations,cn=Monitor")
365 ldap_submit_derive ("operations",
366 "abandon-completed", opc, st);
367 ldap_submit_derive ("operations",
368 "abandon-initiated", opi, st);
371 "cn=Extended,cn=Operations,cn=Monitor")
374 ldap_submit_derive ("operations",
375 "extended-completed", opc, st);
376 ldap_submit_derive ("operations",
377 "extended-initiated", opi, st);
379 else if ((strncmp (dn, "cn=Database", 11) == 0)
380 && ((nc_list = ldap_get_values_len
381 (st->ld, e, "namingContexts")) != NULL))
383 nc_data = *nc_list[0];
384 char typeinst[DATA_MAX_NAME_LEN];
386 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
387 "olmBDBEntryCache")) != NULL)
389 olmbdb_data = *olmbdb_list[0];
390 ssnprintf (typeinst, sizeof (typeinst),
391 "bdbentrycache-%s", nc_data.bv_val);
392 ldap_submit_gauge ("cache_size", typeinst,
393 atoll (olmbdb_data.bv_val), st);
394 ldap_value_free_len (olmbdb_list);
397 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
398 "olmBDBDNCache")) != NULL)
400 olmbdb_data = *olmbdb_list[0];
401 ssnprintf (typeinst, sizeof (typeinst),
402 "bdbdncache-%s", nc_data.bv_val);
403 ldap_submit_gauge ("cache_size", typeinst,
404 atoll (olmbdb_data.bv_val), st);
405 ldap_value_free_len (olmbdb_list);
408 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
409 "olmBDBIDLCache")) != NULL)
411 olmbdb_data = *olmbdb_list[0];
412 ssnprintf (typeinst, sizeof (typeinst),
413 "bdbidlcache-%s", nc_data.bv_val);
414 ldap_submit_gauge ("cache_size", typeinst,
415 atoll (olmbdb_data.bv_val), st);
416 ldap_value_free_len (olmbdb_list);
419 ldap_value_free_len (nc_list);
422 "cn=Bytes,cn=Statistics,cn=Monitor")
425 ldap_submit_derive ("derive", "statistics-bytes",
429 "cn=PDU,cn=Statistics,cn=Monitor")
432 ldap_submit_derive ("derive", "statistics-pdu",
436 "cn=Entries,cn=Statistics,cn=Monitor")
439 ldap_submit_derive ("derive", "statistics-entries",
443 "cn=Referrals,cn=Statistics,cn=Monitor")
446 ldap_submit_derive ("derive", "statistics-referrals",
450 "cn=Open,cn=Threads,cn=Monitor")
453 ldap_submit_gauge ("threads", "threads-open",
457 "cn=Starting,cn=Threads,cn=Monitor")
460 ldap_submit_gauge ("threads", "threads-starting",
464 "cn=Active,cn=Threads,cn=Monitor")
467 ldap_submit_gauge ("threads", "threads-active",
471 "cn=Pending,cn=Threads,cn=Monitor")
474 ldap_submit_gauge ("threads", "threads-pending",
478 "cn=Backload,cn=Threads,cn=Monitor")
481 ldap_submit_gauge ("threads", "threads-backload",
485 "cn=Read,cn=Waiters,cn=Monitor")
488 ldap_submit_derive ("derive", "waiters-read",
492 "cn=Write,cn=Waiters,cn=Monitor")
495 ldap_submit_derive ("derive", "waiters-write",
499 ldap_value_free_len (counter_list);
500 ldap_value_free_len (opc_list);
501 ldap_value_free_len (opi_list);
502 ldap_value_free_len (info_list);
508 ldap_msgfree (result);
509 ldap_unbind_ext_s (st->ld, NULL, NULL);
511 } /* }}} int ldap_read_host */
513 /* Configuration handling functions {{{
516 * <Instance "plugin_instance1">
517 * URL "ldap://localhost"
523 static int ldap_config_set_string (char **ret_string, /* {{{ */
528 if ((ci->values_num != 1)
529 || (ci->values[0].type != OCONFIG_TYPE_STRING))
531 WARNING ("openldap plugin: The `%s' config option "
532 "needs exactly one string argument.", ci->key);
536 string = strdup (ci->values[0].value.string);
539 ERROR ("openldap plugin: strdup failed.");
543 if (*ret_string != NULL)
545 *ret_string = string;
548 } /* }}} int ldap_config_set_string */
550 static int ldap_config_set_int (int *ret_int, /* {{{ */
553 if ((ci->values_num != 1)
554 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
556 WARNING ("openldap plugin: The `%s' config option "
557 "needs exactly one string argument.", ci->key);
561 *ret_int = ci->values[0].value.number;
564 } /* }}} int ldap_config_set_int */
566 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
571 if (ci->values_num != 1)
576 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
577 *ret_boolean = ci->values[0].value.boolean;
578 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
580 if (IS_TRUE (ci->values[0].value.string))
582 else if (IS_FALSE (ci->values[0].value.string))
593 WARNING ("openldap plugin: The `%s' config option "
594 "needs exactly one boolean argument.", ci->key);
598 } /* }}} int ldap_config_set_bool */
600 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
606 if ((ci->values_num != 1)
607 || (ci->values[0].type != OCONFIG_TYPE_STRING))
609 WARNING ("openldap plugin: The `%s' config option "
610 "needs exactly one string argument.", ci->key);
614 st = (ldap_t *) malloc (sizeof (*st));
617 ERROR ("openldap plugin: malloc failed.");
620 memset (st, 0, sizeof (*st));
622 status = ldap_config_set_string (&st->name, ci);
631 st->version = LDAP_VERSION3;
633 for (i = 0; i < ci->children_num; i++)
635 oconfig_item_t *child = ci->children + i;
637 if (strcasecmp ("CACert", child->key) == 0)
638 status = ldap_config_set_string (&st->cacert, child);
639 else if (strcasecmp ("StartTLS", child->key) == 0)
640 status = ldap_config_set_bool (&st->starttls, child);
641 else if (strcasecmp ("Timeout", child->key) == 0)
642 status = ldap_config_set_int (&st->timeout, child);
643 else if (strcasecmp ("URL", child->key) == 0)
644 status = ldap_config_set_string (&st->url, child);
645 else if (strcasecmp ("VerifyHost", child->key) == 0)
646 status = ldap_config_set_bool (&st->verifyhost, child);
647 else if (strcasecmp ("Version", child->key) == 0)
648 status = ldap_config_set_int (&st->version, child);
651 WARNING ("openldap plugin: Option `%s' not allowed here.",
660 /* Check if struct is complete.. */
661 if ((status == 0) && (st->url == NULL))
663 ERROR ("openldap plugin: Instance `%s': "
664 "No URL has been configured.",
669 /* Check if URL is valid */
670 if ((status == 0) && (st->url != NULL))
675 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
677 ERROR ("openldap plugin: Instance `%s': "
684 st->host = strdup (ludpp->lud_host);
687 ldap_free_urldesc(ludpp);
693 char callback_name[3*DATA_MAX_NAME_LEN];
695 memset (&ud, 0, sizeof (ud));
698 memset (callback_name, 0, sizeof (callback_name));
699 ssnprintf (callback_name, sizeof (callback_name),
701 (st->host != NULL) ? st->host : hostname_g,
702 (st->name != NULL) ? st->name : "default"),
704 status = plugin_register_complex_read (/* group = */ NULL,
705 /* name = */ callback_name,
706 /* callback = */ ldap_read_host,
707 /* interval = */ NULL,
708 /* user_data = */ &ud);
718 } /* }}} int ldap_config_add */
720 static int ldap_config (oconfig_item_t *ci) /* {{{ */
725 for (i = 0; i < ci->children_num; i++)
727 oconfig_item_t *child = ci->children + i;
729 if (strcasecmp ("Instance", child->key) == 0)
730 ldap_config_add (child);
732 WARNING ("openldap plugin: The configuration option "
733 "\"%s\" is not allowed here. Did you "
734 "forget to add an <Instance /> block "
735 "around the configuration?",
737 } /* for (ci->children) */
740 } /* }}} int ldap_config */
742 /* }}} End of configuration handling functions */
744 static int ldap_init (void) /* {{{ */
746 /* Initialize LDAP library while still single-threaded as recommended in
747 * ldap_initialize(3) */
749 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 */