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)
173 || (strcmp ("", st->host) == 0)
174 || (strcmp ("localhost", st->host) == 0))
175 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
177 sstrncpy (vl.host, st->host, sizeof (vl.host));
179 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
180 if (st->name != NULL)
181 sstrncpy (vl.plugin_instance, st->name,
182 sizeof (vl.plugin_instance));
184 sstrncpy (vl.type, type, sizeof (vl.type));
185 if (type_instance != NULL)
186 sstrncpy (vl.type_instance, type_instance,
187 sizeof (vl.type_instance));
189 plugin_dispatch_values (&vl);
190 } /* }}} void cldap_submit_value */
192 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
193 derive_t d, cldap_t *st)
197 cldap_submit_value (type, type_instance, v, st);
198 } /* }}} void cldap_submit_derive */
200 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
201 gauge_t g, cldap_t *st)
205 cldap_submit_value (type, type_instance, v, st);
206 } /* }}} void cldap_submit_gauge */
208 static int cldap_read_host (user_data_t *ud) /* {{{ */
216 char *attrs[9] = { "monitorCounter",
217 "monitorOpCompleted",
218 "monitorOpInitiated",
226 if ((ud == NULL) || (ud->data == NULL))
228 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
232 st = (cldap_t *) ud->data;
234 status = cldap_init_host (st);
238 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
239 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
240 NULL, NULL, NULL, 0, &result);
242 if (rc != LDAP_SUCCESS)
244 ERROR ("openldap plugin: Failed to execute search: %s",
245 ldap_err2string (rc));
246 ldap_msgfree (result);
248 ldap_unbind_ext_s (st->ld, NULL, NULL);
252 for (LDAPMessage *e = ldap_first_entry (st->ld, result); e != NULL;
253 e = ldap_next_entry (st->ld, e))
255 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
257 unsigned long long counter = 0;
258 unsigned long long opc = 0;
259 unsigned long long opi = 0;
260 unsigned long long info = 0;
262 struct berval counter_data;
263 struct berval opc_data;
264 struct berval opi_data;
265 struct berval info_data;
266 struct berval olmbdb_data;
267 struct berval nc_data;
269 struct berval **counter_list;
270 struct berval **opc_list;
271 struct berval **opi_list;
272 struct berval **info_list;
273 struct berval **olmbdb_list;
274 struct berval **nc_list;
276 if ((counter_list = ldap_get_values_len (st->ld, e,
277 "monitorCounter")) != NULL)
279 counter_data = *counter_list[0];
280 counter = atoll (counter_data.bv_val);
283 if ((opc_list = ldap_get_values_len (st->ld, e,
284 "monitorOpCompleted")) != NULL)
286 opc_data = *opc_list[0];
287 opc = atoll (opc_data.bv_val);
290 if ((opi_list = ldap_get_values_len (st->ld, e,
291 "monitorOpInitiated")) != NULL)
293 opi_data = *opi_list[0];
294 opi = atoll (opi_data.bv_val);
297 if ((info_list = ldap_get_values_len (st->ld, e,
298 "monitoredInfo")) != NULL)
300 info_data = *info_list[0];
301 info = atoll (info_data.bv_val);
304 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
307 cldap_submit_derive ("total_connections", NULL,
311 "cn=Current,cn=Connections,cn=Monitor")
314 cldap_submit_gauge ("current_connections", NULL,
318 "cn=Operations,cn=Monitor") == 0)
320 cldap_submit_derive ("operations",
321 "completed", opc, st);
322 cldap_submit_derive ("operations",
323 "initiated", opi, st);
326 "cn=Bind,cn=Operations,cn=Monitor")
329 cldap_submit_derive ("operations",
330 "bind-completed", opc, st);
331 cldap_submit_derive ("operations",
332 "bind-initiated", opi, st);
335 "cn=UnBind,cn=Operations,cn=Monitor")
338 cldap_submit_derive ("operations",
339 "unbind-completed", opc, st);
340 cldap_submit_derive ("operations",
341 "unbind-initiated", opi, st);
344 "cn=Search,cn=Operations,cn=Monitor")
347 cldap_submit_derive ("operations",
348 "search-completed", opc, st);
349 cldap_submit_derive ("operations",
350 "search-initiated", opi, st);
353 "cn=Compare,cn=Operations,cn=Monitor")
356 cldap_submit_derive ("operations",
357 "compare-completed", opc, st);
358 cldap_submit_derive ("operations",
359 "compare-initiated", opi, st);
362 "cn=Modify,cn=Operations,cn=Monitor")
365 cldap_submit_derive ("operations",
366 "modify-completed", opc, st);
367 cldap_submit_derive ("operations",
368 "modify-initiated", opi, st);
371 "cn=Modrdn,cn=Operations,cn=Monitor")
374 cldap_submit_derive ("operations",
375 "modrdn-completed", opc, st);
376 cldap_submit_derive ("operations",
377 "modrdn-initiated", opi, st);
380 "cn=Add,cn=Operations,cn=Monitor")
383 cldap_submit_derive ("operations",
384 "add-completed", opc, st);
385 cldap_submit_derive ("operations",
386 "add-initiated", opi, st);
389 "cn=Delete,cn=Operations,cn=Monitor")
392 cldap_submit_derive ("operations",
393 "delete-completed", opc, st);
394 cldap_submit_derive ("operations",
395 "delete-initiated", opi, st);
398 "cn=Abandon,cn=Operations,cn=Monitor")
401 cldap_submit_derive ("operations",
402 "abandon-completed", opc, st);
403 cldap_submit_derive ("operations",
404 "abandon-initiated", opi, st);
407 "cn=Extended,cn=Operations,cn=Monitor")
410 cldap_submit_derive ("operations",
411 "extended-completed", opc, st);
412 cldap_submit_derive ("operations",
413 "extended-initiated", opi, st);
415 else if ((strncmp (dn, "cn=Database", 11) == 0)
416 && ((nc_list = ldap_get_values_len
417 (st->ld, e, "namingContexts")) != NULL))
419 nc_data = *nc_list[0];
420 char typeinst[DATA_MAX_NAME_LEN];
422 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
423 "olmBDBEntryCache")) != NULL)
425 olmbdb_data = *olmbdb_list[0];
426 ssnprintf (typeinst, sizeof (typeinst),
427 "bdbentrycache-%s", nc_data.bv_val);
428 cldap_submit_gauge ("cache_size", typeinst,
429 atoll (olmbdb_data.bv_val), st);
430 ldap_value_free_len (olmbdb_list);
433 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
434 "olmBDBDNCache")) != NULL)
436 olmbdb_data = *olmbdb_list[0];
437 ssnprintf (typeinst, sizeof (typeinst),
438 "bdbdncache-%s", nc_data.bv_val);
439 cldap_submit_gauge ("cache_size", typeinst,
440 atoll (olmbdb_data.bv_val), st);
441 ldap_value_free_len (olmbdb_list);
444 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
445 "olmBDBIDLCache")) != NULL)
447 olmbdb_data = *olmbdb_list[0];
448 ssnprintf (typeinst, sizeof (typeinst),
449 "bdbidlcache-%s", nc_data.bv_val);
450 cldap_submit_gauge ("cache_size", typeinst,
451 atoll (olmbdb_data.bv_val), st);
452 ldap_value_free_len (olmbdb_list);
455 ldap_value_free_len (nc_list);
458 "cn=Bytes,cn=Statistics,cn=Monitor")
461 cldap_submit_derive ("derive", "statistics-bytes",
465 "cn=PDU,cn=Statistics,cn=Monitor")
468 cldap_submit_derive ("derive", "statistics-pdu",
472 "cn=Entries,cn=Statistics,cn=Monitor")
475 cldap_submit_derive ("derive", "statistics-entries",
479 "cn=Referrals,cn=Statistics,cn=Monitor")
482 cldap_submit_derive ("derive", "statistics-referrals",
486 "cn=Open,cn=Threads,cn=Monitor")
489 cldap_submit_gauge ("threads", "threads-open",
493 "cn=Starting,cn=Threads,cn=Monitor")
496 cldap_submit_gauge ("threads", "threads-starting",
500 "cn=Active,cn=Threads,cn=Monitor")
503 cldap_submit_gauge ("threads", "threads-active",
507 "cn=Pending,cn=Threads,cn=Monitor")
510 cldap_submit_gauge ("threads", "threads-pending",
514 "cn=Backload,cn=Threads,cn=Monitor")
517 cldap_submit_gauge ("threads", "threads-backload",
521 "cn=Read,cn=Waiters,cn=Monitor")
524 cldap_submit_derive ("derive", "waiters-read",
528 "cn=Write,cn=Waiters,cn=Monitor")
531 cldap_submit_derive ("derive", "waiters-write",
535 ldap_value_free_len (counter_list);
536 ldap_value_free_len (opc_list);
537 ldap_value_free_len (opi_list);
538 ldap_value_free_len (info_list);
544 ldap_msgfree (result);
546 } /* }}} int cldap_read_host */
548 /* Configuration handling functions {{{
551 * <Instance "plugin_instance1">
552 * URL "ldap://localhost"
558 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
563 st = calloc (1, sizeof (*st));
566 ERROR ("openldap plugin: calloc failed.");
570 status = cf_util_get_string (ci, &st->name);
578 st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
580 st->version = LDAP_VERSION3;
582 for (int i = 0; i < ci->children_num; i++)
584 oconfig_item_t *child = ci->children + i;
586 if (strcasecmp ("BindDN", child->key) == 0)
587 status = cf_util_get_string (child, &st->binddn);
588 else if (strcasecmp ("Password", child->key) == 0)
589 status = cf_util_get_string (child, &st->password);
590 else if (strcasecmp ("CACert", child->key) == 0)
591 status = cf_util_get_string (child, &st->cacert);
592 else if (strcasecmp ("StartTLS", child->key) == 0)
593 status = cf_util_get_boolean (child, &st->starttls);
594 else if (strcasecmp ("Timeout", child->key) == 0)
595 status = cf_util_get_int (child, &st->timeout);
596 else if (strcasecmp ("URL", child->key) == 0)
597 status = cf_util_get_string (child, &st->url);
598 else if (strcasecmp ("VerifyHost", child->key) == 0)
599 status = cf_util_get_boolean (child, &st->verifyhost);
600 else if (strcasecmp ("Version", child->key) == 0)
601 status = cf_util_get_int (child, &st->version);
604 WARNING ("openldap plugin: Option `%s' not allowed here.",
613 /* Check if struct is complete.. */
614 if ((status == 0) && (st->url == NULL))
616 ERROR ("openldap plugin: Instance `%s': "
617 "No URL has been configured.",
622 /* Check if URL is valid */
623 if ((status == 0) && (st->url != NULL))
627 if (ldap_url_parse (st->url, &ludpp) != 0)
629 ERROR ("openldap plugin: Instance `%s': "
635 if ((status == 0) && (ludpp->lud_host != NULL))
636 st->host = strdup (ludpp->lud_host);
638 ldap_free_urldesc (ludpp);
645 temp = (cldap_t **) realloc (databases,
646 sizeof (*databases) * (databases_num + 1));
650 ERROR ("openldap plugin: realloc failed");
655 char callback_name[3*DATA_MAX_NAME_LEN] = { 0 };
658 databases[databases_num] = st;
661 ssnprintf (callback_name, sizeof (callback_name),
663 (st->host != NULL) ? st->host : hostname_g,
664 (st->name != NULL) ? st->name : "default");
666 status = plugin_register_complex_read (/* group = */ NULL,
667 /* name = */ callback_name,
668 /* callback = */ cldap_read_host,
683 } /* }}} int cldap_config_add */
685 static int cldap_config (oconfig_item_t *ci) /* {{{ */
689 for (int i = 0; i < ci->children_num; i++)
691 oconfig_item_t *child = ci->children + i;
693 if (strcasecmp ("Instance", child->key) == 0)
694 cldap_config_add (child);
696 WARNING ("openldap plugin: The configuration option "
697 "\"%s\" is not allowed here. Did you "
698 "forget to add an <Instance /> block "
699 "around the configuration?",
701 } /* for (ci->children) */
704 } /* }}} int cldap_config */
706 /* }}} End of configuration handling functions */
708 static int cldap_init (void) /* {{{ */
710 /* Initialize LDAP library while still single-threaded as recommended in
711 * ldap_initialize(3) */
713 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
715 } /* }}} int cldap_init */
717 static int cldap_shutdown (void) /* {{{ */
719 for (size_t i = 0; i < databases_num; i++)
720 if (databases[i]->ld != NULL)
721 ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
726 } /* }}} int cldap_shutdown */
728 void module_register (void) /* {{{ */
730 plugin_register_complex_config ("openldap", cldap_config);
731 plugin_register_init ("openldap", cldap_init);
732 plugin_register_shutdown ("openldap", cldap_shutdown);
733 } /* }}} void module_register */
735 #if defined(__APPLE__)
736 #pragma clang diagnostic pop