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