391ec8348904035a4d9cad694e8f214ab6673924
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 int ldap_read_host (user_data_t *ud);
50 static void ldap_free (ldap_t *st) /* {{{ */
51 {
52 if(st == NULL)
53 return;
55 sfree (st->cacert);
56 sfree (st->host);
57 sfree (st->name);
58 sfree (st->url);
59 if(st->ld)
60 ldap_memfree(st->ld);
61 sfree (st);
62 } /* }}} void ldap_free */
64 /* Configuration handling functions {{{
65 *
66 * <Plugin ldap>
67 * <Instance "plugin_instance1">
68 * URL "ldap://localhost"
69 * ...
70 * </Instance>
71 * </Plugin>
72 */
74 static int ldap_config_set_string (char **ret_string, /* {{{ */
75 oconfig_item_t *ci)
76 {
77 char *string;
79 if ((ci->values_num != 1)
80 || (ci->values[0].type != OCONFIG_TYPE_STRING))
81 {
82 WARNING ("openldap plugin: The `%s' config option "
83 "needs exactly one string argument.", ci->key);
84 return (-1);
85 }
87 string = strdup (ci->values[0].value.string);
88 if (string == NULL)
89 {
90 ERROR ("openldap plugin: strdup failed.");
91 return (-1);
92 }
94 if (*ret_string != NULL)
95 free (*ret_string);
96 *ret_string = string;
98 return (0);
99 } /* }}} int ldap_config_set_string */
101 static int ldap_config_set_int (int *ret_int, /* {{{ */
102 oconfig_item_t *ci)
103 {
104 if ((ci->values_num != 1)
105 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
106 {
107 WARNING ("openldap plugin: The `%s' config option "
108 "needs exactly one string argument.", ci->key);
109 return (-1);
110 }
112 *ret_int = ci->values[0].value.number;
114 return (0);
115 } /* }}} int ldap_config_set_int */
117 static int ldap_config_set_bool (int *ret_boolean, /* {{{ */
118 oconfig_item_t *ci)
119 {
120 int status = 0;
122 if (ci->values_num != 1)
123 status = -1;
125 if (status == 0)
126 {
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)
130 {
131 if (IS_TRUE (ci->values[0].value.string))
132 *ret_boolean = 1;
133 else if (IS_FALSE (ci->values[0].value.string))
134 *ret_boolean = 0;
135 else
136 status = -1;
137 }
138 else
139 status = -1;
140 }
142 if (status != 0)
143 {
144 WARNING ("openldap plugin: The `%s' config option "
145 "needs exactly one boolean argument.", ci->key);
146 return (-1);
147 }
148 return (0);
149 } /* }}} int ldap_config_set_bool */
151 static int ldap_config_add (oconfig_item_t *ci) /* {{{ */
152 {
153 ldap_t *st;
154 int i;
155 int status;
157 if ((ci->values_num != 1)
158 || (ci->values[0].type != OCONFIG_TYPE_STRING))
159 {
160 WARNING ("openldap plugin: The `%s' config option "
161 "needs exactly one string argument.", ci->key);
162 return (-1);
163 }
165 st = (ldap_t *) malloc (sizeof (*st));
166 if (st == NULL)
167 {
168 ERROR ("openldap plugin: malloc failed.");
169 return (-1);
170 }
171 memset (st, 0, sizeof (*st));
173 status = ldap_config_set_string (&st->name, ci);
174 if (status != 0)
175 {
176 sfree (st);
177 return (status);
178 }
180 st->verifyhost = 1;
181 st->version = LDAP_VERSION3;
183 for (i = 0; i < ci->children_num; i++)
184 {
185 oconfig_item_t *child = ci->children + i;
187 if (strcasecmp ("CACert", child->key) == 0)
188 status = ldap_config_set_string (&st->cacert, child);
189 else if (strcasecmp ("StartTLS", child->key) == 0)
190 status = ldap_config_set_bool (&st->starttls, child);
191 else if (strcasecmp ("Timeout", child->key) == 0)
192 status = ldap_config_set_int (&st->timeout, child);
193 else if (strcasecmp ("URL", child->key) == 0)
194 status = ldap_config_set_string (&st->url, child);
195 else if (strcasecmp ("VerifyHost", child->key) == 0)
196 status = ldap_config_set_bool (&st->verifyhost, child);
197 else if (strcasecmp ("Version", child->key) == 0)
198 status = ldap_config_set_int (&st->version, child);
199 else
200 {
201 WARNING ("openldap plugin: Option `%s' not allowed here.",
202 child->key);
203 status = -1;
204 }
206 if (status != 0)
207 break;
208 }
210 /* Check if struct is complete.. */
211 if ((status == 0) && (st->url == NULL))
212 {
213 ERROR ("openldap plugin: Instance `%s': "
214 "No URL has been configured.",
215 st->name);
216 status = -1;
217 }
219 /* Check if URL is valid */
220 if ((status == 0) && (st->url != NULL))
221 {
222 LDAPURLDesc *ludpp;
223 int rc;
225 if ((rc = ldap_url_parse( st->url, &ludpp)) != 0)
226 {
227 ERROR ("openldap plugin: Instance `%s': "
228 "Invalid URL: `%s'",
229 st->name, st->url);
230 status = -1;
231 }
232 else
233 {
234 st->host = strdup (ludpp->lud_host);
235 }
237 ldap_free_urldesc(ludpp);
238 }
240 if (status == 0)
241 {
242 user_data_t ud;
243 char callback_name[3*DATA_MAX_NAME_LEN];
245 memset (&ud, 0, sizeof (ud));
246 ud.data = st;
248 memset (callback_name, 0, sizeof (callback_name));
249 ssnprintf (callback_name, sizeof (callback_name),
250 "openldap/%s/%s",
251 (st->host != NULL) ? st->host : hostname_g,
252 (st->name != NULL) ? st->name : "default"),
254 status = plugin_register_complex_read (/* group = */ NULL,
255 /* name = */ callback_name,
256 /* callback = */ ldap_read_host,
257 /* interval = */ NULL,
258 /* user_data = */ &ud);
259 }
261 if (status != 0)
262 {
263 ldap_free (st);
264 return (-1);
265 }
267 return (0);
268 } /* }}} int ldap_config_add */
270 static int ldap_config (oconfig_item_t *ci) /* {{{ */
271 {
272 int i;
273 int status = 0;
275 for (i = 0; i < ci->children_num; i++)
276 {
277 oconfig_item_t *child = ci->children + i;
279 if (strcasecmp ("Instance", child->key) == 0)
280 ldap_config_add (child);
281 else
282 WARNING ("openldap plugin: The configuration option "
283 "\"%s\" is not allowed here. Did you "
284 "forget to add an <Instance /> block "
285 "around the configuration?",
286 child->key);
287 } /* for (ci->children) */
289 return (status);
290 } /* }}} int ldap_config */
292 /* }}} End of configuration handling functions */
294 /* initialize ldap for each host */
295 static int ldap_init_host (ldap_t *st) /* {{{ */
296 {
297 LDAP *ld;
298 int rc;
299 rc = ldap_initialize (&ld, st->url);
300 if (rc != LDAP_SUCCESS)
301 {
302 char errbuf[1024];
303 sstrerror (errno, errbuf, sizeof (errbuf));
304 ERROR ("ldap_initialize failed: %s", errbuf);
305 st->state = 0;
306 return (-1);
307 }
309 st->ld = ld;
311 ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
313 if(st->cacert != NULL)
314 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
316 if(st->verifyhost == 0)
317 {
318 int never = LDAP_OPT_X_TLS_NEVER;
319 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
320 }
322 if(st->starttls != 0)
323 {
324 rc = ldap_start_tls_s(ld, NULL, NULL);
325 if (rc != LDAP_SUCCESS)
326 {
327 ERROR ("openldap plugin: Failed to start tls on %s: %s",
328 st->url, ldap_err2string (rc));
329 st->state = 0;
330 ldap_destroy(st->ld);
331 return (-1);
332 }
333 }
335 struct berval cred;
336 cred.bv_val = "";
337 cred.bv_len = 0;
339 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
340 if (rc != LDAP_SUCCESS)
341 {
342 ERROR ("openldap plugin: Failed to bind to %s: %s",
343 st->url, ldap_err2string (rc));
344 st->state = 0;
345 ldap_destroy(st->ld);
346 return (-1);
347 }
348 else
349 {
350 DEBUG ("openldap plugin: Successfully connected to %s",
351 st->url);
352 st->state = 1;
353 return (0);
354 }
355 } /* }}} static ldap_init_host */
357 static void ldap_submit_value (const char *type, const char *type_instance, /* {{{ */
358 value_t value, ldap_t *st)
359 {
360 value_list_t vl = VALUE_LIST_INIT;
362 vl.values = &value;
363 vl.values_len = 1;
365 if ((st->host == NULL)
366 || (strcmp ("", st->host) == 0)
367 || (strcmp ("localhost", st->host) == 0))
368 {
369 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
370 }
371 else
372 {
373 sstrncpy (vl.host, st->host, sizeof (vl.host));
374 }
376 sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
377 if (st->name != NULL)
378 sstrncpy (vl.plugin_instance, st->name,
379 sizeof (vl.plugin_instance));
381 sstrncpy (vl.type, type, sizeof (vl.type));
382 if (type_instance != NULL)
383 sstrncpy (vl.type_instance, type_instance,
384 sizeof (vl.type_instance));
386 plugin_dispatch_values (&vl);
387 } /* }}} void ldap_submit_value */
389 static void ldap_submit_derive (const char *type, const char *type_instance, /* {{{ */
390 derive_t d, ldap_t *st)
391 {
392 value_t v;
393 v.derive = d;
394 ldap_submit_value (type, type_instance, v, st);
395 } /* }}} void ldap_submit_derive */
397 static void ldap_submit_gauge (const char *type, const char *type_instance, /* {{{ */
398 gauge_t g, ldap_t *st)
399 {
400 value_t v;
401 v.gauge = g;
402 ldap_submit_value (type, type_instance, v, st);
403 } /* }}} void ldap_submit_gauge */
405 static int ldap_read_host (user_data_t *ud) /* {{{ */
406 {
407 ldap_t *st;
408 LDAPMessage *e, *result;
409 char *dn;
410 int rc;
411 int status;
413 char *attrs[8] = { "monitorCounter",
414 "monitorOpCompleted",
415 "monitorOpInitiated",
416 "monitoredInfo",
417 "olmBDBEntryCache",
418 "olmBDBDNCache",
419 "olmBDBIDLCache",
420 "namingContexts" };
422 if ((ud == NULL) || (ud->data == NULL))
423 {
424 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
425 return (-1);
426 }
428 st = (ldap_t *) ud->data;
430 status = ldap_init_host (st);
431 if (status != 0)
432 return (-1);
434 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
435 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
436 NULL, NULL, NULL, 0, &result);
438 if (rc != LDAP_SUCCESS)
439 {
440 ERROR ("openldap plugin: Failed to execute search: %s",
441 ldap_err2string (rc));
442 ldap_msgfree (result);
443 return (-1);
444 }
446 for (e = ldap_first_entry (st->ld, result); e != NULL;
447 e = ldap_next_entry (st->ld, e))
448 {
449 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
450 {
451 unsigned long long counter = 0;
452 unsigned long long opc = 0;
453 unsigned long long opi = 0;
455 struct berval counter_data;
456 struct berval opc_data;
457 struct berval opi_data;
458 struct berval info_data;
459 struct berval olmbdb_data;
460 struct berval nc_data;
462 struct berval **counter_list;
463 struct berval **opc_list;
464 struct berval **opi_list;
465 struct berval **info_list;
466 struct berval **olmbdb_list;
467 struct berval **nc_list;
469 if ((counter_list = ldap_get_values_len (st->ld, e,
470 "monitorCounter")) != NULL)
471 {
472 counter_data = *counter_list[0];
473 counter = atoll (counter_data.bv_val);
474 }
476 if ((opc_list = ldap_get_values_len (st->ld, e,
477 "monitorOpCompleted")) != NULL)
478 {
479 opc_data = *opc_list[0];
480 opc = atoll (opc_data.bv_val);
481 }
483 if ((opi_list = ldap_get_values_len (st->ld, e,
484 "monitorOpInitiated")) != NULL)
485 {
486 opi_data = *opi_list[0];
487 opi = atoll (opi_data.bv_val);
488 }
490 if ((info_list = ldap_get_values_len (st->ld, e,
491 "monitoredInfo")) != NULL)
492 {
493 info_data = *info_list[0];
494 // don't convert search result to long long at this point,
495 // because this field is often populated with non-numerical data.
496 }
498 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
499 == 0)
500 {
501 ldap_submit_derive ("total_connections", NULL,
502 counter, st);
503 }
504 else if (strcmp (dn,
505 "cn=Current,cn=Connections,cn=Monitor")
506 == 0)
507 {
508 ldap_submit_gauge ("current_connections", NULL,
509 counter, st);
510 }
511 else if (strcmp (dn,
512 "cn=Operations,cn=Monitor") == 0)
513 {
514 ldap_submit_derive ("operations",
515 "completed", opc, st);
516 ldap_submit_derive ("operations",
517 "initiated", opi, st);
518 }
519 else if (strcmp (dn,
520 "cn=Bind,cn=Operations,cn=Monitor")
521 == 0)
522 {
523 ldap_submit_derive ("operations",
524 "bind-completed", opc, st);
525 ldap_submit_derive ("operations",
526 "bind-initiated", opi, st);
527 }
528 else if (strcmp (dn,
529 "cn=UnBind,cn=Operations,cn=Monitor")
530 == 0)
531 {
532 ldap_submit_derive ("operations",
533 "unbind-completed", opc, st);
534 ldap_submit_derive ("operations",
535 "unbind-initiated", opi, st);
536 }
537 else if (strcmp (dn,
538 "cn=Search,cn=Operations,cn=Monitor")
539 == 0)
540 {
541 ldap_submit_derive ("operations",
542 "search-completed", opc, st);
543 ldap_submit_derive ("operations",
544 "search-initiated", opi, st);
545 }
546 else if (strcmp (dn,
547 "cn=Compare,cn=Operations,cn=Monitor")
548 == 0)
549 {
550 ldap_submit_derive ("operations",
551 "compare-completed", opc, st);
552 ldap_submit_derive ("operations",
553 "compare-initiated", opi, st);
554 }
555 else if (strcmp (dn,
556 "cn=Modify,cn=Operations,cn=Monitor")
557 == 0)
558 {
559 ldap_submit_derive ("operations",
560 "modify-completed", opc, st);
561 ldap_submit_derive ("operations",
562 "modify-initiated", opi, st);
563 }
564 else if (strcmp (dn,
565 "cn=Modrdn,cn=Operations,cn=Monitor")
566 == 0)
567 {
568 ldap_submit_derive ("operations",
569 "modrdn-completed", opc, st);
570 ldap_submit_derive ("operations",
571 "modrdn-initiated", opi, st);
572 }
573 else if (strcmp (dn,
574 "cn=Add,cn=Operations,cn=Monitor")
575 == 0)
576 {
577 ldap_submit_derive ("operations",
578 "add-completed", opc, st);
579 ldap_submit_derive ("operations",
580 "add-initiated", opi, st);
581 }
582 else if (strcmp (dn,
583 "cn=Delete,cn=Operations,cn=Monitor")
584 == 0)
585 {
586 ldap_submit_derive ("operations",
587 "delete-completed", opc, st);
588 ldap_submit_derive ("operations",
589 "delete-initiated", opi, st);
590 }
591 else if (strcmp (dn,
592 "cn=Abandon,cn=Operations,cn=Monitor")
593 == 0)
594 {
595 ldap_submit_derive ("operations",
596 "abandon-completed", opc, st);
597 ldap_submit_derive ("operations",
598 "abandon-initiated", opi, st);
599 }
600 else if (strcmp (dn,
601 "cn=Extended,cn=Operations,cn=Monitor")
602 == 0)
603 {
604 ldap_submit_derive ("operations",
605 "extended-completed", opc, st);
606 ldap_submit_derive ("operations",
607 "extended-initiated", opi, st);
608 }
609 else if ((strncmp (dn, "cn=Database", 11) == 0)
610 && ((nc_list = ldap_get_values_len
611 (st->ld, e, "namingContexts")) != NULL))
612 {
613 nc_data = *nc_list[0];
614 char typeinst[DATA_MAX_NAME_LEN];
616 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
617 "olmBDBEntryCache")) != NULL)
618 {
619 olmbdb_data = *olmbdb_list[0];
620 ssnprintf (typeinst, sizeof (typeinst),
621 "bdbentrycache-%s", nc_data.bv_val);
622 ldap_submit_gauge ("cache_size", typeinst,
623 atoll (olmbdb_data.bv_val), st);
624 ldap_value_free_len (olmbdb_list);
625 }
627 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
628 "olmBDBDNCache")) != NULL)
629 {
630 olmbdb_data = *olmbdb_list[0];
631 ssnprintf (typeinst, sizeof (typeinst),
632 "bdbdncache-%s", nc_data.bv_val);
633 ldap_submit_gauge ("cache_size", typeinst,
634 atoll (olmbdb_data.bv_val), st);
635 ldap_value_free_len (olmbdb_list);
636 }
638 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
639 "olmBDBIDLCache")) != NULL)
640 {
641 olmbdb_data = *olmbdb_list[0];
642 ssnprintf (typeinst, sizeof (typeinst),
643 "bdbidlcache-%s", nc_data.bv_val);
644 ldap_submit_gauge ("cache_size", typeinst,
645 atoll (olmbdb_data.bv_val), st);
646 ldap_value_free_len (olmbdb_list);
647 }
649 ldap_value_free_len (nc_list);
650 }
651 else if (strcmp (dn,
652 "cn=Bytes,cn=Statistics,cn=Monitor")
653 == 0)
654 {
655 ldap_submit_derive ("derive", "statistics-bytes",
656 counter, st);
657 }
658 else if (strcmp (dn,
659 "cn=PDU,cn=Statistics,cn=Monitor")
660 == 0)
661 {
662 ldap_submit_derive ("derive", "statistics-pdu",
663 counter, st);
664 }
665 else if (strcmp (dn,
666 "cn=Entries,cn=Statistics,cn=Monitor")
667 == 0)
668 {
669 ldap_submit_derive ("derive", "statistics-entries",
670 counter, st);
671 }
672 else if (strcmp (dn,
673 "cn=Referrals,cn=Statistics,cn=Monitor")
674 == 0)
675 {
676 ldap_submit_derive ("derive", "statistics-referrals",
677 counter, st);
678 }
679 else if (strcmp (dn,
680 "cn=Open,cn=Threads,cn=Monitor")
681 == 0)
682 {
683 ldap_submit_gauge ("threads", "threads-open",
684 atoll (info_data.bv_val), st);
685 }
686 else if (strcmp (dn,
687 "cn=Starting,cn=Threads,cn=Monitor")
688 == 0)
689 {
690 ldap_submit_gauge ("threads", "threads-starting",
691 atoll (info_data.bv_val), st);
692 }
693 else if (strcmp (dn,
694 "cn=Active,cn=Threads,cn=Monitor")
695 == 0)
696 {
697 ldap_submit_gauge ("threads", "threads-active",
698 atoll (info_data.bv_val), st);
699 }
700 else if (strcmp (dn,
701 "cn=Pending,cn=Threads,cn=Monitor")
702 == 0)
703 {
704 ldap_submit_gauge ("threads", "threads-pending",
705 atoll (info_data.bv_val), st);
706 }
707 else if (strcmp (dn,
708 "cn=Backload,cn=Threads,cn=Monitor")
709 == 0)
710 {
711 ldap_submit_gauge ("threads", "threads-backload",
712 atoll (info_data.bv_val), st);
713 }
714 else if (strcmp (dn,
715 "cn=Read,cn=Waiters,cn=Monitor")
716 == 0)
717 {
718 ldap_submit_derive ("derive", "waiters-read",
719 counter, st);
720 }
721 else if (strcmp (dn,
722 "cn=Write,cn=Waiters,cn=Monitor")
723 == 0)
724 {
725 ldap_submit_derive ("derive", "waiters-write",
726 counter, st);
727 }
729 ldap_value_free_len (counter_list);
730 ldap_value_free_len (opc_list);
731 ldap_value_free_len (opi_list);
732 ldap_value_free_len (info_list);
733 }
735 ldap_memfree (dn);
736 }
738 ldap_msgfree (result);
739 ldap_unbind_ext_s (st->ld, NULL, NULL);
740 return (0);
741 } /* }}} int ldap_read_host */
743 void module_register (void) /* {{{ */
744 {
745 plugin_register_complex_config ("openldap", ldap_config);
746 } /* }}} void module_register */