166dcec34ff2781c509b13980834b95bd02de8e9
[collectd.git] / src / openldap.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 #include <lber.h>
28 #include <ldap.h>
29
30 struct ldap_s /* {{{ */
31 {
32         char *name;
33
34         char *cacert;
35         char *host;
36         int   state;
37         int   starttls;
38         int   timeout;
39         char *url;
40         int   verifyhost;
41         int   version;
42
43         LDAP *ld;
44         char *dn;
45 };
46 typedef struct ldap_s ldap_t; /* }}} */
47
48 static int ldap_read_host (user_data_t *ud);
49
50 static void ldap_free (ldap_t *st) /* {{{ */
51 {
52         if(st == NULL)
53                 return;
54
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 */
63
64 /* Configuration handling functions {{{
65  *
66  * <Plugin ldap>
67  *   <Instance "plugin_instance1">
68  *     URL "ldap://localhost"
69  *     ...
70  *   </Instance>
71  * </Plugin>
72  */
73
74 static int config_set_string (char **ret_string, /* {{{ */
75                                     oconfig_item_t *ci)
76 {
77         char *string;
78
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         }
86
87         string = strdup (ci->values[0].value.string);
88         if (string == NULL)
89         {
90                 ERROR ("openldap plugin: strdup failed.");
91                 return (-1);
92         }
93
94         if (*ret_string != NULL)
95                 free (*ret_string);
96         *ret_string = string;
97
98         return (0);
99 } /* }}} int config_set_string */
100
101 static int 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         }
111
112         *ret_int = ci->values[0].value.number;
113
114         return (0);
115 } /* }}} int config_set_int */
116
117 static int config_set_bool (int *ret_boolean, /* {{{ */
118                                 oconfig_item_t *ci)
119 {
120         int status = 0;
121
122         if (ci->values_num != 1)
123                 status = -1;
124
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         }
141
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 } /* }}} config_set_bool */
150
151 static int config_add (oconfig_item_t *ci) /* {{{ */
152 {
153         ldap_t *st;
154         int i;
155         int status;
156
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         }
164
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));
172
173         status = config_set_string (&st->name, ci);
174         if (status != 0)
175         {
176                 sfree (st);
177                 return (status);
178         }
179
180         st->verifyhost = 1;
181         st->version = LDAP_VERSION3;
182
183         for (i = 0; i < ci->children_num; i++)
184         {
185                 oconfig_item_t *child = ci->children + i;
186
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);
199                 else
200                 {
201                         WARNING ("openldap plugin: Option `%s' not allowed here.",
202                                         child->key);
203                         status = -1;
204                 }
205
206                 if (status != 0)
207                         break;
208         }
209
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         }
218
219         /* Check if URL is valid */
220         if ((status == 0) && (st->url != NULL))
221         {
222                 LDAPURLDesc *ludpp;
223                 int rc;
224
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                 }
236
237                 ldap_free_urldesc(ludpp);
238         }
239
240         if (status == 0)
241         {
242                 user_data_t ud;
243                 char callback_name[3*DATA_MAX_NAME_LEN];
244
245                 memset (&ud, 0, sizeof (ud));
246                 ud.data = st;
247                 ud.free_func = (void *) ldap_free;
248
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"),
254
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         }
261
262         if (status != 0)
263         {
264                 ldap_free (st);
265                 return (-1);
266         }
267
268         return (0);
269 } /* }}} int config_add */
270
271 static int config (oconfig_item_t *ci)
272 {
273         int i;
274         int status = 0;
275
276         for (i = 0; i < ci->children_num; i++)
277         {
278                 oconfig_item_t *child = ci->children + i;
279
280                 if (strcasecmp ("Instance", child->key) == 0)
281                         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) */
289
290         return (status);
291 } /* int config */
292
293 /* }}} End of configuration handling functions */
294
295 /* initialize ldap for each host */
296 static int 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         }
309
310         st->ld = ld;
311
312         ldap_set_option (st->ld, LDAP_OPT_PROTOCOL_VERSION, &st->version);
313
314         if(st->cacert != NULL)
315                 ldap_set_option (st->ld, LDAP_OPT_X_TLS_CACERTFILE, st->cacert);
316
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         }
322
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                         return (-1);
332                 }
333         }
334
335         struct berval cred;
336         cred.bv_val = "";
337         cred.bv_len = 0;
338
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                 return (-1);
346         }
347         else
348         {
349                 DEBUG ("openldap plugin: Successfully connected to %s",
350                                 st->url);
351                 st->state = 1;
352                 return (0);
353         }
354 } /* static init_host (ldap_t *st) */
355
356 static void submit_value (const char *type, const char *type_instance,
357                 value_t value, ldap_t *st)
358 {
359         value_list_t vl = VALUE_LIST_INIT;
360
361         vl.values     = &value;
362         vl.values_len = 1;
363
364         if ((st->host == NULL)
365                         || (strcmp ("", st->host) == 0)
366                         || (strcmp ("localhost", st->host) == 0))
367         {
368                 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
369         }
370         else
371         {
372                 sstrncpy (vl.host, st->host, sizeof (vl.host));
373         }
374
375         sstrncpy (vl.plugin, "openldap", sizeof (vl.plugin));
376         if (st->name != NULL)
377                 sstrncpy (vl.plugin_instance, st->name,
378                                 sizeof (vl.plugin_instance));
379
380         sstrncpy (vl.type, type, sizeof (vl.type));
381         if (type_instance != NULL)
382                 sstrncpy (vl.type_instance, type_instance,
383                                 sizeof (vl.type_instance));
384
385         plugin_dispatch_values (&vl);
386 } /* submit */
387
388 static void submit_derive (const char *type, const char *type_instance,
389                 derive_t d, ldap_t *st)
390 {
391         value_t v;
392         v.derive = d;
393         submit_value (type, type_instance, v, st);
394 } /* void submit_derive */
395
396 static void submit_gauge (const char *type, const char *type_instance,
397                 gauge_t g, ldap_t *st)
398 {
399         value_t v;
400         v.gauge = g;
401         submit_value (type, type_instance, v, st);
402 } /* void submit_gauge */
403
404 static int ldap_read_host (user_data_t *ud)
405 {
406         ldap_t *st;
407         LDAPMessage *e, *result;
408         char *dn;
409         int rc;
410         int status;
411
412         char *attrs[3] = { "monitorCounter",
413                                 "monitorOpCompleted",
414                                 "monitorOpInitiated" };
415
416         if ((ud == NULL) || (ud->data == NULL))
417         {
418                 ERROR ("openldap plugin: ldap_read_host: Invalid user data.");
419                 return (-1);
420         }
421
422         st = (ldap_t *) ud->data;
423
424         status = init_host (st);
425         if (status != 0)
426                 return (-1);
427
428         rc = ldap_search_ext_s (st->ld, "cn=Monitor", LDAP_SCOPE_SUBTREE,
429                 "(!(cn=* *))", attrs, 0,
430                 NULL, NULL, NULL, 0, &result);
431
432         if (rc != LDAP_SUCCESS)
433         {
434                 ERROR ("openldap plugin: Failed to execute search: %s",
435                                 ldap_err2string (rc));
436                 ldap_msgfree (result);
437                 return (-1);
438         }
439
440         for (e = ldap_first_entry (st->ld, result); e != NULL;
441                 e = ldap_next_entry (st->ld, e))
442         {
443                 if ((dn = ldap_get_dn (st->ld, e)) != NULL)
444                 {
445                         unsigned long long counter = 0;
446                         unsigned long long opc = 0;
447                         unsigned long long opi = 0;
448
449                         struct berval counter_data;
450                         struct berval opc_data;
451                         struct berval opi_data;
452
453                         struct berval **counter_list;
454                         struct berval **opc_list;
455                         struct berval **opi_list;
456
457                         if ((counter_list = ldap_get_values_len (st->ld, e,
458                                 "monitorCounter")) != NULL)
459                         {
460                                 counter_data = *counter_list[0];
461                                 counter = atoll (counter_data.bv_val);
462                         }
463
464                         if ((opc_list = ldap_get_values_len (st->ld, e,
465                                 "monitorOpCompleted")) != NULL)
466                         {
467                                 opc_data = *opc_list[0];
468                                 opc = atoll (opc_data.bv_val);
469                         }
470
471                         if ((opi_list = ldap_get_values_len (st->ld, e,
472                                 "monitorOpInitiated")) != NULL)
473                         {
474                                 opi_data = *opi_list[0];
475                                 opi = atoll (opi_data.bv_val);
476                         }
477
478                         if (strcmp (dn, "cn=Total,cn=Connections,cn=Monitor")
479                                         == 0)
480                         {
481                                 submit_derive ("total_connections", NULL,
482                                         counter, st);
483                         }
484                         else if (strcmp (dn,
485                                         "cn=Current,cn=Connections,cn=Monitor")
486                                         == 0)
487                         {
488                                 submit_gauge ("current_connections", NULL,
489                                         counter, st);
490                         }
491                         else if (strcmp (dn,
492                                         "cn=Operations,cn=Monitor") == 0)
493                         {
494                                 submit_derive ("operations",
495                                         "completed", opc, st);
496                                 submit_derive ("operations",
497                                         "initiated", opi, st);
498                         }
499                         else if (strcmp (dn,
500                                         "cn=Bind,cn=Operations,cn=Monitor")
501                                         == 0)
502                         {
503                                 submit_derive ("operations",
504                                         "bind-completed", opc, st);
505                                 submit_derive ("operations",
506                                         "bind-initiated", opi, st);
507                         }
508                         else if (strcmp (dn,
509                                         "cn=UnBind,cn=Operations,cn=Monitor")
510                                         == 0)
511                         {
512                                 submit_derive ("operations",
513                                         "unbind-completed", opc, st);
514                                 submit_derive ("operations",
515                                         "unbind-initiated", opi, st);
516                         }
517                         else if (strcmp (dn,
518                                         "cn=Search,cn=Operations,cn=Monitor")
519                                         == 0)
520                         {
521                                 submit_derive ("operations",
522                                         "search-completed", opc, st);
523                                 submit_derive ("operations",
524                                         "search-initiated", opi, st);
525                         }
526                         else if (strcmp (dn,
527                                         "cn=Compare,cn=Operations,cn=Monitor")
528                                         == 0)
529                         {
530                                 submit_derive ("operations",
531                                         "compare-completed", opc, st);
532                                 submit_derive ("operations",
533                                         "compare-initiated", opi, st);
534                         }
535                         else if (strcmp (dn,
536                                         "cn=Modify,cn=Operations,cn=Monitor")
537                                         == 0)
538                         {
539                                 submit_derive ("operations",
540                                         "modify-completed", opc, st);
541                                 submit_derive ("operations",
542                                         "modify-initiated", opi, st);
543                         }
544                         else if (strcmp (dn,
545                                         "cn=Modrdn,cn=Operations,cn=Monitor")
546                                         == 0)
547                         {
548                                 submit_derive ("operations",
549                                         "modrdn-completed", opc, st);
550                                 submit_derive ("operations",
551                                         "modrdn-initiated", opi, st);
552                         }
553                         else if (strcmp (dn,
554                                         "cn=Add,cn=Operations,cn=Monitor")
555                                         == 0)
556                         {
557                                 submit_derive ("operations",
558                                         "add-completed", opc, st);
559                                 submit_derive ("operations",
560                                         "add-initiated", opi, st);
561                         }
562                         else if (strcmp (dn,
563                                         "cn=Delete,cn=Operations,cn=Monitor")
564                                         == 0)
565                         {
566                                 submit_derive ("operations",
567                                         "delete-completed", opc, st);
568                                 submit_derive ("operations",
569                                         "delete-initiated", opi, st);
570                         }
571                         else if (strcmp (dn,
572                                         "cn=Abandon,cn=Operations,cn=Monitor")
573                                         == 0)
574                         {
575                                 submit_derive ("operations",
576                                         "abandon-completed", opc, st);
577                                 submit_derive ("operations",
578                                         "abandon-initiated", opi, st);
579                         }
580                         else if (strcmp (dn,
581                                         "cn=Extended,cn=Operations,cn=Monitor")
582                                         == 0)
583                         {
584                                 submit_derive ("operations",
585                                         "extended-completed", opc, st);
586                                 submit_derive ("operations",
587                                         "extended-initiated", opi, st);
588                         }
589                         else if (strcmp (dn,
590                                         "cn=Bytes,cn=Statistics,cn=Monitor")
591                                         == 0)
592                         {
593                                 submit_derive ("derive", "statistics-bytes",
594                                         counter, st);
595                         }
596                         else if (strcmp (dn,
597                                         "cn=PDU,cn=Statistics,cn=Monitor")
598                                         == 0)
599                         {
600                                 submit_derive ("derive", "statistics-pdu",
601                                         counter, st);
602                         }
603                         else if (strcmp (dn,
604                                         "cn=Entries,cn=Statistics,cn=Monitor")
605                                         == 0)
606                         {
607                                 submit_derive ("derive", "statistics-entries",
608                                         counter, st);
609                         }
610                         else if (strcmp (dn,
611                                         "cn=Referrals,cn=Statistics,cn=Monitor")
612                                         == 0)
613                         {
614                                 submit_derive ("derive", "statistics-referrals",
615                                         counter, st);
616                         }
617                         else if (strcmp (dn,
618                                         "cn=Read,cn=Waiters,cn=Monitor")
619                                         == 0)
620                         {
621                                 submit_derive ("derive", "waiters-read",
622                                         counter, st);
623                         }
624                         else if (strcmp (dn,
625                                         "cn=Write,cn=Waiters,cn=Monitor")
626                                         == 0)
627                         {
628                                 submit_derive ("derive", "waiters-write",
629                                         counter, st);
630                         }
631
632                         ldap_value_free_len (counter_list);
633                         ldap_value_free_len (opc_list);
634                         ldap_value_free_len (opi_list);
635                 }
636
637                 ldap_memfree (dn);
638         }
639
640         ldap_msgfree (result);
641         ldap_unbind_ext_s (st->ld, NULL, NULL);
642         return (0);
643 } /* int ldap_read_host */
644
645 void module_register (void)
646 {
647         plugin_register_complex_config ("openldap", config);
648 } /* void module_register */