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 /* {{{ */
57 typedef struct cldap_s cldap_t; /* }}} */
59 static void cldap_free (cldap_t *st) /* {{{ */
69 ldap_memfree (st->ld);
71 } /* }}} void cldap_free */
73 /* initialize ldap for each host */
74 static int cldap_init_host (cldap_t *st) /* {{{ */
78 rc = ldap_initialize (&ld, st->url);
79 if (rc != LDAP_SUCCESS)
81 ERROR ("openldap plugin: ldap_initialize failed: %s",
82 ldap_err2string (rc));
84 ldap_unbind_ext_s (ld, NULL, NULL);
90 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
92 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
93 &(const struct timeval){st->timeout, 0});
95 if (st->cacert != NULL)
96 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
98 if (st->verifyhost == 0)
100 int never = LDAP_OPT_X_TLS_NEVER;
101 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
104 if (st->starttls != 0)
106 rc = ldap_start_tls_s (ld, NULL, NULL);
107 if (rc != LDAP_SUCCESS)
109 ERROR ("openldap plugin: Failed to start tls on %s: %s",
110 st->url, ldap_err2string (rc));
112 ldap_unbind_ext_s (st->ld, NULL, NULL);
121 rc = ldap_sasl_bind_s (st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
122 if (rc != LDAP_SUCCESS)
124 ERROR ("openldap plugin: Failed to bind to %s: %s",
125 st->url, ldap_err2string (rc));
127 ldap_unbind_ext_s (st->ld, NULL, NULL);
132 DEBUG ("openldap plugin: Successfully connected to %s",
137 } /* }}} static cldap_init_host */
139 static void cldap_submit_value (const char *type, const char *type_instance, /* {{{ */
140 value_t value, cldap_t *st)
142 value_list_t vl = VALUE_LIST_INIT;
147 if ((st->host == NULL)
148 || (strcmp ("", st->host) == 0)
149 || (strcmp ("localhost", st->host) == 0))
151 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
155 sstrncpy (vl.host, st->host, sizeof (vl.host));
158 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
159 if (st->name != NULL)
160 sstrncpy (vl.plugin_instance, st->name,
161 sizeof (vl.plugin_instance));
163 sstrncpy (vl.type, type, sizeof (vl.type));
164 if (type_instance != NULL)
165 sstrncpy (vl.type_instance, type_instance,
166 sizeof (vl.type_instance));
168 plugin_dispatch_values (&vl);
169 } /* }}} void cldap_submit_value */
171 static void cldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
172 derive_t d, cldap_t *st)
176 cldap_submit_value (type, type_instance, v, st);
177 } /* }}} void cldap_submit_derive */
179 static void cldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
180 gauge_t g, cldap_t *st)
184 cldap_submit_value (type, type_instance, v, st);
185 } /* }}} void cldap_submit_gauge */
187 static int cldap_read_host (user_data_t *ud) /* {{{ */
190 LDAPMessage *e, *result;
195 char *attrs[9] = { "monitorCounter",
196 "monitorOpCompleted",
197 "monitorOpInitiated",
205 if ((ud == NULL) || (ud->data == NULL))
207 ERROR ("openldap plugin: cldap_read_host: Invalid user data.");
211 st = (cldap_t *) ud->data;
213 status = cldap_init_host (st);
217 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
218 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
219 NULL, NULL, NULL, 0, &result);
221 if (rc != LDAP_SUCCESS)
223 ERROR ("openldap plugin: Failed to execute search: %s",
224 ldap_err2string (rc));
225 ldap_msgfree (result);
226 ldap_unbind_ext_s (st->ld, NULL, NULL);
230 for (e = ldap_first_entry (st->ld, result); e != NULL;
231 e = ldap_next_entry (st->ld, e))
233 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,
255 "monitorCounter")) != NULL)
257 counter_data = *counter_list[0];
258 counter = atoll (counter_data.bv_val);
261 if ((opc_list = ldap_get_values_len (st->ld, e,
262 "monitorOpCompleted")) != NULL)
264 opc_data = *opc_list[0];
265 opc = atoll (opc_data.bv_val);
268 if ((opi_list = ldap_get_values_len (st->ld, e,
269 "monitorOpInitiated")) != NULL)
271 opi_data = *opi_list[0];
272 opi = atoll (opi_data.bv_val);
275 if ((info_list = ldap_get_values_len (st->ld, e,
276 "monitoredInfo")) != NULL)
278 info_data = *info_list[0];
279 info = atoll (info_data.bv_val);
282 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
285 cldap_submit_derive ("total_connections", NULL,
289 "cn=Current,cn=Connections,cn=Monitor")
292 cldap_submit_gauge ("current_connections", NULL,
296 "cn=Operations,cn=Monitor") == 0)
298 cldap_submit_derive ("operations",
299 "completed", opc, st);
300 cldap_submit_derive ("operations",
301 "initiated", opi, st);
304 "cn=Bind,cn=Operations,cn=Monitor")
307 cldap_submit_derive ("operations",
308 "bind-completed", opc, st);
309 cldap_submit_derive ("operations",
310 "bind-initiated", opi, st);
313 "cn=UnBind,cn=Operations,cn=Monitor")
316 cldap_submit_derive ("operations",
317 "unbind-completed", opc, st);
318 cldap_submit_derive ("operations",
319 "unbind-initiated", opi, st);
322 "cn=Search,cn=Operations,cn=Monitor")
325 cldap_submit_derive ("operations",
326 "search-completed", opc, st);
327 cldap_submit_derive ("operations",
328 "search-initiated", opi, st);
331 "cn=Compare,cn=Operations,cn=Monitor")
334 cldap_submit_derive ("operations",
335 "compare-completed", opc, st);
336 cldap_submit_derive ("operations",
337 "compare-initiated", opi, st);
340 "cn=Modify,cn=Operations,cn=Monitor")
343 cldap_submit_derive ("operations",
344 "modify-completed", opc, st);
345 cldap_submit_derive ("operations",
346 "modify-initiated", opi, st);
349 "cn=Modrdn,cn=Operations,cn=Monitor")
352 cldap_submit_derive ("operations",
353 "modrdn-completed", opc, st);
354 cldap_submit_derive ("operations",
355 "modrdn-initiated", opi, st);
358 "cn=Add,cn=Operations,cn=Monitor")
361 cldap_submit_derive ("operations",
362 "add-completed", opc, st);
363 cldap_submit_derive ("operations",
364 "add-initiated", opi, st);
367 "cn=Delete,cn=Operations,cn=Monitor")
370 cldap_submit_derive ("operations",
371 "delete-completed", opc, st);
372 cldap_submit_derive ("operations",
373 "delete-initiated", opi, st);
376 "cn=Abandon,cn=Operations,cn=Monitor")
379 cldap_submit_derive ("operations",
380 "abandon-completed", opc, st);
381 cldap_submit_derive ("operations",
382 "abandon-initiated", opi, st);
385 "cn=Extended,cn=Operations,cn=Monitor")
388 cldap_submit_derive ("operations",
389 "extended-completed", opc, st);
390 cldap_submit_derive ("operations",
391 "extended-initiated", opi, st);
393 else if ((strncmp (dn, "cn=Database", 11) == 0)
394 && ((nc_list = ldap_get_values_len
395 (st->ld, e, "namingContexts")) != NULL))
397 nc_data = *nc_list[0];
398 char typeinst[DATA_MAX_NAME_LEN];
400 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
401 "olmBDBEntryCache")) != NULL)
403 olmbdb_data = *olmbdb_list[0];
404 ssnprintf (typeinst, sizeof (typeinst),
405 "bdbentrycache-%s", nc_data.bv_val);
406 cldap_submit_gauge ("cache_size", typeinst,
407 atoll (olmbdb_data.bv_val), st);
408 ldap_value_free_len (olmbdb_list);
411 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
412 "olmBDBDNCache")) != NULL)
414 olmbdb_data = *olmbdb_list[0];
415 ssnprintf (typeinst, sizeof (typeinst),
416 "bdbdncache-%s", nc_data.bv_val);
417 cldap_submit_gauge ("cache_size", typeinst,
418 atoll (olmbdb_data.bv_val), st);
419 ldap_value_free_len (olmbdb_list);
422 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
423 "olmBDBIDLCache")) != NULL)
425 olmbdb_data = *olmbdb_list[0];
426 ssnprintf (typeinst, sizeof (typeinst),
427 "bdbidlcache-%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 ldap_value_free_len (nc_list);
436 "cn=Bytes,cn=Statistics,cn=Monitor")
439 cldap_submit_derive ("derive", "statistics-bytes",
443 "cn=PDU,cn=Statistics,cn=Monitor")
446 cldap_submit_derive ("derive", "statistics-pdu",
450 "cn=Entries,cn=Statistics,cn=Monitor")
453 cldap_submit_derive ("derive", "statistics-entries",
457 "cn=Referrals,cn=Statistics,cn=Monitor")
460 cldap_submit_derive ("derive", "statistics-referrals",
464 "cn=Open,cn=Threads,cn=Monitor")
467 cldap_submit_gauge ("threads", "threads-open",
471 "cn=Starting,cn=Threads,cn=Monitor")
474 cldap_submit_gauge ("threads", "threads-starting",
478 "cn=Active,cn=Threads,cn=Monitor")
481 cldap_submit_gauge ("threads", "threads-active",
485 "cn=Pending,cn=Threads,cn=Monitor")
488 cldap_submit_gauge ("threads", "threads-pending",
492 "cn=Backload,cn=Threads,cn=Monitor")
495 cldap_submit_gauge ("threads", "threads-backload",
499 "cn=Read,cn=Waiters,cn=Monitor")
502 cldap_submit_derive ("derive", "waiters-read",
506 "cn=Write,cn=Waiters,cn=Monitor")
509 cldap_submit_derive ("derive", "waiters-write",
513 ldap_value_free_len (counter_list);
514 ldap_value_free_len (opc_list);
515 ldap_value_free_len (opi_list);
516 ldap_value_free_len (info_list);
522 ldap_msgfree (result);
523 ldap_unbind_ext_s (st->ld, NULL, NULL);
525 } /* }}} int cldap_read_host */
527 /* Configuration handling functions {{{
530 * <Instance "plugin_instance1">
531 * URL "ldap://localhost"
537 static int cldap_config_add (oconfig_item_t *ci) /* {{{ */
543 st = malloc (sizeof (*st));
546 ERROR ("openldap plugin: malloc failed.");
549 memset (st, 0, sizeof (*st));
551 status = cf_util_get_string (ci, &st->name);
561 st->version = LDAP_VERSION3;
563 for (i = 0; i < ci->children_num; i++)
565 oconfig_item_t *child = ci->children + i;
567 if (strcasecmp ("CACert", child->key) == 0)
568 status = cf_util_get_string (child, &st->cacert);
569 else if (strcasecmp ("StartTLS", child->key) == 0)
570 status = cf_util_get_boolean (child, &st->starttls);
571 else if (strcasecmp ("Timeout", child->key) == 0)
572 status = cf_util_get_int (child, &st->timeout);
573 else if (strcasecmp ("URL", child->key) == 0)
574 status = cf_util_get_string (child, &st->url);
575 else if (strcasecmp ("VerifyHost", child->key) == 0)
576 status = cf_util_get_boolean (child, &st->verifyhost);
577 else if (strcasecmp ("Version", child->key) == 0)
578 status = cf_util_get_int (child, &st->version);
581 WARNING ("openldap plugin: Option `%s' not allowed here.",
590 /* Check if struct is complete.. */
591 if ((status == 0) && (st->url == NULL))
593 ERROR ("openldap plugin: Instance `%s': "
594 "No URL has been configured.",
599 /* Check if URL is valid */
600 if ((status == 0) && (st->url != NULL))
605 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
607 ERROR ("openldap plugin: Instance `%s': "
613 if ((status == 0) && (ludpp->lud_host != NULL))
615 st->host = strdup (ludpp->lud_host);
618 ldap_free_urldesc (ludpp);
624 char callback_name[3*DATA_MAX_NAME_LEN];
626 memset (&ud, 0, sizeof (ud));
629 memset (callback_name, 0, sizeof (callback_name));
630 ssnprintf (callback_name, sizeof (callback_name),
632 (st->host != NULL) ? st->host : hostname_g,
633 (st->name != NULL) ? st->name : "default"),
635 status = plugin_register_complex_read (/* group = */ NULL,
636 /* name = */ callback_name,
637 /* callback = */ cldap_read_host,
638 /* interval = */ NULL,
639 /* user_data = */ &ud);
649 } /* }}} int cldap_config_add */
651 static int cldap_config (oconfig_item_t *ci) /* {{{ */
656 for (i = 0; i < ci->children_num; i++)
658 oconfig_item_t *child = ci->children + i;
660 if (strcasecmp ("Instance", child->key) == 0)
661 cldap_config_add (child);
663 WARNING ("openldap plugin: The configuration option "
664 "\"%s\" is not allowed here. Did you "
665 "forget to add an <Instance /> block "
666 "around the configuration?",
668 } /* for (ci->children) */
671 } /* }}} int cldap_config */
673 /* }}} End of configuration handling functions */
675 static int cldap_init (void) /* {{{ */
677 /* Initialize LDAP library while still single-threaded as recommended in
678 * ldap_initialize(3) */
680 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
682 } /* }}} int cldap_init */
684 void module_register (void) /* {{{ */
686 plugin_register_complex_config ("openldap", cldap_config);
687 plugin_register_init ("openldap", cldap_init);
688 } /* }}} void module_register */
690 #if defined(__APPLE__)
691 #pragma clang diagnostic pop