c3740e1ddd9f5c38ffff00adb1acebc0900a6d68
[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                    *event_service_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         {
574                 if (host->event_service_prefix == NULL)
575                         ssnprintf (service_buffer, sizeof (service_buffer), "%s/%s",
576                                         &name_buffer[1], ds->ds[index].name);
577                 else
578                         ssnprintf (service_buffer, sizeof (service_buffer), "%s%s/%s",
579                                         host->event_service_prefix, &name_buffer[1], ds->ds[index].name);
580         }
581         else
582         {
583                 if (host->event_service_prefix == NULL)
584                         sstrncpy (service_buffer, &name_buffer[1], sizeof (service_buffer));
585                 else
586                         ssnprintf (service_buffer, sizeof (service_buffer), "%s%s",
587                                         host->event_service_prefix, &name_buffer[1]);
588         }
589
590         event->service = strdup (service_buffer);
591
592         DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
593                         "host = \"%s\", service = \"%s\"",
594                         event->host, event->service);
595         return (event);
596 } /* }}} Event *riemann_value_to_protobuf */
597
598 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
599                                             data_set_t const *ds,
600                                             value_list_t const *vl,
601                                             int *statuses)
602 {
603         Msg *msg;
604         size_t i;
605         gauge_t *rates = NULL;
606
607         /* Initialize the Msg structure. */
608         msg = malloc (sizeof (*msg));
609         if (msg == NULL)
610         {
611                 ERROR ("write_riemann plugin: malloc failed.");
612                 return (NULL);
613         }
614         memset (msg, 0, sizeof (*msg));
615         msg__init (msg);
616
617         /* Set up events. First, the list of pointers. */
618         msg->n_events = (size_t) vl->values_len;
619         msg->events = calloc (msg->n_events, sizeof (*msg->events));
620         if (msg->events == NULL)
621         {
622                 ERROR ("write_riemann plugin: calloc failed.");
623                 riemann_msg_protobuf_free (msg);
624                 return (NULL);
625         }
626
627         if (host->store_rates)
628         {
629                 rates = uc_get_rate (ds, vl);
630                 if (rates == NULL)
631                 {
632                         ERROR ("write_riemann plugin: uc_get_rate failed.");
633                         riemann_msg_protobuf_free (msg);
634                         return (NULL);
635                 }
636         }
637
638         for (i = 0; i < msg->n_events; i++)
639         {
640                 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
641                                                             (int) i, rates, statuses[i]);
642                 if (msg->events[i] == NULL)
643                 {
644                         riemann_msg_protobuf_free (msg);
645                         sfree (rates);
646                         return (NULL);
647                 }
648         }
649
650         sfree (rates);
651         return (msg);
652 } /* }}} Msg *riemann_value_list_to_protobuf */
653
654 static int riemann_notification(const notification_t *n, user_data_t *ud) /* {{{ */
655 {
656         int                      status;
657         struct riemann_host     *host = ud->data;
658         Msg                     *msg;
659
660         if (!host->notifications)
661                 return 0;
662
663         msg = riemann_notification_to_protobuf (host, n);
664         if (msg == NULL)
665                 return (-1);
666
667         status = riemann_send (host, msg);
668         if (status != 0)
669                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
670                                 status);
671
672         riemann_msg_protobuf_free (msg);
673         return (status);
674 } /* }}} int riemann_notification */
675
676 static int riemann_write(const data_set_t *ds, /* {{{ */
677               const value_list_t *vl,
678               user_data_t *ud)
679 {
680         int                      status;
681         int                      statuses[vl->values_len];
682         struct riemann_host     *host = ud->data;
683         Msg                     *msg;
684
685         if (host->check_thresholds)
686                 write_riemann_threshold_check(ds, vl, statuses);
687         msg = riemann_value_list_to_protobuf (host, ds, vl, statuses);
688         if (msg == NULL)
689                 return (-1);
690
691         status = riemann_send (host, msg);
692         if (status != 0)
693                 ERROR ("write_riemann plugin: riemann_send failed with status %i",
694                                 status);
695
696         riemann_msg_protobuf_free (msg);
697         return status;
698 } /* }}} int riemann_write */
699
700 static void riemann_free(void *p) /* {{{ */
701 {
702         struct riemann_host     *host = p;
703
704         if (host == NULL)
705                 return;
706
707         pthread_mutex_lock (&host->lock);
708
709         host->reference_count--;
710         if (host->reference_count > 0)
711         {
712                 pthread_mutex_unlock (&host->lock);
713                 return;
714         }
715
716         riemann_disconnect (host);
717
718         sfree(host->service);
719         pthread_mutex_destroy (&host->lock);
720         sfree(host);
721 } /* }}} void riemann_free */
722
723 static int riemann_config_node(oconfig_item_t *ci) /* {{{ */
724 {
725         struct riemann_host     *host = NULL;
726         int                      status = 0;
727         int                      i;
728         oconfig_item_t          *child;
729         char                     callback_name[DATA_MAX_NAME_LEN];
730         user_data_t              ud;
731
732         if ((host = calloc(1, sizeof (*host))) == NULL) {
733                 ERROR ("write_riemann plugin: calloc failed.");
734                 return ENOMEM;
735         }
736         pthread_mutex_init (&host->lock, NULL);
737         host->reference_count = 1;
738         host->node = NULL;
739         host->service = NULL;
740         host->notifications = 1;
741         host->check_thresholds = 0;
742         host->store_rates = 1;
743         host->always_append_ds = 0;
744         host->use_tcp = 0;
745         host->ttl_factor = RIEMANN_TTL_FACTOR;
746
747         status = cf_util_get_string (ci, &host->name);
748         if (status != 0) {
749                 WARNING("write_riemann plugin: Required host name is missing.");
750                 riemann_free (host);
751                 return -1;
752         }
753
754         for (i = 0; i < ci->children_num; i++) {
755                 /*
756                  * The code here could be simplified but makes room
757                  * for easy adding of new options later on.
758                  */
759                 child = &ci->children[i];
760                 status = 0;
761
762                 if (strcasecmp ("Host", child->key) == 0) {
763                         status = cf_util_get_string (child, &host->node);
764                         if (status != 0)
765                                 break;
766                 } else if (strcasecmp ("Notifications", child->key) == 0) {
767                         status = cf_util_get_boolean(child, &host->notifications);
768                         if (status != 0)
769                                 break;
770                 } else if (strcasecmp ("EventServicePrefix", child->key) == 0) {
771                         status = cf_util_get_string (child, &host->event_service_prefix);
772                         if (status != 0)
773                                 break;
774                 } else if (strcasecmp ("CheckThresholds", child->key) == 0) {
775                         status = cf_util_get_boolean(child, &host->check_thresholds);
776                         if (status != 0)
777                                 break;
778                 } else if (strcasecmp ("Port", child->key) == 0) {
779                         status = cf_util_get_service (child, &host->service);
780                         if (status != 0) {
781                                 ERROR ("write_riemann plugin: Invalid argument "
782                                                 "configured for the \"Port\" "
783                                                 "option.");
784                                 break;
785                         }
786                 } else if (strcasecmp ("Protocol", child->key) == 0) {
787                         char tmp[16];
788                         status = cf_util_get_string_buffer (child,
789                                         tmp, sizeof (tmp));
790                         if (status != 0)
791                         {
792                                 ERROR ("write_riemann plugin: cf_util_get_"
793                                                 "string_buffer failed with "
794                                                 "status %i.", status);
795                                 break;
796                         }
797
798                         if (strcasecmp ("UDP", tmp) == 0)
799                                 host->use_tcp = 0;
800                         else if (strcasecmp ("TCP", tmp) == 0)
801                                 host->use_tcp = 1;
802                         else
803                                 WARNING ("write_riemann plugin: The value "
804                                                 "\"%s\" is not valid for the "
805                                                 "\"Protocol\" option. Use "
806                                                 "either \"UDP\" or \"TCP\".",
807                                                 tmp);
808                 } else if (strcasecmp ("StoreRates", child->key) == 0) {
809                         status = cf_util_get_boolean (child, &host->store_rates);
810                         if (status != 0)
811                                 break;
812                 } else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) {
813                         status = cf_util_get_boolean (child,
814                                         &host->always_append_ds);
815                         if (status != 0)
816                                 break;
817                 } else if (strcasecmp ("TTLFactor", child->key) == 0) {
818                         double tmp = NAN;
819                         status = cf_util_get_double (child, &tmp);
820                         if (status != 0)
821                                 break;
822                         if (tmp >= 2.0) {
823                                 host->ttl_factor = tmp;
824                         } else if (tmp >= 1.0) {
825                                 NOTICE ("write_riemann plugin: The configured "
826                                                 "TTLFactor is very small "
827                                                 "(%.1f). A value of 2.0 or "
828                                                 "greater is recommended.",
829                                                 tmp);
830                                 host->ttl_factor = tmp;
831                         } else if (tmp > 0.0) {
832                                 WARNING ("write_riemann plugin: The configured "
833                                                 "TTLFactor is too small to be "
834                                                 "useful (%.1f). I'll use it "
835                                                 "since the user knows best, "
836                                                 "but under protest.",
837                                                 tmp);
838                                 host->ttl_factor = tmp;
839                         } else { /* zero, negative and NAN */
840                                 ERROR ("write_riemann plugin: The configured "
841                                                 "TTLFactor is invalid (%.1f).",
842                                                 tmp);
843                         }
844                 } else {
845                         WARNING("write_riemann plugin: ignoring unknown config "
846                                 "option: \"%s\"", child->key);
847                 }
848         }
849         if (status != 0) {
850                 riemann_free (host);
851                 return status;
852         }
853
854         ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
855                         host->name);
856         ud.data = host;
857         ud.free_func = riemann_free;
858
859         pthread_mutex_lock (&host->lock);
860
861         status = plugin_register_write (callback_name, riemann_write, &ud);
862         if (status != 0)
863                 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
864                                 "failed with status %i.",
865                                 callback_name, status);
866         else /* success */
867                 host->reference_count++;
868
869         status = plugin_register_notification (callback_name,
870                         riemann_notification, &ud);
871         if (status != 0)
872                 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
873                                 "failed with status %i.",
874                                 callback_name, status);
875         else /* success */
876                 host->reference_count++;
877
878         if (host->reference_count <= 1)
879         {
880                 /* Both callbacks failed => free memory.
881                  * We need to unlock here, because riemann_free() will lock.
882                  * This is not a race condition, because we're the only one
883                  * holding a reference. */
884                 pthread_mutex_unlock (&host->lock);
885                 riemann_free (host);
886                 return (-1);
887         }
888
889         host->reference_count--;
890         pthread_mutex_unlock (&host->lock);
891
892         return status;
893 } /* }}} int riemann_config_node */
894
895 static int riemann_config(oconfig_item_t *ci) /* {{{ */
896 {
897         int              i;
898         oconfig_item_t  *child;
899         int              status;
900
901         for (i = 0; i < ci->children_num; i++)  {
902                 child = &ci->children[i];
903
904                 if (strcasecmp("Node", child->key) == 0) {
905                         riemann_config_node (child);
906                 } else if (strcasecmp(child->key, "attribute") == 0) {
907                         char *key = NULL;
908                         char *val = NULL;
909
910                         if (child->values_num != 2) {
911                                 WARNING("riemann attributes need both a key and a value.");
912                                 return (-1);
913                         }
914                         if (child->values[0].type != OCONFIG_TYPE_STRING ||
915                             child->values[1].type != OCONFIG_TYPE_STRING) {
916                                 WARNING("riemann attribute needs string arguments.");
917                                 return (-1);
918                         }
919                         if ((key = strdup(child->values[0].value.string)) == NULL) {
920                                 WARNING("cannot allocate memory for attribute key.");
921                                 return (-1);
922                         }
923                         if ((val = strdup(child->values[1].value.string)) == NULL) {
924                                 WARNING("cannot allocate memory for attribute value.");
925                                 return (-1);
926                         }
927                         strarray_add(&riemann_attrs, &riemann_attrs_num, key);
928                         strarray_add(&riemann_attrs, &riemann_attrs_num, val);
929                         DEBUG("write_riemann: got attr: %s => %s", key, val);
930                         sfree(key);
931                         sfree(val);
932                 } else if (strcasecmp(child->key, "tag") == 0) {
933                         char *tmp = NULL;
934                         status = cf_util_get_string(child, &tmp);
935                         if (status != 0)
936                                 continue;
937
938                         strarray_add (&riemann_tags, &riemann_tags_num, tmp);
939                         DEBUG("write_riemann plugin: Got tag: %s", tmp);
940                         sfree (tmp);
941                 } else {
942                         WARNING ("write_riemann plugin: Ignoring unknown "
943                                  "configuration option \"%s\" at top level.",
944                                  child->key);
945                 }
946         }
947         return (0);
948 } /* }}} int riemann_config */
949
950 void module_register(void)
951 {
952         plugin_register_complex_config ("write_riemann", riemann_config);
953 }
954
955 /* vim: set sw=8 sts=8 ts=8 noet : */