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 /* {{{ */
52 typedef struct cldap_s cldap_t; /* }}} */
54 static void cldap_free (cldap_t *st) /* {{{ */
64 ldap_memfree (st->ld);
66 } /* }}} void cldap_free */
68 /* initialize ldap for each host */
69 static int cldap_init_host (cldap_t *st) /* {{{ */
73 rc = ldap_initialize (&ld, st->url);
74 if (rc != LDAP_SUCCESS)
76 ERROR ("openldap plugin: ldap_initialize failed: %s",
77 ldap_err2string (rc));
79 ldap_unbind_ext_s (ld, NULL, NULL);
85 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
87 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
88 &(const struct timeval){st->timeout, 0});
90 if (st->cacert != NULL)
91 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
93 if (st->verifyhost == 0)
95 int never = LDAP_OPT_X_TLS_NEVER;
96 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
99 if (st->starttls != 0)
101 rc = ldap_start_tls_s (ld, NULL, NULL);
102 if (rc != LDAP_SUCCESS)
104 ERROR ("openldap plugin: Failed to start tls on %s: %s",
105 st->url, ldap_err2string (rc));
107 ldap_unbind_ext_s (st->ld, NULL, NULL);
116 rc = ldap_sasl_bind_s (st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
117 if (rc != LDAP_SUCCESS)
119 ERROR ("openldap plugin: Failed to bind to %s: %s",
120 st->url, ldap_err2string (rc));
122 ldap_unbind_ext_s (st->ld, NULL, NULL);
127 DEBUG ("openldap plugin: Successfully connected to %s",
132 } /* }}} static cldap_init_host */
134 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
135 value_t value, cldap_t *st)
137 value_list_t vl = VALUE_LIST_INIT;
142 if ((st->host == NULL)
143 || (strcmp ("", st->host) == 0)
144 || (strcmp ("localhost", st->host) == 0))
146 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
150 sstrncpy (vl.host, st->host, sizeof (vl.host));
153 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
154 if (st->name != NULL)
155 sstrncpy (vl.plugin_instance, st->name,
156 sizeof (vl.plugin_instance));
158 sstrncpy (vl.type, type, sizeof (vl.type));
159 if (type_instance != NULL)
160 sstrncpy (vl.type_instance, type_instance,
161 sizeof (vl.type_instance));
163 plugin_dispatch_values (&vl);
164 } /* }}} void cldap_submit_value */
166 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
167 derive_t d, cldap_t *st)
171 cldap_submit_value (type, type_instance, v, st);
172 } /* }}} void cldap_submit_derive */
174 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
175 gauge_t g, cldap_t *st)
179 cldap_submit_value (type, type_instance, v, st);
180 } /* }}} void cldap_submit_gauge */
182 static int cldap_read_host (user_data_t *ud) /* {{{ */
185 LDAPMessage *e, *result;
190 char *attrs[9] = { "monitorCounter",
191 "monitorOpCompleted",
192 "monitorOpInitiated",
200 if ((ud == NULL) || (ud->data == NULL))
202 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
206 st = (cldap_t *) ud->data;
208 status = cldap_init_host (st);
212 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
213 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
214 NULL, NULL, NULL, 0, &result);
216 if (rc != LDAP_SUCCESS)
218 ERROR ("openldap plugin: Failed to execute search: %s",
219 ldap_err2string (rc));
220 ldap_msgfree (result);
221 ldap_unbind_ext_s (st->ld, NULL, NULL);
225 for (e = ldap_first_entry (st->ld, result); e != NULL;
226 e = ldap_next_entry (st->ld, e))
228 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
230 unsigned long long counter = 0;
231 unsigned long long opc = 0;
232 unsigned long long opi = 0;
233 unsigned long long info = 0;
235 struct berval counter_data;
236 struct berval opc_data;
237 struct berval opi_data;
238 struct berval info_data;
239 struct berval olmbdb_data;
240 struct berval nc_data;
242 struct berval **counter_list;
243 struct berval **opc_list;
244 struct berval **opi_list;
245 struct berval **info_list;
246 struct berval **olmbdb_list;
247 struct berval **nc_list;
249 if ((counter_list = ldap_get_values_len (st->ld, e,
250 "monitorCounter")) != NULL)
252 counter_data = *counter_list[0];
253 counter = atoll (counter_data.bv_val);
256 if ((opc_list = ldap_get_values_len (st->ld, e,
257 "monitorOpCompleted")) != NULL)
259 opc_data = *opc_list[0];
260 opc = atoll (opc_data.bv_val);
263 if ((opi_list = ldap_get_values_len (st->ld, e,
264 "monitorOpInitiated")) != NULL)
266 opi_data = *opi_list[0];
267 opi = atoll (opi_data.bv_val);
270 if ((info_list = ldap_get_values_len (st->ld, e,
271 "monitoredInfo")) != NULL)
273 info_data = *info_list[0];
274 info = atoll (info_data.bv_val);
277 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
280 cldap_submit_derive ("total_connections", NULL,
284 "cn=Current,cn=Connections,cn=Monitor")
287 cldap_submit_gauge ("current_connections", NULL,
291 "cn=Operations,cn=Monitor") == 0)
293 cldap_submit_derive ("operations",
294 "completed", opc, st);
295 cldap_submit_derive ("operations",
296 "initiated", opi, st);
299 "cn=Bind,cn=Operations,cn=Monitor")
302 cldap_submit_derive ("operations",
303 "bind-completed", opc, st);
304 cldap_submit_derive ("operations",
305 "bind-initiated", opi, st);
308 "cn=UnBind,cn=Operations,cn=Monitor")
311 cldap_submit_derive ("operations",
312 "unbind-completed", opc, st);
313 cldap_submit_derive ("operations",
314 "unbind-initiated", opi, st);
317 "cn=Search,cn=Operations,cn=Monitor")
320 cldap_submit_derive ("operations",
321 "search-completed", opc, st);
322 cldap_submit_derive ("operations",
323 "search-initiated", opi, st);
326 "cn=Compare,cn=Operations,cn=Monitor")
329 cldap_submit_derive ("operations",
330 "compare-completed", opc, st);
331 cldap_submit_derive ("operations",
332 "compare-initiated", opi, st);
335 "cn=Modify,cn=Operations,cn=Monitor")
338 cldap_submit_derive ("operations",
339 "modify-completed", opc, st);
340 cldap_submit_derive ("operations",
341 "modify-initiated", opi, st);
344 "cn=Modrdn,cn=Operations,cn=Monitor")
347 cldap_submit_derive ("operations",
348 "modrdn-completed", opc, st);
349 cldap_submit_derive ("operations",
350 "modrdn-initiated", opi, st);
353 "cn=Add,cn=Operations,cn=Monitor")
356 cldap_submit_derive ("operations",
357 "add-completed", opc, st);
358 cldap_submit_derive ("operations",
359 "add-initiated", opi, st);
362 "cn=Delete,cn=Operations,cn=Monitor")
365 cldap_submit_derive ("operations",
366 "delete-completed", opc, st);
367 cldap_submit_derive ("operations",
368 "delete-initiated", opi, st);
371 "cn=Abandon,cn=Operations,cn=Monitor")
374 cldap_submit_derive ("operations",
375 "abandon-completed", opc, st);
376 cldap_submit_derive ("operations",
377 "abandon-initiated", opi, st);
380 "cn=Extended,cn=Operations,cn=Monitor")
383 cldap_submit_derive ("operations",
384 "extended-completed", opc, st);
385 cldap_submit_derive ("operations",
386 "extended-initiated", opi, st);
388 else if ((strncmp (dn, "cn=Database", 11) == 0)
389 && ((nc_list = ldap_get_values_len
390 (st->ld, e, "namingContexts")) != NULL))
392 nc_data = *nc_list[0];
393 char typeinst[DATA_MAX_NAME_LEN];
395 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
396 "olmBDBEntryCache")) != NULL)
398 olmbdb_data = *olmbdb_list[0];
399 ssnprintf (typeinst, sizeof (typeinst),
400 "bdbentrycache-%s", nc_data.bv_val);
401 cldap_submit_gauge ("cache_size", typeinst,
402 atoll (olmbdb_data.bv_val), st);
403 ldap_value_free_len (olmbdb_list);
406 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
407 "olmBDBDNCache")) != NULL)
409 olmbdb_data = *olmbdb_list[0];
410 ssnprintf (typeinst, sizeof (typeinst),
411 "bdbdncache-%s", nc_data.bv_val);
412 cldap_submit_gauge ("cache_size", typeinst,
413 atoll (olmbdb_data.bv_val), st);
414 ldap_value_free_len (olmbdb_list);
417 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
418 "olmBDBIDLCache")) != NULL)
420 olmbdb_data = *olmbdb_list[0];
421 ssnprintf (typeinst, sizeof (typeinst),
422 "bdbidlcache-%s", nc_data.bv_val);
423 cldap_submit_gauge ("cache_size", typeinst,
424 atoll (olmbdb_data.bv_val), st);
425 ldap_value_free_len (olmbdb_list);
428 ldap_value_free_len (nc_list);
431 "cn=Bytes,cn=Statistics,cn=Monitor")
434 cldap_submit_derive ("derive", "statistics-bytes",
438 "cn=PDU,cn=Statistics,cn=Monitor")
441 cldap_submit_derive ("derive", "statistics-pdu",
445 "cn=Entries,cn=Statistics,cn=Monitor")
448 cldap_submit_derive ("derive", "statistics-entries",
452 "cn=Referrals,cn=Statistics,cn=Monitor")
455 cldap_submit_derive ("derive", "statistics-referrals",
459 "cn=Open,cn=Threads,cn=Monitor")
462 cldap_submit_gauge ("threads", "threads-open",
466 "cn=Starting,cn=Threads,cn=Monitor")
469 cldap_submit_gauge ("threads", "threads-starting",
473 "cn=Active,cn=Threads,cn=Monitor")
476 cldap_submit_gauge ("threads", "threads-active",
480 "cn=Pending,cn=Threads,cn=Monitor")
483 cldap_submit_gauge ("threads", "threads-pending",
487 "cn=Backload,cn=Threads,cn=Monitor")
490 cldap_submit_gauge ("threads", "threads-backload",
494 "cn=Read,cn=Waiters,cn=Monitor")
497 cldap_submit_derive ("derive", "waiters-read",
501 "cn=Write,cn=Waiters,cn=Monitor")
504 cldap_submit_derive ("derive", "waiters-write",
508 ldap_value_free_len (counter_list);
509 ldap_value_free_len (opc_list);
510 ldap_value_free_len (opi_list);
511 ldap_value_free_len (info_list);
517 ldap_msgfree (result);
518 ldap_unbind_ext_s (st->ld, NULL, NULL);
520 } /* }}} int cldap_read_host */
522 /* Configuration handling functions {{{
525 * <Instance "plugin_instance1">
526 * URL "ldap://localhost"
532 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
538 st = malloc (sizeof (*st));
541 ERROR ("openldap plugin: malloc failed.");
544 memset (st, 0, sizeof (*st));
546 status = cf_util_get_string (ci, &st->name);
556 st->version = LDAP_VERSION3;
558 for (i = 0; i < ci->children_num; i++)
560 oconfig_item_t *child = ci->children + i;
562 if (strcasecmp ("CACert", child->key) == 0)
563 status = cf_util_get_string (child, &st->cacert);
564 else if (strcasecmp ("StartTLS", child->key) == 0)
565 status = cf_util_get_boolean (child, &st->starttls);
566 else if (strcasecmp ("Timeout", child->key) == 0)
567 status = cf_util_get_int (child, &st->timeout);
568 else if (strcasecmp ("URL", child->key) == 0)
569 status = cf_util_get_string (child, &st->url);
570 else if (strcasecmp ("VerifyHost", child->key) == 0)
571 status = cf_util_get_boolean (child, &st->verifyhost);
572 else if (strcasecmp ("Version", child->key) == 0)
573 status = cf_util_get_int (child, &st->version);
576 WARNING ("openldap plugin: Option `%s' not allowed here.",
585 /* Check if struct is complete.. */
586 if ((status == 0) && (st->url == NULL))
588 ERROR ("openldap plugin: Instance `%s': "
589 "No URL has been configured.",
594 /* Check if URL is valid */
595 if ((status == 0) && (st->url != NULL))
600 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
602 ERROR ("openldap plugin: Instance `%s': "
609 st->host = strdup (ludpp->lud_host);
612 ldap_free_urldesc (ludpp);
618 char callback_name[3*DATA_MAX_NAME_LEN];
620 memset (&ud, 0, sizeof (ud));
623 memset (callback_name, 0, sizeof (callback_name));
624 ssnprintf (callback_name, sizeof (callback_name),
626 (st->host != NULL) ? st->host : hostname_g,
627 (st->name != NULL) ? st->name : "default"),
629 status = plugin_register_complex_read (/* group = */ NULL,
630 /* name = */ callback_name,
631 /* callback = */ cldap_read_host,
632 /* interval = */ NULL,
633 /* user_data = */ &ud);
643 } /* }}} int cldap_config_add */
645 static int cldap_config (oconfig_item_t *ci) /* {{{ */
650 for (i = 0; i < ci->children_num; i++)
652 oconfig_item_t *child = ci->children + i;
654 if (strcasecmp ("Instance", child->key) == 0)
655 cldap_config_add (child);
657 WARNING ("openldap plugin: The configuration option "
658 "\"%s\" is not allowed here. Did you "
659 "forget to add an <Instance /> block "
660 "around the configuration?",
662 } /* for (ci->children) */
665 } /* }}} int cldap_config */
667 /* }}} End of configuration handling functions */
669 static int cldap_init (void) /* {{{ */
671 /* Initialize LDAP library while still single-threaded as recommended in
672 * ldap_initialize(3) */
674 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
676 } /* }}} int cldap_init */
678 void module_register (void) /* {{{ */
680 plugin_register_complex_config ("openldap", cldap_config);
681 plugin_register_init ("openldap", cldap_init);
682 } /* }}} void module_register */