d2d54c9189964e6445d48eff0e792f8d6ca684c8
1 /*
2 * collectd - src/write_riemann.c
3 *
4 * Copyright (C) 2012 Pierre-Yves Ritschard <pyr@spootnik.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
20 #include "collectd.h"
21 #include "plugin.h"
22 #include "common.h"
23 #include "configfile.h"
24 #include "utils_cache.h"
25 #include "riemann.pb-c.h"
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <inttypes.h>
32 #include <pthread.h>
34 #define RIEMANN_DELAY 1
35 #define RIEMANN_HOST "localhost"
36 #define RIEMANN_PORT "5555"
37 #define RIEMANN_MAX_TAGS 37
38 #define RIEMANN_EXTRA_TAGS 32
40 struct riemann_host {
41 char *name;
42 #define F_CONNECT 0x01
43 uint8_t flags;
44 pthread_mutex_t lock;
45 int delay;
46 _Bool store_rates;
47 char *node;
48 char *service;
49 int s;
51 int reference_count;
52 };
54 static char **riemann_tags;
55 static size_t riemann_tags_num;
57 static int riemann_send(struct riemann_host *, Msg const *);
58 static int riemann_notification(const notification_t *, user_data_t *);
59 static int riemann_write(const data_set_t *, const value_list_t *, user_data_t *);
60 static int riemann_connect(struct riemann_host *);
61 static int riemann_disconnect (struct riemann_host *host);
62 static void riemann_free(void *);
63 static int riemann_config_node(oconfig_item_t *);
64 static int riemann_config(oconfig_item_t *);
65 void module_register(void);
67 static void riemann_event_protobuf_free (Event *event) /* {{{ */
68 {
69 if (event == NULL)
70 return;
72 sfree (event->state);
73 sfree (event->service);
74 sfree (event->host);
75 sfree (event->description);
77 strarray_free (event->tags, event->n_tags);
78 event->tags = NULL;
79 event->n_tags = 0;
81 sfree (event);
82 } /* }}} void riemann_event_protobuf_free */
84 static void riemann_msg_protobuf_free (Msg *msg) /* {{{ */
85 {
86 size_t i;
88 if (msg == NULL)
89 return;
91 for (i = 0; i < msg->n_events; i++)
92 {
93 riemann_event_protobuf_free (msg->events[i]);
94 msg->events[i] = NULL;
95 }
97 sfree (msg->events);
98 msg->n_events = 0;
100 sfree (msg);
101 } /* }}} void riemann_msg_protobuf_free */
103 static int
104 riemann_send(struct riemann_host *host, Msg const *msg)
105 {
106 u_char *buffer;
107 size_t buffer_len;
108 int status;
110 pthread_mutex_lock (&host->lock);
112 status = riemann_connect (host);
113 if (status != 0)
114 {
115 pthread_mutex_unlock (&host->lock);
116 return status;
117 }
119 buffer_len = msg__get_packed_size(msg);
120 buffer = malloc (buffer_len);
121 if (buffer == NULL) {
122 pthread_mutex_unlock (&host->lock);
123 ERROR ("write_riemann plugin: malloc failed.");
124 return ENOMEM;
125 }
126 memset (buffer, 0, buffer_len);
128 msg__pack(msg, buffer);
130 status = (int) swrite (host->s, buffer, buffer_len);
131 if (status != 0)
132 {
133 char errbuf[1024];
135 riemann_disconnect (host);
136 pthread_mutex_unlock (&host->lock);
138 ERROR ("write_riemann plugin: Sending to Riemann at %s:%s failed: %s",
139 (host->node != NULL) ? host->node : RIEMANN_HOST,
140 (host->service != NULL) ? host->service : RIEMANN_PORT,
141 sstrerror (errno, errbuf, sizeof (errbuf)));
142 sfree (buffer);
143 return -1;
144 }
146 pthread_mutex_unlock (&host->lock);
147 sfree (buffer);
148 return 0;
149 }
151 static int riemann_event_add_tag (Event *event, /* {{{ */
152 char const *format, ...)
153 {
154 va_list ap;
155 char buffer[1024];
156 size_t ret;
158 va_start (ap, format);
159 ret = vsnprintf (buffer, sizeof (buffer), format, ap);
160 if (ret >= sizeof (buffer))
161 ret = sizeof (buffer) - 1;
162 buffer[ret] = 0;
163 va_end (ap);
165 return (strarray_add (&event->tags, &event->n_tags, buffer));
166 } /* }}} int riemann_event_add_tag */
168 static Msg *riemann_notification_to_protobuf (struct riemann_host *host, /* {{{ */
169 notification_t const *n)
170 {
171 Msg *msg;
172 Event *event;
173 char service_buffer[6 * DATA_MAX_NAME_LEN];
174 char const *severity;
175 notification_meta_t *meta;
176 int i;
178 msg = malloc (sizeof (*msg));
179 if (msg == NULL)
180 {
181 ERROR ("write_riemann plugin: malloc failed.");
182 return (NULL);
183 }
184 memset (msg, 0, sizeof (*msg));
185 msg__init (msg);
187 msg->events = malloc (sizeof (*msg->events));
188 if (msg->events == NULL)
189 {
190 ERROR ("write_riemann plugin: malloc failed.");
191 sfree (msg);
192 return (NULL);
193 }
195 event = malloc (sizeof (*event));
196 if (event == NULL)
197 {
198 ERROR ("write_riemann plugin: malloc failed.");
199 sfree (msg->events);
200 sfree (msg);
201 return (NULL);
202 }
203 memset (event, 0, sizeof (*event));
204 event__init (event);
206 msg->events[0] = event;
207 msg->n_events = 1;
209 event->host = strdup (n->host);
210 event->time = CDTIME_T_TO_TIME_T (n->time);
211 event->has_time = 1;
213 switch (n->severity)
214 {
215 case NOTIF_OKAY: severity = "okay"; break;
216 case NOTIF_WARNING: severity = "warning"; break;
217 case NOTIF_FAILURE: severity = "failure"; break;
218 default: severity = "unknown";
219 }
220 event->state = strdup (severity);
222 riemann_event_add_tag (event, "notification");
223 if (n->plugin[0] != 0)
224 riemann_event_add_tag (event, "plugin:%s", n->plugin);
225 if (n->plugin_instance[0] != 0)
226 riemann_event_add_tag (event, "plugin_instance:%s",
227 n->plugin_instance);
229 if (n->type[0] != 0)
230 riemann_event_add_tag (event, "type:%s", n->type);
231 if (n->type_instance[0] != 0)
232 riemann_event_add_tag (event, "type_instance:%s",
233 n->type_instance);
235 for (i = 0; i < riemann_tags_num; i++)
236 riemann_event_add_tag (event, "%s", riemann_tags[i]);
238 /* TODO: Use FORMAT_VL() here. */
239 ssnprintf (service_buffer, sizeof(service_buffer),
240 "%s-%s-%s-%s", n->plugin, n->plugin_instance,
241 n->type, n->type_instance);
242 event->service = strdup (service_buffer);
244 /* Pull in values from threshold */
245 for (meta = n->meta; meta != NULL; meta = meta->next)
246 {
247 if (strcasecmp ("CurrentValue", meta->name) != 0)
248 continue;
250 event->metric_d = meta->nm_value.nm_double;
251 event->has_metric_d = 1;
252 break;
253 }
255 DEBUG ("write_riemann plugin: Successfully created protobuf for notification: "
256 "host = \"%s\", service = \"%s\", state = \"%s\"",
257 event->host, event->service, event->state);
258 return (msg);
259 } /* }}} Msg *riemann_notification_to_protobuf */
261 static Event *riemann_value_to_protobuf (struct riemann_host const *host, /* {{{ */
262 data_set_t const *ds,
263 value_list_t const *vl, size_t index,
264 gauge_t const *rates)
265 {
266 Event *event;
267 char service_buffer[6 * DATA_MAX_NAME_LEN];
268 int i;
270 event = malloc (sizeof (*event));
271 if (event == NULL)
272 {
273 ERROR ("write_riemann plugin: malloc failed.");
274 return (NULL);
275 }
276 memset (event, 0, sizeof (*event));
277 event__init (event);
279 event->host = strdup (vl->host);
280 event->time = CDTIME_T_TO_TIME_T (vl->time);
281 event->has_time = 1;
282 event->ttl = CDTIME_T_TO_TIME_T (vl->interval) + host->delay;
283 event->has_ttl = 1;
285 riemann_event_add_tag (event, "plugin:%s", vl->plugin);
286 if (vl->plugin_instance[0] != 0)
287 riemann_event_add_tag (event, "plugin_instance:%s",
288 vl->plugin_instance);
290 riemann_event_add_tag (event, "type:%s", vl->type);
291 if (vl->type_instance[0] != 0)
292 riemann_event_add_tag (event, "type_instance:%s",
293 vl->type_instance);
295 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL))
296 {
297 riemann_event_add_tag (event, "ds_type:%s:rate",
298 DS_TYPE_TO_STRING(ds->ds[index].type));
299 }
300 else
301 {
302 riemann_event_add_tag (event, "ds_type:%s",
303 DS_TYPE_TO_STRING(ds->ds[index].type));
304 }
305 riemann_event_add_tag (event, "ds_name:%s", ds->ds[index].name);
306 riemann_event_add_tag (event, "ds_index:%zu", index);
308 for (i = 0; i < riemann_tags_num; i++)
309 riemann_event_add_tag (event, "%s", riemann_tags[i]);
311 if (ds->ds[index].type == DS_TYPE_GAUGE)
312 {
313 event->has_metric_d = 1;
314 event->metric_d = (double) vl->values[index].gauge;
315 }
316 else if (rates != NULL)
317 {
318 event->has_metric_d = 1;
319 event->metric_d = (double) rates[index];
320 }
321 else
322 {
323 event->has_metric_sint64 = 1;
324 if (ds->ds[index].type == DS_TYPE_DERIVE)
325 event->metric_sint64 = (int64_t) vl->values[index].derive;
326 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
327 event->metric_sint64 = (int64_t) vl->values[index].absolute;
328 else
329 event->metric_sint64 = (int64_t) vl->values[index].counter;
330 }
332 /* TODO: Use FORMAT_VL() here. */
333 ssnprintf (service_buffer, sizeof(service_buffer),
334 "%s-%s-%s-%s-%s", vl->plugin, vl->plugin_instance,
335 vl->type, vl->type_instance, ds->ds[index].name);
336 event->service = strdup (service_buffer);
338 DEBUG ("write_riemann plugin: Successfully created protobuf for metric: "
339 "host = \"%s\", service = \"%s\"",
340 event->host, event->service);
341 return (event);
342 } /* }}} Event *riemann_value_to_protobuf */
344 static Msg *riemann_value_list_to_protobuf (struct riemann_host const *host, /* {{{ */
345 data_set_t const *ds,
346 value_list_t const *vl)
347 {
348 Msg *msg;
349 size_t i;
350 gauge_t *rates = NULL;
352 /* Initialize the Msg structure. */
353 msg = malloc (sizeof (*msg));
354 if (msg == NULL)
355 {
356 ERROR ("write_riemann plugin: malloc failed.");
357 return (NULL);
358 }
359 memset (msg, 0, sizeof (*msg));
360 msg__init (msg);
362 /* Set up events. First, the list of pointers. */
363 msg->n_events = (size_t) vl->values_len;
364 msg->events = calloc (msg->n_events, sizeof (*msg->events));
365 if (msg->events == NULL)
366 {
367 ERROR ("write_riemann plugin: calloc failed.");
368 riemann_msg_protobuf_free (msg);
369 return (NULL);
370 }
372 if (host->store_rates)
373 {
374 rates = uc_get_rate (ds, vl);
375 if (rates == NULL)
376 {
377 ERROR ("write_riemann plugin: uc_get_rate failed.");
378 riemann_msg_protobuf_free (msg);
379 return (NULL);
380 }
381 }
383 for (i = 0; i < msg->n_events; i++)
384 {
385 msg->events[i] = riemann_value_to_protobuf (host, ds, vl,
386 (int) i, rates);
387 if (msg->events[i] == NULL)
388 {
389 riemann_msg_protobuf_free (msg);
390 sfree (rates);
391 return (NULL);
392 }
393 }
395 sfree (rates);
396 return (msg);
397 } /* }}} Msg *riemann_value_list_to_protobuf */
399 static int
400 riemann_notification(const notification_t *n, user_data_t *ud)
401 {
402 int status;
403 struct riemann_host *host = ud->data;
404 Msg *msg;
406 msg = riemann_notification_to_protobuf (host, n);
407 if (msg == NULL)
408 return (-1);
410 status = riemann_send (host, msg);
411 if (status != 0)
412 ERROR ("write_riemann plugin: riemann_send failed with status %i",
413 status);
415 riemann_msg_protobuf_free (msg);
416 return (status);
417 } /* }}} int riemann_notification */
419 static int
420 riemann_write(const data_set_t *ds,
421 const value_list_t *vl,
422 user_data_t *ud)
423 {
424 int status;
425 struct riemann_host *host = ud->data;
426 Msg *msg;
428 msg = riemann_value_list_to_protobuf (host, ds, vl);
429 if (msg == NULL)
430 return (-1);
432 status = riemann_send (host, msg);
433 if (status != 0)
434 ERROR ("write_riemann plugin: riemann_send failed with status %i",
435 status);
437 riemann_msg_protobuf_free (msg);
438 return status;
439 }
441 /* host->lock must be held when calling this function. */
442 static int
443 riemann_connect(struct riemann_host *host)
444 {
445 int e;
446 struct addrinfo *ai, *res, hints;
447 char const *node;
448 char const *service;
450 if (host->flags & F_CONNECT)
451 return 0;
453 memset(&hints, 0, sizeof(hints));
454 memset(&service, 0, sizeof(service));
455 hints.ai_family = PF_UNSPEC;
456 hints.ai_socktype = SOCK_DGRAM;
457 #ifdef AI_ADDRCONFIG
458 hints.ai_flags |= AI_ADDRCONFIG;
459 #endif
461 node = (host->node != NULL) ? host->node : RIEMANN_HOST;
462 service = (host->service != NULL) ? host->service : RIEMANN_PORT;
464 if ((e = getaddrinfo(node, service, &hints, &res)) != 0) {
465 ERROR ("write_riemann plugin: Unable to resolve host \"%s\": %s",
466 node, gai_strerror(e));
467 return -1;
468 }
470 host->s = -1;
471 for (ai = res; ai != NULL; ai = ai->ai_next) {
472 if ((host->s = socket(ai->ai_family,
473 ai->ai_socktype,
474 ai->ai_protocol)) == -1) {
475 continue;
476 }
478 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
479 close(host->s);
480 host->s = -1;
481 continue;
482 }
484 host->flags |= F_CONNECT;
485 DEBUG("write_riemann plugin: got a succesful connection for: %s:%s",
486 node, service);
487 break;
488 }
490 freeaddrinfo(res);
492 if (host->s < 0) {
493 WARNING("write_riemann plugin: Unable to connect to Riemann at %s:%s",
494 node, service);
495 return -1;
496 }
497 return 0;
498 }
500 /* host->lock must be held when calling this function. */
501 static int
502 riemann_disconnect (struct riemann_host *host)
503 {
504 if ((host->flags & F_CONNECT) == 0)
505 return (0);
507 close (host->s);
508 host->s = -1;
509 host->flags &= ~F_CONNECT;
511 return (0);
512 }
514 static void
515 riemann_free(void *p)
516 {
517 struct riemann_host *host = p;
519 if (host == NULL)
520 return;
522 pthread_mutex_lock (&host->lock);
524 host->reference_count--;
525 if (host->reference_count > 0)
526 {
527 pthread_mutex_unlock (&host->lock);
528 return;
529 }
531 riemann_disconnect (host);
533 sfree(host->service);
534 pthread_mutex_destroy (&host->lock);
535 sfree(host);
536 }
538 static int
539 riemann_config_node(oconfig_item_t *ci)
540 {
541 struct riemann_host *host = NULL;
542 int status = 0;
543 int i;
544 oconfig_item_t *child;
545 char callback_name[DATA_MAX_NAME_LEN];
546 user_data_t ud;
548 if ((host = calloc(1, sizeof (*host))) == NULL) {
549 ERROR ("write_riemann plugin: calloc failed.");
550 return ENOMEM;
551 }
552 pthread_mutex_init (&host->lock, NULL);
553 host->reference_count = 1;
554 host->node = NULL;
555 host->service = NULL;
556 host->delay = RIEMANN_DELAY;
557 host->store_rates = 1;
559 status = cf_util_get_string (ci, &host->name);
560 if (status != 0) {
561 WARNING("write_riemann plugin: Required host name is missing.");
562 riemann_free (host);
563 return -1;
564 }
566 for (i = 0; i < ci->children_num; i++) {
567 /*
568 * The code here could be simplified but makes room
569 * for easy adding of new options later on.
570 */
571 child = &ci->children[i];
572 status = 0;
574 if (strcasecmp ("Host", child->key) == 0) {
575 status = cf_util_get_string (child, &host->node);
576 if (status != 0)
577 break;
578 } else if (strcasecmp ("Port", child->key) == 0) {
579 status = cf_util_get_service (child, &host->service);
580 if (status != 0) {
581 ERROR ("write_riemann plugin: Invalid argument "
582 "configured for the \"Port\" "
583 "option.");
584 break;
585 }
586 } else if (strcasecmp(child->key, "delay") == 0) {
587 if ((status = cf_util_get_int(ci, &host->delay)) != 0)
588 break;
589 } else if (strcasecmp ("StoreRates", child->key) == 0) {
590 status = cf_util_get_boolean (ci, &host->store_rates);
591 if (status != 0)
592 break;
593 } else {
594 WARNING("write_riemann plugin: ignoring unknown config "
595 "option: \"%s\"", child->key);
596 }
597 }
598 if (status != 0) {
599 riemann_free (host);
600 return status;
601 }
603 ssnprintf (callback_name, sizeof (callback_name), "write_riemann/%s",
604 host->name);
605 ud.data = host;
606 ud.free_func = riemann_free;
608 pthread_mutex_lock (&host->lock);
610 status = plugin_register_write (callback_name, riemann_write, &ud);
611 if (status != 0)
612 WARNING ("write_riemann plugin: plugin_register_write (\"%s\") "
613 "failed with status %i.",
614 callback_name, status);
615 else /* success */
616 host->reference_count++;
618 status = plugin_register_notification (callback_name,
619 riemann_notification, &ud);
620 if (status != 0)
621 WARNING ("write_riemann plugin: plugin_register_notification (\"%s\") "
622 "failed with status %i.",
623 callback_name, status);
624 else /* success */
625 host->reference_count++;
627 if (host->reference_count <= 1)
628 {
629 /* Both callbacks failed => free memory.
630 * We need to unlock here, because riemann_free() will lock.
631 * This is not a race condition, because we're the only one
632 * holding a reference. */
633 pthread_mutex_unlock (&host->lock);
634 riemann_free (host);
635 return (-1);
636 }
638 host->reference_count--;
639 pthread_mutex_unlock (&host->lock);
641 return status;
642 }
644 static int
645 riemann_config(oconfig_item_t *ci)
646 {
647 int i;
648 oconfig_item_t *child;
649 int status;
651 for (i = 0; i < ci->children_num; i++) {
652 child = &ci->children[i];
654 if (strcasecmp("Node", child->key) == 0) {
655 riemann_config_node (child);
656 } else if (strcasecmp(child->key, "tag") == 0) {
657 char *tmp = NULL;
658 status = cf_util_get_string(child, &tmp);
659 if (status != 0)
660 continue;
662 strarray_add (&riemann_tags, &riemann_tags_num, tmp);
663 DEBUG("write_riemann plugin: Got tag: %s", tmp);
664 sfree (tmp);
665 } else {
666 WARNING ("write_riemann plugin: Ignoring unknown "
667 "configuration option \"%s\" at top level.",
668 child->key);
669 }
670 }
671 return (0);
672 }
674 void
675 module_register(void)
676 {
677 plugin_register_complex_config ("write_riemann", riemann_config);
678 }
680 /* vim: set sw=8 sts=8 ts=8 noet : */