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"
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))
171 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
175 sstrncpy (vl.host, st->host, sizeof (vl.host));
178 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
179 if (st->name != NULL)
180 sstrncpy (vl.plugin_instance, st->name,
181 sizeof (vl.plugin_instance));
183 sstrncpy (vl.type, type, sizeof (vl.type));
184 if (type_instance != NULL)
185 sstrncpy (vl.type_instance, type_instance,
186 sizeof (vl.type_instance));
188 plugin_dispatch_values (&vl);
189 } /* }}} void cldap_submit_value */
191 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
192 derive_t d, cldap_t *st)
196 cldap_submit_value (type, type_instance, v, st);
197 } /* }}} void cldap_submit_derive */
199 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
200 gauge_t g, cldap_t *st)
204 cldap_submit_value (type, type_instance, v, st);
205 } /* }}} void cldap_submit_gauge */
207 static int cldap_read_host (user_data_t *ud) /* {{{ */
210 LDAPMessage *e, *result;
215 char *attrs[9] = { "monitorCounter",
216 "monitorOpCompleted",
217 "monitorOpInitiated",
225 if ((ud == NULL) || (ud->data == NULL))
227 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
231 st = (cldap_t *) ud->data;
233 status = cldap_init_host (st);
237 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
238 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
239 NULL, NULL, NULL, 0, &result);
241 if (rc != LDAP_SUCCESS)
243 ERROR ("openldap plugin: Failed to execute search: %s",
244 ldap_err2string (rc));
245 ldap_msgfree (result);
247 ldap_unbind_ext_s (st->ld, NULL, NULL);
251 for (e = ldap_first_entry (st->ld, result); e != NULL;
252 e = ldap_next_entry (st->ld, e))
254 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
256 unsigned long long counter = 0;
257 unsigned long long opc = 0;
258 unsigned long long opi = 0;
259 unsigned long long info = 0;
261 struct berval counter_data;
262 struct berval opc_data;
263 struct berval opi_data;
264 struct berval info_data;
265 struct berval olmbdb_data;
266 struct berval nc_data;
268 struct berval **counter_list;
269 struct berval **opc_list;
270 struct berval **opi_list;
271 struct berval **info_list;
272 struct berval **olmbdb_list;
273 struct berval **nc_list;
275 if ((counter_list = ldap_get_values_len (st->ld, e,
276 "monitorCounter")) != NULL)
278 counter_data = *counter_list[0];
279 counter = atoll (counter_data.bv_val);
282 if ((opc_list = ldap_get_values_len (st->ld, e,
283 "monitorOpCompleted")) != NULL)
285 opc_data = *opc_list[0];
286 opc = atoll (opc_data.bv_val);
289 if ((opi_list = ldap_get_values_len (st->ld, e,
290 "monitorOpInitiated")) != NULL)
292 opi_data = *opi_list[0];
293 opi = atoll (opi_data.bv_val);
296 if ((info_list = ldap_get_values_len (st->ld, e,
297 "monitoredInfo")) != NULL)
299 info_data = *info_list[0];
300 info = atoll (info_data.bv_val);
303 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
306 cldap_submit_derive ("total_connections", NULL,
310 "cn=Current,cn=Connections,cn=Monitor")
313 cldap_submit_gauge ("current_connections", NULL,
317 "cn=Operations,cn=Monitor") == 0)
319 cldap_submit_derive ("operations",
320 "completed", opc, st);
321 cldap_submit_derive ("operations",
322 "initiated", opi, st);
325 "cn=Bind,cn=Operations,cn=Monitor")
328 cldap_submit_derive ("operations",
329 "bind-completed", opc, st);
330 cldap_submit_derive ("operations",
331 "bind-initiated", opi, st);
334 "cn=UnBind,cn=Operations,cn=Monitor")
337 cldap_submit_derive ("operations",
338 "unbind-completed", opc, st);
339 cldap_submit_derive ("operations",
340 "unbind-initiated", opi, st);
343 "cn=Search,cn=Operations,cn=Monitor")
346 cldap_submit_derive ("operations",
347 "search-completed", opc, st);
348 cldap_submit_derive ("operations",
349 "search-initiated", opi, st);
352 "cn=Compare,cn=Operations,cn=Monitor")
355 cldap_submit_derive ("operations",
356 "compare-completed", opc, st);
357 cldap_submit_derive ("operations",
358 "compare-initiated", opi, st);
361 "cn=Modify,cn=Operations,cn=Monitor")
364 cldap_submit_derive ("operations",
365 "modify-completed", opc, st);
366 cldap_submit_derive ("operations",
367 "modify-initiated", opi, st);
370 "cn=Modrdn,cn=Operations,cn=Monitor")
373 cldap_submit_derive ("operations",
374 "modrdn-completed", opc, st);
375 cldap_submit_derive ("operations",
376 "modrdn-initiated", opi, st);
379 "cn=Add,cn=Operations,cn=Monitor")
382 cldap_submit_derive ("operations",
383 "add-completed", opc, st);
384 cldap_submit_derive ("operations",
385 "add-initiated", opi, st);
388 "cn=Delete,cn=Operations,cn=Monitor")
391 cldap_submit_derive ("operations",
392 "delete-completed", opc, st);
393 cldap_submit_derive ("operations",
394 "delete-initiated", opi, st);
397 "cn=Abandon,cn=Operations,cn=Monitor")
400 cldap_submit_derive ("operations",
401 "abandon-completed", opc, st);
402 cldap_submit_derive ("operations",
403 "abandon-initiated", opi, st);
406 "cn=Extended,cn=Operations,cn=Monitor")
409 cldap_submit_derive ("operations",
410 "extended-completed", opc, st);
411 cldap_submit_derive ("operations",
412 "extended-initiated", opi, st);
414 else if ((strncmp (dn, "cn=Database", 11) == 0)
415 && ((nc_list = ldap_get_values_len
416 (st->ld, e, "namingContexts")) != NULL))
418 nc_data = *nc_list[0];
419 char typeinst[DATA_MAX_NAME_LEN];
421 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
422 "olmBDBEntryCache")) != NULL)
424 olmbdb_data = *olmbdb_list[0];
425 ssnprintf (typeinst, sizeof (typeinst),
426 "bdbentrycache-%s", nc_data.bv_val);
427 cldap_submit_gauge ("cache_size", typeinst,
428 atoll (olmbdb_data.bv_val), st);
429 ldap_value_free_len (olmbdb_list);
432 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
433 "olmBDBDNCache")) != NULL)
435 olmbdb_data = *olmbdb_list[0];
436 ssnprintf (typeinst, sizeof (typeinst),
437 "bdbdncache-%s", nc_data.bv_val);
438 cldap_submit_gauge ("cache_size", typeinst,
439 atoll (olmbdb_data.bv_val), st);
440 ldap_value_free_len (olmbdb_list);
443 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
444 "olmBDBIDLCache")) != NULL)
446 olmbdb_data = *olmbdb_list[0];
447 ssnprintf (typeinst, sizeof (typeinst),
448 "bdbidlcache-%s", nc_data.bv_val);
449 cldap_submit_gauge ("cache_size", typeinst,
450 atoll (olmbdb_data.bv_val), st);
451 ldap_value_free_len (olmbdb_list);
454 ldap_value_free_len (nc_list);
457 "cn=Bytes,cn=Statistics,cn=Monitor")
460 cldap_submit_derive ("derive", "statistics-bytes",
464 "cn=PDU,cn=Statistics,cn=Monitor")
467 cldap_submit_derive ("derive", "statistics-pdu",
471 "cn=Entries,cn=Statistics,cn=Monitor")
474 cldap_submit_derive ("derive", "statistics-entries",
478 "cn=Referrals,cn=Statistics,cn=Monitor")
481 cldap_submit_derive ("derive", "statistics-referrals",
485 "cn=Open,cn=Threads,cn=Monitor")
488 cldap_submit_gauge ("threads", "threads-open",
492 "cn=Starting,cn=Threads,cn=Monitor")
495 cldap_submit_gauge ("threads", "threads-starting",
499 "cn=Active,cn=Threads,cn=Monitor")
502 cldap_submit_gauge ("threads", "threads-active",
506 "cn=Pending,cn=Threads,cn=Monitor")
509 cldap_submit_gauge ("threads", "threads-pending",
513 "cn=Backload,cn=Threads,cn=Monitor")
516 cldap_submit_gauge ("threads", "threads-backload",
520 "cn=Read,cn=Waiters,cn=Monitor")
523 cldap_submit_derive ("derive", "waiters-read",
527 "cn=Write,cn=Waiters,cn=Monitor")
530 cldap_submit_derive ("derive", "waiters-write",
534 ldap_value_free_len (counter_list);
535 ldap_value_free_len (opc_list);
536 ldap_value_free_len (opi_list);
537 ldap_value_free_len (info_list);
543 ldap_msgfree (result);
545 } /* }}} int cldap_read_host */
547 /* Configuration handling functions {{{
550 * <Instance "plugin_instance1">
551 * URL "ldap://localhost"
557 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
563 st = malloc (sizeof (*st));
566 ERROR ("openldap plugin: malloc failed.");
569 memset (st, 0, sizeof (*st));
571 status = cf_util_get_string (ci, &st->name);
579 st->timeout = (long) (CDTIME_T_TO_MS(plugin_get_interval()) / 1000);
581 st->version = LDAP_VERSION3;
583 for (i = 0; i < ci->children_num; i++)
585 oconfig_item_t *child = ci->children + i;
587 if (strcasecmp ("BindDN", child->key) == 0)
588 status = cf_util_get_string (child, &st->binddn);
589 else if (strcasecmp ("Password", child->key) == 0)
590 status = cf_util_get_string (child, &st->password);
591 else if (strcasecmp ("CACert", child->key) == 0)
592 status = cf_util_get_string (child, &st->cacert);
593 else if (strcasecmp ("StartTLS", child->key) == 0)
594 status = cf_util_get_boolean (child, &st->starttls);
595 else if (strcasecmp ("Timeout", child->key) == 0)
596 status = cf_util_get_int (child, &st->timeout);
597 else if (strcasecmp ("URL", child->key) == 0)
598 status = cf_util_get_string (child, &st->url);
599 else if (strcasecmp ("VerifyHost", child->key) == 0)
600 status = cf_util_get_boolean (child, &st->verifyhost);
601 else if (strcasecmp ("Version", child->key) == 0)
602 status = cf_util_get_int (child, &st->version);
605 WARNING ("openldap plugin: Option `%s' not allowed here.",
614 /* Check if struct is complete.. */
615 if ((status == 0) && (st->url == NULL))
617 ERROR ("openldap plugin: Instance `%s': "
618 "No URL has been configured.",
623 /* Check if URL is valid */
624 if ((status == 0) && (st->url != NULL))
629 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
631 ERROR ("openldap plugin: Instance `%s': "
637 if ((status == 0) && (ludpp->lud_host != NULL))
639 st->host = strdup (ludpp->lud_host);
642 ldap_free_urldesc (ludpp);
649 temp = (cldap_t **) realloc (databases,
650 sizeof (*databases) * (databases_num + 1));
654 ERROR ("openldap plugin: realloc failed");
660 char callback_name[3*DATA_MAX_NAME_LEN];
663 databases[databases_num] = st;
666 memset (&ud, 0, sizeof (ud));
669 memset (callback_name, 0, sizeof (callback_name));
670 ssnprintf (callback_name, sizeof (callback_name),
672 (st->host != NULL) ? st->host : hostname_g,
673 (st->name != NULL) ? st->name : "default"),
675 status = plugin_register_complex_read (/* group = */ NULL,
676 /* name = */ callback_name,
677 /* callback = */ cldap_read_host,
679 /* user_data = */ &ud);
690 } /* }}} int cldap_config_add */
692 static int cldap_config (oconfig_item_t *ci) /* {{{ */
697 for (i = 0; i < ci->children_num; i++)
699 oconfig_item_t *child = ci->children + i;
701 if (strcasecmp ("Instance", child->key) == 0)
702 cldap_config_add (child);
704 WARNING ("openldap plugin: The configuration option "
705 "\"%s\" is not allowed here. Did you "
706 "forget to add an <Instance /> block "
707 "around the configuration?",
709 } /* for (ci->children) */
712 } /* }}} int cldap_config */
714 /* }}} End of configuration handling functions */
716 static int cldap_init (void) /* {{{ */
718 /* Initialize LDAP library while still single-threaded as recommended in
719 * ldap_initialize(3) */
721 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
723 } /* }}} int cldap_init */
725 static int cldap_shutdown (void) /* {{{ */
729 for (i = 0; i < databases_num; i++)
730 if (databases[i]->ld != NULL)
731 ldap_unbind_ext_s (databases[i]->ld, NULL, NULL);
736 } /* }}} int cldap_shutdown */
738 void module_register (void) /* {{{ */
740 plugin_register_complex_config ("openldap", cldap_config);
741 plugin_register_init ("openldap", cldap_init);
742 plugin_register_shutdown ("openldap", cldap_shutdown);
743 } /* }}} void module_register */