4d8fa68fbafde40523e455368b226b28170aa0c5
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 _Bool starttls;
38 int timeout;
39 char *url;
40 _Bool verifyhost;
41 int version;
43 LDAP *ld;
44 };
45 typedef struct ldap_s ldap_t; /* }}} */
47 static void ldap_free (ldap_t *st) /* {{{ */
48 {
49 if (st == NULL)
50 return;
52 sfree (st->cacert);
53 sfree (st->host);
54 sfree (st->name);
55 sfree (st->url);
56 if (st->ld)
57 ldap_memfree (st->ld);
58 sfree (st);
59 } /* }}} 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_add (oconfig_item_t *ci) /* {{{ */
524 {
525 ldap_t *st;
526 int i;
527 int status;
529 st = malloc (sizeof (*st));
530 if (st == NULL)
531 {
532 ERROR ("openldap plugin: malloc failed.");
533 return (-1);
534 }
535 memset (st, 0, sizeof (*st));
537 status = cf_util_get_string (ci, &st->name);
538 if (status != 0)
539 {
540 sfree (st);
541 return (status);
542 }
544 st->starttls = 0;
545 st->timeout = -1;
546 st->verifyhost = 1;
547 st->version = LDAP_VERSION3;
549 for (i = 0; i < ci->children_num; i++)
550 {
551 oconfig_item_t *child = ci->children + i;
553 if (strcasecmp ("CACert", child->key) == 0)
554 status = cf_util_get_string (child, &st->cacert);
555 else if (strcasecmp ("StartTLS", child->key) == 0)
556 status = cf_util_get_boolean (child, &st->starttls);
557 else if (strcasecmp ("Timeout", child->key) == 0)
558 status = cf_util_get_int (child, &st->timeout);
559 else if (strcasecmp ("URL", child->key) == 0)
560 status = cf_util_get_string (child, &st->url);
561 else if (strcasecmp ("VerifyHost", child->key) == 0)
562 status = cf_util_get_boolean (child, &st->verifyhost);
563 else if (strcasecmp ("Version", child->key) == 0)
564 status = cf_util_get_int (child, &st->version);
565 else
566 {
567 WARNING ("openldap plugin: Option `%s' not allowed here.",
568 child->key);
569 status = -1;
570 }
572 if (status != 0)
573 break;
574 }
576 /* Check if struct is complete.. */
577 if ((status == 0) && (st->url == NULL))
578 {
579 ERROR ("openldap plugin: Instance `%s': "
580 "No URL has been configured.",
581 st->name);
582 status = -1;
583 }
585 /* Check if URL is valid */
586 if ((status == 0) && (st->url != NULL))
587 {
588 LDAPURLDesc *ludpp;
589 int rc;
591 if ((rc = ldap_url_parse (st->url, &ludpp)) != 0)
592 {
593 ERROR ("openldap plugin: Instance `%s': "
594 "Invalid URL: `%s'",
595 st->name, st->url);
596 status = -1;
597 }
598 else
599 {
600 st->host = strdup (ludpp->lud_host);
601 }
603 ldap_free_urldesc (ludpp);
604 }
606 if (status == 0)
607 {
608 user_data_t ud;
609 char callback_name[3*DATA_MAX_NAME_LEN];
611 memset (&ud, 0, sizeof (ud));
612 ud.data = st;
614 memset (callback_name, 0, sizeof (callback_name));
615 ssnprintf (callback_name, sizeof (callback_name),
616 "openldap/%s/%s",
617 (st->host != NULL) ? st->host : hostname_g,
618 (st->name != NULL) ? st->name : "default"),
620 status = plugin_register_complex_read (/* group = */ NULL,
621 /* name = */ callback_name,
622 /* callback = */ ldap_read_host,
623 /* interval = */ NULL,
624 /* user_data = */ &ud);
625 }
627 if (status != 0)
628 {
629 ldap_free (st);
630 return (-1);
631 }
633 return (0);
634 } /* }}} int ldap_config_add */
636 static int ldap_config (oconfig_item_t *ci) /* {{{ */
637 {
638 int i;
639 int status = 0;
641 for (i = 0; i < ci->children_num; i++)
642 {
643 oconfig_item_t *child = ci->children + i;
645 if (strcasecmp ("Instance", child->key) == 0)
646 ldap_config_add (child);
647 else
648 WARNING ("openldap plugin: The configuration option "
649 "\"%s\" is not allowed here. Did you "
650 "forget to add an <Instance /> block "
651 "around the configuration?",
652 child->key);
653 } /* for (ci->children) */
655 return (status);
656 } /* }}} int ldap_config */
658 /* }}} End of configuration handling functions */
660 static int ldap_init (void) /* {{{ */
661 {
662 /* Initialize LDAP library while still single-threaded as recommended in
663 * ldap_initialize(3) */
664 int debug_level;
665 ldap_get_option (NULL, LDAP_OPT_DEBUG_LEVEL, &debug_level);
666 return (0);
667 } /* }}} int ldap_init */
669 void module_register (void) /* {{{ */
670 {
671 plugin_register_complex_config ("openldap", ldap_config);
672 plugin_register_init ("openldap", ldap_init);
673 } /* }}} void module_register */