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