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>
31 #include <arpa/inet.h>
38 #include "utils_cache.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 = NULL;
123 static char **sensu_attrs = NULL;
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, sizeof(char *) * (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) {
234 ret_str = malloc(sizeof(char));
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,
342 char name_buffer[5 * DATA_MAX_NAME_LEN];
343 char service_buffer[6 * DATA_MAX_NAME_LEN];
348 // First part of the JSON string
349 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
352 build_json_str_list("handlers", &(host->metric_handlers));
353 if (handlers_str == NULL) {
354 ERROR("write_sensu plugin: Unable to alloc memory");
358 // incorporate the handlers
359 if (strlen(handlers_str) == 0) {
361 ret_str = strdup(part1);
362 if (ret_str == NULL) {
363 ERROR("write_sensu plugin: Unable to alloc memory");
367 res = my_asprintf(&ret_str, "%s, %s", part1, handlers_str);
370 ERROR("write_sensu plugin: Unable to alloc memory");
375 // incorporate the plugin name information
376 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str,
380 ERROR("write_sensu plugin: Unable to alloc memory");
385 // incorporate the plugin type
386 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str,
390 ERROR("write_sensu plugin: Unable to alloc memory");
395 // incorporate the plugin instance if any
396 if (vl->plugin_instance[0] != 0) {
397 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"",
398 ret_str, vl->plugin_instance);
401 ERROR("write_sensu plugin: Unable to alloc memory");
407 // incorporate the plugin type instance if any
408 if (vl->type_instance[0] != 0) {
410 my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"",
411 ret_str, vl->type_instance);
414 ERROR("write_sensu plugin: Unable to alloc memory");
420 // incorporate the data source type
421 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
422 char ds_type[DATA_MAX_NAME_LEN];
423 ssnprintf(ds_type, sizeof(ds_type), "%s:rate",
424 DS_TYPE_TO_STRING(ds->ds[index].type));
425 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"",
429 ERROR("write_sensu plugin: Unable to alloc memory");
434 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"",
435 ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
438 ERROR("write_sensu plugin: Unable to alloc memory");
444 // incorporate the data source name
445 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"",
446 ret_str, ds->ds[index].name);
449 ERROR("write_sensu plugin: Unable to alloc memory");
454 // incorporate the data source index
456 char ds_index[DATA_MAX_NAME_LEN];
457 ssnprintf(ds_index, sizeof(ds_index), "%zu", index);
458 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s",
462 ERROR("write_sensu plugin: Unable to alloc memory");
468 // add key value attributes from config if any
469 for (size_t i = 0; i < sensu_attrs_num; i += 2) {
470 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i],
474 ERROR("write_sensu plugin: Unable to alloc memory");
480 // incorporate sensu tags from config if any
481 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
482 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
485 ERROR("write_sensu plugin: Unable to alloc memory");
491 // calculate the value and set to a string
492 if (ds->ds[index].type == DS_TYPE_GAUGE) {
493 res = my_asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
496 ERROR("write_sensu plugin: Unable to alloc memory");
499 } else if (rates != NULL) {
500 res = my_asprintf(&value_str, GAUGE_FORMAT, rates[index]);
503 ERROR("write_sensu plugin: Unable to alloc memory");
507 if (ds->ds[index].type == DS_TYPE_DERIVE) {
508 res = my_asprintf(&value_str, "%" PRIi64, vl->values[index].derive);
511 ERROR("write_sensu plugin: Unable to alloc memory");
514 } else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
515 res = my_asprintf(&value_str, "%" PRIu64, vl->values[index].absolute);
518 ERROR("write_sensu plugin: Unable to alloc memory");
522 res = my_asprintf(&value_str, "%llu", 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 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s", name_buffer,
540 ssnprintf(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 ssnprintf(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 strncpy(r, p, strlen(p));
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);
883 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
884 (host->node != NULL) ? host->node : SENSU_HOST,
885 (host->service != NULL) ? host->service : SENSU_PORT,
886 sstrerror(errno, errbuf, sizeof(errbuf)));
891 } /* }}} int sensu_send_msg */
893 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
897 status = sensu_send_msg(host, msg);
899 host->flags &= ~F_READY;
900 if (host->res != NULL) {
901 freeaddrinfo(host->res);
908 } /* }}} int sensu_send */
910 static int sensu_write(const data_set_t *ds, /* {{{ */
911 const value_list_t *vl, user_data_t *ud) {
913 int statuses[vl->values_len];
914 struct sensu_host *host = ud->data;
915 gauge_t *rates = NULL;
918 pthread_mutex_lock(&host->lock);
919 memset(statuses, 0, vl->values_len * sizeof(*statuses));
921 if (host->store_rates) {
922 rates = uc_get_rate(ds, vl);
924 ERROR("write_sensu plugin: uc_get_rate failed.");
925 pthread_mutex_unlock(&host->lock);
929 for (size_t i = 0; i < vl->values_len; i++) {
930 msg = sensu_value_to_json(host, ds, vl, (int)i, rates, statuses[i]);
933 pthread_mutex_unlock(&host->lock);
936 status = sensu_send(host, msg);
939 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
940 pthread_mutex_unlock(&host->lock);
946 pthread_mutex_unlock(&host->lock);
948 } /* }}} int sensu_write */
950 static int sensu_notification(const notification_t *n,
951 user_data_t *ud) /* {{{ */
954 struct sensu_host *host = ud->data;
957 pthread_mutex_lock(&host->lock);
959 msg = sensu_notification_to_json(host, n);
961 pthread_mutex_unlock(&host->lock);
965 status = sensu_send(host, msg);
968 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
969 pthread_mutex_unlock(&host->lock);
972 } /* }}} int sensu_notification */
974 static void sensu_free(void *p) /* {{{ */
976 struct sensu_host *host = p;
981 pthread_mutex_lock(&host->lock);
983 host->reference_count--;
984 if (host->reference_count > 0) {
985 pthread_mutex_unlock(&host->lock);
989 sensu_close_socket(host);
990 if (host->res != NULL) {
991 freeaddrinfo(host->res);
994 sfree(host->service);
995 sfree(host->event_service_prefix);
998 sfree(host->separator);
999 free_str_list(&(host->metric_handlers));
1000 free_str_list(&(host->notification_handlers));
1001 pthread_mutex_destroy(&host->lock);
1003 } /* }}} void sensu_free */
1005 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
1007 struct sensu_host *host = NULL;
1009 oconfig_item_t *child;
1010 char callback_name[DATA_MAX_NAME_LEN];
1012 if ((host = calloc(1, sizeof(*host))) == NULL) {
1013 ERROR("write_sensu plugin: calloc failed.");
1016 pthread_mutex_init(&host->lock, NULL);
1017 host->reference_count = 1;
1019 host->service = NULL;
1020 host->notifications = 0;
1022 host->store_rates = 1;
1023 host->always_append_ds = 0;
1024 host->metric_handlers.nb_strs = 0;
1025 host->metric_handlers.strs = NULL;
1026 host->notification_handlers.nb_strs = 0;
1027 host->notification_handlers.strs = NULL;
1028 host->separator = strdup("/");
1029 if (host->separator == NULL) {
1030 ERROR("write_sensu plugin: Unable to alloc memory");
1035 status = cf_util_get_string(ci, &host->name);
1037 WARNING("write_sensu plugin: Required host name is missing.");
1042 for (int i = 0; i < ci->children_num; i++) {
1043 child = &ci->children[i];
1046 if (strcasecmp("Host", child->key) == 0) {
1047 status = cf_util_get_string(child, &host->node);
1050 } else if (strcasecmp("Notifications", child->key) == 0) {
1051 status = cf_util_get_boolean(child, &host->notifications);
1054 } else if (strcasecmp("Metrics", child->key) == 0) {
1055 status = cf_util_get_boolean(child, &host->metrics);
1058 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
1059 status = cf_util_get_string(child, &host->event_service_prefix);
1062 } else if (strcasecmp("Separator", child->key) == 0) {
1063 status = cf_util_get_string(child, &host->separator);
1066 } else if (strcasecmp("MetricHandler", child->key) == 0) {
1067 char *temp_str = NULL;
1068 status = cf_util_get_string(child, &temp_str);
1071 status = add_str_to_list(&(host->metric_handlers), temp_str);
1075 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1076 char *temp_str = NULL;
1077 status = cf_util_get_string(child, &temp_str);
1080 status = add_str_to_list(&(host->notification_handlers), temp_str);
1084 } else if (strcasecmp("Port", child->key) == 0) {
1085 status = cf_util_get_service(child, &host->service);
1087 ERROR("write_sensu plugin: Invalid argument "
1088 "configured for the \"Port\" "
1092 } else if (strcasecmp("StoreRates", child->key) == 0) {
1093 status = cf_util_get_boolean(child, &host->store_rates);
1096 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1097 status = cf_util_get_boolean(child, &host->always_append_ds);
1101 WARNING("write_sensu plugin: ignoring unknown config "
1111 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1113 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. "
1118 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1120 WARNING("write_sensu plugin: notifications enabled but no "
1121 "NotificationHandler defined. Giving up.");
1125 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1126 WARNING("write_sensu plugin: NotificationHandler given so forcing "
1127 "notifications to be enabled");
1128 host->notifications = 1;
1131 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1132 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be "
1137 if (!(host->notifications || host->metrics)) {
1138 WARNING("write_sensu plugin: neither metrics nor notifications enabled. "
1144 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1146 user_data_t ud = {.data = host, .free_func = sensu_free};
1148 pthread_mutex_lock(&host->lock);
1150 if (host->metrics) {
1151 status = plugin_register_write(callback_name, sensu_write, &ud);
1153 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1154 "failed with status %i.",
1155 callback_name, status);
1157 host->reference_count++;
1160 if (host->notifications) {
1162 plugin_register_notification(callback_name, sensu_notification, &ud);
1164 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1165 "failed with status %i.",
1166 callback_name, status);
1168 host->reference_count++;
1171 if (host->reference_count <= 1) {
1172 /* Both callbacks failed => free memory.
1173 * We need to unlock here, because sensu_free() will lock.
1174 * This is not a race condition, because we're the only one
1175 * holding a reference. */
1176 pthread_mutex_unlock(&host->lock);
1181 host->reference_count--;
1182 pthread_mutex_unlock(&host->lock);
1185 } /* }}} int sensu_config_node */
1187 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1189 oconfig_item_t *child;
1191 struct str_list sensu_tags_arr;
1193 sensu_tags_arr.nb_strs = 0;
1194 sensu_tags_arr.strs = NULL;
1196 for (int i = 0; i < ci->children_num; i++) {
1197 child = &ci->children[i];
1199 if (strcasecmp("Node", child->key) == 0) {
1200 sensu_config_node(child);
1201 } else if (strcasecmp(child->key, "attribute") == 0) {
1202 if (child->values_num != 2) {
1203 WARNING("sensu attributes need both a key and a value.");
1206 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1207 child->values[1].type != OCONFIG_TYPE_STRING) {
1208 WARNING("sensu attribute needs string arguments.");
1212 strarray_add(&sensu_attrs, &sensu_attrs_num,
1213 child->values[0].value.string);
1214 strarray_add(&sensu_attrs, &sensu_attrs_num,
1215 child->values[1].value.string);
1217 DEBUG("write_sensu plugin: New attribute: %s => %s",
1218 child->values[0].value.string, child->values[1].value.string);
1219 } else if (strcasecmp(child->key, "tag") == 0) {
1221 status = cf_util_get_string(child, &tmp);
1225 status = add_str_to_list(&sensu_tags_arr, tmp);
1229 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1231 WARNING("write_sensu plugin: Ignoring unknown "
1232 "configuration option \"%s\" at top level.",
1236 if (sensu_tags_arr.nb_strs > 0) {
1238 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1239 free_str_list(&sensu_tags_arr);
1240 if (sensu_tags == NULL) {
1241 ERROR("write_sensu plugin: Unable to alloc memory");
1246 } /* }}} int sensu_config */
1248 void module_register(void) {
1249 plugin_register_complex_config("write_sensu", sensu_config);