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 /* {{{ */
45 typedef struct ldap_s ldap_t; /* }}} */
47 static void ldap_free (ldap_t *st) /* {{{ */
57 ldap_memfree (st->ld);
59 } /* }}} 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));
72 ldap_unbind_ext_s (ld, NULL, NULL);
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);
214 ldap_unbind_ext_s (st->ld, NULL, NULL);
218 for (e = ldap_first_entry (st->ld, result); e != NULL;
219 e = ldap_next_entry (st->ld, e))
221 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
223 unsigned long long counter = 0;
224 unsigned long long opc = 0;
225 unsigned long long opi = 0;
226 unsigned long long info = 0;
228 struct berval counter_data;
229 struct berval opc_data;
230 struct berval opi_data;
231 struct berval info_data;
232 struct berval olmbdb_data;
233 struct berval nc_data;
235 struct berval **counter_list;
236 struct berval **opc_list;
237 struct berval **opi_list;
238 struct berval **info_list;
239 struct berval **olmbdb_list;
240 struct berval **nc_list;
242 if ((counter_list = ldap_get_values_len (st->ld, e,
243 "monitorCounter")) != NULL)
245 counter_data = *counter_list[0];
246 counter = atoll (counter_data.bv_val);
249 if ((opc_list = ldap_get_values_len (st->ld, e,
250 "monitorOpCompleted")) != NULL)
252 opc_data = *opc_list[0];
253 opc = atoll (opc_data.bv_val);
256 if ((opi_list = ldap_get_values_len (st->ld, e,
257 "monitorOpInitiated")) != NULL)
259 opi_data = *opi_list[0];
260 opi = atoll (opi_data.bv_val);
263 if ((info_list = ldap_get_values_len (st->ld, e,
264 "monitoredInfo")) != NULL)
266 info_data = *info_list[0];
267 info = atoll (info_data.bv_val);
270 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
273 ldap_submit_derive ("total_connections", NULL,
277 "cn=Current,cn=Connections,cn=Monitor")
280 ldap_submit_gauge ("current_connections", NULL,
284 "cn=Operations,cn=Monitor") == 0)
286 ldap_submit_derive ("operations",
287 "completed", opc, st);
288 ldap_submit_derive ("operations",
289 "initiated", opi, st);
292 "cn=Bind,cn=Operations,cn=Monitor")
295 ldap_submit_derive ("operations",
296 "bind-completed", opc, st);
297 ldap_submit_derive ("operations",
298 "bind-initiated", opi, st);
301 "cn=UnBind,cn=Operations,cn=Monitor")
304 ldap_submit_derive ("operations",
305 "unbind-completed", opc, st);
306 ldap_submit_derive ("operations",
307 "unbind-initiated", opi, st);
310 "cn=Search,cn=Operations,cn=Monitor")
313 ldap_submit_derive ("operations",
314 "search-completed", opc, st);
315 ldap_submit_derive ("operations",
316 "search-initiated", opi, st);
319 "cn=Compare,cn=Operations,cn=Monitor")
322 ldap_submit_derive ("operations",
323 "compare-completed", opc, st);
324 ldap_submit_derive ("operations",
325 "compare-initiated", opi, st);
328 "cn=Modify,cn=Operations,cn=Monitor")
331 ldap_submit_derive ("operations",
332 "modify-completed", opc, st);
333 ldap_submit_derive ("operations",
334 "modify-initiated", opi, st);
337 "cn=Modrdn,cn=Operations,cn=Monitor")
340 ldap_submit_derive ("operations",
341 "modrdn-completed", opc, st);
342 ldap_submit_derive ("operations",
343 "modrdn-initiated", opi, st);
346 "cn=Add,cn=Operations,cn=Monitor")
349 ldap_submit_derive ("operations",
350 "add-completed", opc, st);
351 ldap_submit_derive ("operations",
352 "add-initiated", opi, st);
355 "cn=Delete,cn=Operations,cn=Monitor")
358 ldap_submit_derive ("operations",
359 "delete-completed", opc, st);
360 ldap_submit_derive ("operations",
361 "delete-initiated", opi, st);
364 "cn=Abandon,cn=Operations,cn=Monitor")
367 ldap_submit_derive ("operations",
368 "abandon-completed", opc, st);
369 ldap_submit_derive ("operations",
370 "abandon-initiated", opi, st);
373 "cn=Extended,cn=Operations,cn=Monitor")
376 ldap_submit_derive ("operations",
377 "extended-completed", opc, st);
378 ldap_submit_derive ("operations",
379 "extended-initiated", opi, st);
381 else if ((strncmp (dn, "cn=Database", 11) == 0)
382 && ((nc_list = ldap_get_values_len
383 (st->ld, e, "namingContexts")) != NULL))
385 nc_data = *nc_list[0];
386 char typeinst[DATA_MAX_NAME_LEN];
388 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
389 "olmBDBEntryCache")) != NULL)
391 olmbdb_data = *olmbdb_list[0];
392 ssnprintf (typeinst, sizeof (typeinst),
393 "bdbentrycache-%s", nc_data.bv_val);
394 ldap_submit_gauge ("cache_size", typeinst,
395 atoll (olmbdb_data.bv_val), st);
396 ldap_value_free_len (olmbdb_list);
399 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
400 "olmBDBDNCache")) != NULL)
402 olmbdb_data = *olmbdb_list[0];
403 ssnprintf (typeinst, sizeof (typeinst),
404 "bdbdncache-%s", nc_data.bv_val);
405 ldap_submit_gauge ("cache_size", typeinst,
406 atoll (olmbdb_data.bv_val), st);
407 ldap_value_free_len (olmbdb_list);
410 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
411 "olmBDBIDLCache")) != NULL)
413 olmbdb_data = *olmbdb_list[0];
414 ssnprintf (typeinst, sizeof (typeinst),
415 "bdbidlcache-%s", nc_data.bv_val);
416 ldap_submit_gauge ("cache_size", typeinst,
417 atoll (olmbdb_data.bv_val), st);
418 ldap_value_free_len (olmbdb_list);
421 ldap_value_free_len (nc_list);
424 "cn=Bytes,cn=Statistics,cn=Monitor")
427 ldap_submit_derive ("derive", "statistics-bytes",
431 "cn=PDU,cn=Statistics,cn=Monitor")
434 ldap_submit_derive ("derive", "statistics-pdu",
438 "cn=Entries,cn=Statistics,cn=Monitor")
441 ldap_submit_derive ("derive", "statistics-entries",
445 "cn=Referrals,cn=Statistics,cn=Monitor")
448 ldap_submit_derive ("derive", "statistics-referrals",
452 "cn=Open,cn=Threads,cn=Monitor")
455 ldap_submit_gauge ("threads", "threads-open",
459 "cn=Starting,cn=Threads,cn=Monitor")
462 ldap_submit_gauge ("threads", "threads-starting",
466 "cn=Active,cn=Threads,cn=Monitor")
469 ldap_submit_gauge ("threads", "threads-active",
473 "cn=Pending,cn=Threads,cn=Monitor")
476 ldap_submit_gauge ("threads", "threads-pending",
480 "cn=Backload,cn=Threads,cn=Monitor")
483 ldap_submit_gauge ("threads", "threads-backload",
487 "cn=Read,cn=Waiters,cn=Monitor")
490 ldap_submit_derive ("derive", "waiters-read",
494 "cn=Write,cn=Waiters,cn=Monitor")
497 ldap_submit_derive ("derive", "waiters-write",
501 ldap_value_free_len (counter_list);
502 ldap_value_free_len (opc_list);
503 ldap_value_free_len (opi_list);
504 ldap_value_free_len (info_list);
510 ldap_msgfree (result);
511 ldap_unbind_ext_s (st->ld, NULL, NULL);
513 } /* }}} int ldap_read_host */
515 /* Configuration handling functions {{{
518 * <Instance "plugin_instance1">
519 * URL "ldap://localhost"
525 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
531 st = malloc (sizeof (*st));
534 ERROR ("openldap plugin: malloc failed.");
537 memset (st, 0, sizeof (*st));
539 status = cf_util_get_string (ci, &st->name);
549 st->version = LDAP_VERSION3;
551 for (i = 0; i < ci->children_num; i++)
553 oconfig_item_t *child = ci->children + i;
555 if (strcasecmp ("CACert", child->key) == 0)
556 status = cf_util_get_string (child, &st->cacert);
557 else if (strcasecmp ("StartTLS", child->key) == 0)
558 status = cf_util_get_boolean (child, &st->starttls);
559 else if (strcasecmp ("Timeout", child->key) == 0)
560 status = cf_util_get_int (child, &st->timeout);
561 else if (strcasecmp ("URL", child->key) == 0)
562 status = cf_util_get_string (child, &st->url);
563 else if (strcasecmp ("VerifyHost", child->key) == 0)
564 status = cf_util_get_boolean (child, &st->verifyhost);
565 else if (strcasecmp ("Version", child->key) == 0)
566 status = cf_util_get_int (child, &st->version);
569 WARNING ("openldap plugin: Option `%s' not allowed here.",
578 /* Check if struct is complete.. */
579 if ((status == 0) && (st->url == NULL))
581 ERROR ("openldap plugin: Instance `%s': "
582 "No URL has been configured.",
587 /* Check if URL is valid */
588 if ((status == 0) && (st->url != NULL))
593 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
595 ERROR ("openldap plugin: Instance `%s': "
602 st->host = strdup (ludpp->lud_host);
605 ldap_free_urldesc (ludpp);
611 char callback_name[3*DATA_MAX_NAME_LEN];
613 memset (&ud, 0, sizeof (ud));
616 memset (callback_name, 0, sizeof (callback_name));
617 ssnprintf (callback_name, sizeof (callback_name),
619 (st->host != NULL) ? st->host : hostname_g,
620 (st->name != NULL) ? st->name : "default"),
622 status = plugin_register_complex_read (/* group = */ NULL,
623 /* name = */ callback_name,
624 /* callback = */ ldap_read_host,
625 /* interval = */ NULL,
626 /* user_data = */ &ud);
636 } /* }}} int ldap_config_add */
638 static int ldap_config (oconfig_item_t *ci) /* {{{ */
643 for (i = 0; i < ci->children_num; i++)
645 oconfig_item_t *child = ci->children + i;
647 if (strcasecmp ("Instance", child->key) == 0)
648 ldap_config_add (child);
650 WARNING ("openldap plugin: The configuration option "
651 "\"%s\" is not allowed here. Did you "
652 "forget to add an <Instance /> block "
653 "around the configuration?",
655 } /* for (ci->children) */
658 } /* }}} int ldap_config */
660 /* }}} End of configuration handling functions */
662 static int ldap_init (void) /* {{{ */
664 /* Initialize LDAP library while still single-threaded as recommended in
665 * ldap_initialize(3) */
667 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
669 } /* }}} int ldap_init */
671 void module_register (void) /* {{{ */
673 plugin_register_complex_config ("openldap", ldap_config);
674 plugin_register_init ("openldap", ldap_init);
675 } /* }}} void module_register */