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"
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 void cldap_free (cldap_t *st) /* {{{ */
73 ldap_memfree (st->ld);
75 } /* }}} void cldap_free */
77 /* initialize ldap for each host */
78 static int cldap_init_host (cldap_t *st) /* {{{ */
82 rc = ldap_initialize (&ld, st->url);
83 if (rc != LDAP_SUCCESS)
85 ERROR ("openldap plugin: ldap_initialize failed: %s",
86 ldap_err2string (rc));
88 ldap_unbind_ext_s (ld, NULL, NULL);
94 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
96 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
97 &(const struct timeval){st->timeout, 0});
99 if (st->cacert != NULL)
100 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
102 if (st->verifyhost == 0)
104 int never = LDAP_OPT_X_TLS_NEVER;
105 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
108 if (st->starttls != 0)
110 rc = ldap_start_tls_s (ld, NULL, NULL);
111 if (rc != LDAP_SUCCESS)
113 ERROR ("openldap plugin: Failed to start tls on %s: %s",
114 st->url, ldap_err2string (rc));
116 ldap_unbind_ext_s (st->ld, NULL, NULL);
122 if (st->password != NULL)
124 cred.bv_val = st->password;
125 cred.bv_len = strlen (st->password);
133 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
135 if (rc != LDAP_SUCCESS)
137 ERROR ("openldap plugin: Failed to bind to %s: %s",
138 st->url, ldap_err2string (rc));
140 ldap_unbind_ext_s (st->ld, NULL, NULL);
145 DEBUG ("openldap plugin: Successfully connected to %s",
150 } /* }}} static cldap_init_host */
152 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
153 value_t value, cldap_t *st)
155 value_list_t vl = VALUE_LIST_INIT;
160 if ((st->host == NULL)
161 || (strcmp ("", st->host) == 0)
162 || (strcmp ("localhost", st->host) == 0))
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
168 sstrncpy (vl.host, st->host, sizeof (vl.host));
171 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
172 if (st->name != NULL)
173 sstrncpy (vl.plugin_instance, st->name,
174 sizeof (vl.plugin_instance));
176 sstrncpy (vl.type, type, sizeof (vl.type));
177 if (type_instance != NULL)
178 sstrncpy (vl.type_instance, type_instance,
179 sizeof (vl.type_instance));
181 plugin_dispatch_values (&vl);
182 } /* }}} void cldap_submit_value */
184 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
185 derive_t d, cldap_t *st)
189 cldap_submit_value (type, type_instance, v, st);
190 } /* }}} void cldap_submit_derive */
192 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
193 gauge_t g, cldap_t *st)
197 cldap_submit_value (type, type_instance, v, st);
198 } /* }}} void cldap_submit_gauge */
200 static int cldap_read_host (user_data_t *ud) /* {{{ */
203 LDAPMessage *e, *result;
208 char *attrs[9] = { "monitorCounter",
209 "monitorOpCompleted",
210 "monitorOpInitiated",
218 if ((ud == NULL) || (ud->data == NULL))
220 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
224 st = (cldap_t *) ud->data;
226 status = cldap_init_host (st);
230 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
231 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
232 NULL, NULL, NULL, 0, &result);
234 if (rc != LDAP_SUCCESS)
236 ERROR ("openldap plugin: Failed to execute search: %s",
237 ldap_err2string (rc));
238 ldap_msgfree (result);
239 ldap_unbind_ext_s (st->ld, NULL, NULL);
243 for (e = ldap_first_entry (st->ld, result); e != NULL;
244 e = ldap_next_entry (st->ld, e))
246 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
248 unsigned long long counter = 0;
249 unsigned long long opc = 0;
250 unsigned long long opi = 0;
251 unsigned long long info = 0;
253 struct berval counter_data;
254 struct berval opc_data;
255 struct berval opi_data;
256 struct berval info_data;
257 struct berval olmbdb_data;
258 struct berval nc_data;
260 struct berval **counter_list;
261 struct berval **opc_list;
262 struct berval **opi_list;
263 struct berval **info_list;
264 struct berval **olmbdb_list;
265 struct berval **nc_list;
267 if ((counter_list = ldap_get_values_len (st->ld, e,
268 "monitorCounter")) != NULL)
270 counter_data = *counter_list[0];
271 counter = atoll (counter_data.bv_val);
274 if ((opc_list = ldap_get_values_len (st->ld, e,
275 "monitorOpCompleted")) != NULL)
277 opc_data = *opc_list[0];
278 opc = atoll (opc_data.bv_val);
281 if ((opi_list = ldap_get_values_len (st->ld, e,
282 "monitorOpInitiated")) != NULL)
284 opi_data = *opi_list[0];
285 opi = atoll (opi_data.bv_val);
288 if ((info_list = ldap_get_values_len (st->ld, e,
289 "monitoredInfo")) != NULL)
291 info_data = *info_list[0];
292 info = atoll (info_data.bv_val);
295 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
298 cldap_submit_derive ("total_connections", NULL,
302 "cn=Current,cn=Connections,cn=Monitor")
305 cldap_submit_gauge ("current_connections", NULL,
309 "cn=Operations,cn=Monitor") == 0)
311 cldap_submit_derive ("operations",
312 "completed", opc, st);
313 cldap_submit_derive ("operations",
314 "initiated", opi, st);
317 "cn=Bind,cn=Operations,cn=Monitor")
320 cldap_submit_derive ("operations",
321 "bind-completed", opc, st);
322 cldap_submit_derive ("operations",
323 "bind-initiated", opi, st);
326 "cn=UnBind,cn=Operations,cn=Monitor")
329 cldap_submit_derive ("operations",
330 "unbind-completed", opc, st);
331 cldap_submit_derive ("operations",
332 "unbind-initiated", opi, st);
335 "cn=Search,cn=Operations,cn=Monitor")
338 cldap_submit_derive ("operations",
339 "search-completed", opc, st);
340 cldap_submit_derive ("operations",
341 "search-initiated", opi, st);
344 "cn=Compare,cn=Operations,cn=Monitor")
347 cldap_submit_derive ("operations",
348 "compare-completed", opc, st);
349 cldap_submit_derive ("operations",
350 "compare-initiated", opi, st);
353 "cn=Modify,cn=Operations,cn=Monitor")
356 cldap_submit_derive ("operations",
357 "modify-completed", opc, st);
358 cldap_submit_derive ("operations",
359 "modify-initiated", opi, st);
362 "cn=Modrdn,cn=Operations,cn=Monitor")
365 cldap_submit_derive ("operations",
366 "modrdn-completed", opc, st);
367 cldap_submit_derive ("operations",
368 "modrdn-initiated", opi, st);
371 "cn=Add,cn=Operations,cn=Monitor")
374 cldap_submit_derive ("operations",
375 "add-completed", opc, st);
376 cldap_submit_derive ("operations",
377 "add-initiated", opi, st);
380 "cn=Delete,cn=Operations,cn=Monitor")
383 cldap_submit_derive ("operations",
384 "delete-completed", opc, st);
385 cldap_submit_derive ("operations",
386 "delete-initiated", opi, st);
389 "cn=Abandon,cn=Operations,cn=Monitor")
392 cldap_submit_derive ("operations",
393 "abandon-completed", opc, st);
394 cldap_submit_derive ("operations",
395 "abandon-initiated", opi, st);
398 "cn=Extended,cn=Operations,cn=Monitor")
401 cldap_submit_derive ("operations",
402 "extended-completed", opc, st);
403 cldap_submit_derive ("operations",
404 "extended-initiated", opi, st);
406 else if ((strncmp (dn, "cn=Database", 11) == 0)
407 && ((nc_list = ldap_get_values_len
408 (st->ld, e, "namingContexts")) != NULL))
410 nc_data = *nc_list[0];
411 char typeinst[DATA_MAX_NAME_LEN];
413 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
414 "olmBDBEntryCache")) != NULL)
416 olmbdb_data = *olmbdb_list[0];
417 ssnprintf (typeinst, sizeof (typeinst),
418 "bdbentrycache-%s", nc_data.bv_val);
419 cldap_submit_gauge ("cache_size", typeinst,
420 atoll (olmbdb_data.bv_val), st);
421 ldap_value_free_len (olmbdb_list);
424 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
425 "olmBDBDNCache")) != NULL)
427 olmbdb_data = *olmbdb_list[0];
428 ssnprintf (typeinst, sizeof (typeinst),
429 "bdbdncache-%s", nc_data.bv_val);
430 cldap_submit_gauge ("cache_size", typeinst,
431 atoll (olmbdb_data.bv_val), st);
432 ldap_value_free_len (olmbdb_list);
435 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
436 "olmBDBIDLCache")) != NULL)
438 olmbdb_data = *olmbdb_list[0];
439 ssnprintf (typeinst, sizeof (typeinst),
440 "bdbidlcache-%s", nc_data.bv_val);
441 cldap_submit_gauge ("cache_size", typeinst,
442 atoll (olmbdb_data.bv_val), st);
443 ldap_value_free_len (olmbdb_list);
446 ldap_value_free_len (nc_list);
449 "cn=Bytes,cn=Statistics,cn=Monitor")
452 cldap_submit_derive ("derive", "statistics-bytes",
456 "cn=PDU,cn=Statistics,cn=Monitor")
459 cldap_submit_derive ("derive", "statistics-pdu",
463 "cn=Entries,cn=Statistics,cn=Monitor")
466 cldap_submit_derive ("derive", "statistics-entries",
470 "cn=Referrals,cn=Statistics,cn=Monitor")
473 cldap_submit_derive ("derive", "statistics-referrals",
477 "cn=Open,cn=Threads,cn=Monitor")
480 cldap_submit_gauge ("threads", "threads-open",
484 "cn=Starting,cn=Threads,cn=Monitor")
487 cldap_submit_gauge ("threads", "threads-starting",
491 "cn=Active,cn=Threads,cn=Monitor")
494 cldap_submit_gauge ("threads", "threads-active",
498 "cn=Pending,cn=Threads,cn=Monitor")
501 cldap_submit_gauge ("threads", "threads-pending",
505 "cn=Backload,cn=Threads,cn=Monitor")
508 cldap_submit_gauge ("threads", "threads-backload",
512 "cn=Read,cn=Waiters,cn=Monitor")
515 cldap_submit_derive ("derive", "waiters-read",
519 "cn=Write,cn=Waiters,cn=Monitor")
522 cldap_submit_derive ("derive", "waiters-write",
526 ldap_value_free_len (counter_list);
527 ldap_value_free_len (opc_list);
528 ldap_value_free_len (opi_list);
529 ldap_value_free_len (info_list);
535 ldap_msgfree (result);
536 ldap_unbind_ext_s (st->ld, NULL, NULL);
538 } /* }}} int cldap_read_host */
540 /* Configuration handling functions {{{
543 * <Instance "plugin_instance1">
544 * URL "ldap://localhost"
550 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
556 st = malloc (sizeof (*st));
559 ERROR ("openldap plugin: malloc failed.");
562 memset (st, 0, sizeof (*st));
564 status = cf_util_get_string (ci, &st->name);
574 st->version = LDAP_VERSION3;
576 for (i = 0; i < ci->children_num; i++)
578 oconfig_item_t *child = ci->children + i;
580 if (strcasecmp ("BindDN", child->key) == 0)
581 status = cf_util_get_string (child, &st->binddn);
582 else if (strcasecmp ("Password", child->key) == 0)
583 status = cf_util_get_string (child, &st->password);
584 else if (strcasecmp ("CACert", child->key) == 0)
585 status = cf_util_get_string (child, &st->cacert);
586 else if (strcasecmp ("StartTLS", child->key) == 0)
587 status = cf_util_get_boolean (child, &st->starttls);
588 else if (strcasecmp ("Timeout", child->key) == 0)
589 status = cf_util_get_int (child, &st->timeout);
590 else if (strcasecmp ("URL", child->key) == 0)
591 status = cf_util_get_string (child, &st->url);
592 else if (strcasecmp ("VerifyHost", child->key) == 0)
593 status = cf_util_get_boolean (child, &st->verifyhost);
594 else if (strcasecmp ("Version", child->key) == 0)
595 status = cf_util_get_int (child, &st->version);
598 WARNING ("openldap plugin: Option `%s' not allowed here.",
607 /* Check if struct is complete.. */
608 if ((status == 0) && (st->url == NULL))
610 ERROR ("openldap plugin: Instance `%s': "
611 "No URL has been configured.",
616 /* Check if URL is valid */
617 if ((status == 0) && (st->url != NULL))
622 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
624 ERROR ("openldap plugin: Instance `%s': "
630 if ((status == 0) && (ludpp->lud_host != NULL))
632 st->host = strdup (ludpp->lud_host);
635 ldap_free_urldesc (ludpp);
641 char callback_name[3*DATA_MAX_NAME_LEN];
643 memset (&ud, 0, sizeof (ud));
646 memset (callback_name, 0, sizeof (callback_name));
647 ssnprintf (callback_name, sizeof (callback_name),
649 (st->host != NULL) ? st->host : hostname_g,
650 (st->name != NULL) ? st->name : "default"),
652 status = plugin_register_complex_read (/* group = */ NULL,
653 /* name = */ callback_name,
654 /* callback = */ cldap_read_host,
656 /* user_data = */ &ud);
666 } /* }}} int cldap_config_add */
668 static int cldap_config (oconfig_item_t *ci) /* {{{ */
673 for (i = 0; i < ci->children_num; i++)
675 oconfig_item_t *child = ci->children + i;
677 if (strcasecmp ("Instance", child->key) == 0)
678 cldap_config_add (child);
680 WARNING ("openldap plugin: The configuration option "
681 "\"%s\" is not allowed here. Did you "
682 "forget to add an <Instance /> block "
683 "around the configuration?",
685 } /* for (ci->children) */
688 } /* }}} int cldap_config */
690 /* }}} End of configuration handling functions */
692 static int cldap_init (void) /* {{{ */
694 /* Initialize LDAP library while still single-threaded as recommended in
695 * ldap_initialize(3) */
697 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
699 } /* }}} int cldap_init */
701 void module_register (void) /* {{{ */
703 plugin_register_complex_config ("openldap", cldap_config);
704 plugin_register_init ("openldap", cldap_init);
705 } /* }}} void module_register */
707 #if defined(__APPLE__)
708 #pragma clang diagnostic pop