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) /* {{{ */
58 ldap_memfree (st->ld);
60 } /* }}} void ldap_free */
62 /* initialize ldap for each host */
63 static int ldap_init_host (ldap_t *st) /* {{{ */
67 rc = ldap_initialize (&ld, st->url);
68 if (rc != LDAP_SUCCESS)
70 ERROR ("openldap plugin: ldap_initialize failed: %s",
71 ldap_err2string (rc));
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);
217 for (e = ldap_first_entry (st->ld, result); e != NULL;
218 e = ldap_next_entry (st->ld, e))
220 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
222 unsigned long long counter = 0;
223 unsigned long long opc = 0;
224 unsigned long long opi = 0;
225 unsigned long long info = 0;
227 struct berval counter_data;
228 struct berval opc_data;
229 struct berval opi_data;
230 struct berval info_data;
231 struct berval olmbdb_data;
232 struct berval nc_data;
234 struct berval **counter_list;
235 struct berval **opc_list;
236 struct berval **opi_list;
237 struct berval **info_list;
238 struct berval **olmbdb_list;
239 struct berval **nc_list;
241 if ((counter_list = ldap_get_values_len (st->ld, e,
242 "monitorCounter")) != NULL)
244 counter_data = *counter_list[0];
245 counter = atoll (counter_data.bv_val);
248 if ((opc_list = ldap_get_values_len (st->ld, e,
249 "monitorOpCompleted")) != NULL)
251 opc_data = *opc_list[0];
252 opc = atoll (opc_data.bv_val);
255 if ((opi_list = ldap_get_values_len (st->ld, e,
256 "monitorOpInitiated")) != NULL)
258 opi_data = *opi_list[0];
259 opi = atoll (opi_data.bv_val);
262 if ((info_list = ldap_get_values_len (st->ld, e,
263 "monitoredInfo")) != NULL)
265 info_data = *info_list[0];
266 info = atoll (info_data.bv_val);
269 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
272 ldap_submit_derive ("total_connections", NULL,
276 "cn=Current,cn=Connections,cn=Monitor")
279 ldap_submit_gauge ("current_connections", NULL,
283 "cn=Operations,cn=Monitor") == 0)
285 ldap_submit_derive ("operations",
286 "completed", opc, st);
287 ldap_submit_derive ("operations",
288 "initiated", opi, st);
291 "cn=Bind,cn=Operations,cn=Monitor")
294 ldap_submit_derive ("operations",
295 "bind-completed", opc, st);
296 ldap_submit_derive ("operations",
297 "bind-initiated", opi, st);
300 "cn=UnBind,cn=Operations,cn=Monitor")
303 ldap_submit_derive ("operations",
304 "unbind-completed", opc, st);
305 ldap_submit_derive ("operations",
306 "unbind-initiated", opi, st);
309 "cn=Search,cn=Operations,cn=Monitor")
312 ldap_submit_derive ("operations",
313 "search-completed", opc, st);
314 ldap_submit_derive ("operations",
315 "search-initiated", opi, st);
318 "cn=Compare,cn=Operations,cn=Monitor")
321 ldap_submit_derive ("operations",
322 "compare-completed", opc, st);
323 ldap_submit_derive ("operations",
324 "compare-initiated", opi, st);
327 "cn=Modify,cn=Operations,cn=Monitor")
330 ldap_submit_derive ("operations",
331 "modify-completed", opc, st);
332 ldap_submit_derive ("operations",
333 "modify-initiated", opi, st);
336 "cn=Modrdn,cn=Operations,cn=Monitor")
339 ldap_submit_derive ("operations",
340 "modrdn-completed", opc, st);
341 ldap_submit_derive ("operations",
342 "modrdn-initiated", opi, st);
345 "cn=Add,cn=Operations,cn=Monitor")
348 ldap_submit_derive ("operations",
349 "add-completed", opc, st);
350 ldap_submit_derive ("operations",
351 "add-initiated", opi, st);
354 "cn=Delete,cn=Operations,cn=Monitor")
357 ldap_submit_derive ("operations",
358 "delete-completed", opc, st);
359 ldap_submit_derive ("operations",
360 "delete-initiated", opi, st);
363 "cn=Abandon,cn=Operations,cn=Monitor")
366 ldap_submit_derive ("operations",
367 "abandon-completed", opc, st);
368 ldap_submit_derive ("operations",
369 "abandon-initiated", opi, st);
372 "cn=Extended,cn=Operations,cn=Monitor")
375 ldap_submit_derive ("operations",
376 "extended-completed", opc, st);
377 ldap_submit_derive ("operations",
378 "extended-initiated", opi, st);
380 else if ((strncmp (dn, "cn=Database", 11) == 0)
381 && ((nc_list = ldap_get_values_len
382 (st->ld, e, "namingContexts")) != NULL))
384 nc_data = *nc_list[0];
385 char typeinst[DATA_MAX_NAME_LEN];
387 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
388 "olmBDBEntryCache")) != NULL)
390 olmbdb_data = *olmbdb_list[0];
391 ssnprintf (typeinst, sizeof (typeinst),
392 "bdbentrycache-%s", nc_data.bv_val);
393 ldap_submit_gauge ("cache_size", typeinst,
394 atoll (olmbdb_data.bv_val), st);
395 ldap_value_free_len (olmbdb_list);
398 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
399 "olmBDBDNCache")) != NULL)
401 olmbdb_data = *olmbdb_list[0];
402 ssnprintf (typeinst, sizeof (typeinst),
403 "bdbdncache-%s", nc_data.bv_val);
404 ldap_submit_gauge ("cache_size", typeinst,
405 atoll (olmbdb_data.bv_val), st);
406 ldap_value_free_len (olmbdb_list);
409 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
410 "olmBDBIDLCache")) != NULL)
412 olmbdb_data = *olmbdb_list[0];
413 ssnprintf (typeinst, sizeof (typeinst),
414 "bdbidlcache-%s", nc_data.bv_val);
415 ldap_submit_gauge ("cache_size", typeinst,
416 atoll (olmbdb_data.bv_val), st);
417 ldap_value_free_len (olmbdb_list);
420 ldap_value_free_len (nc_list);
423 "cn=Bytes,cn=Statistics,cn=Monitor")
426 ldap_submit_derive ("derive", "statistics-bytes",
430 "cn=PDU,cn=Statistics,cn=Monitor")
433 ldap_submit_derive ("derive", "statistics-pdu",
437 "cn=Entries,cn=Statistics,cn=Monitor")
440 ldap_submit_derive ("derive", "statistics-entries",
444 "cn=Referrals,cn=Statistics,cn=Monitor")
447 ldap_submit_derive ("derive", "statistics-referrals",
451 "cn=Open,cn=Threads,cn=Monitor")
454 ldap_submit_gauge ("threads", "threads-open",
458 "cn=Starting,cn=Threads,cn=Monitor")
461 ldap_submit_gauge ("threads", "threads-starting",
465 "cn=Active,cn=Threads,cn=Monitor")
468 ldap_submit_gauge ("threads", "threads-active",
472 "cn=Pending,cn=Threads,cn=Monitor")
475 ldap_submit_gauge ("threads", "threads-pending",
479 "cn=Backload,cn=Threads,cn=Monitor")
482 ldap_submit_gauge ("threads", "threads-backload",
486 "cn=Read,cn=Waiters,cn=Monitor")
489 ldap_submit_derive ("derive", "waiters-read",
493 "cn=Write,cn=Waiters,cn=Monitor")
496 ldap_submit_derive ("derive", "waiters-write",
500 ldap_value_free_len (counter_list);
501 ldap_value_free_len (opc_list);
502 ldap_value_free_len (opi_list);
503 ldap_value_free_len (info_list);
509 ldap_msgfree (result);
510 ldap_unbind_ext_s (st->ld, NULL, NULL);
512 } /* }}} int ldap_read_host */
514 /* Configuration handling functions {{{
517 * <Instance "plugin_instance1">
518 * URL "ldap://localhost"
524 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
530 st = malloc (sizeof (*st));
533 ERROR ("openldap plugin: malloc failed.");
536 memset (st, 0, sizeof (*st));
538 status = cf_util_get_string (ci, &st->name);
548 st->version = LDAP_VERSION3;
550 for (i = 0; i < ci->children_num; i++)
552 oconfig_item_t *child = ci->children + i;
554 if (strcasecmp ("CACert", child->key) == 0)
555 status = cf_util_get_string (child, &st->cacert);
556 else if (strcasecmp ("StartTLS", child->key) == 0)
557 status = cf_util_get_boolean (child, &st->starttls);
558 else if (strcasecmp ("Timeout", child->key) == 0)
559 status = cf_util_get_int (child, &st->timeout);
560 else if (strcasecmp ("URL", child->key) == 0)
561 status = cf_util_get_string (child, &st->url);
562 else if (strcasecmp ("VerifyHost", child->key) == 0)
563 status = cf_util_get_boolean (child, &st->verifyhost);
564 else if (strcasecmp ("Version", child->key) == 0)
565 status = cf_util_get_int (child, &st->version);
568 WARNING ("openldap plugin: Option `%s' not allowed here.",
577 /* Check if struct is complete.. */
578 if ((status == 0) && (st->url == NULL))
580 ERROR ("openldap plugin: Instance `%s': "
581 "No URL has been configured.",
586 /* Check if URL is valid */
587 if ((status == 0) && (st->url != NULL))
592 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
594 ERROR ("openldap plugin: Instance `%s': "
601 st->host = strdup (ludpp->lud_host);
604 ldap_free_urldesc (ludpp);
610 char callback_name[3*DATA_MAX_NAME_LEN];
612 memset (&ud, 0, sizeof (ud));
615 memset (callback_name, 0, sizeof (callback_name));
616 ssnprintf (callback_name, sizeof (callback_name),
618 (st->host != NULL) ? st->host : hostname_g,
619 (st->name != NULL) ? st->name : "default"),
621 status = plugin_register_complex_read (/* group = */ NULL,
622 /* name = */ callback_name,
623 /* callback = */ ldap_read_host,
624 /* interval = */ NULL,
625 /* user_data = */ &ud);
635 } /* }}} int ldap_config_add */
637 static int ldap_config (oconfig_item_t *ci) /* {{{ */
642 for (i = 0; i < ci->children_num; i++)
644 oconfig_item_t *child = ci->children + i;
646 if (strcasecmp ("Instance", child->key) == 0)
647 ldap_config_add (child);
649 WARNING ("openldap plugin: The configuration option "
650 "\"%s\" is not allowed here. Did you "
651 "forget to add an <Instance /> block "
652 "around the configuration?",
654 } /* for (ci->children) */
657 } /* }}} int ldap_config */
659 /* }}} End of configuration handling functions */
661 static int ldap_init (void) /* {{{ */
663 /* Initialize LDAP library while still single-threaded as recommended in
664 * ldap_initialize(3) */
666 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
668 } /* }}} int ldap_init */
670 void module_register (void) /* {{{ */
672 plugin_register_complex_config ("openldap", ldap_config);
673 plugin_register_init ("openldap", ldap_init);
674 } /* }}} void module_register */