2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
4 * Copyright (C) 2014 Marc Fournier
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kimo Rosenbaum <kimor79 at yahoo.com>
26 * Marc Fournier <marc.fournier at camptocamp.com>
32 #include "configfile.h"
37 struct cldap_s /* {{{ */
54 typedef struct cldap_s cldap_t; /* }}} */
56 static void cldap_free (cldap_t *st) /* {{{ */
68 ldap_memfree (st->ld);
70 } /* }}} void cldap_free */
72 /* initialize ldap for each host */
73 static int cldap_init_host (cldap_t *st) /* {{{ */
77 rc = ldap_initialize (&ld, st->url);
78 if (rc != LDAP_SUCCESS)
80 ERROR ("openldap plugin: ldap_initialize failed: %s",
81 ldap_err2string (rc));
83 ldap_unbind_ext_s (ld, NULL, NULL);
89 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
91 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
92 &(const struct timeval){st->timeout, 0});
94 if (st->cacert != NULL)
95 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
97 if (st->verifyhost == 0)
99 int never = LDAP_OPT_X_TLS_NEVER;
100 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
103 if (st->starttls != 0)
105 rc = ldap_start_tls_s (ld, NULL, NULL);
106 if (rc != LDAP_SUCCESS)
108 ERROR ("openldap plugin: Failed to start tls on %s: %s",
109 st->url, ldap_err2string (rc));
111 ldap_unbind_ext_s (st->ld, NULL, NULL);
117 if (st->password != NULL)
119 cred.bv_val = st->password;
120 cred.bv_len = strlen (st->password);
128 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
130 if (rc != LDAP_SUCCESS)
132 ERROR ("openldap plugin: Failed to bind to %s: %s",
133 st->url, ldap_err2string (rc));
135 ldap_unbind_ext_s (st->ld, NULL, NULL);
140 DEBUG ("openldap plugin: Successfully connected to %s",
145 } /* }}} static cldap_init_host */
147 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
148 value_t value, cldap_t *st)
150 value_list_t vl = VALUE_LIST_INIT;
155 if ((st->host == NULL)
156 || (strcmp ("", st->host) == 0)
157 || (strcmp ("localhost", st->host) == 0))
159 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
163 sstrncpy (vl.host, st->host, sizeof (vl.host));
166 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
167 if (st->name != NULL)
168 sstrncpy (vl.plugin_instance, st->name,
169 sizeof (vl.plugin_instance));
171 sstrncpy (vl.type, type, sizeof (vl.type));
172 if (type_instance != NULL)
173 sstrncpy (vl.type_instance, type_instance,
174 sizeof (vl.type_instance));
176 plugin_dispatch_values (&vl);
177 } /* }}} void cldap_submit_value */
179 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
180 derive_t d, cldap_t *st)
184 cldap_submit_value (type, type_instance, v, st);
185 } /* }}} void cldap_submit_derive */
187 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
188 gauge_t g, cldap_t *st)
192 cldap_submit_value (type, type_instance, v, st);
193 } /* }}} void cldap_submit_gauge */
195 static int cldap_read_host (user_data_t *ud) /* {{{ */
198 LDAPMessage *e, *result;
203 char *attrs[9] = { "monitorCounter",
204 "monitorOpCompleted",
205 "monitorOpInitiated",
213 if ((ud == NULL) || (ud->data == NULL))
215 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
219 st = (cldap_t *) ud->data;
221 status = cldap_init_host (st);
225 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
226 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
227 NULL, NULL, NULL, 0, &result);
229 if (rc != LDAP_SUCCESS)
231 ERROR ("openldap plugin: Failed to execute search: %s",
232 ldap_err2string (rc));
233 ldap_msgfree (result);
234 ldap_unbind_ext_s (st->ld, NULL, NULL);
238 for (e = ldap_first_entry (st->ld, result); e != NULL;
239 e = ldap_next_entry (st->ld, e))
241 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
243 unsigned long long counter = 0;
244 unsigned long long opc = 0;
245 unsigned long long opi = 0;
246 unsigned long long info = 0;
248 struct berval counter_data;
249 struct berval opc_data;
250 struct berval opi_data;
251 struct berval info_data;
252 struct berval olmbdb_data;
253 struct berval nc_data;
255 struct berval **counter_list;
256 struct berval **opc_list;
257 struct berval **opi_list;
258 struct berval **info_list;
259 struct berval **olmbdb_list;
260 struct berval **nc_list;
262 if ((counter_list = ldap_get_values_len (st->ld, e,
263 "monitorCounter")) != NULL)
265 counter_data = *counter_list[0];
266 counter = atoll (counter_data.bv_val);
269 if ((opc_list = ldap_get_values_len (st->ld, e,
270 "monitorOpCompleted")) != NULL)
272 opc_data = *opc_list[0];
273 opc = atoll (opc_data.bv_val);
276 if ((opi_list = ldap_get_values_len (st->ld, e,
277 "monitorOpInitiated")) != NULL)
279 opi_data = *opi_list[0];
280 opi = atoll (opi_data.bv_val);
283 if ((info_list = ldap_get_values_len (st->ld, e,
284 "monitoredInfo")) != NULL)
286 info_data = *info_list[0];
287 info = atoll (info_data.bv_val);
290 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
293 cldap_submit_derive ("total_connections", NULL,
297 "cn=Current,cn=Connections,cn=Monitor")
300 cldap_submit_gauge ("current_connections", NULL,
304 "cn=Operations,cn=Monitor") == 0)
306 cldap_submit_derive ("operations",
307 "completed", opc, st);
308 cldap_submit_derive ("operations",
309 "initiated", opi, st);
312 "cn=Bind,cn=Operations,cn=Monitor")
315 cldap_submit_derive ("operations",
316 "bind-completed", opc, st);
317 cldap_submit_derive ("operations",
318 "bind-initiated", opi, st);
321 "cn=UnBind,cn=Operations,cn=Monitor")
324 cldap_submit_derive ("operations",
325 "unbind-completed", opc, st);
326 cldap_submit_derive ("operations",
327 "unbind-initiated", opi, st);
330 "cn=Search,cn=Operations,cn=Monitor")
333 cldap_submit_derive ("operations",
334 "search-completed", opc, st);
335 cldap_submit_derive ("operations",
336 "search-initiated", opi, st);
339 "cn=Compare,cn=Operations,cn=Monitor")
342 cldap_submit_derive ("operations",
343 "compare-completed", opc, st);
344 cldap_submit_derive ("operations",
345 "compare-initiated", opi, st);
348 "cn=Modify,cn=Operations,cn=Monitor")
351 cldap_submit_derive ("operations",
352 "modify-completed", opc, st);
353 cldap_submit_derive ("operations",
354 "modify-initiated", opi, st);
357 "cn=Modrdn,cn=Operations,cn=Monitor")
360 cldap_submit_derive ("operations",
361 "modrdn-completed", opc, st);
362 cldap_submit_derive ("operations",
363 "modrdn-initiated", opi, st);
366 "cn=Add,cn=Operations,cn=Monitor")
369 cldap_submit_derive ("operations",
370 "add-completed", opc, st);
371 cldap_submit_derive ("operations",
372 "add-initiated", opi, st);
375 "cn=Delete,cn=Operations,cn=Monitor")
378 cldap_submit_derive ("operations",
379 "delete-completed", opc, st);
380 cldap_submit_derive ("operations",
381 "delete-initiated", opi, st);
384 "cn=Abandon,cn=Operations,cn=Monitor")
387 cldap_submit_derive ("operations",
388 "abandon-completed", opc, st);
389 cldap_submit_derive ("operations",
390 "abandon-initiated", opi, st);
393 "cn=Extended,cn=Operations,cn=Monitor")
396 cldap_submit_derive ("operations",
397 "extended-completed", opc, st);
398 cldap_submit_derive ("operations",
399 "extended-initiated", opi, st);
401 else if ((strncmp (dn, "cn=Database", 11) == 0)
402 && ((nc_list = ldap_get_values_len
403 (st->ld, e, "namingContexts")) != NULL))
405 nc_data = *nc_list[0];
406 char typeinst[DATA_MAX_NAME_LEN];
408 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
409 "olmBDBEntryCache")) != NULL)
411 olmbdb_data = *olmbdb_list[0];
412 ssnprintf (typeinst, sizeof (typeinst),
413 "bdbentrycache-%s", nc_data.bv_val);
414 cldap_submit_gauge ("cache_size", typeinst,
415 atoll (olmbdb_data.bv_val), st);
416 ldap_value_free_len (olmbdb_list);
419 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
420 "olmBDBDNCache")) != NULL)
422 olmbdb_data = *olmbdb_list[0];
423 ssnprintf (typeinst, sizeof (typeinst),
424 "bdbdncache-%s", nc_data.bv_val);
425 cldap_submit_gauge ("cache_size", typeinst,
426 atoll (olmbdb_data.bv_val), st);
427 ldap_value_free_len (olmbdb_list);
430 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
431 "olmBDBIDLCache")) != NULL)
433 olmbdb_data = *olmbdb_list[0];
434 ssnprintf (typeinst, sizeof (typeinst),
435 "bdbidlcache-%s", nc_data.bv_val);
436 cldap_submit_gauge ("cache_size", typeinst,
437 atoll (olmbdb_data.bv_val), st);
438 ldap_value_free_len (olmbdb_list);
441 ldap_value_free_len (nc_list);
444 "cn=Bytes,cn=Statistics,cn=Monitor")
447 cldap_submit_derive ("derive", "statistics-bytes",
451 "cn=PDU,cn=Statistics,cn=Monitor")
454 cldap_submit_derive ("derive", "statistics-pdu",
458 "cn=Entries,cn=Statistics,cn=Monitor")
461 cldap_submit_derive ("derive", "statistics-entries",
465 "cn=Referrals,cn=Statistics,cn=Monitor")
468 cldap_submit_derive ("derive", "statistics-referrals",
472 "cn=Open,cn=Threads,cn=Monitor")
475 cldap_submit_gauge ("threads", "threads-open",
479 "cn=Starting,cn=Threads,cn=Monitor")
482 cldap_submit_gauge ("threads", "threads-starting",
486 "cn=Active,cn=Threads,cn=Monitor")
489 cldap_submit_gauge ("threads", "threads-active",
493 "cn=Pending,cn=Threads,cn=Monitor")
496 cldap_submit_gauge ("threads", "threads-pending",
500 "cn=Backload,cn=Threads,cn=Monitor")
503 cldap_submit_gauge ("threads", "threads-backload",
507 "cn=Read,cn=Waiters,cn=Monitor")
510 cldap_submit_derive ("derive", "waiters-read",
514 "cn=Write,cn=Waiters,cn=Monitor")
517 cldap_submit_derive ("derive", "waiters-write",
521 ldap_value_free_len (counter_list);
522 ldap_value_free_len (opc_list);
523 ldap_value_free_len (opi_list);
524 ldap_value_free_len (info_list);
530 ldap_msgfree (result);
531 ldap_unbind_ext_s (st->ld, NULL, NULL);
533 } /* }}} int cldap_read_host */
535 /* Configuration handling functions {{{
538 * <Instance "plugin_instance1">
539 * URL "ldap://localhost"
545 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
551 st = malloc (sizeof (*st));
554 ERROR ("openldap plugin: malloc failed.");
557 memset (st, 0, sizeof (*st));
559 status = cf_util_get_string (ci, &st->name);
569 st->version = LDAP_VERSION3;
571 for (i = 0; i < ci->children_num; i++)
573 oconfig_item_t *child = ci->children + i;
575 if (strcasecmp ("BindDN", child->key) == 0)
576 status = cf_util_get_string (child, &st->binddn);
577 else if (strcasecmp ("Password", child->key) == 0)
578 status = cf_util_get_string (child, &st->password);
579 else if (strcasecmp ("CACert", child->key) == 0)
580 status = cf_util_get_string (child, &st->cacert);
581 else if (strcasecmp ("StartTLS", child->key) == 0)
582 status = cf_util_get_boolean (child, &st->starttls);
583 else if (strcasecmp ("Timeout", child->key) == 0)
584 status = cf_util_get_int (child, &st->timeout);
585 else if (strcasecmp ("URL", child->key) == 0)
586 status = cf_util_get_string (child, &st->url);
587 else if (strcasecmp ("VerifyHost", child->key) == 0)
588 status = cf_util_get_boolean (child, &st->verifyhost);
589 else if (strcasecmp ("Version", child->key) == 0)
590 status = cf_util_get_int (child, &st->version);
593 WARNING ("openldap plugin: Option `%s' not allowed here.",
602 /* Check if struct is complete.. */
603 if ((status == 0) && (st->url == NULL))
605 ERROR ("openldap plugin: Instance `%s': "
606 "No URL has been configured.",
611 /* Check if URL is valid */
612 if ((status == 0) && (st->url != NULL))
617 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
619 ERROR ("openldap plugin: Instance `%s': "
625 if ((status == 0) && (ludpp->lud_host != NULL))
627 st->host = strdup (ludpp->lud_host);
630 ldap_free_urldesc (ludpp);
636 char callback_name[3*DATA_MAX_NAME_LEN];
638 memset (&ud, 0, sizeof (ud));
641 memset (callback_name, 0, sizeof (callback_name));
642 ssnprintf (callback_name, sizeof (callback_name),
644 (st->host != NULL) ? st->host : hostname_g,
645 (st->name != NULL) ? st->name : "default"),
647 status = plugin_register_complex_read (/* group = */ NULL,
648 /* name = */ callback_name,
649 /* callback = */ cldap_read_host,
651 /* user_data = */ &ud);
661 } /* }}} int cldap_config_add */
663 static int cldap_config (oconfig_item_t *ci) /* {{{ */
668 for (i = 0; i < ci->children_num; i++)
670 oconfig_item_t *child = ci->children + i;
672 if (strcasecmp ("Instance", child->key) == 0)
673 cldap_config_add (child);
675 WARNING ("openldap plugin: The configuration option "
676 "\"%s\" is not allowed here. Did you "
677 "forget to add an <Instance /> block "
678 "around the configuration?",
680 } /* for (ci->children) */
683 } /* }}} int cldap_config */
685 /* }}} End of configuration handling functions */
687 static int cldap_init (void) /* {{{ */
689 /* Initialize LDAP library while still single-threaded as recommended in
690 * ldap_initialize(3) */
692 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
694 } /* }}} int cldap_init */
696 void module_register (void) /* {{{ */
698 plugin_register_complex_config ("openldap", cldap_config);
699 plugin_register_init ("openldap", cldap_init);
700 } /* }}} void module_register */