2 * collectd - src/write_sensu.c
3 * Copyright (C) 2015 Fabrice A. Marie
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Fabrice A. Marie <fabrice at kibinlabs.com>
33 #include "utils_cache.h"
34 #include <arpa/inet.h>
41 #define SENSU_HOST "localhost"
42 #define SENSU_PORT "3030"
45 #define my_asprintf asprintf
46 #define my_vasprintf vasprintf
49 * asprintf() is available from Solaris 10 update 11.
50 * For older versions, use asprintf() portable implementation from
51 * https://github.com/littlstar/asprintf.c/blob/master/
52 * copyright (c) 2014 joseph werle <joseph.werle@gmail.com> under MIT license.
55 static int my_vasprintf(char **str, const char *fmt, va_list args) {
60 // apply variadic arguments to
61 // sprintf with format to get size
62 size = vsnprintf(NULL, size, fmt, tmpa);
65 // return -1 to be compliant if
66 // size is less than 0
70 // alloc with size plus 1 for `\0'
71 *str = (char *)malloc(size + 1);
72 // return -1 to be compliant
73 // if pointer is `NULL'
77 // format string with original
78 // variadic arguments and set new size
79 size = vsprintf(*str, fmt, args);
83 static int my_asprintf(char **str, const char *fmt, ...) {
86 // init variadic argumens
88 // format and get size
89 size = my_vasprintf(str, fmt, args);
104 char *event_service_prefix;
105 struct str_list metric_handlers;
106 struct str_list notification_handlers;
109 pthread_mutex_t lock;
113 bool always_append_ds;
118 struct addrinfo *res;
122 static char *sensu_tags;
123 static char **sensu_attrs;
124 static size_t sensu_attrs_num;
126 static int add_str_to_list(struct str_list *strs,
127 const char *str_to_add) /* {{{ */
129 char **old_strs_ptr = strs->strs;
130 char *newstr = strdup(str_to_add);
131 if (newstr == NULL) {
132 ERROR("write_sensu plugin: Unable to alloc memory");
135 strs->strs = realloc(strs->strs, strs->nb_strs + 1);
136 if (strs->strs == NULL) {
137 strs->strs = old_strs_ptr;
139 ERROR("write_sensu plugin: Unable to alloc memory");
142 strs->strs[strs->nb_strs] = newstr;
146 /* }}} int add_str_to_list */
148 static void free_str_list(struct str_list *strs) /* {{{ */
150 for (int i = 0; i < strs->nb_strs; i++)
154 /* }}} void free_str_list */
156 static int sensu_connect(struct sensu_host *host) /* {{{ */
162 // Resolve the target if we haven't done already
163 if (!(host->flags & F_READY)) {
164 memset(&service, 0, sizeof(service));
167 node = (host->node != NULL) ? host->node : SENSU_HOST;
168 service = (host->service != NULL) ? host->service : SENSU_PORT;
170 struct addrinfo ai_hints = {.ai_family = AF_INET,
171 .ai_flags = AI_ADDRCONFIG,
172 .ai_socktype = SOCK_STREAM};
174 if ((e = getaddrinfo(node, service, &ai_hints, &(host->res))) != 0) {
175 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s", node,
179 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s", node,
181 host->flags |= F_READY;
184 struct linger so_linger;
186 for (struct addrinfo *ai = host->res; ai != NULL; ai = ai->ai_next) {
188 if ((host->s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) ==
193 // Set very low close() lingering
194 so_linger.l_onoff = 1;
195 so_linger.l_linger = 3;
196 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger,
197 sizeof so_linger) != 0)
198 WARNING("write_sensu plugin: failed to set socket close() lingering");
200 set_sock_opts(host->s);
202 // connect the socket
203 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
208 DEBUG("write_sensu plugin: connected");
213 WARNING("write_sensu plugin: Unable to connect to sensu client");
217 } /* }}} int sensu_connect */
219 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
225 } /* }}} void sensu_close_socket */
227 static char *build_json_str_list(const char *tag,
228 struct str_list const *list) /* {{{ */
231 char *ret_str = NULL;
233 if (list->nb_strs == 0) {
235 if (ret_str == NULL) {
236 ERROR("write_sensu plugin: Unable to alloc memory");
242 res = my_asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
244 ERROR("write_sensu plugin: Unable to alloc memory");
248 for (int i = 1; i < list->nb_strs; i++) {
249 res = my_asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
252 ERROR("write_sensu plugin: Unable to alloc memory");
257 res = my_asprintf(&ret_str, "%s]", temp_str);
260 ERROR("write_sensu plugin: Unable to alloc memory");
265 } /* }}} char *build_json_str_list*/
267 static int sensu_format_name2(char *ret, int ret_len, const char *hostname,
268 const char *plugin, const char *plugin_instance,
269 const char *type, const char *type_instance,
270 const char *separator) {
275 buffer_size = (size_t)ret_len;
277 #define APPEND(str) \
279 size_t l = strlen(str); \
280 if (l >= buffer_size) \
282 memcpy(buffer, (str), l); \
287 assert(plugin != NULL);
288 assert(type != NULL);
293 if ((plugin_instance != NULL) && (plugin_instance[0] != 0)) {
295 APPEND(plugin_instance);
299 if ((type_instance != NULL) && (type_instance[0] != 0)) {
301 APPEND(type_instance);
303 assert(buffer_size > 0);
308 } /* int sensu_format_name2 */
310 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
312 int len = strlen(orig_name);
313 for (int i = 0; i < len; i++) {
314 // some plugins like ipmi generate special characters in metric name
315 switch (orig_name[i]) {
336 } /* }}} char *replace_sensu_name_reserved */
338 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
339 data_set_t const *ds, value_list_t const *vl,
340 size_t index, gauge_t const *rates) {
341 char name_buffer[5 * DATA_MAX_NAME_LEN];
342 char service_buffer[6 * DATA_MAX_NAME_LEN];
347 // First part of the JSON string
348 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
351 build_json_str_list("handlers", &(host->metric_handlers));
352 if (handlers_str == NULL) {
353 ERROR("write_sensu plugin: Unable to alloc memory");
357 // incorporate the handlers
358 if (strlen(handlers_str) == 0) {
360 ret_str = strdup(part1);
361 if (ret_str == NULL) {
362 ERROR("write_sensu plugin: Unable to alloc memory");
366 res = my_asprintf(&ret_str, "%s, %s", part1, handlers_str);
369 ERROR("write_sensu plugin: Unable to alloc memory");
374 // incorporate the plugin name information
375 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str,
379 ERROR("write_sensu plugin: Unable to alloc memory");
384 // incorporate the plugin type
385 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str,
389 ERROR("write_sensu plugin: Unable to alloc memory");
394 // incorporate the plugin instance if any
395 if (vl->plugin_instance[0] != 0) {
396 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"",
397 ret_str, vl->plugin_instance);
400 ERROR("write_sensu plugin: Unable to alloc memory");
406 // incorporate the plugin type instance if any
407 if (vl->type_instance[0] != 0) {
409 my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"",
410 ret_str, vl->type_instance);
413 ERROR("write_sensu plugin: Unable to alloc memory");
419 // incorporate the data source type
420 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
421 char ds_type[DATA_MAX_NAME_LEN];
422 snprintf(ds_type, sizeof(ds_type), "%s:rate",
423 DS_TYPE_TO_STRING(ds->ds[index].type));
424 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"",
428 ERROR("write_sensu plugin: Unable to alloc memory");
433 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"",
434 ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
437 ERROR("write_sensu plugin: Unable to alloc memory");
443 // incorporate the data source name
444 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"",
445 ret_str, ds->ds[index].name);
448 ERROR("write_sensu plugin: Unable to alloc memory");
453 // incorporate the data source index
455 char ds_index[DATA_MAX_NAME_LEN];
456 snprintf(ds_index, sizeof(ds_index), "%" PRIsz, index);
457 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s",
461 ERROR("write_sensu plugin: Unable to alloc memory");
467 // add key value attributes from config if any
468 for (size_t i = 0; i < sensu_attrs_num; i += 2) {
469 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i],
473 ERROR("write_sensu plugin: Unable to alloc memory");
479 // incorporate sensu tags from config if any
480 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
481 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
484 ERROR("write_sensu plugin: Unable to alloc memory");
490 // calculate the value and set to a string
491 if (ds->ds[index].type == DS_TYPE_GAUGE) {
492 res = my_asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
495 ERROR("write_sensu plugin: Unable to alloc memory");
498 } else if (rates != NULL) {
499 res = my_asprintf(&value_str, GAUGE_FORMAT, rates[index]);
502 ERROR("write_sensu plugin: Unable to alloc memory");
506 if (ds->ds[index].type == DS_TYPE_DERIVE) {
507 res = my_asprintf(&value_str, "%" PRIi64, vl->values[index].derive);
510 ERROR("write_sensu plugin: Unable to alloc memory");
513 } else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
514 res = my_asprintf(&value_str, "%" PRIu64, vl->values[index].absolute);
517 ERROR("write_sensu plugin: Unable to alloc memory");
521 res = my_asprintf(&value_str, "%" PRIu64,
522 (uint64_t)vl->values[index].counter);
525 ERROR("write_sensu plugin: Unable to alloc memory");
531 // Generate the full service name
532 sensu_format_name2(name_buffer, sizeof(name_buffer), vl->host, vl->plugin,
533 vl->plugin_instance, vl->type, vl->type_instance,
535 if (host->always_append_ds || (ds->ds_num > 1)) {
536 if (host->event_service_prefix == NULL)
537 snprintf(service_buffer, sizeof(service_buffer), "%s.%s", name_buffer,
540 snprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
541 host->event_service_prefix, name_buffer, ds->ds[index].name);
543 if (host->event_service_prefix == NULL)
544 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
546 snprintf(service_buffer, sizeof(service_buffer), "%s%s",
547 host->event_service_prefix, name_buffer);
550 // Replace collectd sensor name reserved characters so that time series DB is
552 in_place_replace_sensu_name_reserved(service_buffer);
554 // finalize the buffer by setting the output and closing curly bracket
555 res = my_asprintf(&temp_str, "%s, \"output\": \"%s %s %lld\"}\n", ret_str,
556 service_buffer, value_str,
557 (long long)CDTIME_T_TO_TIME_T(vl->time));
561 ERROR("write_sensu plugin: Unable to alloc memory");
566 DEBUG("write_sensu plugin: Successfully created json for metric: "
567 "host = \"%s\", service = \"%s\"",
568 vl->host, service_buffer);
570 } /* }}} char *sensu_value_to_json */
573 * Uses replace_str2() implementation from
574 * http://creativeandcritical.net/str-replace-c/
575 * copyright (c) Laird Shaw, under public domain.
577 static char *replace_str(const char *str, const char *old, /* {{{ */
581 size_t oldlen = strlen(old);
582 size_t count = strlen(new);
584 size_t newlen = count;
585 int samesize = (oldlen == newlen);
588 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
590 /* This is undefined if p - str > PTRDIFF_MAX */
591 retlen = p - str + strlen(p) + count * (newlen - oldlen);
593 retlen = strlen(str);
595 ret = calloc(1, retlen + 1);
598 // added to original: not optimized, but keeps valgrind happy.
603 /* If the old and new strings are different lengths - in other
604 * words we have already iterated through with strstr above,
605 * and thus we know how many times we need to call it - then we
606 * can avoid the final (potentially lengthy) call to strstr,
607 * which we already know is going to return NULL, by
608 * decrementing and checking count.
610 if (!samesize && !count--)
612 /* Otherwise i.e. when the old and new strings are the same
613 * length, and we don't know how many times to call strstr,
614 * we must check for a NULL return here (we check it in any
615 * event, to avoid further conditions, and because there's
616 * no harm done with the check even when the old and new
617 * strings are different lengths).
619 if ((q = strstr(p, old)) == NULL)
621 /* This is undefined if q - p > PTRDIFF_MAX */
625 memcpy(r, new, newlen);
629 sstrncpy(r, p, retlen + 1);
632 } /* }}} char *replace_str */
634 static char *replace_json_reserved(const char *message) /* {{{ */
636 char *msg = replace_str(message, "\\", "\\\\");
638 ERROR("write_sensu plugin: Unable to alloc memory");
641 char *tmp = replace_str(msg, "\"", "\\\"");
644 ERROR("write_sensu plugin: Unable to alloc memory");
647 msg = replace_str(tmp, "\n", "\\\n");
650 ERROR("write_sensu plugin: Unable to alloc memory");
654 } /* }}} char *replace_json_reserved */
656 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
657 notification_t const *n) {
658 char service_buffer[6 * DATA_MAX_NAME_LEN];
659 char const *severity;
665 // add the severity/status
666 switch (n->severity) {
672 severity = "WARNING";
676 severity = "CRITICAL";
680 severity = "UNKNOWN";
683 res = my_asprintf(&temp_str, "{\"status\": %d", status);
685 ERROR("write_sensu plugin: Unable to alloc memory");
690 // incorporate the timestamp
691 res = my_asprintf(&temp_str, "%s, \"timestamp\": %lld", ret_str,
692 (long long)CDTIME_T_TO_TIME_T(n->time));
695 ERROR("write_sensu plugin: Unable to alloc memory");
701 build_json_str_list("handlers", &(host->notification_handlers));
702 if (handlers_str == NULL) {
703 ERROR("write_sensu plugin: Unable to alloc memory");
707 // incorporate the handlers
708 if (strlen(handlers_str) != 0) {
709 res = my_asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
713 ERROR("write_sensu plugin: Unable to alloc memory");
721 // incorporate the plugin name information if any
722 if (n->plugin[0] != 0) {
723 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str,
727 ERROR("write_sensu plugin: Unable to alloc memory");
733 // incorporate the plugin type if any
734 if (n->type[0] != 0) {
735 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"",
739 ERROR("write_sensu plugin: Unable to alloc memory");
745 // incorporate the plugin instance if any
746 if (n->plugin_instance[0] != 0) {
747 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"",
748 ret_str, n->plugin_instance);
751 ERROR("write_sensu plugin: Unable to alloc memory");
757 // incorporate the plugin type instance if any
758 if (n->type_instance[0] != 0) {
760 my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"",
761 ret_str, n->type_instance);
764 ERROR("write_sensu plugin: Unable to alloc memory");
770 // add key value attributes from config if any
771 for (i = 0; i < sensu_attrs_num; i += 2) {
772 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i],
776 ERROR("write_sensu plugin: Unable to alloc memory");
782 // incorporate sensu tags from config if any
783 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
784 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
787 ERROR("write_sensu plugin: Unable to alloc memory");
793 // incorporate the service name
794 sensu_format_name2(service_buffer, sizeof(service_buffer),
795 /* host */ "", n->plugin, n->plugin_instance, n->type,
796 n->type_instance, host->separator);
797 // replace sensu event name chars that are considered illegal
798 in_place_replace_sensu_name_reserved(service_buffer);
799 res = my_asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str,
803 ERROR("write_sensu plugin: Unable to alloc memory");
808 // incorporate the check output
809 if (n->message[0] != 0) {
810 char *msg = replace_json_reserved(n->message);
812 ERROR("write_sensu plugin: Unable to alloc memory");
816 res = my_asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str,
821 ERROR("write_sensu plugin: Unable to alloc memory");
827 // Pull in values from threshold and add extra attributes
828 for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
829 if (strcasecmp("CurrentValue", meta->name) == 0 &&
830 meta->type == NM_TYPE_DOUBLE) {
831 res = my_asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str,
832 meta->nm_value.nm_double);
835 ERROR("write_sensu plugin: Unable to alloc memory");
840 if (meta->type == NM_TYPE_STRING) {
841 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name,
842 meta->nm_value.nm_string);
845 ERROR("write_sensu plugin: Unable to alloc memory");
852 // close the curly bracket
853 res = my_asprintf(&temp_str, "%s}\n", ret_str);
856 ERROR("write_sensu plugin: Unable to alloc memory");
861 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
862 "host = \"%s\", service = \"%s\", state = \"%s\"",
863 n->host, service_buffer, severity);
865 } /* }}} char *sensu_notification_to_json */
867 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
872 status = sensu_connect(host);
876 buffer_len = strlen(msg);
878 status = (int)swrite(host->s, msg, buffer_len);
879 sensu_close_socket(host);
882 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
883 (host->node != NULL) ? host->node : SENSU_HOST,
884 (host->service != NULL) ? host->service : SENSU_PORT, STRERRNO);
889 } /* }}} int sensu_send_msg */
891 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
895 status = sensu_send_msg(host, msg);
897 host->flags &= ~F_READY;
898 if (host->res != NULL) {
899 freeaddrinfo(host->res);
906 } /* }}} int sensu_send */
908 static int sensu_write(const data_set_t *ds, /* {{{ */
909 const value_list_t *vl, user_data_t *ud) {
911 int statuses[vl->values_len];
912 struct sensu_host *host = ud->data;
913 gauge_t *rates = NULL;
916 pthread_mutex_lock(&host->lock);
917 memset(statuses, 0, vl->values_len * sizeof(*statuses));
919 if (host->store_rates) {
920 rates = uc_get_rate(ds, vl);
922 ERROR("write_sensu plugin: uc_get_rate failed.");
923 pthread_mutex_unlock(&host->lock);
927 for (size_t i = 0; i < vl->values_len; i++) {
928 msg = sensu_value_to_json(host, ds, vl, (int)i, rates);
931 pthread_mutex_unlock(&host->lock);
934 status = sensu_send(host, msg);
937 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
938 pthread_mutex_unlock(&host->lock);
944 pthread_mutex_unlock(&host->lock);
946 } /* }}} int sensu_write */
948 static int sensu_notification(const notification_t *n,
949 user_data_t *ud) /* {{{ */
952 struct sensu_host *host = ud->data;
955 pthread_mutex_lock(&host->lock);
957 msg = sensu_notification_to_json(host, n);
959 pthread_mutex_unlock(&host->lock);
963 status = sensu_send(host, msg);
966 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
967 pthread_mutex_unlock(&host->lock);
970 } /* }}} int sensu_notification */
972 static void sensu_free(void *p) /* {{{ */
974 struct sensu_host *host = p;
979 pthread_mutex_lock(&host->lock);
981 host->reference_count--;
982 if (host->reference_count > 0) {
983 pthread_mutex_unlock(&host->lock);
987 sensu_close_socket(host);
988 if (host->res != NULL) {
989 freeaddrinfo(host->res);
992 sfree(host->service);
993 sfree(host->event_service_prefix);
996 sfree(host->separator);
997 free_str_list(&(host->metric_handlers));
998 free_str_list(&(host->notification_handlers));
1000 pthread_mutex_unlock(&host->lock);
1001 pthread_mutex_destroy(&host->lock);
1004 } /* }}} void sensu_free */
1006 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
1008 struct sensu_host *host = NULL;
1010 oconfig_item_t *child;
1011 char callback_name[DATA_MAX_NAME_LEN];
1013 if ((host = calloc(1, sizeof(*host))) == NULL) {
1014 ERROR("write_sensu plugin: calloc failed.");
1017 pthread_mutex_init(&host->lock, NULL);
1018 host->reference_count = 1;
1020 host->service = NULL;
1021 host->notifications = false;
1022 host->metrics = false;
1023 host->store_rates = true;
1024 host->always_append_ds = false;
1025 host->metric_handlers.nb_strs = 0;
1026 host->metric_handlers.strs = NULL;
1027 host->notification_handlers.nb_strs = 0;
1028 host->notification_handlers.strs = NULL;
1029 host->separator = strdup("/");
1030 if (host->separator == NULL) {
1031 ERROR("write_sensu plugin: Unable to alloc memory");
1036 status = cf_util_get_string(ci, &host->name);
1038 WARNING("write_sensu plugin: Required host name is missing.");
1043 for (int i = 0; i < ci->children_num; i++) {
1044 child = &ci->children[i];
1047 if (strcasecmp("Host", child->key) == 0) {
1048 status = cf_util_get_string(child, &host->node);
1051 } else if (strcasecmp("Notifications", child->key) == 0) {
1052 status = cf_util_get_boolean(child, &host->notifications);
1055 } else if (strcasecmp("Metrics", child->key) == 0) {
1056 status = cf_util_get_boolean(child, &host->metrics);
1059 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
1060 status = cf_util_get_string(child, &host->event_service_prefix);
1063 } else if (strcasecmp("Separator", child->key) == 0) {
1064 status = cf_util_get_string(child, &host->separator);
1067 } else if (strcasecmp("MetricHandler", child->key) == 0) {
1068 char *temp_str = NULL;
1069 status = cf_util_get_string(child, &temp_str);
1072 status = add_str_to_list(&(host->metric_handlers), temp_str);
1076 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1077 char *temp_str = NULL;
1078 status = cf_util_get_string(child, &temp_str);
1081 status = add_str_to_list(&(host->notification_handlers), temp_str);
1085 } else if (strcasecmp("Port", child->key) == 0) {
1086 status = cf_util_get_service(child, &host->service);
1088 ERROR("write_sensu plugin: Invalid argument "
1089 "configured for the \"Port\" "
1093 } else if (strcasecmp("StoreRates", child->key) == 0) {
1094 status = cf_util_get_boolean(child, &host->store_rates);
1097 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1098 status = cf_util_get_boolean(child, &host->always_append_ds);
1102 WARNING("write_sensu plugin: ignoring unknown config "
1112 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1114 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. "
1119 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1121 WARNING("write_sensu plugin: notifications enabled but no "
1122 "NotificationHandler defined. Giving up.");
1126 if ((host->notification_handlers.nb_strs > 0) &&
1127 (host->notifications == false)) {
1128 WARNING("write_sensu plugin: NotificationHandler given so forcing "
1129 "notifications to be enabled");
1130 host->notifications = 1;
1133 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == false)) {
1134 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be "
1136 host->metrics = true;
1139 if (!(host->notifications || host->metrics)) {
1140 WARNING("write_sensu plugin: neither metrics nor notifications enabled. "
1146 snprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1148 user_data_t ud = {.data = host, .free_func = sensu_free};
1150 pthread_mutex_lock(&host->lock);
1152 if (host->metrics) {
1153 status = plugin_register_write(callback_name, sensu_write, &ud);
1155 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1156 "failed with status %i.",
1157 callback_name, status);
1159 host->reference_count++;
1162 if (host->notifications) {
1164 plugin_register_notification(callback_name, sensu_notification, &ud);
1166 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1167 "failed with status %i.",
1168 callback_name, status);
1170 host->reference_count++;
1173 if (host->reference_count <= 1) {
1174 /* Both callbacks failed => free memory.
1175 * We need to unlock here, because sensu_free() will lock.
1176 * This is not a race condition, because we're the only one
1177 * holding a reference. */
1178 pthread_mutex_unlock(&host->lock);
1183 host->reference_count--;
1184 pthread_mutex_unlock(&host->lock);
1187 } /* }}} int sensu_config_node */
1189 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1191 oconfig_item_t *child;
1193 struct str_list sensu_tags_arr;
1195 sensu_tags_arr.nb_strs = 0;
1196 sensu_tags_arr.strs = NULL;
1198 for (int i = 0; i < ci->children_num; i++) {
1199 child = &ci->children[i];
1201 if (strcasecmp("Node", child->key) == 0) {
1202 sensu_config_node(child);
1203 } else if (strcasecmp(child->key, "attribute") == 0) {
1204 if (child->values_num != 2) {
1205 WARNING("sensu attributes need both a key and a value.");
1208 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1209 child->values[1].type != OCONFIG_TYPE_STRING) {
1210 WARNING("sensu attribute needs string arguments.");
1214 strarray_add(&sensu_attrs, &sensu_attrs_num,
1215 child->values[0].value.string);
1216 strarray_add(&sensu_attrs, &sensu_attrs_num,
1217 child->values[1].value.string);
1219 DEBUG("write_sensu plugin: New attribute: %s => %s",
1220 child->values[0].value.string, child->values[1].value.string);
1221 } else if (strcasecmp(child->key, "tag") == 0) {
1223 status = cf_util_get_string(child, &tmp);
1227 status = add_str_to_list(&sensu_tags_arr, tmp);
1231 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1233 WARNING("write_sensu plugin: Ignoring unknown "
1234 "configuration option \"%s\" at top level.",
1238 if (sensu_tags_arr.nb_strs > 0) {
1240 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1241 free_str_list(&sensu_tags_arr);
1242 if (sensu_tags == NULL) {
1243 ERROR("write_sensu plugin: Unable to alloc memory");
1248 } /* }}} int sensu_config */
1250 void module_register(void) {
1251 plugin_register_complex_config("write_sensu", sensu_config);