2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Kimo Rosenbaum <kimor79 at yahoo.com>
25 #include "configfile.h"
30 struct ldap_s /* {{{ */
46 typedef struct ldap_s ldap_t; /* }}} */
48 static int ldap_read_host (user_data_t *ud);
50 static void ldap_free (ldap_t *st) /* {{{ */
62 } /* }}} void ldap_free */
64 /* Configuration handling functions {{{
67 * <Instance "plugin_instance1">
68 * URL "ldap://localhost"
74 static int config_set_string (char **ret_string, /* {{{ */
79 if ((ci->values_num != 1)
80 || (ci->values[0].type != OCONFIG_TYPE_STRING))
82 WARNING ("openldap plugin: The `%s' config option "
83 "needs exactly one string argument.", ci->key);
87 string = strdup (ci->values[0].value.string);
90 ERROR ("openldap plugin: strdup failed.");
94 if (*ret_string != NULL)
99 } /* }}} int config_set_string */
101 static int config_set_int (int *ret_int, /* {{{ */
104 if ((ci->values_num != 1)
105 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
107 WARNING ("openldap plugin: The `%s' config option "
108 "needs exactly one string argument.", ci->key);
112 *ret_int = ci->values[0].value.number;
115 } /* }}} int config_set_int */
117 static int config_set_bool (int *ret_boolean, /* {{{ */
122 if (ci->values_num != 1)
127 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
128 *ret_boolean = ci->values[0].value.boolean;
129 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
131 if (IS_TRUE (ci->values[0].value.string))
133 else if (IS_FALSE (ci->values[0].value.string))
144 WARNING ("openldap plugin: The `%s' config option "
145 "needs exactly one boolean argument.", ci->key);
149 } /* }}} config_set_bool */
151 static int config_add (oconfig_item_t *ci) /* {{{ */
157 if ((ci->values_num != 1)
158 || (ci->values[0].type != OCONFIG_TYPE_STRING))
160 WARNING ("openldap plugin: The `%s' config option "
161 "needs exactly one string argument.", ci->key);
165 st = (ldap_t *) malloc (sizeof (*st));
168 ERROR ("openldap plugin: malloc failed.");
171 memset (st, 0, sizeof (*st));
173 status = config_set_string (&st->name, ci);
181 st->version = LDAP_VERSION3;
183 for (i = 0; i < ci->children_num; i++)
185 oconfig_item_t *child = ci->children + i;
187 if (strcasecmp ("CACert", child->key) == 0)
188 status = config_set_string (&st->cacert, child);
189 else if (strcasecmp ("StartTLS", child->key) == 0)
190 status = config_set_bool (&st->starttls, child);
191 else if (strcasecmp ("Timeout", child->key) == 0)
192 status = config_set_int (&st->timeout, child);
193 else if (strcasecmp ("URL", child->key) == 0)
194 status = config_set_string (&st->url, child);
195 else if (strcasecmp ("VerifyHost", child->key) == 0)
196 status = config_set_bool (&st->verifyhost, child);
197 else if (strcasecmp ("Version", child->key) == 0)
198 status = config_set_int (&st->version, child);
201 WARNING ("openldap plugin: Option `%s' not allowed here.",
210 /* Check if struct is complete.. */
211 if ((status == 0) && (st->url == NULL))
213 ERROR ("openldap plugin: Instance `%s': "
214 "No URL has been configured.",
219 /* Check if URL is valid */
220 if ((status == 0) && (st->url != NULL))
225 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
227 ERROR ("openldap plugin: Instance `%s': "
234 st->host = strdup (ludpp->lud_host);
237 ldap_free_urldesc(ludpp);
243 char callback_name[3*DATA_MAX_NAME_LEN];
245 memset (&ud, 0, sizeof (ud));
247 ud.free_func = (void *) ldap_free;
249 memset (callback_name, 0, sizeof (callback_name));
250 ssnprintf (callback_name, sizeof (callback_name),
252 (st->host != NULL) ? st->host : hostname_g,
253 (st->name != NULL) ? st->name : "default"),
255 status = plugin_register_complex_read (/* group = */ NULL,
256 /* name = */ callback_name,
257 /* callback = */ ldap_read_host,
258 /* interval = */ NULL,
259 /* user_data = */ &ud);
269 } /* }}} int config_add */
271 static int config (oconfig_item_t *ci)
276 for (i = 0; i < ci->children_num; i++)
278 oconfig_item_t *child = ci->children + i;
280 if (strcasecmp ("Instance", child->key) == 0)
283 WARNING ("openldap plugin: The configuration option "
284 "\"%s\" is not allowed here. Did you "
285 "forget to add an <Instance /> block "
286 "around the configuration?",
288 } /* for (ci->children) */
293 /* }}} End of configuration handling functions */
295 /* initialize ldap for each host */
296 static int init_host (ldap_t *st)
300 rc = ldap_initialize (&ld, st->url);
301 if (rc != LDAP_SUCCESS)
304 sstrerror (errno, errbuf, sizeof (errbuf));
305 ERROR ("ldap_initialize failed: %s", errbuf);
312 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
314 if(st->cacert != NULL)
315 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
317 if(st->verifyhost == 0)
319 int never = LDAP_OPT_X_TLS_NEVER;
320 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
323 if(st->starttls != 0)
325 rc = ldap_start_tls_s(ld, NULL, NULL);
326 if (rc != LDAP_SUCCESS)
328 ERROR ("openldap plugin: Failed to start tls on %s: %s",
329 st->url, ldap_err2string (rc));
331 ldap_destroy(st->ld);
340 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
341 if (rc != LDAP_SUCCESS)
343 ERROR ("openldap plugin: Failed to bind to %s: %s",
344 st->url, ldap_err2string (rc));
346 ldap_destroy(st->ld);
351 DEBUG ("openldap plugin: Successfully connected to %s",
356 } /* static init_host (ldap_t *st) */
358 static void submit_value (const char *type, const char *type_instance,
359 value_t value, ldap_t *st)
361 value_list_t vl = VALUE_LIST_INIT;
366 if ((st->host == NULL)
367 || (strcmp ("", st->host) == 0)
368 || (strcmp ("localhost", st->host) == 0))
370 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
374 sstrncpy (vl.host, st->host, sizeof (vl.host));
377 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
378 if (st->name != NULL)
379 sstrncpy (vl.plugin_instance, st->name,
380 sizeof (vl.plugin_instance));
382 sstrncpy (vl.type, type, sizeof (vl.type));
383 if (type_instance != NULL)
384 sstrncpy (vl.type_instance, type_instance,
385 sizeof (vl.type_instance));
387 plugin_dispatch_values (&vl);
390 static void submit_derive (const char *type, const char *type_instance,
391 derive_t d, ldap_t *st)
395 submit_value (type, type_instance, v, st);
396 } /* void submit_derive */
398 static void submit_gauge (const char *type, const char *type_instance,
399 gauge_t g, ldap_t *st)
403 submit_value (type, type_instance, v, st);
404 } /* void submit_gauge */
406 static int ldap_read_host (user_data_t *ud)
409 LDAPMessage *e, *result;
414 char *attrs[4] = { "monitorCounter",
415 "monitorOpCompleted",
416 "monitorOpInitiated",
419 if ((ud == NULL) || (ud->data == NULL))
421 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
425 st = (ldap_t *) ud->data;
427 status = init_host (st);
431 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
432 "(!(cn=* *))", attrs, 0,
433 NULL, NULL, NULL, 0, &result);
435 if (rc != LDAP_SUCCESS)
437 ERROR ("openldap plugin: Failed to execute search: %s",
438 ldap_err2string (rc));
439 ldap_msgfree (result);
443 for (e = ldap_first_entry (st->ld, result); e != NULL;
444 e = ldap_next_entry (st->ld, e))
446 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
448 unsigned long long counter = 0;
449 unsigned long long opc = 0;
450 unsigned long long opi = 0;
452 struct berval counter_data;
453 struct berval opc_data;
454 struct berval opi_data;
455 struct berval info_data;
457 struct berval **counter_list;
458 struct berval **opc_list;
459 struct berval **opi_list;
460 struct berval **info_list;
462 if ((counter_list = ldap_get_values_len (st->ld, e,
463 "monitorCounter")) != NULL)
465 counter_data = *counter_list[0];
466 counter = atoll (counter_data.bv_val);
469 if ((opc_list = ldap_get_values_len (st->ld, e,
470 "monitorOpCompleted")) != NULL)
472 opc_data = *opc_list[0];
473 opc = atoll (opc_data.bv_val);
476 if ((opi_list = ldap_get_values_len (st->ld, e,
477 "monitorOpInitiated")) != NULL)
479 opi_data = *opi_list[0];
480 opi = atoll (opi_data.bv_val);
483 if ((info_list = ldap_get_values_len (st->ld, e,
484 "monitoredInfo")) != NULL)
486 info_data = *info_list[0];
487 // don't convert search result to long long at this point,
488 // because this field is often populated with non-numerical data.
491 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
494 submit_derive ("total_connections", NULL,
498 "cn=Current,cn=Connections,cn=Monitor")
501 submit_gauge ("current_connections", NULL,
505 "cn=Operations,cn=Monitor") == 0)
507 submit_derive ("operations",
508 "completed", opc, st);
509 submit_derive ("operations",
510 "initiated", opi, st);
513 "cn=Bind,cn=Operations,cn=Monitor")
516 submit_derive ("operations",
517 "bind-completed", opc, st);
518 submit_derive ("operations",
519 "bind-initiated", opi, st);
522 "cn=UnBind,cn=Operations,cn=Monitor")
525 submit_derive ("operations",
526 "unbind-completed", opc, st);
527 submit_derive ("operations",
528 "unbind-initiated", opi, st);
531 "cn=Search,cn=Operations,cn=Monitor")
534 submit_derive ("operations",
535 "search-completed", opc, st);
536 submit_derive ("operations",
537 "search-initiated", opi, st);
540 "cn=Compare,cn=Operations,cn=Monitor")
543 submit_derive ("operations",
544 "compare-completed", opc, st);
545 submit_derive ("operations",
546 "compare-initiated", opi, st);
549 "cn=Modify,cn=Operations,cn=Monitor")
552 submit_derive ("operations",
553 "modify-completed", opc, st);
554 submit_derive ("operations",
555 "modify-initiated", opi, st);
558 "cn=Modrdn,cn=Operations,cn=Monitor")
561 submit_derive ("operations",
562 "modrdn-completed", opc, st);
563 submit_derive ("operations",
564 "modrdn-initiated", opi, st);
567 "cn=Add,cn=Operations,cn=Monitor")
570 submit_derive ("operations",
571 "add-completed", opc, st);
572 submit_derive ("operations",
573 "add-initiated", opi, st);
576 "cn=Delete,cn=Operations,cn=Monitor")
579 submit_derive ("operations",
580 "delete-completed", opc, st);
581 submit_derive ("operations",
582 "delete-initiated", opi, st);
585 "cn=Abandon,cn=Operations,cn=Monitor")
588 submit_derive ("operations",
589 "abandon-completed", opc, st);
590 submit_derive ("operations",
591 "abandon-initiated", opi, st);
594 "cn=Extended,cn=Operations,cn=Monitor")
597 submit_derive ("operations",
598 "extended-completed", opc, st);
599 submit_derive ("operations",
600 "extended-initiated", opi, st);
603 "cn=Bytes,cn=Statistics,cn=Monitor")
606 submit_derive ("derive", "statistics-bytes",
610 "cn=PDU,cn=Statistics,cn=Monitor")
613 submit_derive ("derive", "statistics-pdu",
617 "cn=Entries,cn=Statistics,cn=Monitor")
620 submit_derive ("derive", "statistics-entries",
624 "cn=Referrals,cn=Statistics,cn=Monitor")
627 submit_derive ("derive", "statistics-referrals",
631 "cn=Open,cn=Threads,cn=Monitor")
634 submit_gauge ("threads", "threads-open",
635 atoll (info_data.bv_val), st);
638 "cn=Starting,cn=Threads,cn=Monitor")
641 submit_gauge ("threads", "threads-starting",
642 atoll (info_data.bv_val), st);
645 "cn=Active,cn=Threads,cn=Monitor")
648 submit_gauge ("threads", "threads-active",
649 atoll (info_data.bv_val), st);
652 "cn=Pending,cn=Threads,cn=Monitor")
655 submit_gauge ("threads", "threads-pending",
656 atoll (info_data.bv_val), st);
659 "cn=Backload,cn=Threads,cn=Monitor")
662 submit_gauge ("threads", "threads-backload",
663 atoll (info_data.bv_val), st);
666 "cn=Read,cn=Waiters,cn=Monitor")
669 submit_derive ("derive", "waiters-read",
673 "cn=Write,cn=Waiters,cn=Monitor")
676 submit_derive ("derive", "waiters-write",
680 ldap_value_free_len (counter_list);
681 ldap_value_free_len (opc_list);
682 ldap_value_free_len (opi_list);
683 ldap_value_free_len (info_list);
689 ldap_msgfree (result);
690 ldap_unbind_ext_s (st->ld, NULL, NULL);
692 } /* int ldap_read_host */
694 void module_register (void)
696 plugin_register_complex_config ("openldap", config);
697 } /* void module_register */