2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
4 * Copyright (C) 2014-2015 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 cldap_t **databases = NULL;
57 static size_t databases_num = 0;
59 static void cldap_free (cldap_t *st) /* {{{ */
71 ldap_memfree (st->ld);
73 } /* }}} void cldap_free */
75 /* initialize ldap for each host */
76 static int cldap_init_host (cldap_t *st) /* {{{ */
81 if (st->state && st->ld)
83 DEBUG ("openldap plugin: Already connected to %s", st->url);
87 rc = ldap_initialize (&ld, st->url);
88 if (rc != LDAP_SUCCESS)
90 ERROR ("openldap plugin: ldap_initialize failed: %s",
91 ldap_err2string (rc));
93 ldap_unbind_ext_s (ld, NULL, NULL);
99 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
101 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
102 &(const struct timeval){st->timeout, 0});
104 ldap_set_option (st->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
106 if (st->cacert != NULL)
107 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
109 if (st->verifyhost == 0)
111 int never = LDAP_OPT_X_TLS_NEVER;
112 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
115 if (st->starttls != 0)
117 rc = ldap_start_tls_s (ld, NULL, NULL);
118 if (rc != LDAP_SUCCESS)
120 ERROR ("openldap plugin: Failed to start tls on %s: %s",
121 st->url, ldap_err2string (rc));
123 ldap_unbind_ext_s (st->ld, NULL, NULL);
129 if (st->password != NULL)
131 cred.bv_val = st->password;
132 cred.bv_len = strlen (st->password);
140 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
142 if (rc != LDAP_SUCCESS)
144 ERROR ("openldap plugin: Failed to bind to %s: %s",
145 st->url, ldap_err2string (rc));
147 ldap_unbind_ext_s (st->ld, NULL, NULL);
152 DEBUG ("openldap plugin: Successfully connected to %s",
157 } /* }}} static cldap_init_host */
159 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
160 value_t value, cldap_t *st)
162 value_list_t vl = VALUE_LIST_INIT;
167 if ((st->host == NULL)
168 || (strcmp ("", st->host) == 0)
169 || (strcmp ("localhost", st->host) == 0))
170 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
172 sstrncpy (vl.host, st->host, sizeof (vl.host));
174 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
175 if (st->name != NULL)
176 sstrncpy (vl.plugin_instance, st->name,
177 sizeof (vl.plugin_instance));
179 sstrncpy (vl.type, type, sizeof (vl.type));
180 if (type_instance != NULL)
181 sstrncpy (vl.type_instance, type_instance,
182 sizeof (vl.type_instance));
184 plugin_dispatch_values (&vl);
185 } /* }}} void cldap_submit_value */
187 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
188 derive_t d, cldap_t *st)
192 cldap_submit_value (type, type_instance, v, st);
193 } /* }}} void cldap_submit_derive */
195 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
196 gauge_t g, cldap_t *st)
200 cldap_submit_value (type, type_instance, v, st);
201 } /* }}} void cldap_submit_gauge */
203 static int cldap_read_host (user_data_t *ud) /* {{{ */
206 LDAPMessage *e, *result;
211 char *attrs[9] = { "monitorCounter",
212 "monitorOpCompleted",
213 "monitorOpInitiated",
221 if ((ud == NULL) || (ud->data == NULL))
223 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
227 st = (cldap_t *) ud->data;
229 status = cldap_init_host (st);
233 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
234 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
235 NULL, NULL, NULL, 0, &result);
237 if (rc != LDAP_SUCCESS)
239 ERROR ("openldap plugin: Failed to execute search: %s",
240 ldap_err2string (rc));
241 ldap_msgfree (result);
243 ldap_unbind_ext_s (st->ld, NULL, NULL);
247 for (e = ldap_first_entry (st->ld, result); e != NULL;
248 e = ldap_next_entry (st->ld, e))
250 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
252 unsigned long long counter = 0;
253 unsigned long long opc = 0;
254 unsigned long long opi = 0;
255 unsigned long long info = 0;
257 struct berval counter_data;
258 struct berval opc_data;
259 struct berval opi_data;
260 struct berval info_data;
261 struct berval olmbdb_data;
262 struct berval nc_data;
264 struct berval **counter_list;
265 struct berval **opc_list;
266 struct berval **opi_list;
267 struct berval **info_list;
268 struct berval **olmbdb_list;
269 struct berval **nc_list;
271 if ((counter_list = ldap_get_values_len (st->ld, e,
272 "monitorCounter")) != NULL)
274 counter_data = *counter_list[0];
275 counter = atoll (counter_data.bv_val);
278 if ((opc_list = ldap_get_values_len (st->ld, e,
279 "monitorOpCompleted")) != NULL)
281 opc_data = *opc_list[0];
282 opc = atoll (opc_data.bv_val);
285 if ((opi_list = ldap_get_values_len (st->ld, e,
286 "monitorOpInitiated")) != NULL)
288 opi_data = *opi_list[0];
289 opi = atoll (opi_data.bv_val);
292 if ((info_list = ldap_get_values_len (st->ld, e,
293 "monitoredInfo")) != NULL)
295 info_data = *info_list[0];
296 info = atoll (info_data.bv_val);
299 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
302 cldap_submit_derive ("total_connections", NULL,
306 "cn=Current,cn=Connections,cn=Monitor")
309 cldap_submit_gauge ("current_connections", NULL,
313 "cn=Operations,cn=Monitor") == 0)
315 cldap_submit_derive ("operations",
316 "completed", opc, st);
317 cldap_submit_derive ("operations",
318 "initiated", opi, st);
321 "cn=Bind,cn=Operations,cn=Monitor")
324 cldap_submit_derive ("operations",
325 "bind-completed", opc, st);
326 cldap_submit_derive ("operations",
327 "bind-initiated", opi, st);
330 "cn=UnBind,cn=Operations,cn=Monitor")
333 cldap_submit_derive ("operations",
334 "unbind-completed", opc, st);
335 cldap_submit_derive ("operations",
336 "unbind-initiated", opi, st);
339 "cn=Search,cn=Operations,cn=Monitor")
342 cldap_submit_derive ("operations",
343 "search-completed", opc, st);
344 cldap_submit_derive ("operations",
345 "search-initiated", opi, st);
348 "cn=Compare,cn=Operations,cn=Monitor")
351 cldap_submit_derive ("operations",
352 "compare-completed", opc, st);
353 cldap_submit_derive ("operations",
354 "compare-initiated", opi, st);
357 "cn=Modify,cn=Operations,cn=Monitor")
360 cldap_submit_derive ("operations",
361 "modify-completed", opc, st);
362 cldap_submit_derive ("operations",
363 "modify-initiated", opi, st);
366 "cn=Modrdn,cn=Operations,cn=Monitor")
369 cldap_submit_derive ("operations",
370 "modrdn-completed", opc, st);
371 cldap_submit_derive ("operations",
372 "modrdn-initiated", opi, st);
375 "cn=Add,cn=Operations,cn=Monitor")
378 cldap_submit_derive ("operations",
379 "add-completed", opc, st);
380 cldap_submit_derive ("operations",
381 "add-initiated", opi, st);
384 "cn=Delete,cn=Operations,cn=Monitor")
387 cldap_submit_derive ("operations",
388 "delete-completed", opc, st);
389 cldap_submit_derive ("operations",
390 "delete-initiated", opi, st);
393 "cn=Abandon,cn=Operations,cn=Monitor")
396 cldap_submit_derive ("operations",
397 "abandon-completed", opc, st);
398 cldap_submit_derive ("operations",
399 "abandon-initiated", opi, st);
402 "cn=Extended,cn=Operations,cn=Monitor")
405 cldap_submit_derive ("operations",
406 "extended-completed", opc, st);
407 cldap_submit_derive ("operations",
408 "extended-initiated", opi, st);
410 else if ((strncmp (dn, "cn=Database", 11) == 0)
411 && ((nc_list = ldap_get_values_len
412 (st->ld, e, "namingContexts")) != NULL))
414 nc_data = *nc_list[0];
415 char typeinst[DATA_MAX_NAME_LEN];
417 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
418 "olmBDBEntryCache")) != NULL)
420 olmbdb_data = *olmbdb_list[0];
421 ssnprintf (typeinst, sizeof (typeinst),
422 "bdbentrycache-%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 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
429 "olmBDBDNCache")) != NULL)
431 olmbdb_data = *olmbdb_list[0];
432 ssnprintf (typeinst, sizeof (typeinst),
433 "bdbdncache-%s", nc_data.bv_val);
434 cldap_submit_gauge ("cache_size", typeinst,
435 atoll (olmbdb_data.bv_val), st);
436 ldap_value_free_len (olmbdb_list);
439 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
440 "olmBDBIDLCache")) != NULL)
442 olmbdb_data = *olmbdb_list[0];
443 ssnprintf (typeinst, sizeof (typeinst),
444 "bdbidlcache-%s", nc_data.bv_val);
445 cldap_submit_gauge ("cache_size", typeinst,
446 atoll (olmbdb_data.bv_val), st);
447 ldap_value_free_len (olmbdb_list);
450 ldap_value_free_len (nc_list);
453 "cn=Bytes,cn=Statistics,cn=Monitor")
456 cldap_submit_derive ("derive", "statistics-bytes",
460 "cn=PDU,cn=Statistics,cn=Monitor")
463 cldap_submit_derive ("derive", "statistics-pdu",
467 "cn=Entries,cn=Statistics,cn=Monitor")
470 cldap_submit_derive ("derive", "statistics-entries",
474 "cn=Referrals,cn=Statistics,cn=Monitor")
477 cldap_submit_derive ("derive", "statistics-referrals",
481 "cn=Open,cn=Threads,cn=Monitor")
484 cldap_submit_gauge ("threads", "threads-open",
488 "cn=Starting,cn=Threads,cn=Monitor")
491 cldap_submit_gauge ("threads", "threads-starting",
495 "cn=Active,cn=Threads,cn=Monitor")
498 cldap_submit_gauge ("threads", "threads-active",
502 "cn=Pending,cn=Threads,cn=Monitor")
505 cldap_submit_gauge ("threads", "threads-pending",
509 "cn=Backload,cn=Threads,cn=Monitor")
512 cldap_submit_gauge ("threads", "threads-backload",
516 "cn=Read,cn=Waiters,cn=Monitor")
519 cldap_submit_derive ("derive", "waiters-read",
523 "cn=Write,cn=Waiters,cn=Monitor")
526 cldap_submit_derive ("derive", "waiters-write",
530 ldap_value_free_len (counter_list);
531 ldap_value_free_len (opc_list);
532 ldap_value_free_len (opi_list);
533 ldap_value_free_len (info_list);
539 ldap_msgfree (result);
541 } /* }}} int cldap_read_host */
543 /* Configuration handling functions {{{
546 * <Instance "plugin_instance1">
547 * URL "ldap://localhost"
553 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
559 st = malloc (sizeof (*st));
562 ERROR ("openldap plugin: malloc failed.");
565 memset (st, 0, sizeof (*st));
567 status = cf_util_get_string (ci, &st->name);
575 st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
577 st->version = LDAP_VERSION3;
579 for (i = 0; i < ci->children_num; i++)
581 oconfig_item_t *child = ci->children + i;
583 if (strcasecmp ("BindDN", child->key) == 0)
584 status = cf_util_get_string (child, &st->binddn);
585 else if (strcasecmp ("Password", child->key) == 0)
586 status = cf_util_get_string (child, &st->password);
587 else if (strcasecmp ("CACert", child->key) == 0)
588 status = cf_util_get_string (child, &st->cacert);
589 else if (strcasecmp ("StartTLS", child->key) == 0)
590 status = cf_util_get_boolean (child, &st->starttls);
591 else if (strcasecmp ("Timeout", child->key) == 0)
592 status = cf_util_get_int (child, &st->timeout);
593 else if (strcasecmp ("URL", child->key) == 0)
594 status = cf_util_get_string (child, &st->url);
595 else if (strcasecmp ("VerifyHost", child->key) == 0)
596 status = cf_util_get_boolean (child, &st->verifyhost);
597 else if (strcasecmp ("Version", child->key) == 0)
598 status = cf_util_get_int (child, &st->version);
601 WARNING ("openldap plugin: Option `%s' not allowed here.",
610 /* Check if struct is complete.. */
611 if ((status == 0) && (st->url == NULL))
613 ERROR ("openldap plugin: Instance `%s': "
614 "No URL has been configured.",
619 /* Check if URL is valid */
620 if ((status == 0) && (st->url != NULL))
625 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
627 ERROR ("openldap plugin: Instance `%s': "
633 if ((status == 0) && (ludpp->lud_host != NULL))
634 st->host = strdup (ludpp->lud_host);
636 ldap_free_urldesc (ludpp);
643 temp = (cldap_t **) realloc (databases,
644 sizeof (*databases) * (databases_num + 1));
648 ERROR ("openldap plugin: realloc failed");
654 char callback_name[3*DATA_MAX_NAME_LEN];
657 databases[databases_num] = st;
660 memset (&ud, 0, sizeof (ud));
663 memset (callback_name, 0, sizeof (callback_name));
664 ssnprintf (callback_name, sizeof (callback_name),
666 (st->host != NULL) ? st->host : hostname_g,
667 (st->name != NULL) ? st->name : "default"),
669 status = plugin_register_complex_read (/* group = */ NULL,
670 /* name = */ callback_name,
671 /* callback = */ cldap_read_host,
673 /* user_data = */ &ud);
684 } /* }}} int cldap_config_add */
686 static int cldap_config (oconfig_item_t *ci) /* {{{ */
691 for (i = 0; i < ci->children_num; i++)
693 oconfig_item_t *child = ci->children + i;
695 if (strcasecmp ("Instance", child->key) == 0)
696 cldap_config_add (child);
698 WARNING ("openldap plugin: The configuration option "
699 "\"%s\" is not allowed here. Did you "
700 "forget to add an <Instance /> block "
701 "around the configuration?",
703 } /* for (ci->children) */
706 } /* }}} int cldap_config */
708 /* }}} End of configuration handling functions */
710 static int cldap_init (void) /* {{{ */
712 /* Initialize LDAP library while still single-threaded as recommended in
713 * ldap_initialize(3) */
715 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
717 } /* }}} int cldap_init */
719 static int cldap_shutdown (void) /* {{{ */
723 for (i = 0; i < databases_num; i++)
724 if (databases[i]->ld != NULL)
725 ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
730 } /* }}} int cldap_shutdown */
732 void module_register (void) /* {{{ */
734 plugin_register_complex_config ("openldap", cldap_config);
735 plugin_register_init ("openldap", cldap_init);
736 plugin_register_shutdown ("openldap", cldap_shutdown);
737 } /* }}} void module_register */