Merge remote-tracking branch 'github/pr/703'
[collectd.git] / src / write_riemann.c
1 /**
2  * collectd - src/write_riemann.c
3  * Copyright (C) 2012,2013  Pierre-Yves Ritschard
4  * Copyright (C) 2013       Florian octo Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Pierre-Yves Ritschard <pyr at spootnik.org>
26  *   Florian octo Forster <octo at collectd.org>
27  */
28
29 #include "collectd.h"
30 #include "plugin.h"
31 #include "common.h"
32 #include "configfile.h"
33 #include "utils_cache.h"
34 #include "riemann.pb-c.h"
35
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <errno.h>
39 #include <netdb.h>
40 #include <inttypes.h>
41 #include <pthread.h>
42
43 #define RIEMANN_HOST            "localhost"
44 #define RIEMANN_PORT            "5555"
45 #define RIEMANN_TTL_FACTOR      2.0
46
47 int write_riemann_threshold_check(const data_set_t *, const value_list_t *, int *);
48
49 struct riemann_host {
50         char                    *name;
51 #define F_CONNECT                0x01
52         uint8_t                  flags;
53         pthread_mutex_t          lock;
54     _Bool            notifications;
55     _Bool            check_thresholds;
56         _Bool                    store_rates;
57         _Bool                    always_append_ds;
58         char                    *node;
59         char                    *service;
60         _Bool                    use_tcp;
61         int                      s;
62         double                   ttl_factor;
63
64         int                      reference_count;
65 };
66
67 static char     **riemann_tags;
68 static size_t     riemann_tags_num;
69 static char     **riemann_attrs;
70 static size_t     riemann_attrs_num;
71
72 static void riemann_event_protobuf_free (Event *event) /* {{{ */
73 {
74         size_t i;
75
76         if (event == NULL)
77                 return;
78
79         sfree (event->state);
80         sfree (event->service);
81         sfree (event->host);
82         sfree (event->description);
83
84         strarray_free (event->tags, event->n_tags);
85         event->tags = NULL;
86         event->n_tags = 0;
87
88         for (i = 0; i < event->n_attributes; i++)
89         {
90                 sfree (event->attributes[i]->key);
91                 sfree (event->attributes[i]->value);
92                 sfree (event->attributes[i]);
93         }
94         sfree (event->attributes);
95         event->n_attributes = 0;
96
97         sfree (event);
98 } /* }}} void riemann_event_protobuf_free */
99
100 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
101 {
102         size_t i;
103
104         if (msg == NULL)
105                 return;
106
107         for (i = 0; i < msg->n_events; i++)
108         {
109                 riemann_event_protobuf_free (msg->events[i]);
110                 msg->events[i] = NULL;
111         }
112
113         sfree (msg->events);
114         msg->n_events = 0;
115
116         sfree (msg);
117 } /* }}} void riemann_msg_protobuf_free */
118
119 /* host->lock must be held when calling this function. */
120 static int riemann_connect(struct riemann_host *host) /* {{{ */
121 {
122         int                      e;
123         struct addrinfo         *ai, *res, hints;
124         char const              *node;
125         char const              *service;
126
127         if (host->flags & F_CONNECT)
128                 return 0;
129
130         memset(&hints, 0, sizeof(hints));
131         memset(&service, 0, sizeof(service));
132         hints.ai_family = AF_UNSPEC;
133         hints.ai_socktype = host->use_tcp ? SOCK_STREAM : SOCK_DGRAM;
134 #ifdef AI_ADDRCONFIG
135         hints.ai_flags |= AI_ADDRCONFIG;
136 #endif
137
138         node = (host->node != NULL) ? host->node : RIEMANN_HOST;
139         service = (host->service != NULL) ? host->service : RIEMANN_PORT;
140
141         if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
142                 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
143                         node, gai_strerror(e));
144                 return -1;
145         }
146
147         host->s = -1;
148         for (ai = res; ai != NULL; ai = ai->ai_next) {
149                 if ((host->s = socket(ai->ai_family,
150                                       ai->ai_socktype,
151                                       ai->ai_protocol)) == -1) {
152                         continue;
153                 }
154
155                 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
156                         close(host->s);
157                         host->s = -1;
158                         continue;
159                 }
160
161                 host->flags |= F_CONNECT;
162                 DEBUG("write_riemann plugin: got a successful connection for: %s:%s",
163                                 node, service);
164                 break;
165         }
166
167         freeaddrinfo(res);
168
169         if (host->s < 0) {
170                 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
171                                 node, service);
172                 return -1;
173         }
174         return 0;
175 } /* }}} int riemann_connect */
176
177 /* host->lock must be held when calling this function. */
178 static int riemann_disconnect (struct riemann_host *host) /* {{{ */
179 {
180         if ((host->flags & F_CONNECT) == 0)
181                 return (0);
182
183         close (host->s);
184         host->s = -1;
185         host->flags &= ~F_CONNECT;
186
187         return (0);
188 } /* }}} int riemann_disconnect */
189
190 static int riemann_send_msg (struct riemann_host *host, const Msg *msg) /* {{{ */
191 {
192         int status = 0;
193         u_char *buffer = NULL;
194         size_t  buffer_len;
195
196         status = riemann_connect (host);
197         if (status != 0)
198                 return status;
199
200         buffer_len = msg__get_packed_size(msg);
201
202         if (host->use_tcp)
203                 buffer_len += 4;
204
205         buffer = malloc (buffer_len);
206         if (buffer == NULL) {
207                 ERROR ("write_riemann plugin: malloc failed.");
208                 return ENOMEM;
209         }
210         memset (buffer, 0, buffer_len);
211
212         if (host->use_tcp)
213         {
214                 uint32_t length = htonl ((uint32_t) (buffer_len - 4));
215                 memcpy (buffer, &length, 4);
216                 msg__pack(msg, buffer + 4);
217         }
218         else
219         {
220                 msg__pack(msg, buffer);
221         }
222
223         status = (int) swrite (host->s, buffer, buffer_len);
224         if (status != 0)
225         {
226                 char errbuf[1024];
227                 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
228                                 (host->node != NULL) ? host->node : RIEMANN_HOST,
229                                 (host->service != NULL) ? host->service : RIEMANN_PORT,
230                                 sstrerror (errno, errbuf, sizeof (errbuf)));
231                 sfree (buffer);
232                 return -1;
233         }
234
235         sfree (buffer);
236         return 0;
237 } /* }}} int riemann_send_msg */
238
239 static int riemann_recv_ack(struct riemann_host *host) /* {{{ */
240 {
241         int status = 0;
242         Msg *msg = NULL;
243         uint32_t header;
244
245         status = (int) sread (host->s, &header, 4);
246
247         if (status != 0)
248                 return -1;
249
250         size_t size = ntohl(header);
251
252         // Buffer on the stack since acknowledges are typically small.
253         u_char buffer[size];
254         memset (buffer, 0, size);
255
256         status = (int) sread (host->s, buffer, size);
257
258         if (status != 0)
259                 return status;
260
261         msg = msg__unpack (NULL, size, buffer);
262
263         if (msg == NULL)
264                 return -1;
265
266         if (!msg->ok)
267         {
268                 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s acknowledgement message reported error: %s",
269                                 (host->node != NULL) ? host->node : RIEMANN_HOST,
270                                 (host->service != NULL) ? host->service : RIEMANN_PORT,
271                                 msg->error);
272
273                 msg__free_unpacked(msg, NULL);
274                 return -1;
275         }
276
277         msg__free_unpacked (msg, NULL);
278         return 0;
279 } /* }}} int riemann_recv_ack */
280
281 /**
282  * Function to send messages (Msg) to riemann.
283  *
284  * Acquires the host lock, disconnects on errors.
285  */
286 static int riemann_send(struct riemann_host *host, Msg const *msg) /* {{{ */
287 {
288         int status = 0;
289         pthread_mutex_lock (&host->lock);
290
291         status = riemann_send_msg(host, msg);
292         if (status != 0) {
293                 riemann_disconnect (host);
294                 pthread_mutex_unlock (&host->lock);
295                 return status;
296         }
297
298         /*
299          * For TCP we need to receive message acknowledgemenent.
300          */
301         if (host->use_tcp)
302         {
303                 status = riemann_recv_ack(host);
304
305                 if (status != 0)
306                 {
307                         riemann_disconnect (host);
308                         pthread_mutex_unlock (&host->lock);
309                         return status;
310                 }
311         }
312
313         pthread_mutex_unlock (&host->lock);
314         return 0;
315 } /* }}} int riemann_send */
316
317 static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
318 {
319         return (strarray_add (&event->tags, &event->n_tags, tag));
320 } /* }}} int riemann_event_add_tag */
321
322 static int riemann_event_add_attribute (Event *event, /* {{{ */
323                 char const *key, char const *value)
324 {
325         Attribute **new_attributes;
326         Attribute *a;
327
328         new_attributes = realloc (event->attributes,
329                         sizeof (*event->attributes) * (event->n_attributes + 1));
330         if (new_attributes == NULL)
331         {
332                 ERROR ("write_riemann plugin: realloc failed.");
333                 return (ENOMEM);
334         }
335         event->attributes = new_attributes;
336
337         a = malloc (sizeof (*a));
338         if (a == NULL)
339         {
340                 ERROR ("write_riemann plugin: malloc failed.");
341                 return (ENOMEM);
342         }
343         attribute__init (a);
344
345         a->key = strdup (key);
346         if (value != NULL)
347                 a->value = strdup (value);
348
349         event->attributes[event->n_attributes] = a;
350         event->n_attributes++;
351
352         return (0);
353 } /* }}} int riemann_event_add_attribute */
354
355 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
356                 notification_t const *n)
357 {
358         Msg *msg;
359         Event *event;
360         char service_buffer[6 * DATA_MAX_NAME_LEN];
361         char const *severity;
362         notification_meta_t *meta;
363         int i;
364
365         msg = malloc (sizeof (*msg));
366         if (msg == NULL)
367         {
368                 ERROR ("write_riemann plugin: malloc failed.");
369                 return (NULL);
370         }
371         memset (msg, 0, sizeof (*msg));
372         msg__init (msg);
373
374         msg->events = malloc (sizeof (*msg->events));
375         if (msg->events == NULL)
376         {
377                 ERROR ("write_riemann plugin: malloc failed.");
378                 sfree (msg);
379                 return (NULL);
380         }
381
382         event = malloc (sizeof (*event));
383         if (event == NULL)
384         {
385                 ERROR ("write_riemann plugin: malloc failed.");
386                 sfree (msg->events);
387                 sfree (msg);
388                 return (NULL);
389         }
390         memset (event, 0, sizeof (*event));
391         event__init (event);
392
393         msg->events[0] = event;
394         msg->n_events = 1;
395
396         event->host = strdup (n->host);
397         event->time = CDTIME_T_TO_TIME_T (n->time);
398         event->has_time = 1;
399
400         switch (n->severity)
401         {
402                 case NOTIF_OKAY:        severity = "ok"; break;
403                 case NOTIF_WARNING:     severity = "warning"; break;
404                 case NOTIF_FAILURE:     severity = "critical"; break;
405                 default:                severity = "unknown";
406         }
407         event->state = strdup (severity);
408
409         riemann_event_add_tag (event, "notification");
410         if (n->host[0] != 0)
411                 riemann_event_add_attribute (event, "host", n->host);
412         if (n->plugin[0] != 0)
413                 riemann_event_add_attribute (event, "plugin", n->plugin);
414         if (n->plugin_instance[0] != 0)
415                 riemann_event_add_attribute (event, "plugin_instance",
416                                 n->plugin_instance);
417
418         if (n->type[0] != 0)
419                 riemann_event_add_attribute (event, "type", n->type);
420         if (n->type_instance[0] != 0)
421                 riemann_event_add_attribute (event, "type_instance",
422                                 n->type_instance);
423
424         for (i = 0; i < riemann_attrs_num; i += 2)
425                 riemann_event_add_attribute(event,
426                                             riemann_attrs[i],
427                                             riemann_attrs[i +1]);
428
429         for (i = 0; i < riemann_tags_num; i++)
430                 riemann_event_add_tag (event, riemann_tags[i]);
431
432         format_name (service_buffer, sizeof (service_buffer),
433                         /* host = */ "", n->plugin, n->plugin_instance,
434                         n->type, n->type_instance);
435         event->service = strdup (&service_buffer[1]);
436
437         if (n->message[0] != 0)
438                 riemann_event_add_attribute (event, "description", n->message);
439
440         /* Pull in values from threshold and add extra attributes */
441         for (meta = n->meta; meta != NULL; meta = meta->next)
442         {
443                 if (strcasecmp ("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE)
444                 {
445                         event->metric_d = meta->nm_value.nm_double;
446                         event->has_metric_d = 1;
447                         continue;
448                 }
449
450                 if (meta->type == NM_TYPE_STRING) {
451                         riemann_event_add_attribute (event, meta->name, meta->nm_value.nm_string);
452                         continue;
453                 }
454         }
455
456         DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
457                         "host = \"%s\", service = \"%s\", state = \"%s\"",
458                         event->host, event->service, event->state);
459         return (msg);
460 } /* }}} Msg *riemann_notification_to_protobuf */
461
462 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
463                 data_set_t const *ds,
464                 value_list_t const *vl, size_t index,
465                                          gauge_t const *rates,
466                                          int status)
467 {
468         Event *event;
469         char name_buffer[5 * DATA_MAX_NAME_LEN];
470         char service_buffer[6 * DATA_MAX_NAME_LEN];
471         double ttl;
472         int i;
473
474         event = malloc (sizeof (*event));
475         if (event == NULL)
476         {
477                 ERROR ("write_riemann plugin: malloc failed.");
478                 return (NULL);
479         }
480         memset (event, 0, sizeof (*event));
481         event__init (event);
482
483         event->host = strdup (vl->host);
484         event->time = CDTIME_T_TO_TIME_T (vl->time);
485         event->has_time = 1;
486
487     if (host->check_thresholds) {
488         switch (status) {
489         case STATE_OKAY:
490             event->state = strdup("ok");
491             break;
492         case STATE_ERROR:
493             event->state = strdup("critical");
494             break;
495         case STATE_WARNING:
496             event->state = strdup("warning");
497             break;
498         case STATE_MISSING:
499             event->state = strdup("unknown");
500             break;
501         }
502     }
503
504         ttl = CDTIME_T_TO_DOUBLE (vl->interval) * host->ttl_factor;
505         event->ttl = (float) ttl;
506         event->has_ttl = 1;
507
508         riemann_event_add_attribute (event, "plugin", vl->plugin);
509         if (vl->plugin_instance[0] != 0)
510                 riemann_event_add_attribute (event, "plugin_instance",
511                                 vl->plugin_instance);
512
513         riemann_event_add_attribute (event, "type", vl->type);
514         if (vl->type_instance[0] != 0)
515                 riemann_event_add_attribute (event, "type_instance",
516                                 vl->type_instance);
517
518         if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
519         {
520                 char ds_type[DATA_MAX_NAME_LEN];
521
522                 ssnprintf (ds_type, sizeof (ds_type), "%s:rate",
523                                 DS_TYPE_TO_STRING(ds->ds[index].type));
524                 riemann_event_add_attribute (event, "ds_type", ds_type);
525         }
526         else
527         {
528                 riemann_event_add_attribute (event, "ds_type",
529                                 DS_TYPE_TO_STRING(ds->ds[index].type));
530         }
531         riemann_event_add_attribute (event, "ds_name", ds->ds[index].name);
532         {
533                 char ds_index[DATA_MAX_NAME_LEN];
534
535                 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
536                 riemann_event_add_attribute (event, "ds_index", ds_index);
537         }
538
539         for (i = 0; i < riemann_attrs_num; i += 2)
540                 riemann_event_add_attribute(event,
541                                             riemann_attrs[i],
542                                             riemann_attrs[i +1]);
543
544         for (i = 0; i < riemann_tags_num; i++)
545                 riemann_event_add_tag (event, riemann_tags[i]);
546
547         if (ds->ds[index].type == DS_TYPE_GAUGE)
548         {
549                 event->has_metric_d = 1;
550                 event->metric_d = (double) vl->values[index].gauge;
551         }
552         else if (rates != NULL)
553         {
554                 event->has_metric_d = 1;
555                 event->metric_d = (double) rates[index];
556         }
557         else
558         {
559                 event->has_metric_sint64 = 1;
560                 if (ds->ds[index].type == DS_TYPE_DERIVE)
561                         event->metric_sint64 = (int64_t) vl->values[index].derive;
562                 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
563                         event->metric_sint64 = (int64_t) vl->values[index].absolute;
564                 else
565                         event->metric_sint64 = (int64_t) vl->values[index].counter;
566         }
567
568         format_name (name_buffer, sizeof (name_buffer),
569                         /* host = */ "", vl->plugin, vl->plugin_instance,
570                         vl->type, vl->type_instance);
571         if (host->always_append_ds || (ds->ds_num > 1))
572                 ssnprintf (service_buffer, sizeof (service_buffer),
573                                 "%s/%s", &name_buffer[1], ds->ds[index].name);
574         else
575                 sstrncpy (service_buffer, &name_buffer[1],
576                                 sizeof (service_buffer));
577
578         event->service = strdup (service_buffer);
579
580         DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
581                         "host = \"%s\", service = \"%s\"",
582                         event->host, event->service);
583         return (event);
584 } /* }}} Event *riemann_value_to_protobuf */
585
586 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
587                                             data_set_t const *ds,
588                                             value_list_t const *vl,
589                                             int *statuses)
590 {
591         Msg *msg;
592         size_t i;
593         gauge_t *rates = NULL;
594
595         /* Initialize the Msg structure. */
596         msg = malloc (sizeof (*msg));
597         if (msg == NULL)
598         {
599                 ERROR ("write_riemann plugin: malloc failed.");
600                 return (NULL);
601         }
602         memset (msg, 0, sizeof (*msg));
603         msg__init (msg);
604
605         /* Set up events. First, the list of pointers. */
606         msg->n_events = (size_t) vl->values_len;
607         msg->events = calloc (msg->n_events, sizeof (*msg->events));
608         if (msg->events == NULL)
609         {
610                 ERROR ("write_riemann plugin: calloc failed.");
611                 riemann_msg_protobuf_free (msg);
612                 return (NULL);
613         }
614
615         if (host->store_rates)
616         {
617                 rates = uc_get_rate (ds, vl);
618                 if (rates == NULL)
619                 {
620                         ERROR ("write_riemann plugin: uc_get_rate failed.");
621                         riemann_msg_protobuf_free (msg);
622                         return (NULL);
623                 }
624         }
625
626         for (i = 0; i < msg->n_events; i++)
627         {
628                 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
629                                                             (int) i, rates, statuses[i]);
630                 if (msg->events[i] == NULL)
631                 {
632                         riemann_msg_protobuf_free (msg);
633                         sfree (rates);
634                         return (NULL);
635                 }
636         }
637
638         sfree (rates);
639         return (msg);
640 } /* }}} Msg *riemann_value_list_to_protobuf */
641
642 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
643 {
644         int                      status;
645         struct riemann_host     *host = ud->data;
646         Msg                     *msg;
647
648     if (!host->notifications)
649         return 0;
650
651         msg = riemann_notification_to_protobuf (host, n);
652         if (msg == NULL)
653                 return (-1);
654
655         status = riemann_send (host, msg);
656         if (status != 0)
657                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
658                                 status);
659
660         riemann_msg_protobuf_free (msg);
661         return (status);
662 } /* }}} int riemann_notification */
663
664 static int riemann_write(const data_set_t *ds, /* {{{ */
665               const value_list_t *vl,
666               user_data_t *ud)
667 {
668         int                      status;
669         int                      statuses[vl->values_len];
670         struct riemann_host     *host = ud->data;
671         Msg                     *msg;
672
673     if (host->check_thresholds)
674         write_riemann_threshold_check(ds, vl, statuses);
675         msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
676         if (msg == NULL)
677                 return (-1);
678
679         status = riemann_send (host, msg);
680         if (status != 0)
681                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
682                                 status);
683
684         riemann_msg_protobuf_free (msg);
685         return status;
686 } /* }}} int riemann_write */
687
688 static void riemann_free(void *p) /* {{{ */
689 {
690         struct riemann_host     *host = p;
691
692         if (host == NULL)
693                 return;
694
695         pthread_mutex_lock (&host->lock);
696
697         host->reference_count--;
698         if (host->reference_count > 0)
699         {
700                 pthread_mutex_unlock (&host->lock);
701                 return;
702         }
703
704         riemann_disconnect (host);
705
706         sfree(host->service);
707         pthread_mutex_destroy (&host->lock);
708         sfree(host);
709 } /* }}} void riemann_free */
710
711 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
712 {
713         struct riemann_host     *host = NULL;
714         int                      status = 0;
715         int                      i;
716         oconfig_item_t          *child;
717         char                     callback_name[DATA_MAX_NAME_LEN];
718         user_data_t              ud;
719
720         if ((host = calloc(1, sizeof (*host))) == NULL) {
721                 ERROR ("write_riemann plugin: calloc failed.");
722                 return ENOMEM;
723         }
724         pthread_mutex_init (&host->lock, NULL);
725         host->reference_count = 1;
726         host->node = NULL;
727         host->service = NULL;
728     host->notifications = 1;
729     host->check_thresholds = 0;
730         host->store_rates = 1;
731         host->always_append_ds = 0;
732         host->use_tcp = 0;
733         host->ttl_factor = RIEMANN_TTL_FACTOR;
734
735         status = cf_util_get_string (ci, &host->name);
736         if (status != 0) {
737                 WARNING("write_riemann plugin: Required host name is missing.");
738                 riemann_free (host);
739                 return -1;
740         }
741
742         for (i = 0; i < ci->children_num; i++) {
743                 /*
744                  * The code here could be simplified but makes room
745                  * for easy adding of new options later on.
746                  */
747                 child = &ci->children[i];
748                 status = 0;
749
750                 if (strcasecmp ("Host", child->key) == 0) {
751                         status = cf_util_get_string (child, &host->node);
752                         if (status != 0)
753                                 break;
754         } else if (strcasecmp ("Notifications", child->key) == 0) {
755             status = cf_util_get_boolean(child, &host->notifications);
756             if (status != 0)
757                 break;
758         } else if (strcasecmp ("CheckThresholds", child->key) == 0) {
759             status = cf_util_get_boolean(child, &host->check_thresholds);
760             if (status != 0)
761                 break;
762                 } else if (strcasecmp ("Port", child->key) == 0) {
763                         status = cf_util_get_service (child, &host->service);
764                         if (status != 0) {
765                                 ERROR ("write_riemann plugin: Invalid argument "
766                                                 "configured for the \"Port\" "
767                                                 "option.");
768                                 break;
769                         }
770                 } else if (strcasecmp ("Protocol", child->key) == 0) {
771                         char tmp[16];
772                         status = cf_util_get_string_buffer (child,
773                                         tmp, sizeof (tmp));
774                         if (status != 0)
775                         {
776                                 ERROR ("write_riemann plugin: cf_util_get_"
777                                                 "string_buffer failed with "
778                                                 "status %i.", status);
779                                 break;
780                         }
781
782                         if (strcasecmp ("UDP", tmp) == 0)
783                                 host->use_tcp = 0;
784                         else if (strcasecmp ("TCP", tmp) == 0)
785                                 host->use_tcp = 1;
786                         else
787                                 WARNING ("write_riemann plugin: The value "
788                                                 "\"%s\" is not valid for the "
789                                                 "\"Protocol\" option. Use "
790                                                 "either \"UDP\" or \"TCP\".",
791                                                 tmp);
792                 } else if (strcasecmp ("StoreRates", child->key) == 0) {
793                         status = cf_util_get_boolean (child, &host->store_rates);
794                         if (status != 0)
795                                 break;
796                 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
797                         status = cf_util_get_boolean (child,
798                                         &host->always_append_ds);
799                         if (status != 0)
800                                 break;
801                 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
802                         double tmp = NAN;
803                         status = cf_util_get_double (child, &tmp);
804                         if (status != 0)
805                                 break;
806                         if (tmp >= 2.0) {
807                                 host->ttl_factor = tmp;
808                         } else if (tmp >= 1.0) {
809                                 NOTICE ("write_riemann plugin: The configured "
810                                                 "TTLFactor is very small "
811                                                 "(%.1f). A value of 2.0 or "
812                                                 "greater is recommended.",
813                                                 tmp);
814                                 host->ttl_factor = tmp;
815                         } else if (tmp > 0.0) {
816                                 WARNING ("write_riemann plugin: The configured "
817                                                 "TTLFactor is too small to be "
818                                                 "useful (%.1f). I'll use it "
819                                                 "since the user knows best, "
820                                                 "but under protest.",
821                                                 tmp);
822                                 host->ttl_factor = tmp;
823                         } else { /* zero, negative and NAN */
824                                 ERROR ("write_riemann plugin: The configured "
825                                                 "TTLFactor is invalid (%.1f).",
826                                                 tmp);
827                         }
828                 } else {
829                         WARNING("write_riemann plugin: ignoring unknown config "
830                                 "option: \"%s\"", child->key);
831                 }
832         }
833         if (status != 0) {
834                 riemann_free (host);
835                 return status;
836         }
837
838         ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
839                         host->name);
840         ud.data = host;
841         ud.free_func = riemann_free;
842
843         pthread_mutex_lock (&host->lock);
844
845         status = plugin_register_write (callback_name, riemann_write, &ud);
846         if (status != 0)
847                 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
848                                 "failed with status %i.",
849                                 callback_name, status);
850         else /* success */
851                 host->reference_count++;
852
853         status = plugin_register_notification (callback_name,
854                         riemann_notification, &ud);
855         if (status != 0)
856                 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
857                                 "failed with status %i.",
858                                 callback_name, status);
859         else /* success */
860                 host->reference_count++;
861
862         if (host->reference_count <= 1)
863         {
864                 /* Both callbacks failed => free memory.
865                  * We need to unlock here, because riemann_free() will lock.
866                  * This is not a race condition, because we're the only one
867                  * holding a reference. */
868                 pthread_mutex_unlock (&host->lock);
869                 riemann_free (host);
870                 return (-1);
871         }
872
873         host->reference_count--;
874         pthread_mutex_unlock (&host->lock);
875
876         return status;
877 } /* }}} int riemann_config_node */
878
879 static int riemann_config(oconfig_item_t *ci) /* {{{ */
880 {
881         int              i;
882         oconfig_item_t  *child;
883         int              status;
884
885         for (i = 0; i < ci->children_num; i++)  {
886                 child = &ci->children[i];
887
888                 if (strcasecmp("Node", child->key) == 0) {
889                         riemann_config_node (child);
890                 } else if (strcasecmp(child->key, "attribute") == 0) {
891                         char *key = NULL;
892                         char *val = NULL;
893
894                         if (child->values_num != 2) {
895                                 WARNING("riemann attributes need both a key and a value.");
896                                 return (-1);
897                         }
898                         if (child->values[0].type != OCONFIG_TYPE_STRING ||
899                             child->values[1].type != OCONFIG_TYPE_STRING) {
900                                 WARNING("riemann attribute needs string arguments.");
901                                 return (-1);
902                         }
903                         if ((key = strdup(child->values[0].value.string)) == NULL) {
904                                 WARNING("cannot allocate memory for attribute key.");
905                                 return (-1);
906                         }
907                         if ((val = strdup(child->values[1].value.string)) == NULL) {
908                                 WARNING("cannot allocate memory for attribute value.");
909                                 return (-1);
910                         }
911                         strarray_add(&riemann_attrs, &riemann_attrs_num, key);
912                         strarray_add(&riemann_attrs, &riemann_attrs_num, val);
913                         DEBUG("write_riemann: got attr: %s => %s", key, val);
914                         sfree(key);
915                         sfree(val);
916                 } else if (strcasecmp(child->key, "tag") == 0) {
917                         char *tmp = NULL;
918                         status = cf_util_get_string(child, &tmp);
919                         if (status != 0)
920                                 continue;
921
922                         strarray_add (&riemann_tags, &riemann_tags_num, tmp);
923                         DEBUG("write_riemann plugin: Got tag: %s", tmp);
924                         sfree (tmp);
925                 } else {
926                         WARNING ("write_riemann plugin: Ignoring unknown "
927                                  "configuration option \"%s\" at top level.",
928                                  child->key);
929                 }
930         }
931     return 0;
932 } /* }}} int riemann_config */
933
934 void module_register(void)
935 {
936         plugin_register_complex_config ("write_riemann", riemann_config);
937 }
938
939 /* vim: set sw=8 sts=8 ts=8 noet : */