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) /* {{{ */
76 ldap_memfree (st->ld);
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)
88 DEBUG ("openldap plugin: Already connected to %s", st->url);
92 rc = ldap_initialize (&ld, st->url);
93 if (rc != LDAP_SUCCESS)
95 ERROR ("openldap plugin: ldap_initialize failed: %s",
96 ldap_err2string (rc));
98 ldap_unbind_ext_s (ld, NULL, NULL);
104 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
106 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
107 &(const struct timeval){st->timeout, 0});
109 ldap_set_option (st->ld, LDAP_OPT_RESTART, LDAP_OPT_ON);
111 if (st->cacert != NULL)
112 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
114 if (st->verifyhost == 0)
116 int never = LDAP_OPT_X_TLS_NEVER;
117 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
120 if (st->starttls != 0)
122 rc = ldap_start_tls_s (ld, NULL, NULL);
123 if (rc != LDAP_SUCCESS)
125 ERROR ("openldap plugin: Failed to start tls on %s: %s",
126 st->url, ldap_err2string (rc));
128 ldap_unbind_ext_s (st->ld, NULL, NULL);
134 if (st->password != NULL)
136 cred.bv_val = st->password;
137 cred.bv_len = strlen (st->password);
145 rc = ldap_sasl_bind_s (st->ld, st->binddn, LDAP_SASL_SIMPLE, &cred,
147 if (rc != LDAP_SUCCESS)
149 ERROR ("openldap plugin: Failed to bind to %s: %s",
150 st->url, ldap_err2string (rc));
152 ldap_unbind_ext_s (st->ld, NULL, NULL);
157 DEBUG ("openldap plugin: Successfully connected to %s",
162 } /* }}} static cldap_init_host */
164 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
165 value_t value, cldap_t *st)
167 value_list_t vl = VALUE_LIST_INIT;
172 if ((st->host != NULL) && (strcmp ("localhost", st->host) != 0))
173 sstrncpy (vl.host, st->host, sizeof (vl.host));
175 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
176 if (st->name != NULL)
177 sstrncpy (vl.plugin_instance, st->name,
178 sizeof (vl.plugin_instance));
180 sstrncpy (vl.type, type, sizeof (vl.type));
181 if (type_instance != NULL)
182 sstrncpy (vl.type_instance, type_instance,
183 sizeof (vl.type_instance));
185 plugin_dispatch_values (&vl);
186 } /* }}} void cldap_submit_value */
188 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
189 derive_t d, cldap_t *st)
191 cldap_submit_value (type, type_instance, (value_t) { .derive = d }, 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)
197 cldap_submit_value (type, type_instance, (value_t) { .gauge = g }, st);
198 } /* }}} void cldap_submit_gauge */
200 static int cldap_read_host (user_data_t *ud) /* {{{ */
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);
240 ldap_unbind_ext_s (st->ld, NULL, NULL);
244 for (LDAPMessage *e = ldap_first_entry (st->ld, result); e != NULL;
245 e = ldap_next_entry (st->ld, e))
247 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
249 unsigned long long counter = 0;
250 unsigned long long opc = 0;
251 unsigned long long opi = 0;
252 unsigned long long info = 0;
254 struct berval counter_data;
255 struct berval opc_data;
256 struct berval opi_data;
257 struct berval info_data;
258 struct berval olmbdb_data;
259 struct berval nc_data;
261 struct berval **counter_list;
262 struct berval **opc_list;
263 struct berval **opi_list;
264 struct berval **info_list;
265 struct berval **olmbdb_list;
266 struct berval **nc_list;
268 if ((counter_list = ldap_get_values_len (st->ld, e,
269 "monitorCounter")) != NULL)
271 counter_data = *counter_list[0];
272 counter = atoll (counter_data.bv_val);
275 if ((opc_list = ldap_get_values_len (st->ld, e,
276 "monitorOpCompleted")) != NULL)
278 opc_data = *opc_list[0];
279 opc = atoll (opc_data.bv_val);
282 if ((opi_list = ldap_get_values_len (st->ld, e,
283 "monitorOpInitiated")) != NULL)
285 opi_data = *opi_list[0];
286 opi = atoll (opi_data.bv_val);
289 if ((info_list = ldap_get_values_len (st->ld, e,
290 "monitoredInfo")) != NULL)
292 info_data = *info_list[0];
293 info = atoll (info_data.bv_val);
296 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
299 cldap_submit_derive ("total_connections", NULL,
303 "cn=Current,cn=Connections,cn=Monitor")
306 cldap_submit_gauge ("current_connections", NULL,
310 "cn=Operations,cn=Monitor") == 0)
312 cldap_submit_derive ("operations",
313 "completed", opc, st);
314 cldap_submit_derive ("operations",
315 "initiated", opi, st);
318 "cn=Bind,cn=Operations,cn=Monitor")
321 cldap_submit_derive ("operations",
322 "bind-completed", opc, st);
323 cldap_submit_derive ("operations",
324 "bind-initiated", opi, st);
327 "cn=UnBind,cn=Operations,cn=Monitor")
330 cldap_submit_derive ("operations",
331 "unbind-completed", opc, st);
332 cldap_submit_derive ("operations",
333 "unbind-initiated", opi, st);
336 "cn=Search,cn=Operations,cn=Monitor")
339 cldap_submit_derive ("operations",
340 "search-completed", opc, st);
341 cldap_submit_derive ("operations",
342 "search-initiated", opi, st);
345 "cn=Compare,cn=Operations,cn=Monitor")
348 cldap_submit_derive ("operations",
349 "compare-completed", opc, st);
350 cldap_submit_derive ("operations",
351 "compare-initiated", opi, st);
354 "cn=Modify,cn=Operations,cn=Monitor")
357 cldap_submit_derive ("operations",
358 "modify-completed", opc, st);
359 cldap_submit_derive ("operations",
360 "modify-initiated", opi, st);
363 "cn=Modrdn,cn=Operations,cn=Monitor")
366 cldap_submit_derive ("operations",
367 "modrdn-completed", opc, st);
368 cldap_submit_derive ("operations",
369 "modrdn-initiated", opi, st);
372 "cn=Add,cn=Operations,cn=Monitor")
375 cldap_submit_derive ("operations",
376 "add-completed", opc, st);
377 cldap_submit_derive ("operations",
378 "add-initiated", opi, st);
381 "cn=Delete,cn=Operations,cn=Monitor")
384 cldap_submit_derive ("operations",
385 "delete-completed", opc, st);
386 cldap_submit_derive ("operations",
387 "delete-initiated", opi, st);
390 "cn=Abandon,cn=Operations,cn=Monitor")
393 cldap_submit_derive ("operations",
394 "abandon-completed", opc, st);
395 cldap_submit_derive ("operations",
396 "abandon-initiated", opi, st);
399 "cn=Extended,cn=Operations,cn=Monitor")
402 cldap_submit_derive ("operations",
403 "extended-completed", opc, st);
404 cldap_submit_derive ("operations",
405 "extended-initiated", opi, st);
407 else if ((strncmp (dn, "cn=Database", 11) == 0)
408 && ((nc_list = ldap_get_values_len
409 (st->ld, e, "namingContexts")) != NULL))
411 nc_data = *nc_list[0];
412 char typeinst[DATA_MAX_NAME_LEN];
414 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
415 "olmBDBEntryCache")) != NULL)
417 olmbdb_data = *olmbdb_list[0];
418 ssnprintf (typeinst, sizeof (typeinst),
419 "bdbentrycache-%s", nc_data.bv_val);
420 cldap_submit_gauge ("cache_size", typeinst,
421 atoll (olmbdb_data.bv_val), st);
422 ldap_value_free_len (olmbdb_list);
425 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
426 "olmBDBDNCache")) != NULL)
428 olmbdb_data = *olmbdb_list[0];
429 ssnprintf (typeinst, sizeof (typeinst),
430 "bdbdncache-%s", nc_data.bv_val);
431 cldap_submit_gauge ("cache_size", typeinst,
432 atoll (olmbdb_data.bv_val), st);
433 ldap_value_free_len (olmbdb_list);
436 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
437 "olmBDBIDLCache")) != NULL)
439 olmbdb_data = *olmbdb_list[0];
440 ssnprintf (typeinst, sizeof (typeinst),
441 "bdbidlcache-%s", nc_data.bv_val);
442 cldap_submit_gauge ("cache_size", typeinst,
443 atoll (olmbdb_data.bv_val), st);
444 ldap_value_free_len (olmbdb_list);
447 ldap_value_free_len (nc_list);
450 "cn=Bytes,cn=Statistics,cn=Monitor")
453 cldap_submit_derive ("derive", "statistics-bytes",
457 "cn=PDU,cn=Statistics,cn=Monitor")
460 cldap_submit_derive ("derive", "statistics-pdu",
464 "cn=Entries,cn=Statistics,cn=Monitor")
467 cldap_submit_derive ("derive", "statistics-entries",
471 "cn=Referrals,cn=Statistics,cn=Monitor")
474 cldap_submit_derive ("derive", "statistics-referrals",
478 "cn=Open,cn=Threads,cn=Monitor")
481 cldap_submit_gauge ("threads", "threads-open",
485 "cn=Starting,cn=Threads,cn=Monitor")
488 cldap_submit_gauge ("threads", "threads-starting",
492 "cn=Active,cn=Threads,cn=Monitor")
495 cldap_submit_gauge ("threads", "threads-active",
499 "cn=Pending,cn=Threads,cn=Monitor")
502 cldap_submit_gauge ("threads", "threads-pending",
506 "cn=Backload,cn=Threads,cn=Monitor")
509 cldap_submit_gauge ("threads", "threads-backload",
513 "cn=Read,cn=Waiters,cn=Monitor")
516 cldap_submit_derive ("derive", "waiters-read",
520 "cn=Write,cn=Waiters,cn=Monitor")
523 cldap_submit_derive ("derive", "waiters-write",
527 ldap_value_free_len (counter_list);
528 ldap_value_free_len (opc_list);
529 ldap_value_free_len (opi_list);
530 ldap_value_free_len (info_list);
536 ldap_msgfree (result);
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) /* {{{ */
555 st = calloc (1, sizeof (*st));
558 ERROR ("openldap plugin: calloc failed.");
562 status = cf_util_get_string (ci, &st->name);
570 st->timeout = (long) CDTIME_T_TO_TIME_T(plugin_get_interval());
572 st->version = LDAP_VERSION3;
574 for (int i = 0; i < ci->children_num; i++)
576 oconfig_item_t *child = ci->children + i;
578 if (strcasecmp ("BindDN", child->key) == 0)
579 status = cf_util_get_string (child, &st->binddn);
580 else if (strcasecmp ("Password", child->key) == 0)
581 status = cf_util_get_string (child, &st->password);
582 else if (strcasecmp ("CACert", child->key) == 0)
583 status = cf_util_get_string (child, &st->cacert);
584 else if (strcasecmp ("StartTLS", child->key) == 0)
585 status = cf_util_get_boolean (child, &st->starttls);
586 else if (strcasecmp ("Timeout", child->key) == 0)
587 status = cf_util_get_int (child, &st->timeout);
588 else if (strcasecmp ("URL", child->key) == 0)
589 status = cf_util_get_string (child, &st->url);
590 else if (strcasecmp ("VerifyHost", child->key) == 0)
591 status = cf_util_get_boolean (child, &st->verifyhost);
592 else if (strcasecmp ("Version", child->key) == 0)
593 status = cf_util_get_int (child, &st->version);
596 WARNING ("openldap plugin: Option `%s' not allowed here.",
605 /* Check if struct is complete.. */
606 if ((status == 0) && (st->url == NULL))
608 ERROR ("openldap plugin: Instance `%s': "
609 "No URL has been configured.",
614 /* Check if URL is valid */
615 if ((status == 0) && (st->url != NULL))
619 if (ldap_url_parse (st->url, &ludpp) != 0)
621 ERROR ("openldap plugin: Instance `%s': "
627 if ((status == 0) && (ludpp->lud_host != NULL))
628 st->host = strdup (ludpp->lud_host);
630 ldap_free_urldesc (ludpp);
637 temp = (cldap_t **) realloc (databases,
638 sizeof (*databases) * (databases_num + 1));
642 ERROR ("openldap plugin: realloc failed");
647 char callback_name[3*DATA_MAX_NAME_LEN] = { 0 };
650 databases[databases_num] = st;
653 ssnprintf (callback_name, sizeof (callback_name),
655 (st->host != NULL) ? st->host : hostname_g,
656 (st->name != NULL) ? st->name : "default");
658 status = plugin_register_complex_read (/* group = */ NULL,
659 /* name = */ callback_name,
660 /* callback = */ cldap_read_host,
675 } /* }}} int cldap_config_add */
677 static int cldap_config (oconfig_item_t *ci) /* {{{ */
681 for (int i = 0; i < ci->children_num; i++)
683 oconfig_item_t *child = ci->children + i;
685 if (strcasecmp ("Instance", child->key) == 0)
686 cldap_config_add (child);
688 WARNING ("openldap plugin: The configuration option "
689 "\"%s\" is not allowed here. Did you "
690 "forget to add an <Instance /> block "
691 "around the configuration?",
693 } /* for (ci->children) */
696 } /* }}} int cldap_config */
698 /* }}} End of configuration handling functions */
700 static int cldap_init (void) /* {{{ */
702 /* Initialize LDAP library while still single-threaded as recommended in
703 * ldap_initialize(3) */
705 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
707 } /* }}} int cldap_init */
709 static int cldap_shutdown (void) /* {{{ */
711 for (size_t i = 0; i < databases_num; i++)
712 if (databases[i]->ld != NULL)
713 ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
718 } /* }}} int cldap_shutdown */
720 void module_register (void) /* {{{ */
722 plugin_register_complex_config ("openldap", cldap_config);
723 plugin_register_init ("openldap", cldap_init);
724 plugin_register_shutdown ("openldap", cldap_shutdown);
725 } /* }}} void module_register */
727 #if defined(__APPLE__)
728 #pragma clang diagnostic pop