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) /* {{{ */
78 if (st->state && st->ld)
80 DEBUG ("openldap plugin: Already connected to %s", st->url);
84 rc = ldap_initialize (&ld, st->url);
85 if (rc != LDAP_SUCCESS)
87 ERROR ("openldap plugin: ldap_initialize failed: %s",
88 ldap_err2string (rc));
90 ldap_unbind_ext_s (ld, NULL, NULL);
96 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
98 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
99 &(const struct timeval){st->timeout, 0});
101 if (st->cacert != NULL)
102 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
104 if (st->verifyhost == 0)
106 int never = LDAP_OPT_X_TLS_NEVER;
107 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
110 if (st->starttls != 0)
112 rc = ldap_start_tls_s (ld, NULL, NULL);
113 if (rc != LDAP_SUCCESS)
115 ERROR ("openldap plugin: Failed to start tls on %s: %s",
116 st->url, ldap_err2string (rc));
118 ldap_unbind_ext_s (st->ld, NULL, NULL);
124 if (st->password != NULL)
126 cred.bv_val = st->password;
127 cred.bv_len = strlen (st->password);
135 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
137 if (rc != LDAP_SUCCESS)
139 ERROR ("openldap plugin: Failed to bind to %s: %s",
140 st->url, ldap_err2string (rc));
142 ldap_unbind_ext_s (st->ld, NULL, NULL);
147 DEBUG ("openldap plugin: Successfully connected to %s",
152 } /* }}} static cldap_init_host */
154 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
155 value_t value, cldap_t *st)
157 value_list_t vl = VALUE_LIST_INIT;
162 if ((st->host == NULL)
163 || (strcmp ("", st->host) == 0)
164 || (strcmp ("localhost", st->host) == 0))
166 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
170 sstrncpy (vl.host, st->host, sizeof (vl.host));
173 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
174 if (st->name != NULL)
175 sstrncpy (vl.plugin_instance, st->name,
176 sizeof (vl.plugin_instance));
178 sstrncpy (vl.type, type, sizeof (vl.type));
179 if (type_instance != NULL)
180 sstrncpy (vl.type_instance, type_instance,
181 sizeof (vl.type_instance));
183 plugin_dispatch_values (&vl);
184 } /* }}} void cldap_submit_value */
186 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
187 derive_t d, cldap_t *st)
191 cldap_submit_value (type, type_instance, v, st);
192 } /* }}} void cldap_submit_derive */
194 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
195 gauge_t g, cldap_t *st)
199 cldap_submit_value (type, type_instance, v, st);
200 } /* }}} void cldap_submit_gauge */
202 static int cldap_read_host (user_data_t *ud) /* {{{ */
205 LDAPMessage *e, *result;
210 char *attrs[9] = { "monitorCounter",
211 "monitorOpCompleted",
212 "monitorOpInitiated",
220 if ((ud == NULL) || (ud->data == NULL))
222 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
226 st = (cldap_t *) ud->data;
228 status = cldap_init_host (st);
232 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
233 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
234 NULL, NULL, NULL, 0, &result);
236 if (rc != LDAP_SUCCESS)
238 ERROR ("openldap plugin: Failed to execute search: %s",
239 ldap_err2string (rc));
240 ldap_msgfree (result);
242 ldap_unbind_ext_s (st->ld, NULL, NULL);
246 for (e = ldap_first_entry (st->ld, result); e != NULL;
247 e = ldap_next_entry (st->ld, e))
249 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
251 unsigned long long counter = 0;
252 unsigned long long opc = 0;
253 unsigned long long opi = 0;
254 unsigned long long info = 0;
256 struct berval counter_data;
257 struct berval opc_data;
258 struct berval opi_data;
259 struct berval info_data;
260 struct berval olmbdb_data;
261 struct berval nc_data;
263 struct berval **counter_list;
264 struct berval **opc_list;
265 struct berval **opi_list;
266 struct berval **info_list;
267 struct berval **olmbdb_list;
268 struct berval **nc_list;
270 if ((counter_list = ldap_get_values_len (st->ld, e,
271 "monitorCounter")) != NULL)
273 counter_data = *counter_list[0];
274 counter = atoll (counter_data.bv_val);
277 if ((opc_list = ldap_get_values_len (st->ld, e,
278 "monitorOpCompleted")) != NULL)
280 opc_data = *opc_list[0];
281 opc = atoll (opc_data.bv_val);
284 if ((opi_list = ldap_get_values_len (st->ld, e,
285 "monitorOpInitiated")) != NULL)
287 opi_data = *opi_list[0];
288 opi = atoll (opi_data.bv_val);
291 if ((info_list = ldap_get_values_len (st->ld, e,
292 "monitoredInfo")) != NULL)
294 info_data = *info_list[0];
295 info = atoll (info_data.bv_val);
298 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
301 cldap_submit_derive ("total_connections", NULL,
305 "cn=Current,cn=Connections,cn=Monitor")
308 cldap_submit_gauge ("current_connections", NULL,
312 "cn=Operations,cn=Monitor") == 0)
314 cldap_submit_derive ("operations",
315 "completed", opc, st);
316 cldap_submit_derive ("operations",
317 "initiated", opi, st);
320 "cn=Bind,cn=Operations,cn=Monitor")
323 cldap_submit_derive ("operations",
324 "bind-completed", opc, st);
325 cldap_submit_derive ("operations",
326 "bind-initiated", opi, st);
329 "cn=UnBind,cn=Operations,cn=Monitor")
332 cldap_submit_derive ("operations",
333 "unbind-completed", opc, st);
334 cldap_submit_derive ("operations",
335 "unbind-initiated", opi, st);
338 "cn=Search,cn=Operations,cn=Monitor")
341 cldap_submit_derive ("operations",
342 "search-completed", opc, st);
343 cldap_submit_derive ("operations",
344 "search-initiated", opi, st);
347 "cn=Compare,cn=Operations,cn=Monitor")
350 cldap_submit_derive ("operations",
351 "compare-completed", opc, st);
352 cldap_submit_derive ("operations",
353 "compare-initiated", opi, st);
356 "cn=Modify,cn=Operations,cn=Monitor")
359 cldap_submit_derive ("operations",
360 "modify-completed", opc, st);
361 cldap_submit_derive ("operations",
362 "modify-initiated", opi, st);
365 "cn=Modrdn,cn=Operations,cn=Monitor")
368 cldap_submit_derive ("operations",
369 "modrdn-completed", opc, st);
370 cldap_submit_derive ("operations",
371 "modrdn-initiated", opi, st);
374 "cn=Add,cn=Operations,cn=Monitor")
377 cldap_submit_derive ("operations",
378 "add-completed", opc, st);
379 cldap_submit_derive ("operations",
380 "add-initiated", opi, st);
383 "cn=Delete,cn=Operations,cn=Monitor")
386 cldap_submit_derive ("operations",
387 "delete-completed", opc, st);
388 cldap_submit_derive ("operations",
389 "delete-initiated", opi, st);
392 "cn=Abandon,cn=Operations,cn=Monitor")
395 cldap_submit_derive ("operations",
396 "abandon-completed", opc, st);
397 cldap_submit_derive ("operations",
398 "abandon-initiated", opi, st);
401 "cn=Extended,cn=Operations,cn=Monitor")
404 cldap_submit_derive ("operations",
405 "extended-completed", opc, st);
406 cldap_submit_derive ("operations",
407 "extended-initiated", opi, st);
409 else if ((strncmp (dn, "cn=Database", 11) == 0)
410 && ((nc_list = ldap_get_values_len
411 (st->ld, e, "namingContexts")) != NULL))
413 nc_data = *nc_list[0];
414 char typeinst[DATA_MAX_NAME_LEN];
416 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
417 "olmBDBEntryCache")) != NULL)
419 olmbdb_data = *olmbdb_list[0];
420 ssnprintf (typeinst, sizeof (typeinst),
421 "bdbentrycache-%s", nc_data.bv_val);
422 cldap_submit_gauge ("cache_size", typeinst,
423 atoll (olmbdb_data.bv_val), st);
424 ldap_value_free_len (olmbdb_list);
427 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
428 "olmBDBDNCache")) != NULL)
430 olmbdb_data = *olmbdb_list[0];
431 ssnprintf (typeinst, sizeof (typeinst),
432 "bdbdncache-%s", nc_data.bv_val);
433 cldap_submit_gauge ("cache_size", typeinst,
434 atoll (olmbdb_data.bv_val), st);
435 ldap_value_free_len (olmbdb_list);
438 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
439 "olmBDBIDLCache")) != NULL)
441 olmbdb_data = *olmbdb_list[0];
442 ssnprintf (typeinst, sizeof (typeinst),
443 "bdbidlcache-%s", nc_data.bv_val);
444 cldap_submit_gauge ("cache_size", typeinst,
445 atoll (olmbdb_data.bv_val), st);
446 ldap_value_free_len (olmbdb_list);
449 ldap_value_free_len (nc_list);
452 "cn=Bytes,cn=Statistics,cn=Monitor")
455 cldap_submit_derive ("derive", "statistics-bytes",
459 "cn=PDU,cn=Statistics,cn=Monitor")
462 cldap_submit_derive ("derive", "statistics-pdu",
466 "cn=Entries,cn=Statistics,cn=Monitor")
469 cldap_submit_derive ("derive", "statistics-entries",
473 "cn=Referrals,cn=Statistics,cn=Monitor")
476 cldap_submit_derive ("derive", "statistics-referrals",
480 "cn=Open,cn=Threads,cn=Monitor")
483 cldap_submit_gauge ("threads", "threads-open",
487 "cn=Starting,cn=Threads,cn=Monitor")
490 cldap_submit_gauge ("threads", "threads-starting",
494 "cn=Active,cn=Threads,cn=Monitor")
497 cldap_submit_gauge ("threads", "threads-active",
501 "cn=Pending,cn=Threads,cn=Monitor")
504 cldap_submit_gauge ("threads", "threads-pending",
508 "cn=Backload,cn=Threads,cn=Monitor")
511 cldap_submit_gauge ("threads", "threads-backload",
515 "cn=Read,cn=Waiters,cn=Monitor")
518 cldap_submit_derive ("derive", "waiters-read",
522 "cn=Write,cn=Waiters,cn=Monitor")
525 cldap_submit_derive ("derive", "waiters-write",
529 ldap_value_free_len (counter_list);
530 ldap_value_free_len (opc_list);
531 ldap_value_free_len (opi_list);
532 ldap_value_free_len (info_list);
538 ldap_msgfree (result);
540 } /* }}} int cldap_read_host */
542 /* Configuration handling functions {{{
545 * <Instance "plugin_instance1">
546 * URL "ldap://localhost"
552 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
558 st = malloc (sizeof (*st));
561 ERROR ("openldap plugin: malloc failed.");
564 memset (st, 0, sizeof (*st));
566 status = cf_util_get_string (ci, &st->name);
576 st->version = LDAP_VERSION3;
578 for (i = 0; i < ci->children_num; i++)
580 oconfig_item_t *child = ci->children + i;
582 if (strcasecmp ("BindDN", child->key) == 0)
583 status = cf_util_get_string (child, &st->binddn);
584 else if (strcasecmp ("Password", child->key) == 0)
585 status = cf_util_get_string (child, &st->password);
586 else if (strcasecmp ("CACert", child->key) == 0)
587 status = cf_util_get_string (child, &st->cacert);
588 else if (strcasecmp ("StartTLS", child->key) == 0)
589 status = cf_util_get_boolean (child, &st->starttls);
590 else if (strcasecmp ("Timeout", child->key) == 0)
591 status = cf_util_get_int (child, &st->timeout);
592 else if (strcasecmp ("URL", child->key) == 0)
593 status = cf_util_get_string (child, &st->url);
594 else if (strcasecmp ("VerifyHost", child->key) == 0)
595 status = cf_util_get_boolean (child, &st->verifyhost);
596 else if (strcasecmp ("Version", child->key) == 0)
597 status = cf_util_get_int (child, &st->version);
600 WARNING ("openldap plugin: Option `%s' not allowed here.",
609 /* Check if struct is complete.. */
610 if ((status == 0) && (st->url == NULL))
612 ERROR ("openldap plugin: Instance `%s': "
613 "No URL has been configured.",
618 /* Check if URL is valid */
619 if ((status == 0) && (st->url != NULL))
624 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
626 ERROR ("openldap plugin: Instance `%s': "
632 if ((status == 0) && (ludpp->lud_host != NULL))
634 st->host = strdup (ludpp->lud_host);
637 ldap_free_urldesc (ludpp);
643 char callback_name[3*DATA_MAX_NAME_LEN];
645 memset (&ud, 0, sizeof (ud));
648 memset (callback_name, 0, sizeof (callback_name));
649 ssnprintf (callback_name, sizeof (callback_name),
651 (st->host != NULL) ? st->host : hostname_g,
652 (st->name != NULL) ? st->name : "default"),
654 status = plugin_register_complex_read (/* group = */ NULL,
655 /* name = */ callback_name,
656 /* callback = */ cldap_read_host,
658 /* user_data = */ &ud);
668 } /* }}} int cldap_config_add */
670 static int cldap_config (oconfig_item_t *ci) /* {{{ */
675 for (i = 0; i < ci->children_num; i++)
677 oconfig_item_t *child = ci->children + i;
679 if (strcasecmp ("Instance", child->key) == 0)
680 cldap_config_add (child);
682 WARNING ("openldap plugin: The configuration option "
683 "\"%s\" is not allowed here. Did you "
684 "forget to add an <Instance /> block "
685 "around the configuration?",
687 } /* for (ci->children) */
690 } /* }}} int cldap_config */
692 /* }}} End of configuration handling functions */
694 static int cldap_init (void) /* {{{ */
696 /* Initialize LDAP library while still single-threaded as recommended in
697 * ldap_initialize(3) */
699 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
701 } /* }}} int cldap_init */
703 void module_register (void) /* {{{ */
705 plugin_register_complex_config ("openldap", cldap_config);
706 plugin_register_init ("openldap", cldap_init);
707 } /* }}} void module_register */