28f4f5fc8f94261d8153286388c49ff6934f9640
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 } /* }}} 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;
247 ud.free_func = (void *) ldap_free;
249 memset (callback_name, 0, sizeof (callback_name));
250 ssnprintf (callback_name, sizeof (callback_name),
251 "openldap/%s/%s",
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);
260 }
262 if (status != 0)
263 {
264 ldap_free (st);
265 return (-1);
266 }
268 return (0);
269 } /* }}} int ldap_config_add */
271 static int ldap_config (oconfig_item_t *ci)
272 {
273 int i;
274 int status = 0;
276 for (i = 0; i < ci->children_num; i++)
277 {
278 oconfig_item_t *child = ci->children + i;
280 if (strcasecmp ("Instance", child->key) == 0)
281 ldap_config_add (child);
282 else
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?",
287 child->key);
288 } /* for (ci->children) */
290 return (status);
291 } /* int config */
293 /* }}} End of configuration handling functions */
295 /* initialize ldap for each host */
296 static int ldap_init_host (ldap_t *st)
297 {
298 LDAP *ld;
299 int rc;
300 rc = ldap_initialize (&ld, st->url);
301 if (rc != LDAP_SUCCESS)
302 {
303 char errbuf[1024];
304 sstrerror (errno, errbuf, sizeof (errbuf));
305 ERROR ("ldap_initialize failed: %s", errbuf);
306 st->state = 0;
307 return (-1);
308 }
310 st->ld = ld;
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)
318 {
319 int never = LDAP_OPT_X_TLS_NEVER;
320 ldap_set_option (st->ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &never);
321 }
323 if(st->starttls != 0)
324 {
325 rc = ldap_start_tls_s(ld, NULL, NULL);
326 if (rc != LDAP_SUCCESS)
327 {
328 ERROR ("openldap plugin: Failed to start tls on %s: %s",
329 st->url, ldap_err2string (rc));
330 st->state = 0;
331 ldap_destroy(st->ld);
332 return (-1);
333 }
334 }
336 struct berval cred;
337 cred.bv_val = "";
338 cred.bv_len = 0;
340 rc = ldap_sasl_bind_s(st->ld, NULL, NULL, &cred, NULL, NULL, NULL);
341 if (rc != LDAP_SUCCESS)
342 {
343 ERROR ("openldap plugin: Failed to bind to %s: %s",
344 st->url, ldap_err2string (rc));
345 st->state = 0;
346 ldap_destroy(st->ld);
347 return (-1);
348 }
349 else
350 {
351 DEBUG ("openldap plugin: Successfully connected to %s",
352 st->url);
353 st->state = 1;
354 return (0);
355 }
356 } /* static init_host (ldap_t *st) */
358 static void ldap_submit_value (const char *type, const char *type_instance,
359 value_t value, ldap_t *st)
360 {
361 value_list_t vl = VALUE_LIST_INIT;
363 vl.values = &value;
364 vl.values_len = 1;
366 if ((st->host == NULL)
367 || (strcmp ("", st->host) == 0)
368 || (strcmp ("localhost", st->host) == 0))
369 {
370 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
371 }
372 else
373 {
374 sstrncpy (vl.host, st->host, sizeof (vl.host));
375 }
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);
388 } /* ldap_submit_value */
390 static void ldap_submit_derive (const char *type, const char *type_instance,
391 derive_t d, ldap_t *st)
392 {
393 value_t v;
394 v.derive = d;
395 ldap_submit_value (type, type_instance, v, st);
396 } /* void ldap_submit_derive */
398 static void ldap_submit_gauge (const char *type, const char *type_instance,
399 gauge_t g, ldap_t *st)
400 {
401 value_t v;
402 v.gauge = g;
403 ldap_submit_value (type, type_instance, v, st);
404 } /* void ldap_submit_gauge */
406 static int ldap_read_host (user_data_t *ud)
407 {
408 ldap_t *st;
409 LDAPMessage *e, *result;
410 char *dn;
411 int rc;
412 int status;
414 char *attrs[8] = { "monitorCounter",
415 "monitorOpCompleted",
416 "monitorOpInitiated",
417 "monitoredInfo",
418 "olmBDBEntryCache",
419 "olmBDBDNCache",
420 "olmBDBIDLCache",
421 "namingContexts" };
423 if ((ud == NULL) || (ud->data == NULL))
424 {
425 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
426 return (-1);
427 }
429 st = (ldap_t *) ud->data;
431 status = ldap_init_host (st);
432 if (status != 0)
433 return (-1);
435 rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
436 "(|(!(cn=* *))(cn=Database*))", attrs, 0,
437 NULL, NULL, NULL, 0, &result);
439 if (rc != LDAP_SUCCESS)
440 {
441 ERROR ("openldap plugin: Failed to execute search: %s",
442 ldap_err2string (rc));
443 ldap_msgfree (result);
444 return (-1);
445 }
447 for (e = ldap_first_entry (st->ld, result); e != NULL;
448 e = ldap_next_entry (st->ld, e))
449 {
450 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
451 {
452 unsigned long long counter = 0;
453 unsigned long long opc = 0;
454 unsigned long long opi = 0;
456 struct berval counter_data;
457 struct berval opc_data;
458 struct berval opi_data;
459 struct berval info_data;
460 struct berval olmbdb_data;
461 struct berval nc_data;
463 struct berval **counter_list;
464 struct berval **opc_list;
465 struct berval **opi_list;
466 struct berval **info_list;
467 struct berval **olmbdb_list;
468 struct berval **nc_list;
470 if ((counter_list = ldap_get_values_len (st->ld, e,
471 "monitorCounter")) != NULL)
472 {
473 counter_data = *counter_list[0];
474 counter = atoll (counter_data.bv_val);
475 }
477 if ((opc_list = ldap_get_values_len (st->ld, e,
478 "monitorOpCompleted")) != NULL)
479 {
480 opc_data = *opc_list[0];
481 opc = atoll (opc_data.bv_val);
482 }
484 if ((opi_list = ldap_get_values_len (st->ld, e,
485 "monitorOpInitiated")) != NULL)
486 {
487 opi_data = *opi_list[0];
488 opi = atoll (opi_data.bv_val);
489 }
491 if ((info_list = ldap_get_values_len (st->ld, e,
492 "monitoredInfo")) != NULL)
493 {
494 info_data = *info_list[0];
495 // don't convert search result to long long at this point,
496 // because this field is often populated with non-numerical data.
497 }
499 if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
500 == 0)
501 {
502 ldap_submit_derive ("total_connections", NULL,
503 counter, st);
504 }
505 else if (strcmp (dn,
506 "cn=Current,cn=Connections,cn=Monitor")
507 == 0)
508 {
509 ldap_submit_gauge ("current_connections", NULL,
510 counter, st);
511 }
512 else if (strcmp (dn,
513 "cn=Operations,cn=Monitor") == 0)
514 {
515 ldap_submit_derive ("operations",
516 "completed", opc, st);
517 ldap_submit_derive ("operations",
518 "initiated", opi, st);
519 }
520 else if (strcmp (dn,
521 "cn=Bind,cn=Operations,cn=Monitor")
522 == 0)
523 {
524 ldap_submit_derive ("operations",
525 "bind-completed", opc, st);
526 ldap_submit_derive ("operations",
527 "bind-initiated", opi, st);
528 }
529 else if (strcmp (dn,
530 "cn=UnBind,cn=Operations,cn=Monitor")
531 == 0)
532 {
533 ldap_submit_derive ("operations",
534 "unbind-completed", opc, st);
535 ldap_submit_derive ("operations",
536 "unbind-initiated", opi, st);
537 }
538 else if (strcmp (dn,
539 "cn=Search,cn=Operations,cn=Monitor")
540 == 0)
541 {
542 ldap_submit_derive ("operations",
543 "search-completed", opc, st);
544 ldap_submit_derive ("operations",
545 "search-initiated", opi, st);
546 }
547 else if (strcmp (dn,
548 "cn=Compare,cn=Operations,cn=Monitor")
549 == 0)
550 {
551 ldap_submit_derive ("operations",
552 "compare-completed", opc, st);
553 ldap_submit_derive ("operations",
554 "compare-initiated", opi, st);
555 }
556 else if (strcmp (dn,
557 "cn=Modify,cn=Operations,cn=Monitor")
558 == 0)
559 {
560 ldap_submit_derive ("operations",
561 "modify-completed", opc, st);
562 ldap_submit_derive ("operations",
563 "modify-initiated", opi, st);
564 }
565 else if (strcmp (dn,
566 "cn=Modrdn,cn=Operations,cn=Monitor")
567 == 0)
568 {
569 ldap_submit_derive ("operations",
570 "modrdn-completed", opc, st);
571 ldap_submit_derive ("operations",
572 "modrdn-initiated", opi, st);
573 }
574 else if (strcmp (dn,
575 "cn=Add,cn=Operations,cn=Monitor")
576 == 0)
577 {
578 ldap_submit_derive ("operations",
579 "add-completed", opc, st);
580 ldap_submit_derive ("operations",
581 "add-initiated", opi, st);
582 }
583 else if (strcmp (dn,
584 "cn=Delete,cn=Operations,cn=Monitor")
585 == 0)
586 {
587 ldap_submit_derive ("operations",
588 "delete-completed", opc, st);
589 ldap_submit_derive ("operations",
590 "delete-initiated", opi, st);
591 }
592 else if (strcmp (dn,
593 "cn=Abandon,cn=Operations,cn=Monitor")
594 == 0)
595 {
596 ldap_submit_derive ("operations",
597 "abandon-completed", opc, st);
598 ldap_submit_derive ("operations",
599 "abandon-initiated", opi, st);
600 }
601 else if (strcmp (dn,
602 "cn=Extended,cn=Operations,cn=Monitor")
603 == 0)
604 {
605 ldap_submit_derive ("operations",
606 "extended-completed", opc, st);
607 ldap_submit_derive ("operations",
608 "extended-initiated", opi, st);
609 }
610 else if ((strncmp (dn, "cn=Database", 11) == 0)
611 && ((nc_list = ldap_get_values_len
612 (st->ld, e, "namingContexts")) != NULL))
613 {
614 nc_data = *nc_list[0];
615 char typeinst[DATA_MAX_NAME_LEN];
617 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
618 "olmBDBEntryCache")) != NULL)
619 {
620 olmbdb_data = *olmbdb_list[0];
621 ssnprintf (typeinst, sizeof (typeinst),
622 "bdbentrycache-%s", nc_data.bv_val);
623 ldap_submit_gauge ("cache_size", typeinst,
624 atoll (olmbdb_data.bv_val), st);
625 ldap_value_free_len (olmbdb_list);
626 }
628 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
629 "olmBDBDNCache")) != NULL)
630 {
631 olmbdb_data = *olmbdb_list[0];
632 ssnprintf (typeinst, sizeof (typeinst),
633 "bdbdncache-%s", nc_data.bv_val);
634 ldap_submit_gauge ("cache_size", typeinst,
635 atoll (olmbdb_data.bv_val), st);
636 ldap_value_free_len (olmbdb_list);
637 }
639 if ((olmbdb_list = ldap_get_values_len (st->ld, e,
640 "olmBDBIDLCache")) != NULL)
641 {
642 olmbdb_data = *olmbdb_list[0];
643 ssnprintf (typeinst, sizeof (typeinst),
644 "bdbidlcache-%s", nc_data.bv_val);
645 ldap_submit_gauge ("cache_size", typeinst,
646 atoll (olmbdb_data.bv_val), st);
647 ldap_value_free_len (olmbdb_list);
648 }
650 ldap_value_free_len (nc_list);
651 }
652 else if (strcmp (dn,
653 "cn=Bytes,cn=Statistics,cn=Monitor")
654 == 0)
655 {
656 ldap_submit_derive ("derive", "statistics-bytes",
657 counter, st);
658 }
659 else if (strcmp (dn,
660 "cn=PDU,cn=Statistics,cn=Monitor")
661 == 0)
662 {
663 ldap_submit_derive ("derive", "statistics-pdu",
664 counter, st);
665 }
666 else if (strcmp (dn,
667 "cn=Entries,cn=Statistics,cn=Monitor")
668 == 0)
669 {
670 ldap_submit_derive ("derive", "statistics-entries",
671 counter, st);
672 }
673 else if (strcmp (dn,
674 "cn=Referrals,cn=Statistics,cn=Monitor")
675 == 0)
676 {
677 ldap_submit_derive ("derive", "statistics-referrals",
678 counter, st);
679 }
680 else if (strcmp (dn,
681 "cn=Open,cn=Threads,cn=Monitor")
682 == 0)
683 {
684 ldap_submit_gauge ("threads", "threads-open",
685 atoll (info_data.bv_val), st);
686 }
687 else if (strcmp (dn,
688 "cn=Starting,cn=Threads,cn=Monitor")
689 == 0)
690 {
691 ldap_submit_gauge ("threads", "threads-starting",
692 atoll (info_data.bv_val), st);
693 }
694 else if (strcmp (dn,
695 "cn=Active,cn=Threads,cn=Monitor")
696 == 0)
697 {
698 ldap_submit_gauge ("threads", "threads-active",
699 atoll (info_data.bv_val), st);
700 }
701 else if (strcmp (dn,
702 "cn=Pending,cn=Threads,cn=Monitor")
703 == 0)
704 {
705 ldap_submit_gauge ("threads", "threads-pending",
706 atoll (info_data.bv_val), st);
707 }
708 else if (strcmp (dn,
709 "cn=Backload,cn=Threads,cn=Monitor")
710 == 0)
711 {
712 ldap_submit_gauge ("threads", "threads-backload",
713 atoll (info_data.bv_val), st);
714 }
715 else if (strcmp (dn,
716 "cn=Read,cn=Waiters,cn=Monitor")
717 == 0)
718 {
719 ldap_submit_derive ("derive", "waiters-read",
720 counter, st);
721 }
722 else if (strcmp (dn,
723 "cn=Write,cn=Waiters,cn=Monitor")
724 == 0)
725 {
726 ldap_submit_derive ("derive", "waiters-write",
727 counter, st);
728 }
730 ldap_value_free_len (counter_list);
731 ldap_value_free_len (opc_list);
732 ldap_value_free_len (opi_list);
733 ldap_value_free_len (info_list);
734 }
736 ldap_memfree (dn);
737 }
739 ldap_msgfree (result);
740 ldap_unbind_ext_s (st->ld, NULL, NULL);
741 return (0);
742 } /* int ldap_read_host */
744 void module_register (void)
745 {
746 plugin_register_complex_config ("openldap", ldap_config);
747 } /* void module_register */