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