1 /**
2 * collectd - src/openldap.c
3 * Copyright (C) 2011 Kimo Rosenbaum
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Kimo Rosenbaum <kimor79 at yahoo.com>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #include <lber.h>
28 #include <ldap.h>
30 struct ldap_s /* {{{ */
31 {
32 char *name;
34 char *cacert;
35 char *host;
36 int state;
37 int starttls;
38 int timeout;
39 char *url;
40 int verifyhost;
41 int version;
43 LDAP *ld;
44 char *dn;
45 };
46 typedef struct ldap_s ldap_t; /* }}} */
48 static void ldap_free (ldap_t *st) /* {{{ */
49 {
50 if(st == NULL)
51 return;
53 sfree (st->cacert);
54 sfree (st->host);
55 sfree (st->name);
56 sfree (st->url);
57 if(st->ld)
58 ldap_memfree(st->ld);
59 sfree (st);
60 } /* }}} void ldap_free */
61 /* initialize ldap for each host */
62 static int ldap_init_host (ldap_t *st) /* {{{ */
63 {
64 LDAP *ld;
65 int rc;
66 rc = ldap_initialize (&ld, st->url);
67 if (rc != LDAP_SUCCESS)
68 {
69 ERROR ("openldap plugin: ldap_initialize failed: %s",
70 ldap_err2string (rc));
71 st->state = 0;
72 return (-1);
73 }
75 st->ld = ld;
77 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
79 ldap_set_option (st->ld, LDAP_OPT_TIMEOUT,
80 &(const struct timeval){st->timeout, 0});
82 if(st->cacert != NULL)
83 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
85 if(st->verifyhost == 0)
86 {
87 int never = LDAP_OPT_X_TLS_NEVER;
88 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
89 }
91 if(st->starttls != 0)
92 {
93 rc = ldap_start_tls_s(ld, NULL, NULL);
94 if (rc != LDAP_SUCCESS)
95 {
96 ERROR ("openldap plugin: Failed to start tls on %s: %s",
97 st->url, ldap_err2string (rc));
98 st->state = 0;
99 ldap_unbind_ext_s(st->ld, NULL, NULL);
100 return (-1);
101 }
102 }
104 struct berval cred;
105 cred.bv_val = "";
106 cred.bv_len = 0;
108 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
109 if (rc != LDAP_SUCCESS)
110 {
111 ERROR ("openldap plugin: Failed to bind to %s: %s",
112 st->url, ldap_err2string (rc));
113 st->state = 0;
114 ldap_unbind_ext_s(st->ld, NULL, NULL);
115 return (-1);
116 }
117 else
118 {
119 DEBUG ("openldap plugin: Successfully connected to %s",
120 st->url);
121 st->state = 1;
122 return (0);
123 }
124 } /* }}} static ldap_init_host */
126 static void ldap_submit_value (const char *type, const char *type_instance, /* {{{ */
127 value_t value, ldap_t *st)
128 {
129 value_list_t vl = VALUE_LIST_INIT;
131 vl.values = &value;
132 vl.values_len = 1;
134 if ((st->host == NULL)
135 || (strcmp ("", st->host) == 0)
136 || (strcmp ("localhost", st->host) == 0))
137 {
138 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
139 }
140 else
141 {
142 sstrncpy (vl.host, st->host, sizeof (vl.host));
143 }
145 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
146 if (st->name != NULL)
147 sstrncpy (vl.plugin_instance, st->name,
148 sizeof (vl.plugin_instance));
150 sstrncpy (vl.type, type, sizeof (vl.type));
151 if (type_instance != NULL)
152 sstrncpy (vl.type_instance, type_instance,
153 sizeof (vl.type_instance));
155 plugin_dispatch_values (&vl);
156 } /* }}} void ldap_submit_value */
158 static void ldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
159 derive_t d, ldap_t *st)
160 {
161 value_t v;
162 v.derive = d;
163 ldap_submit_value (type, type_instance, v, st);
164 } /* }}} void ldap_submit_derive */
166 static void ldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
167 gauge_t g, ldap_t *st)
168 {
169 value_t v;
170 v.gauge = g;
171 ldap_submit_value (type, type_instance, v, st);
172 } /* }}} void ldap_submit_gauge */
174 static int ldap_read_host (user_data_t *ud) /* {{{ */
175 {
176 ldap_t *st;
177 LDAPMessage *e, *result;
178 char *dn;
179 int rc;
180 int status;
182 char *attrs[9] = { "monitorCounter",
183 "monitorOpCompleted",
184 "monitorOpInitiated",
185 "monitoredInfo",
186 "olmBDBEntryCache",
187 "olmBDBDNCache",
188 "olmBDBIDLCache",
189 "namingContexts",
190 NULL };
192 if ((ud == NULL) || (ud->data == NULL))
193 {
194 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
195 return (-1);
196 }
198 st = (ldap_t *) ud->data;
200 status = ldap_init_host (st);
201 if (status != 0)
202 return (-1);
204 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
205 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
206 NULL, NULL, NULL, 0, &result);
208 if (rc != LDAP_SUCCESS)
209 {
210 ERROR ("openldap plugin: Failed to execute search: %s",
211 ldap_err2string (rc));
212 ldap_msgfree (result);
213 return (-1);
214 }
216 for (e = ldap_first_entry (st->ld, result); e != NULL;
217 e = ldap_next_entry (st->ld, e))
218 {
219 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
220 {
221 unsigned long long counter = 0;
222 unsigned long long opc = 0;
223 unsigned long long opi = 0;
224 unsigned long long info = 0;
226 struct berval counter_data;
227 struct berval opc_data;
228 struct berval opi_data;
229 struct berval info_data;
230 struct berval olmbdb_data;
231 struct berval nc_data;
233 struct berval **counter_list;
234 struct berval **opc_list;
235 struct berval **opi_list;
236 struct berval **info_list;
237 struct berval **olmbdb_list;
238 struct berval **nc_list;
240 if ((counter_list = ldap_get_values_len (st->ld, e,
241 "monitorCounter")) != NULL)
242 {
243 counter_data = *counter_list[0];
244 counter = atoll (counter_data.bv_val);
245 }
247 if ((opc_list = ldap_get_values_len (st->ld, e,
248 "monitorOpCompleted")) != NULL)
249 {
250 opc_data = *opc_list[0];
251 opc = atoll (opc_data.bv_val);
252 }
254 if ((opi_list = ldap_get_values_len (st->ld, e,
255 "monitorOpInitiated")) != NULL)
256 {
257 opi_data = *opi_list[0];
258 opi = atoll (opi_data.bv_val);
259 }
261 if ((info_list = ldap_get_values_len (st->ld, e,
262 "monitoredInfo")) != NULL)
263 {
264 info_data = *info_list[0];
265 info = atoll (info_data.bv_val);
266 }
268 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
269 == 0)
270 {
271 ldap_submit_derive ("total_connections", NULL,
272 counter, st);
273 }
274 else if (strcmp (dn,
275 "cn=Current,cn=Connections,cn=Monitor")
276 == 0)
277 {
278 ldap_submit_gauge ("current_connections", NULL,
279 counter, st);
280 }
281 else if (strcmp (dn,
282 "cn=Operations,cn=Monitor") == 0)
283 {
284 ldap_submit_derive ("operations",
285 "completed", opc, st);
286 ldap_submit_derive ("operations",
287 "initiated", opi, st);
288 }
289 else if (strcmp (dn,
290 "cn=Bind,cn=Operations,cn=Monitor")
291 == 0)
292 {
293 ldap_submit_derive ("operations",
294 "bind-completed", opc, st);
295 ldap_submit_derive ("operations",
296 "bind-initiated", opi, st);
297 }
298 else if (strcmp (dn,
299 "cn=UnBind,cn=Operations,cn=Monitor")
300 == 0)
301 {
302 ldap_submit_derive ("operations",
303 "unbind-completed", opc, st);
304 ldap_submit_derive ("operations",
305 "unbind-initiated", opi, st);
306 }
307 else if (strcmp (dn,
308 "cn=Search,cn=Operations,cn=Monitor")
309 == 0)
310 {
311 ldap_submit_derive ("operations",
312 "search-completed", opc, st);
313 ldap_submit_derive ("operations",
314 "search-initiated", opi, st);
315 }
316 else if (strcmp (dn,
317 "cn=Compare,cn=Operations,cn=Monitor")
318 == 0)
319 {
320 ldap_submit_derive ("operations",
321 "compare-completed", opc, st);
322 ldap_submit_derive ("operations",
323 "compare-initiated", opi, st);
324 }
325 else if (strcmp (dn,
326 "cn=Modify,cn=Operations,cn=Monitor")
327 == 0)
328 {
329 ldap_submit_derive ("operations",
330 "modify-completed", opc, st);
331 ldap_submit_derive ("operations",
332 "modify-initiated", opi, st);
333 }
334 else if (strcmp (dn,
335 "cn=Modrdn,cn=Operations,cn=Monitor")
336 == 0)
337 {
338 ldap_submit_derive ("operations",
339 "modrdn-completed", opc, st);
340 ldap_submit_derive ("operations",
341 "modrdn-initiated", opi, st);
342 }
343 else if (strcmp (dn,
344 "cn=Add,cn=Operations,cn=Monitor")
345 == 0)
346 {
347 ldap_submit_derive ("operations",
348 "add-completed", opc, st);
349 ldap_submit_derive ("operations",
350 "add-initiated", opi, st);
351 }
352 else if (strcmp (dn,
353 "cn=Delete,cn=Operations,cn=Monitor")
354 == 0)
355 {
356 ldap_submit_derive ("operations",
357 "delete-completed", opc, st);
358 ldap_submit_derive ("operations",
359 "delete-initiated", opi, st);
360 }
361 else if (strcmp (dn,
362 "cn=Abandon,cn=Operations,cn=Monitor")
363 == 0)
364 {
365 ldap_submit_derive ("operations",
366 "abandon-completed", opc, st);
367 ldap_submit_derive ("operations",
368 "abandon-initiated", opi, st);
369 }
370 else if (strcmp (dn,
371 "cn=Extended,cn=Operations,cn=Monitor")
372 == 0)
373 {
374 ldap_submit_derive ("operations",
375 "extended-completed", opc, st);
376 ldap_submit_derive ("operations",
377 "extended-initiated", opi, st);
378 }
379 else if ((strncmp (dn, "cn=Database", 11) == 0)
380 && ((nc_list = ldap_get_values_len
381 (st->ld, e, "namingContexts")) != NULL))
382 {
383 nc_data = *nc_list[0];
384 char typeinst[DATA_MAX_NAME_LEN];
386 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
387 "olmBDBEntryCache")) != NULL)
388 {
389 olmbdb_data = *olmbdb_list[0];
390 ssnprintf (typeinst, sizeof (typeinst),
391 "bdbentrycache-%s", nc_data.bv_val);
392 ldap_submit_gauge ("cache_size", typeinst,
393 atoll (olmbdb_data.bv_val), st);
394 ldap_value_free_len (olmbdb_list);
395 }
397 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
398 "olmBDBDNCache")) != NULL)
399 {
400 olmbdb_data = *olmbdb_list[0];
401 ssnprintf (typeinst, sizeof (typeinst),
402 "bdbdncache-%s", nc_data.bv_val);
403 ldap_submit_gauge ("cache_size", typeinst,
404 atoll (olmbdb_data.bv_val), st);
405 ldap_value_free_len (olmbdb_list);
406 }
408 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
409 "olmBDBIDLCache")) != NULL)
410 {
411 olmbdb_data = *olmbdb_list[0];
412 ssnprintf (typeinst, sizeof (typeinst),
413 "bdbidlcache-%s", nc_data.bv_val);
414 ldap_submit_gauge ("cache_size", typeinst,
415 atoll (olmbdb_data.bv_val), st);
416 ldap_value_free_len (olmbdb_list);
417 }
419 ldap_value_free_len (nc_list);
420 }
421 else if (strcmp (dn,
422 "cn=Bytes,cn=Statistics,cn=Monitor")
423 == 0)
424 {
425 ldap_submit_derive ("derive", "statistics-bytes",
426 counter, st);
427 }
428 else if (strcmp (dn,
429 "cn=PDU,cn=Statistics,cn=Monitor")
430 == 0)
431 {
432 ldap_submit_derive ("derive", "statistics-pdu",
433 counter, st);
434 }
435 else if (strcmp (dn,
436 "cn=Entries,cn=Statistics,cn=Monitor")
437 == 0)
438 {
439 ldap_submit_derive ("derive", "statistics-entries",
440 counter, st);
441 }
442 else if (strcmp (dn,
443 "cn=Referrals,cn=Statistics,cn=Monitor")
444 == 0)
445 {
446 ldap_submit_derive ("derive", "statistics-referrals",
447 counter, st);
448 }
449 else if (strcmp (dn,
450 "cn=Open,cn=Threads,cn=Monitor")
451 == 0)
452 {
453 ldap_submit_gauge ("threads", "threads-open",
454 info, st);
455 }
456 else if (strcmp (dn,
457 "cn=Starting,cn=Threads,cn=Monitor")
458 == 0)
459 {
460 ldap_submit_gauge ("threads", "threads-starting",
461 info, st);
462 }
463 else if (strcmp (dn,
464 "cn=Active,cn=Threads,cn=Monitor")
465 == 0)
466 {
467 ldap_submit_gauge ("threads", "threads-active",
468 info, st);
469 }
470 else if (strcmp (dn,
471 "cn=Pending,cn=Threads,cn=Monitor")
472 == 0)
473 {
474 ldap_submit_gauge ("threads", "threads-pending",
475 info, st);
476 }
477 else if (strcmp (dn,
478 "cn=Backload,cn=Threads,cn=Monitor")
479 == 0)
480 {
481 ldap_submit_gauge ("threads", "threads-backload",
482 info, st);
483 }
484 else if (strcmp (dn,
485 "cn=Read,cn=Waiters,cn=Monitor")
486 == 0)
487 {
488 ldap_submit_derive ("derive", "waiters-read",
489 counter, st);
490 }
491 else if (strcmp (dn,
492 "cn=Write,cn=Waiters,cn=Monitor")
493 == 0)
494 {
495 ldap_submit_derive ("derive", "waiters-write",
496 counter, st);
497 }
499 ldap_value_free_len (counter_list);
500 ldap_value_free_len (opc_list);
501 ldap_value_free_len (opi_list);
502 ldap_value_free_len (info_list);
503 }
505 ldap_memfree (dn);
506 }
508 ldap_msgfree (result);
509 ldap_unbind_ext_s (st->ld, NULL, NULL);
510 return (0);
511 } /* }}} int ldap_read_host */
513 /* Configuration handling functions {{{
514 *
515 * <Plugin ldap>
516 * <Instance "plugin_instance1">
517 * URL "ldap://localhost"
518 * ...
519 * </Instance>
520 * </Plugin>
521 */
523 static int ldap_config_set_string (char **ret_string, /* {{{ */
524 oconfig_item_t *ci)
525 {
526 char *string;
528 if ((ci->values_num != 1)
529 || (ci->values[0].type != OCONFIG_TYPE_STRING))
530 {
531 WARNING ("openldap plugin: The `%s' config option "
532 "needs exactly one string argument.", ci->key);
533 return (-1);
534 }
536 string = strdup (ci->values[0].value.string);
537 if (string == NULL)
538 {
539 ERROR ("openldap plugin: strdup failed.");
540 return (-1);
541 }
543 if (*ret_string != NULL)
544 free (*ret_string);
545 *ret_string = string;
547 return (0);
548 } /* }}} int ldap_config_set_string */
550 static int ldap_config_set_int (int *ret_int, /* {{{ */
551 oconfig_item_t *ci)
552 {
553 if ((ci->values_num != 1)
554 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
555 {
556 WARNING ("openldap plugin: The `%s' config option "
557 "needs exactly one string argument.", ci->key);
558 return (-1);
559 }
561 *ret_int = ci->values[0].value.number;
563 return (0);
564 } /* }}} int ldap_config_set_int */
566 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
567 oconfig_item_t *ci)
568 {
569 int status = 0;
571 if (ci->values_num != 1)
572 status = -1;
574 if (status == 0)
575 {
576 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
577 *ret_boolean = ci->values[0].value.boolean;
578 else if (ci->values[0].type == OCONFIG_TYPE_STRING)
579 {
580 if (IS_TRUE (ci->values[0].value.string))
581 *ret_boolean = 1;
582 else if (IS_FALSE (ci->values[0].value.string))
583 *ret_boolean = 0;
584 else
585 status = -1;
586 }
587 else
588 status = -1;
589 }
591 if (status != 0)
592 {
593 WARNING ("openldap plugin: The `%s' config option "
594 "needs exactly one boolean argument.", ci->key);
595 return (-1);
596 }
597 return (0);
598 } /* }}} int ldap_config_set_bool */
600 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
601 {
602 ldap_t *st;
603 int i;
604 int status;
606 if ((ci->values_num != 1)
607 || (ci->values[0].type != OCONFIG_TYPE_STRING))
608 {
609 WARNING ("openldap plugin: The `%s' config option "
610 "needs exactly one string argument.", ci->key);
611 return (-1);
612 }
614 st = (ldap_t *) malloc (sizeof (*st));
615 if (st == NULL)
616 {
617 ERROR ("openldap plugin: malloc failed.");
618 return (-1);
619 }
620 memset (st, 0, sizeof (*st));
622 status = ldap_config_set_string (&st->name, ci);
623 if (status != 0)
624 {
625 sfree (st);
626 return (status);
627 }
629 st->timeout = -1;
630 st->verifyhost = 1;
631 st->version = LDAP_VERSION3;
633 for (i = 0; i < ci->children_num; i++)
634 {
635 oconfig_item_t *child = ci->children + i;
637 if (strcasecmp ("CACert", child->key) == 0)
638 status = ldap_config_set_string (&st->cacert, child);
639 else if (strcasecmp ("StartTLS", child->key) == 0)
640 status = ldap_config_set_bool (&st->starttls, child);
641 else if (strcasecmp ("Timeout", child->key) == 0)
642 status = ldap_config_set_int (&st->timeout, child);
643 else if (strcasecmp ("URL", child->key) == 0)
644 status = ldap_config_set_string (&st->url, child);
645 else if (strcasecmp ("VerifyHost", child->key) == 0)
646 status = ldap_config_set_bool (&st->verifyhost, child);
647 else if (strcasecmp ("Version", child->key) == 0)
648 status = ldap_config_set_int (&st->version, child);
649 else
650 {
651 WARNING ("openldap plugin: Option `%s' not allowed here.",
652 child->key);
653 status = -1;
654 }
656 if (status != 0)
657 break;
658 }
660 /* Check if struct is complete.. */
661 if ((status == 0) && (st->url == NULL))
662 {
663 ERROR ("openldap plugin: Instance `%s': "
664 "No URL has been configured.",
665 st->name);
666 status = -1;
667 }
669 /* Check if URL is valid */
670 if ((status == 0) && (st->url != NULL))
671 {
672 LDAPURLDesc *ludpp;
673 int rc;
675 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
676 {
677 ERROR ("openldap plugin: Instance `%s': "
678 "Invalid URL: `%s'",
679 st->name, st->url);
680 status = -1;
681 }
682 else
683 {
684 st->host = strdup (ludpp->lud_host);
685 }
687 ldap_free_urldesc(ludpp);
688 }
690 if (status == 0)
691 {
692 user_data_t ud;
693 char callback_name[3*DATA_MAX_NAME_LEN];
695 memset (&ud, 0, sizeof (ud));
696 ud.data = st;
698 memset (callback_name, 0, sizeof (callback_name));
699 ssnprintf (callback_name, sizeof (callback_name),
700 "openldap/%s/%s",
701 (st->host != NULL) ? st->host : hostname_g,
702 (st->name != NULL) ? st->name : "default"),
704 status = plugin_register_complex_read (/* group = */ NULL,
705 /* name = */ callback_name,
706 /* callback = */ ldap_read_host,
707 /* interval = */ NULL,
708 /* user_data = */ &ud);
709 }
711 if (status != 0)
712 {
713 ldap_free (st);
714 return (-1);
715 }
717 return (0);
718 } /* }}} int ldap_config_add */
720 static int ldap_config (oconfig_item_t *ci) /* {{{ */
721 {
722 int i;
723 int status = 0;
725 for (i = 0; i < ci->children_num; i++)
726 {
727 oconfig_item_t *child = ci->children + i;
729 if (strcasecmp ("Instance", child->key) == 0)
730 ldap_config_add (child);
731 else
732 WARNING ("openldap plugin: The configuration option "
733 "\"%s\" is not allowed here. Did you "
734 "forget to add an <Instance /> block "
735 "around the configuration?",
736 child->key);
737 } /* for (ci->children) */
739 return (status);
740 } /* }}} int ldap_config */
742 /* }}} End of configuration handling functions */
744 static int ldap_init (void) /* {{{ */
745 {
746 /* Initialize LDAP library while still single-threaded as recommended in
747 * ldap_initialize(3) */
748 int debug_level;
749 ldap_get_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
750 return (0);
752 } /* }}} int ldap_init */
754 void module_register (void) /* {{{ */
755 {
756 plugin_register_complex_config ("openldap", ldap_config);
757 plugin_register_init ("openldap", ldap_init);
758 } /* }}} void module_register */