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>
34 #if defined(__APPLE__)
35 #pragma clang diagnostic push
36 #pragma clang diagnostic warning "-Wdeprecated-declarations"
42 struct cldap_s /* {{{ */
59 typedef struct cldap_s cldap_t; /* }}} */
61 static cldap_t **databases = NULL;
62 static size_t databases_num = 0;
64 static void cldap_free(cldap_t *st) /* {{{ */
78 } /* }}} void cldap_free */
80 /* initialize ldap for each host */
81 static int cldap_init_host(cldap_t *st) /* {{{ */
86 if (st->state && st->ld) {
87 DEBUG("openldap plugin: Already connected to %s", st->url);
91 rc = ldap_initialize(&ld, st->url);
92 if (rc != LDAP_SUCCESS) {
93 ERROR("openldap plugin: ldap_initialize failed: %s", ldap_err2string(rc));
96 ldap_unbind_ext_s(ld, NULL, NULL);
102 ldap_set_option(st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
104 ldap_set_option(st->ld, LDAP_OPT_TIMEOUT,
105 &(const struct timeval){st->timeout, 0});
107 ldap_set_option(st->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
109 if (st->cacert != NULL)
110 ldap_set_option(st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
112 if (st->verifyhost == 0) {
113 int never = LDAP_OPT_X_TLS_NEVER;
114 ldap_set_option(st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
117 if (st->starttls != 0) {
118 rc = ldap_start_tls_s(ld, NULL, NULL);
119 if (rc != LDAP_SUCCESS) {
120 ERROR("openldap plugin: Failed to start tls on %s: %s", st->url,
121 ldap_err2string(rc));
124 ldap_unbind_ext_s(st->ld, NULL, NULL);
130 if (st->password != NULL) {
131 cred.bv_val = st->password;
132 cred.bv_len = strlen(st->password);
138 rc = ldap_sasl_bind_s(st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred, NULL, NULL,
140 if (rc != LDAP_SUCCESS) {
141 ERROR("openldap plugin: Failed to bind to %s: %s", st->url,
142 ldap_err2string(rc));
145 ldap_unbind_ext_s(st->ld, NULL, NULL);
148 DEBUG("openldap plugin: Successfully connected to %s", st->url);
152 } /* }}} static cldap_init_host */
154 static void cldap_submit_value(const char *type,
155 const char *type_instance, /* {{{ */
156 value_t value, cldap_t *st) {
157 value_list_t vl = VALUE_LIST_INIT;
162 if ((st->host == NULL) || (strcmp("", st->host) == 0) ||
163 (strcmp("localhost", st->host) == 0))
164 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
166 sstrncpy(vl.host, st->host, sizeof(vl.host));
168 sstrncpy(vl.plugin, "openldap", sizeof(vl.plugin));
169 if (st->name != NULL)
170 sstrncpy(vl.plugin_instance, st->name, sizeof(vl.plugin_instance));
172 sstrncpy(vl.type, type, sizeof(vl.type));
173 if (type_instance != NULL)
174 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
176 plugin_dispatch_values(&vl);
177 } /* }}} void cldap_submit_value */
179 static void cldap_submit_derive(const char *type,
180 const char *type_instance, /* {{{ */
181 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,
188 const char *type_instance, /* {{{ */
189 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) /* {{{ */
204 "monitorCounter", "monitorOpCompleted", "monitorOpInitiated",
205 "monitoredInfo", "olmBDBEntryCache", "olmBDBDNCache",
206 "olmBDBIDLCache", "namingContexts", NULL};
208 if ((ud == NULL) || (ud->data == NULL)) {
209 ERROR("openldap plugin: cldap_read_host: Invalid user data.");
213 st = (cldap_t *)ud->data;
215 status = cldap_init_host(st);
219 rc = ldap_search_ext_s(st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
220 "(|(!(cn=* *))(cn=Database*))", attrs, 0, NULL, NULL,
223 if (rc != LDAP_SUCCESS) {
224 ERROR("openldap plugin: Failed to execute search: %s", ldap_err2string(rc));
225 ldap_msgfree(result);
228 ldap_unbind_ext_s(st->ld, NULL, NULL);
232 for (LDAPMessage *e = ldap_first_entry(st->ld, result); e != NULL;
233 e = ldap_next_entry(st->ld, e)) {
234 if ((dn = ldap_get_dn(st->ld, e)) != NULL) {
235 unsigned long long counter = 0;
236 unsigned long long opc = 0;
237 unsigned long long opi = 0;
238 unsigned long long info = 0;
240 struct berval counter_data;
241 struct berval opc_data;
242 struct berval opi_data;
243 struct berval info_data;
244 struct berval olmbdb_data;
245 struct berval nc_data;
247 struct berval **counter_list;
248 struct berval **opc_list;
249 struct berval **opi_list;
250 struct berval **info_list;
251 struct berval **olmbdb_list;
252 struct berval **nc_list;
254 if ((counter_list = ldap_get_values_len(st->ld, e, "monitorCounter")) !=
256 counter_data = *counter_list[0];
257 counter = atoll(counter_data.bv_val);
260 if ((opc_list = ldap_get_values_len(st->ld, e, "monitorOpCompleted")) !=
262 opc_data = *opc_list[0];
263 opc = atoll(opc_data.bv_val);
266 if ((opi_list = ldap_get_values_len(st->ld, e, "monitorOpInitiated")) !=
268 opi_data = *opi_list[0];
269 opi = atoll(opi_data.bv_val);
272 if ((info_list = ldap_get_values_len(st->ld, e, "monitoredInfo")) !=
274 info_data = *info_list[0];
275 info = atoll(info_data.bv_val);
278 if (strcmp(dn, "cn=Total,cn=Connections,cn=Monitor") == 0) {
279 cldap_submit_derive("total_connections", NULL, counter, st);
280 } else if (strcmp(dn, "cn=Current,cn=Connections,cn=Monitor") == 0) {
281 cldap_submit_gauge("current_connections", NULL, counter, st);
282 } else if (strcmp(dn, "cn=Operations,cn=Monitor") == 0) {
283 cldap_submit_derive("operations", "completed", opc, st);
284 cldap_submit_derive("operations", "initiated", opi, st);
285 } else if (strcmp(dn, "cn=Bind,cn=Operations,cn=Monitor") == 0) {
286 cldap_submit_derive("operations", "bind-completed", opc, st);
287 cldap_submit_derive("operations", "bind-initiated", opi, st);
288 } else if (strcmp(dn, "cn=UnBind,cn=Operations,cn=Monitor") == 0) {
289 cldap_submit_derive("operations", "unbind-completed", opc, st);
290 cldap_submit_derive("operations", "unbind-initiated", opi, st);
291 } else if (strcmp(dn, "cn=Search,cn=Operations,cn=Monitor") == 0) {
292 cldap_submit_derive("operations", "search-completed", opc, st);
293 cldap_submit_derive("operations", "search-initiated", opi, st);
294 } else if (strcmp(dn, "cn=Compare,cn=Operations,cn=Monitor") == 0) {
295 cldap_submit_derive("operations", "compare-completed", opc, st);
296 cldap_submit_derive("operations", "compare-initiated", opi, st);
297 } else if (strcmp(dn, "cn=Modify,cn=Operations,cn=Monitor") == 0) {
298 cldap_submit_derive("operations", "modify-completed", opc, st);
299 cldap_submit_derive("operations", "modify-initiated", opi, st);
300 } else if (strcmp(dn, "cn=Modrdn,cn=Operations,cn=Monitor") == 0) {
301 cldap_submit_derive("operations", "modrdn-completed", opc, st);
302 cldap_submit_derive("operations", "modrdn-initiated", opi, st);
303 } else if (strcmp(dn, "cn=Add,cn=Operations,cn=Monitor") == 0) {
304 cldap_submit_derive("operations", "add-completed", opc, st);
305 cldap_submit_derive("operations", "add-initiated", opi, st);
306 } else if (strcmp(dn, "cn=Delete,cn=Operations,cn=Monitor") == 0) {
307 cldap_submit_derive("operations", "delete-completed", opc, st);
308 cldap_submit_derive("operations", "delete-initiated", opi, st);
309 } else if (strcmp(dn, "cn=Abandon,cn=Operations,cn=Monitor") == 0) {
310 cldap_submit_derive("operations", "abandon-completed", opc, st);
311 cldap_submit_derive("operations", "abandon-initiated", opi, st);
312 } else if (strcmp(dn, "cn=Extended,cn=Operations,cn=Monitor") == 0) {
313 cldap_submit_derive("operations", "extended-completed", opc, st);
314 cldap_submit_derive("operations", "extended-initiated", opi, st);
315 } else if ((strncmp(dn, "cn=Database", 11) == 0) &&
316 ((nc_list = ldap_get_values_len(st->ld, e,
317 "namingContexts")) != NULL)) {
318 nc_data = *nc_list[0];
319 char typeinst[DATA_MAX_NAME_LEN];
322 ldap_get_values_len(st->ld, e, "olmBDBEntryCache")) != NULL) {
323 olmbdb_data = *olmbdb_list[0];
324 ssnprintf(typeinst, sizeof(typeinst), "bdbentrycache-%s",
326 cldap_submit_gauge("cache_size", typeinst, atoll(olmbdb_data.bv_val),
328 ldap_value_free_len(olmbdb_list);
331 if ((olmbdb_list = ldap_get_values_len(st->ld, e, "olmBDBDNCache")) !=
333 olmbdb_data = *olmbdb_list[0];
334 ssnprintf(typeinst, sizeof(typeinst), "bdbdncache-%s",
336 cldap_submit_gauge("cache_size", typeinst, atoll(olmbdb_data.bv_val),
338 ldap_value_free_len(olmbdb_list);
341 if ((olmbdb_list = ldap_get_values_len(st->ld, e, "olmBDBIDLCache")) !=
343 olmbdb_data = *olmbdb_list[0];
344 ssnprintf(typeinst, sizeof(typeinst), "bdbidlcache-%s",
346 cldap_submit_gauge("cache_size", typeinst, atoll(olmbdb_data.bv_val),
348 ldap_value_free_len(olmbdb_list);
351 ldap_value_free_len(nc_list);
352 } else if (strcmp(dn, "cn=Bytes,cn=Statistics,cn=Monitor") == 0) {
353 cldap_submit_derive("derive", "statistics-bytes", counter, st);
354 } else if (strcmp(dn, "cn=PDU,cn=Statistics,cn=Monitor") == 0) {
355 cldap_submit_derive("derive", "statistics-pdu", counter, st);
356 } else if (strcmp(dn, "cn=Entries,cn=Statistics,cn=Monitor") == 0) {
357 cldap_submit_derive("derive", "statistics-entries", counter, st);
358 } else if (strcmp(dn, "cn=Referrals,cn=Statistics,cn=Monitor") == 0) {
359 cldap_submit_derive("derive", "statistics-referrals", counter, st);
360 } else if (strcmp(dn, "cn=Open,cn=Threads,cn=Monitor") == 0) {
361 cldap_submit_gauge("threads", "threads-open", info, st);
362 } else if (strcmp(dn, "cn=Starting,cn=Threads,cn=Monitor") == 0) {
363 cldap_submit_gauge("threads", "threads-starting", info, st);
364 } else if (strcmp(dn, "cn=Active,cn=Threads,cn=Monitor") == 0) {
365 cldap_submit_gauge("threads", "threads-active", info, st);
366 } else if (strcmp(dn, "cn=Pending,cn=Threads,cn=Monitor") == 0) {
367 cldap_submit_gauge("threads", "threads-pending", info, st);
368 } else if (strcmp(dn, "cn=Backload,cn=Threads,cn=Monitor") == 0) {
369 cldap_submit_gauge("threads", "threads-backload", info, st);
370 } else if (strcmp(dn, "cn=Read,cn=Waiters,cn=Monitor") == 0) {
371 cldap_submit_derive("derive", "waiters-read", counter, st);
372 } else if (strcmp(dn, "cn=Write,cn=Waiters,cn=Monitor") == 0) {
373 cldap_submit_derive("derive", "waiters-write", counter, st);
376 ldap_value_free_len(counter_list);
377 ldap_value_free_len(opc_list);
378 ldap_value_free_len(opi_list);
379 ldap_value_free_len(info_list);
385 ldap_msgfree(result);
387 } /* }}} int cldap_read_host */
389 /* Configuration handling functions {{{
392 * <Instance "plugin_instance1">
393 * URL "ldap://localhost"
399 static int cldap_config_add(oconfig_item_t *ci) /* {{{ */
404 st = calloc(1, sizeof(*st));
406 ERROR("openldap plugin: calloc failed.");
410 status = cf_util_get_string(ci, &st->name);
417 st->timeout = (long)(CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
419 st->version = LDAP_VERSION3;
421 for (int i = 0; i < ci->children_num; i++) {
422 oconfig_item_t *child = ci->children + i;
424 if (strcasecmp("BindDN", child->key) == 0)
425 status = cf_util_get_string(child, &st->binddn);
426 else if (strcasecmp("Password", child->key) == 0)
427 status = cf_util_get_string(child, &st->password);
428 else if (strcasecmp("CACert", child->key) == 0)
429 status = cf_util_get_string(child, &st->cacert);
430 else if (strcasecmp("StartTLS", child->key) == 0)
431 status = cf_util_get_boolean(child, &st->starttls);
432 else if (strcasecmp("Timeout", child->key) == 0)
433 status = cf_util_get_int(child, &st->timeout);
434 else if (strcasecmp("URL", child->key) == 0)
435 status = cf_util_get_string(child, &st->url);
436 else if (strcasecmp("VerifyHost", child->key) == 0)
437 status = cf_util_get_boolean(child, &st->verifyhost);
438 else if (strcasecmp("Version", child->key) == 0)
439 status = cf_util_get_int(child, &st->version);
441 WARNING("openldap plugin: Option `%s' not allowed here.", child->key);
449 /* Check if struct is complete.. */
450 if ((status == 0) && (st->url == NULL)) {
451 ERROR("openldap plugin: Instance `%s': "
452 "No URL has been configured.",
457 /* Check if URL is valid */
458 if ((status == 0) && (st->url != NULL)) {
461 if (ldap_url_parse(st->url, &ludpp) != 0) {
462 ERROR("openldap plugin: Instance `%s': "
468 if ((status == 0) && (ludpp->lud_host != NULL))
469 st->host = strdup(ludpp->lud_host);
471 ldap_free_urldesc(ludpp);
477 temp = (cldap_t **)realloc(databases,
478 sizeof(*databases) * (databases_num + 1));
481 ERROR("openldap plugin: realloc failed");
484 char callback_name[3 * DATA_MAX_NAME_LEN] = {0};
487 databases[databases_num] = st;
490 ssnprintf(callback_name, sizeof(callback_name), "openldap/%s/%s",
491 (st->host != NULL) ? st->host : hostname_g,
492 (st->name != NULL) ? st->name : "default");
494 user_data_t ud = {.data = st};
496 status = plugin_register_complex_read(/* group = */ NULL,
497 /* name = */ callback_name,
498 /* callback = */ cldap_read_host,
500 /* user_data = */ &ud);
510 } /* }}} int cldap_config_add */
512 static int cldap_config(oconfig_item_t *ci) /* {{{ */
516 for (int i = 0; i < ci->children_num; i++) {
517 oconfig_item_t *child = ci->children + i;
519 if (strcasecmp("Instance", child->key) == 0)
520 cldap_config_add(child);
522 WARNING("openldap plugin: The configuration option "
523 "\"%s\" is not allowed here. Did you "
524 "forget to add an <Instance /> block "
525 "around the configuration?",
527 } /* for (ci->children) */
530 } /* }}} int cldap_config */
532 /* }}} End of configuration handling functions */
534 static int cldap_init(void) /* {{{ */
536 /* Initialize LDAP library while still single-threaded as recommended in
537 * ldap_initialize(3) */
539 ldap_get_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
541 } /* }}} int cldap_init */
543 static int cldap_shutdown(void) /* {{{ */
545 for (size_t i = 0; i < databases_num; i++)
546 if (databases[i]->ld != NULL)
547 ldap_unbind_ext_s(databases[i]->ld, NULL, NULL);
552 } /* }}} int cldap_shutdown */
554 void module_register(void) /* {{{ */
556 plugin_register_complex_config("openldap", cldap_config);
557 plugin_register_init("openldap", cldap_init);
558 plugin_register_shutdown("openldap", cldap_shutdown);
559 } /* }}} void module_register */
561 #if defined(__APPLE__)
562 #pragma clang diagnostic pop