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"
35 #include <arpa/inet.h>
42 #define SENSU_HOST "localhost"
43 #define SENSU_PORT "3030"
46 #define my_asprintf asprintf
47 #define my_vasprintf vasprintf
50 * asprintf() is available from Solaris 10 update 11.
51 * For older versions, use asprintf() portable implementation from
52 * https://github.com/littlstar/asprintf.c/blob/master/
53 * copyright (c) 2014 joseph werle <joseph.werle@gmail.com> under MIT license.
56 static int my_vasprintf(char **str, const char *fmt, va_list args) {
61 // apply variadic arguments to
62 // sprintf with format to get size
63 size = vsnprintf(NULL, size, fmt, tmpa);
66 // return -1 to be compliant if
67 // size is less than 0
71 // alloc with size plus 1 for `\0'
72 *str = (char *)malloc(size + 1);
73 // return -1 to be compliant
74 // if pointer is `NULL'
78 // format string with original
79 // variadic arguments and set new size
80 size = vsprintf(*str, fmt, args);
84 static int my_asprintf(char **str, const char *fmt, ...) {
87 // init variadic argumens
89 // format and get size
90 size = my_vasprintf(str, fmt, args);
105 char *event_service_prefix;
106 struct str_list metric_handlers;
107 struct str_list notification_handlers;
110 pthread_mutex_t lock;
114 _Bool always_append_ds;
119 struct addrinfo *res;
123 static char *sensu_tags = NULL;
124 static char **sensu_attrs = NULL;
125 static size_t sensu_attrs_num;
127 static int add_str_to_list(struct str_list *strs,
128 const char *str_to_add) /* {{{ */
130 char **old_strs_ptr = strs->strs;
131 char *newstr = strdup(str_to_add);
132 if (newstr == NULL) {
133 ERROR("write_sensu plugin: Unable to alloc memory");
136 strs->strs = realloc(strs->strs, strs->nb_strs + 1);
137 if (strs->strs == NULL) {
138 strs->strs = old_strs_ptr;
140 ERROR("write_sensu plugin: Unable to alloc memory");
143 strs->strs[strs->nb_strs] = newstr;
147 /* }}} int add_str_to_list */
149 static void free_str_list(struct str_list *strs) /* {{{ */
151 for (int i = 0; i < strs->nb_strs; i++)
155 /* }}} void free_str_list */
157 static int sensu_connect(struct sensu_host *host) /* {{{ */
163 // Resolve the target if we haven't done already
164 if (!(host->flags & F_READY)) {
165 memset(&service, 0, sizeof(service));
168 node = (host->node != NULL) ? host->node : SENSU_HOST;
169 service = (host->service != NULL) ? host->service : SENSU_PORT;
171 struct addrinfo ai_hints = {.ai_family = AF_INET,
172 .ai_flags = AI_ADDRCONFIG,
173 .ai_socktype = SOCK_STREAM};
175 if ((e = getaddrinfo(node, service, &ai_hints, &(host->res))) != 0) {
176 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s", node,
180 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s", node,
182 host->flags |= F_READY;
185 struct linger so_linger;
187 for (struct addrinfo *ai = host->res; ai != NULL; ai = ai->ai_next) {
189 if ((host->s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) ==
194 // Set very low close() lingering
195 so_linger.l_onoff = 1;
196 so_linger.l_linger = 3;
197 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger,
198 sizeof so_linger) != 0)
199 WARNING("write_sensu plugin: failed to set socket close() lingering");
201 set_sock_opts(host->s);
203 // connect the socket
204 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
209 DEBUG("write_sensu plugin: connected");
214 WARNING("write_sensu plugin: Unable to connect to sensu client");
218 } /* }}} int sensu_connect */
220 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
226 } /* }}} void sensu_close_socket */
228 static char *build_json_str_list(const char *tag,
229 struct str_list const *list) /* {{{ */
232 char *ret_str = NULL;
234 if (list->nb_strs == 0) {
236 if (ret_str == NULL) {
237 ERROR("write_sensu plugin: Unable to alloc memory");
243 res = my_asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
245 ERROR("write_sensu plugin: Unable to alloc memory");
249 for (int i = 1; i < list->nb_strs; i++) {
250 res = my_asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
253 ERROR("write_sensu plugin: Unable to alloc memory");
258 res = my_asprintf(&ret_str, "%s]", temp_str);
261 ERROR("write_sensu plugin: Unable to alloc memory");
266 } /* }}} char *build_json_str_list*/
268 static int sensu_format_name2(char *ret, int ret_len, const char *hostname,
269 const char *plugin, const char *plugin_instance,
270 const char *type, const char *type_instance,
271 const char *separator) {
276 buffer_size = (size_t)ret_len;
278 #define APPEND(str) \
280 size_t l = strlen(str); \
281 if (l >= buffer_size) \
283 memcpy(buffer, (str), l); \
288 assert(plugin != NULL);
289 assert(type != NULL);
294 if ((plugin_instance != NULL) && (plugin_instance[0] != 0)) {
296 APPEND(plugin_instance);
300 if ((type_instance != NULL) && (type_instance[0] != 0)) {
302 APPEND(type_instance);
304 assert(buffer_size > 0);
309 } /* int sensu_format_name2 */
311 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
313 int len = strlen(orig_name);
314 for (int i = 0; i < len; i++) {
315 // some plugins like ipmi generate special characters in metric name
316 switch (orig_name[i]) {
337 } /* }}} char *replace_sensu_name_reserved */
339 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
340 data_set_t const *ds, value_list_t const *vl,
341 size_t index, gauge_t const *rates,
343 char name_buffer[5 * DATA_MAX_NAME_LEN];
344 char service_buffer[6 * DATA_MAX_NAME_LEN];
349 // First part of the JSON string
350 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
353 build_json_str_list("handlers", &(host->metric_handlers));
354 if (handlers_str == NULL) {
355 ERROR("write_sensu plugin: Unable to alloc memory");
359 // incorporate the handlers
360 if (strlen(handlers_str) == 0) {
362 ret_str = strdup(part1);
363 if (ret_str == NULL) {
364 ERROR("write_sensu plugin: Unable to alloc memory");
368 res = my_asprintf(&ret_str, "%s, %s", part1, handlers_str);
371 ERROR("write_sensu plugin: Unable to alloc memory");
376 // incorporate the plugin name information
377 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str,
381 ERROR("write_sensu plugin: Unable to alloc memory");
386 // incorporate the plugin type
387 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str,
391 ERROR("write_sensu plugin: Unable to alloc memory");
396 // incorporate the plugin instance if any
397 if (vl->plugin_instance[0] != 0) {
398 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"",
399 ret_str, vl->plugin_instance);
402 ERROR("write_sensu plugin: Unable to alloc memory");
408 // incorporate the plugin type instance if any
409 if (vl->type_instance[0] != 0) {
411 my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"",
412 ret_str, vl->type_instance);
415 ERROR("write_sensu plugin: Unable to alloc memory");
421 // incorporate the data source type
422 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
423 char ds_type[DATA_MAX_NAME_LEN];
424 snprintf(ds_type, sizeof(ds_type), "%s:rate",
425 DS_TYPE_TO_STRING(ds->ds[index].type));
426 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"",
430 ERROR("write_sensu plugin: Unable to alloc memory");
435 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"",
436 ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
439 ERROR("write_sensu plugin: Unable to alloc memory");
445 // incorporate the data source name
446 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"",
447 ret_str, ds->ds[index].name);
450 ERROR("write_sensu plugin: Unable to alloc memory");
455 // incorporate the data source index
457 char ds_index[DATA_MAX_NAME_LEN];
458 snprintf(ds_index, sizeof(ds_index), "%zu", index);
459 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s",
463 ERROR("write_sensu plugin: Unable to alloc memory");
469 // add key value attributes from config if any
470 for (size_t i = 0; i < sensu_attrs_num; i += 2) {
471 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i],
475 ERROR("write_sensu plugin: Unable to alloc memory");
481 // incorporate sensu tags from config if any
482 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
483 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
486 ERROR("write_sensu plugin: Unable to alloc memory");
492 // calculate the value and set to a string
493 if (ds->ds[index].type == DS_TYPE_GAUGE) {
494 res = my_asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
497 ERROR("write_sensu plugin: Unable to alloc memory");
500 } else if (rates != NULL) {
501 res = my_asprintf(&value_str, GAUGE_FORMAT, rates[index]);
504 ERROR("write_sensu plugin: Unable to alloc memory");
508 if (ds->ds[index].type == DS_TYPE_DERIVE) {
509 res = my_asprintf(&value_str, "%" PRIi64, vl->values[index].derive);
512 ERROR("write_sensu plugin: Unable to alloc memory");
515 } else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
516 res = my_asprintf(&value_str, "%" PRIu64, vl->values[index].absolute);
519 ERROR("write_sensu plugin: Unable to alloc memory");
523 res = my_asprintf(&value_str, "%llu", vl->values[index].counter);
526 ERROR("write_sensu plugin: Unable to alloc memory");
532 // Generate the full service name
533 sensu_format_name2(name_buffer, sizeof(name_buffer), vl->host, vl->plugin,
534 vl->plugin_instance, vl->type, vl->type_instance,
536 if (host->always_append_ds || (ds->ds_num > 1)) {
537 if (host->event_service_prefix == NULL)
538 snprintf(service_buffer, sizeof(service_buffer), "%s.%s", name_buffer,
541 snprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
542 host->event_service_prefix, name_buffer, ds->ds[index].name);
544 if (host->event_service_prefix == NULL)
545 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
547 snprintf(service_buffer, sizeof(service_buffer), "%s%s",
548 host->event_service_prefix, name_buffer);
551 // Replace collectd sensor name reserved characters so that time series DB is
553 in_place_replace_sensu_name_reserved(service_buffer);
555 // finalize the buffer by setting the output and closing curly bracket
556 res = my_asprintf(&temp_str, "%s, \"output\": \"%s %s %lld\"}\n", ret_str,
557 service_buffer, value_str,
558 (long long)CDTIME_T_TO_TIME_T(vl->time));
562 ERROR("write_sensu plugin: Unable to alloc memory");
567 DEBUG("write_sensu plugin: Successfully created json for metric: "
568 "host = \"%s\", service = \"%s\"",
569 vl->host, service_buffer);
571 } /* }}} char *sensu_value_to_json */
574 * Uses replace_str2() implementation from
575 * http://creativeandcritical.net/str-replace-c/
576 * copyright (c) Laird Shaw, under public domain.
578 static char *replace_str(const char *str, const char *old, /* {{{ */
582 size_t oldlen = strlen(old);
583 size_t count = strlen(new);
585 size_t newlen = count;
586 int samesize = (oldlen == newlen);
589 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
591 /* This is undefined if p - str > PTRDIFF_MAX */
592 retlen = p - str + strlen(p) + count * (newlen - oldlen);
594 retlen = strlen(str);
596 ret = calloc(1, retlen + 1);
599 // added to original: not optimized, but keeps valgrind happy.
604 /* If the old and new strings are different lengths - in other
605 * words we have already iterated through with strstr above,
606 * and thus we know how many times we need to call it - then we
607 * can avoid the final (potentially lengthy) call to strstr,
608 * which we already know is going to return NULL, by
609 * decrementing and checking count.
611 if (!samesize && !count--)
613 /* Otherwise i.e. when the old and new strings are the same
614 * length, and we don't know how many times to call strstr,
615 * we must check for a NULL return here (we check it in any
616 * event, to avoid further conditions, and because there's
617 * no harm done with the check even when the old and new
618 * strings are different lengths).
620 if ((q = strstr(p, old)) == NULL)
622 /* This is undefined if q - p > PTRDIFF_MAX */
626 memcpy(r, new, newlen);
630 strncpy(r, p, strlen(p));
633 } /* }}} char *replace_str */
635 static char *replace_json_reserved(const char *message) /* {{{ */
637 char *msg = replace_str(message, "\\", "\\\\");
639 ERROR("write_sensu plugin: Unable to alloc memory");
642 char *tmp = replace_str(msg, "\"", "\\\"");
645 ERROR("write_sensu plugin: Unable to alloc memory");
648 msg = replace_str(tmp, "\n", "\\\n");
651 ERROR("write_sensu plugin: Unable to alloc memory");
655 } /* }}} char *replace_json_reserved */
657 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
658 notification_t const *n) {
659 char service_buffer[6 * DATA_MAX_NAME_LEN];
660 char const *severity;
666 // add the severity/status
667 switch (n->severity) {
673 severity = "WARNING";
677 severity = "CRITICAL";
681 severity = "UNKNOWN";
684 res = my_asprintf(&temp_str, "{\"status\": %d", status);
686 ERROR("write_sensu plugin: Unable to alloc memory");
691 // incorporate the timestamp
692 res = my_asprintf(&temp_str, "%s, \"timestamp\": %lld", ret_str,
693 (long long)CDTIME_T_TO_TIME_T(n->time));
696 ERROR("write_sensu plugin: Unable to alloc memory");
702 build_json_str_list("handlers", &(host->notification_handlers));
703 if (handlers_str == NULL) {
704 ERROR("write_sensu plugin: Unable to alloc memory");
708 // incorporate the handlers
709 if (strlen(handlers_str) != 0) {
710 res = my_asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
714 ERROR("write_sensu plugin: Unable to alloc memory");
722 // incorporate the plugin name information if any
723 if (n->plugin[0] != 0) {
724 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str,
728 ERROR("write_sensu plugin: Unable to alloc memory");
734 // incorporate the plugin type if any
735 if (n->type[0] != 0) {
736 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"",
740 ERROR("write_sensu plugin: Unable to alloc memory");
746 // incorporate the plugin instance if any
747 if (n->plugin_instance[0] != 0) {
748 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"",
749 ret_str, n->plugin_instance);
752 ERROR("write_sensu plugin: Unable to alloc memory");
758 // incorporate the plugin type instance if any
759 if (n->type_instance[0] != 0) {
761 my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"",
762 ret_str, n->type_instance);
765 ERROR("write_sensu plugin: Unable to alloc memory");
771 // add key value attributes from config if any
772 for (i = 0; i < sensu_attrs_num; i += 2) {
773 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i],
777 ERROR("write_sensu plugin: Unable to alloc memory");
783 // incorporate sensu tags from config if any
784 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
785 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
788 ERROR("write_sensu plugin: Unable to alloc memory");
794 // incorporate the service name
795 sensu_format_name2(service_buffer, sizeof(service_buffer),
796 /* host */ "", n->plugin, n->plugin_instance, n->type,
797 n->type_instance, host->separator);
798 // replace sensu event name chars that are considered illegal
799 in_place_replace_sensu_name_reserved(service_buffer);
800 res = my_asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str,
804 ERROR("write_sensu plugin: Unable to alloc memory");
809 // incorporate the check output
810 if (n->message[0] != 0) {
811 char *msg = replace_json_reserved(n->message);
813 ERROR("write_sensu plugin: Unable to alloc memory");
817 res = my_asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str,
822 ERROR("write_sensu plugin: Unable to alloc memory");
828 // Pull in values from threshold and add extra attributes
829 for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
830 if (strcasecmp("CurrentValue", meta->name) == 0 &&
831 meta->type == NM_TYPE_DOUBLE) {
832 res = my_asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str,
833 meta->nm_value.nm_double);
836 ERROR("write_sensu plugin: Unable to alloc memory");
841 if (meta->type == NM_TYPE_STRING) {
842 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name,
843 meta->nm_value.nm_string);
846 ERROR("write_sensu plugin: Unable to alloc memory");
853 // close the curly bracket
854 res = my_asprintf(&temp_str, "%s}\n", ret_str);
857 ERROR("write_sensu plugin: Unable to alloc memory");
862 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
863 "host = \"%s\", service = \"%s\", state = \"%s\"",
864 n->host, service_buffer, severity);
866 } /* }}} char *sensu_notification_to_json */
868 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
873 status = sensu_connect(host);
877 buffer_len = strlen(msg);
879 status = (int)swrite(host->s, msg, buffer_len);
880 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, STRERRNO);
890 } /* }}} int sensu_send_msg */
892 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
896 status = sensu_send_msg(host, msg);
898 host->flags &= ~F_READY;
899 if (host->res != NULL) {
900 freeaddrinfo(host->res);
907 } /* }}} int sensu_send */
909 static int sensu_write(const data_set_t *ds, /* {{{ */
910 const value_list_t *vl, user_data_t *ud) {
912 int statuses[vl->values_len];
913 struct sensu_host *host = ud->data;
914 gauge_t *rates = NULL;
917 pthread_mutex_lock(&host->lock);
918 memset(statuses, 0, vl->values_len * sizeof(*statuses));
920 if (host->store_rates) {
921 rates = uc_get_rate(ds, vl);
923 ERROR("write_sensu plugin: uc_get_rate failed.");
924 pthread_mutex_unlock(&host->lock);
928 for (size_t i = 0; i < vl->values_len; i++) {
929 msg = sensu_value_to_json(host, ds, vl, (int)i, rates, statuses[i]);
932 pthread_mutex_unlock(&host->lock);
935 status = sensu_send(host, msg);
938 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
939 pthread_mutex_unlock(&host->lock);
945 pthread_mutex_unlock(&host->lock);
947 } /* }}} int sensu_write */
949 static int sensu_notification(const notification_t *n,
950 user_data_t *ud) /* {{{ */
953 struct sensu_host *host = ud->data;
956 pthread_mutex_lock(&host->lock);
958 msg = sensu_notification_to_json(host, n);
960 pthread_mutex_unlock(&host->lock);
964 status = sensu_send(host, msg);
967 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
968 pthread_mutex_unlock(&host->lock);
971 } /* }}} int sensu_notification */
973 static void sensu_free(void *p) /* {{{ */
975 struct sensu_host *host = p;
980 pthread_mutex_lock(&host->lock);
982 host->reference_count--;
983 if (host->reference_count > 0) {
984 pthread_mutex_unlock(&host->lock);
988 sensu_close_socket(host);
989 if (host->res != NULL) {
990 freeaddrinfo(host->res);
993 sfree(host->service);
994 sfree(host->event_service_prefix);
997 sfree(host->separator);
998 free_str_list(&(host->metric_handlers));
999 free_str_list(&(host->notification_handlers));
1001 pthread_mutex_unlock(&host->lock);
1002 pthread_mutex_destroy(&host->lock);
1005 } /* }}} void sensu_free */
1007 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
1009 struct sensu_host *host = NULL;
1011 oconfig_item_t *child;
1012 char callback_name[DATA_MAX_NAME_LEN];
1014 if ((host = calloc(1, sizeof(*host))) == NULL) {
1015 ERROR("write_sensu plugin: calloc failed.");
1018 pthread_mutex_init(&host->lock, NULL);
1019 host->reference_count = 1;
1021 host->service = NULL;
1022 host->notifications = 0;
1024 host->store_rates = 1;
1025 host->always_append_ds = 0;
1026 host->metric_handlers.nb_strs = 0;
1027 host->metric_handlers.strs = NULL;
1028 host->notification_handlers.nb_strs = 0;
1029 host->notification_handlers.strs = NULL;
1030 host->separator = strdup("/");
1031 if (host->separator == NULL) {
1032 ERROR("write_sensu plugin: Unable to alloc memory");
1037 status = cf_util_get_string(ci, &host->name);
1039 WARNING("write_sensu plugin: Required host name is missing.");
1044 for (int i = 0; i < ci->children_num; i++) {
1045 child = &ci->children[i];
1048 if (strcasecmp("Host", child->key) == 0) {
1049 status = cf_util_get_string(child, &host->node);
1052 } else if (strcasecmp("Notifications", child->key) == 0) {
1053 status = cf_util_get_boolean(child, &host->notifications);
1056 } else if (strcasecmp("Metrics", child->key) == 0) {
1057 status = cf_util_get_boolean(child, &host->metrics);
1060 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
1061 status = cf_util_get_string(child, &host->event_service_prefix);
1064 } else if (strcasecmp("Separator", child->key) == 0) {
1065 status = cf_util_get_string(child, &host->separator);
1068 } else if (strcasecmp("MetricHandler", child->key) == 0) {
1069 char *temp_str = NULL;
1070 status = cf_util_get_string(child, &temp_str);
1073 status = add_str_to_list(&(host->metric_handlers), temp_str);
1077 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1078 char *temp_str = NULL;
1079 status = cf_util_get_string(child, &temp_str);
1082 status = add_str_to_list(&(host->notification_handlers), temp_str);
1086 } else if (strcasecmp("Port", child->key) == 0) {
1087 status = cf_util_get_service(child, &host->service);
1089 ERROR("write_sensu plugin: Invalid argument "
1090 "configured for the \"Port\" "
1094 } else if (strcasecmp("StoreRates", child->key) == 0) {
1095 status = cf_util_get_boolean(child, &host->store_rates);
1098 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1099 status = cf_util_get_boolean(child, &host->always_append_ds);
1103 WARNING("write_sensu plugin: ignoring unknown config "
1113 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1115 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. "
1120 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1122 WARNING("write_sensu plugin: notifications enabled but no "
1123 "NotificationHandler defined. Giving up.");
1127 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
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 == 0)) {
1134 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be "
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);