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>
30 #include "configfile.h"
31 #include "utils_cache.h"
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
43 * Uses asprintf() portable implementation from
44 * https://github.com/littlstar/asprintf.c/blob/master/
45 * copyright (c) 2014 joseph werle <joseph.werle@gmail.com> under MIT license.
50 int vasprintf(char **str, const char *fmt, va_list args) {
55 // apply variadic arguments to
56 // sprintf with format to get size
57 size = vsnprintf(NULL, size, fmt, tmpa);
60 // return -1 to be compliant if
61 // size is less than 0
62 if (size < 0) { return -1; }
63 // alloc with size plus 1 for `\0'
64 *str = (char *) malloc(size + 1);
65 // return -1 to be compliant
66 // if pointer is `NULL'
67 if (NULL == *str) { return -1; }
68 // format string with original
69 // variadic arguments and set new size
70 size = vsprintf(*str, fmt, args);
74 int asprintf(char **str, const char *fmt, ...) {
77 // init variadic argumens
79 // format and get size
80 size = vasprintf(str, fmt, args);
88 #define SENSU_HOST "localhost"
89 #define SENSU_PORT "3030"
98 char *event_service_prefix;
99 struct str_list metric_handlers;
100 struct str_list notification_handlers;
103 pthread_mutex_t lock;
107 _Bool always_append_ds;
112 struct addrinfo *res;
116 static char *sensu_tags;
117 static char **sensu_attrs;
118 static size_t sensu_attrs_num;
119 static const char *alloc_err ="write_sensu plugin: Unable to alloc memory";
121 static int add_str_to_list(struct str_list *strs,
122 const char *str_to_add) /* {{{ */
124 char **old_strs_ptr = strs->strs;
125 char *newstr = strdup(str_to_add);
126 if (newstr == NULL) {
130 strs->strs = realloc(strs->strs, sizeof(char *) *(strs->nb_strs + 1));
131 if (strs->strs == NULL) {
132 strs->strs = old_strs_ptr;
137 strs->strs[strs->nb_strs] = newstr;
141 /* }}} int add_str_to_list */
143 static void free_str_list(struct str_list *strs) /* {{{ */
146 for (i=0; i<strs->nb_strs; i++)
150 /* }}} void free_str_list */
152 static int sensu_connect(struct sensu_host *host) /* {{{ */
155 struct addrinfo *ai, hints;
159 // Resolve the target if we haven't done already
160 if (!(host->flags & F_READY)) {
161 memset(&hints, 0, sizeof(hints));
162 memset(&service, 0, sizeof(service));
164 hints.ai_family = AF_INET;
165 hints.ai_socktype = SOCK_STREAM;
167 hints.ai_flags |= AI_ADDRCONFIG;
170 node = (host->node != NULL) ? host->node : SENSU_HOST;
171 service = (host->service != NULL) ? host->service : SENSU_PORT;
173 if ((e = getaddrinfo(node, service, &hints, &(host->res))) != 0) {
174 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s",
175 node, gai_strerror(e));
178 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s",
180 host->flags |= F_READY;
183 struct linger so_linger;
185 for (ai = host->res; ai != NULL; ai = ai->ai_next) {
187 if ((host->s = socket(ai->ai_family,
189 ai->ai_protocol)) == -1) {
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, sizeof so_linger) != 0)
197 WARNING("write_sensu plugin: failed to set socket close() lingering");
199 // connect the socket
200 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
205 DEBUG("write_sensu plugin: connected");
210 WARNING("write_sensu plugin: Unable to connect to sensu client");
214 } /* }}} int sensu_connect */
216 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
222 } /* }}} void sensu_close_socket */
224 static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */
230 if (list->nb_strs == 0) {
231 ret_str = malloc(sizeof(char));
232 if (ret_str == NULL) {
239 res = asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
244 for (i=1; i<list->nb_strs; i++) {
245 res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
253 res = asprintf(&ret_str, "%s]", temp_str);
261 } /* }}} char *build_json_str_list*/
263 int sensu_format_name2(char *ret, int ret_len,
264 const char *hostname,
265 const char *plugin, const char *plugin_instance,
266 const char *type, const char *type_instance,
267 const char *separator)
273 buffer_size = (size_t) ret_len;
275 #define APPEND(str) do { \
276 size_t l = strlen (str); \
277 if (l >= buffer_size) \
279 memcpy (buffer, (str), l); \
280 buffer += l; buffer_size -= l; \
283 assert (plugin != NULL);
284 assert (type != NULL);
289 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
292 APPEND (plugin_instance);
296 if ((type_instance != NULL) && (type_instance[0] != 0))
299 APPEND (type_instance);
301 assert (buffer_size > 0);
306 } /* int sensu_format_name2 */
308 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
311 int len=strlen(orig_name);
312 for (i=0; i<len; i++) {
313 // some plugins like ipmi generate special characters in metric name
314 switch(orig_name[i]) {
315 case '(': orig_name[i] = '_'; break;
316 case ')': orig_name[i] = '_'; break;
317 case ' ': orig_name[i] = '_'; break;
318 case '"': orig_name[i] = '_'; break;
319 case '\'': orig_name[i] = '_'; break;
320 case '+': orig_name[i] = '_'; break;
323 } /* }}} char *replace_sensu_name_reserved */
325 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
326 data_set_t const *ds,
327 value_list_t const *vl, size_t index,
328 gauge_t const *rates,
331 char name_buffer[5 * DATA_MAX_NAME_LEN];
332 char service_buffer[6 * DATA_MAX_NAME_LEN];
338 // First part of the JSON string
339 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
341 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
342 if (handlers_str == NULL) {
347 // incorporate the handlers
348 if (strlen(handlers_str) == 0) {
350 ret_str = strdup(part1);
351 if (ret_str == NULL) {
357 res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
365 // incorporate the plugin name information
366 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
374 // incorporate the plugin type
375 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
383 // incorporate the plugin instance if any
384 if (vl->plugin_instance[0] != 0) {
385 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
394 // incorporate the plugin type instance if any
395 if (vl->type_instance[0] != 0) {
396 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
405 // incorporate the data source type
406 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
407 char ds_type[DATA_MAX_NAME_LEN];
408 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
409 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
417 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
426 // incorporate the data source name
427 res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
435 // incorporate the data source index
437 char ds_index[DATA_MAX_NAME_LEN];
438 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
439 res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
448 // add key value attributes from config if any
449 for (i = 0; i < sensu_attrs_num; i += 2) {
450 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
459 // incorporate sensu tags from config if any
460 if (strlen(sensu_tags) != 0) {
461 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
470 // calculate the value and set to a string
471 if (ds->ds[index].type == DS_TYPE_GAUGE) {
472 double tmp_v = (double) vl->values[index].gauge;
473 res = asprintf(&value_str, "%.8f", tmp_v, sensu_tags);
479 } else if (rates != NULL) {
480 double tmp_v = (double) rates[index];
481 res = asprintf(&value_str, "%.8f", tmp_v, sensu_tags);
489 if (ds->ds[index].type == DS_TYPE_DERIVE)
490 tmp_v = (int64_t) vl->values[index].derive;
491 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE)
492 tmp_v = (int64_t) vl->values[index].absolute;
494 tmp_v = (int64_t) vl->values[index].counter;
495 res = asprintf(&value_str, "%lld", tmp_v, sensu_tags);
503 // Generate the full service name
504 sensu_format_name2(name_buffer, sizeof(name_buffer),
505 vl->host, vl->plugin, vl->plugin_instance,
506 vl->type, vl->type_instance, host->separator);
507 if (host->always_append_ds || (ds->ds_num > 1)) {
508 if (host->event_service_prefix == NULL)
509 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
510 name_buffer, ds->ds[index].name);
512 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
513 host->event_service_prefix, name_buffer, ds->ds[index].name);
515 if (host->event_service_prefix == NULL)
516 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
518 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
519 host->event_service_prefix, name_buffer);
522 // Replace collectd sensor name reserved characters so that time series DB is happy
523 in_place_replace_sensu_name_reserved(service_buffer);
525 // finalize the buffer by setting the output and closing curly bracket
526 res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
535 DEBUG("write_sensu plugin: Successfully created json for metric: "
536 "host = \"%s\", service = \"%s\"",
537 vl->host, service_buffer);
539 } /* }}} char *sensu_value_to_json */
542 * Uses replace_str2() implementation from
543 * http://creativeandcritical.net/str-replace-c/
544 * copyright (c) Laird Shaw, under public domain.
546 char *replace_str(const char *str, const char *old, /* {{{ */
551 size_t oldlen = strlen(old);
552 size_t count = strlen(new);
553 size_t retlen = count;
554 size_t newlen = count;
555 int samesize = (oldlen == newlen);
558 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
560 /* This is undefined if p - str > PTRDIFF_MAX */
561 retlen = p - str + strlen(p) + count * (newlen - oldlen);
563 retlen = strlen(str);
565 ret = malloc(retlen + 1);
568 // added to original: not optimized, but keeps valgrind happy.
569 memset(ret, 0, retlen + 1);
574 /* If the old and new strings are different lengths - in other
575 * words we have already iterated through with strstr above,
576 * and thus we know how many times we need to call it - then we
577 * can avoid the final (potentially lengthy) call to strstr,
578 * which we already know is going to return NULL, by
579 * decrementing and checking count.
581 if (!samesize && !count--)
583 /* Otherwise i.e. when the old and new strings are the same
584 * length, and we don't know how many times to call strstr,
585 * we must check for a NULL return here (we check it in any
586 * event, to avoid further conditions, and because there's
587 * no harm done with the check even when the old and new
588 * strings are different lengths).
590 if ((q = strstr(p, old)) == NULL)
592 /* This is undefined if q - p > PTRDIFF_MAX */
596 memcpy(r, new, newlen);
600 strncpy(r, p, strlen(p));
603 } /* }}} char *replace_str */
605 static char *replace_json_reserved(const char *message) /* {{{ */
607 char *msg = replace_str(message, "\\", "\\\\");
612 char *tmp = replace_str(msg, "\"", "\\\"");
618 msg = replace_str(tmp, "\n", "\\\n");
625 } /* }}} char *replace_json_reserved */
627 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
628 notification_t const *n)
630 char service_buffer[6 * DATA_MAX_NAME_LEN];
631 char const *severity;
632 notification_meta_t *meta;
638 // add the severity/status
639 switch (n->severity) {
645 severity = "WARNING";
649 severity = "CRITICAL";
653 severity = "UNKNOWN";
656 res = asprintf(&temp_str, "{\"status\": %d", status);
663 // incorporate the timestamp
664 res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
672 char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
673 if (handlers_str == NULL) {
677 // incorporate the handlers
678 if (strlen(handlers_str) != 0) {
679 res = asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
691 // incorporate the plugin name information if any
692 if (n->plugin[0] != 0) {
693 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
702 // incorporate the plugin type if any
703 if (n->type[0] != 0) {
704 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
713 // incorporate the plugin instance if any
714 if (n->plugin_instance[0] != 0) {
715 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
724 // incorporate the plugin type instance if any
725 if (n->type_instance[0] != 0) {
726 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
735 // add key value attributes from config if any
736 for (i = 0; i < sensu_attrs_num; i += 2) {
737 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
746 // incorporate sensu tags from config if any
747 if (strlen(sensu_tags) != 0) {
748 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
757 // incorporate the service name
758 sensu_format_name2(service_buffer, sizeof(service_buffer),
759 /* host */ "", n->plugin, n->plugin_instance,
760 n->type, n->type_instance, host->separator);
761 // replace sensu event name chars that are considered illegal
762 in_place_replace_sensu_name_reserved(service_buffer);
763 res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
771 // incorporate the check output
772 if (n->message[0] != 0) {
773 char *msg = replace_json_reserved(n->message);
778 res = asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
788 // Pull in values from threshold and add extra attributes
789 for (meta = n->meta; meta != NULL; meta = meta->next) {
790 if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) {
791 res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
799 if (meta->type == NM_TYPE_STRING) {
800 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
810 // close the curly bracket
811 res = asprintf(&temp_str, "%s}\n", ret_str);
819 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
820 "host = \"%s\", service = \"%s\", state = \"%s\"",
821 n->host, service_buffer, severity);
823 } /* }}} char *sensu_notification_to_json */
825 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
830 status = sensu_connect(host);
834 buffer_len = strlen(msg);
836 status = (int) swrite(host->s, msg, buffer_len);
837 sensu_close_socket(host);
841 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
842 (host->node != NULL) ? host->node : SENSU_HOST,
843 (host->service != NULL) ? host->service : SENSU_PORT,
844 sstrerror(errno, errbuf, sizeof(errbuf)));
849 } /* }}} int sensu_send_msg */
852 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
856 status = sensu_send_msg(host, msg);
858 host->flags &= ~F_READY;
859 if (host->res != NULL) {
860 freeaddrinfo(host->res);
867 } /* }}} int sensu_send */
870 static int sensu_write(const data_set_t *ds, /* {{{ */
871 const value_list_t *vl,
875 int statuses[vl->values_len];
876 struct sensu_host *host = ud->data;
877 gauge_t *rates = NULL;
881 pthread_mutex_lock(&host->lock);
882 memset(statuses, 0, vl->values_len * sizeof(*statuses));
884 if (host->store_rates) {
885 rates = uc_get_rate(ds, vl);
887 ERROR("write_sensu plugin: uc_get_rate failed.");
888 pthread_mutex_unlock(&host->lock);
892 for (i = 0; i < (size_t) vl->values_len; i++) {
893 msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]);
896 pthread_mutex_unlock(&host->lock);
899 status = sensu_send(host, msg);
902 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
903 pthread_mutex_unlock(&host->lock);
909 pthread_mutex_unlock(&host->lock);
911 } /* }}} int sensu_write */
913 static int sensu_notification(const notification_t *n, user_data_t *ud) /* {{{ */
916 struct sensu_host *host = ud->data;
919 pthread_mutex_lock(&host->lock);
921 msg = sensu_notification_to_json(host, n);
923 pthread_mutex_unlock(&host->lock);
927 status = sensu_send(host, msg);
930 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
931 pthread_mutex_unlock(&host->lock);
934 } /* }}} int sensu_notification */
936 static void sensu_free(void *p) /* {{{ */
938 struct sensu_host *host = p;
943 pthread_mutex_lock(&host->lock);
945 host->reference_count--;
946 if (host->reference_count > 0) {
947 pthread_mutex_unlock(&host->lock);
951 sensu_close_socket(host);
952 if (host->res != NULL) {
953 freeaddrinfo(host->res);
956 sfree(host->service);
957 sfree(host->event_service_prefix);
960 sfree(host->separator);
961 free_str_list(&(host->metric_handlers));
962 free_str_list(&(host->notification_handlers));
963 pthread_mutex_destroy(&host->lock);
965 } /* }}} void sensu_free */
968 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
970 struct sensu_host *host = NULL;
973 oconfig_item_t *child;
974 char callback_name[DATA_MAX_NAME_LEN];
977 if ((host = calloc(1, sizeof(*host))) == NULL) {
978 ERROR("write_sensu plugin: calloc failed.");
981 pthread_mutex_init(&host->lock, NULL);
982 host->reference_count = 1;
984 host->service = NULL;
985 host->notifications = 0;
987 host->store_rates = 1;
988 host->always_append_ds = 0;
989 host->metric_handlers.nb_strs = 0;
990 host->metric_handlers.strs = NULL;
991 host->notification_handlers.nb_strs = 0;
992 host->notification_handlers.strs = NULL;
993 host->separator = strdup("/");
994 if (host->separator == NULL) {
1000 status = cf_util_get_string(ci, &host->name);
1002 WARNING("write_sensu plugin: Required host name is missing.");
1007 for (i = 0; i < ci->children_num; i++) {
1008 child = &ci->children[i];
1011 if (strcasecmp("Host", child->key) == 0) {
1012 status = cf_util_get_string(child, &host->node);
1015 } else if (strcasecmp("Notifications", child->key) == 0) {
1016 status = cf_util_get_boolean(child, &host->notifications);
1019 } else if (strcasecmp("Metrics", child->key) == 0) {
1020 status = cf_util_get_boolean(child, &host->metrics);
1023 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
1024 status = cf_util_get_string(child, &host->event_service_prefix);
1027 } else if (strcasecmp("Separator", child->key) == 0) {
1028 status = cf_util_get_string(child, &host->separator);
1031 } else if (strcasecmp("MetricHandler", child->key) == 0) {
1032 char *temp_str = NULL;
1033 status = cf_util_get_string(child, &temp_str);
1036 status = add_str_to_list(&(host->metric_handlers), temp_str);
1040 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1041 char *temp_str = NULL;
1042 status = cf_util_get_string(child, &temp_str);
1045 status = add_str_to_list(&(host->notification_handlers), temp_str);
1049 } else if (strcasecmp("Port", child->key) == 0) {
1050 status = cf_util_get_service(child, &host->service);
1052 ERROR("write_sensu plugin: Invalid argument "
1053 "configured for the \"Port\" "
1057 } else if (strcasecmp("StoreRates", child->key) == 0) {
1058 status = cf_util_get_boolean(child, &host->store_rates);
1061 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1062 status = cf_util_get_boolean(child,
1063 &host->always_append_ds);
1067 WARNING("write_sensu plugin: ignoring unknown config "
1068 "option: \"%s\"", child->key);
1076 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1078 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. Giving up.");
1082 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1084 WARNING("write_sensu plugin: notifications enabled but no NotificationHandler defined. Giving up.");
1088 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1089 WARNING("write_sensu plugin: NotificationHandler given so forcing notifications to be enabled");
1090 host->notifications = 1;
1093 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1094 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be enabled");
1098 if (!(host->notifications || host->metrics)) {
1099 WARNING("write_sensu plugin: neither metrics nor notifications enabled. Giving up.");
1104 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1106 ud.free_func = sensu_free;
1108 pthread_mutex_lock(&host->lock);
1110 if (host->metrics) {
1111 status = plugin_register_write(callback_name, sensu_write, &ud);
1113 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1114 "failed with status %i.",
1115 callback_name, status);
1117 host->reference_count++;
1120 if (host->notifications) {
1121 status = plugin_register_notification(callback_name, sensu_notification, &ud);
1123 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1124 "failed with status %i.",
1125 callback_name, status);
1127 host->reference_count++;
1130 if (host->reference_count <= 1) {
1131 /* Both callbacks failed => free memory.
1132 * We need to unlock here, because sensu_free() will lock.
1133 * This is not a race condition, because we're the only one
1134 * holding a reference. */
1135 pthread_mutex_unlock(&host->lock);
1140 host->reference_count--;
1141 pthread_mutex_unlock(&host->lock);
1144 } /* }}} int sensu_config_node */
1146 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1149 oconfig_item_t *child;
1151 struct str_list sensu_tags_arr;
1153 sensu_tags_arr.nb_strs = 0;
1154 sensu_tags_arr.strs = NULL;
1155 sensu_tags = malloc(sizeof(char));
1156 if (sensu_tags == NULL) {
1160 sensu_tags[0] = '\0';
1162 for (i = 0; i < ci->children_num; i++) {
1163 child = &ci->children[i];
1165 if (strcasecmp("Node", child->key) == 0) {
1166 sensu_config_node(child);
1167 } else if (strcasecmp(child->key, "attribute") == 0) {
1171 if (child->values_num != 2) {
1172 WARNING("sensu attributes need both a key and a value.");
1176 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1177 child->values[1].type != OCONFIG_TYPE_STRING) {
1178 WARNING("sensu attribute needs string arguments.");
1182 if ((key = strdup(child->values[0].value.string)) == NULL) {
1187 if ((val = strdup(child->values[1].value.string)) == NULL) {
1193 strarray_add(&sensu_attrs, &sensu_attrs_num, key);
1194 strarray_add(&sensu_attrs, &sensu_attrs_num, val);
1195 DEBUG("write_sensu: got attr: %s => %s", key, val);
1198 } else if (strcasecmp(child->key, "tag") == 0) {
1200 status = cf_util_get_string(child, &tmp);
1204 status = add_str_to_list(&sensu_tags_arr, tmp);
1208 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1210 WARNING("write_sensu plugin: Ignoring unknown "
1211 "configuration option \"%s\" at top level.",
1215 if (sensu_tags_arr.nb_strs > 0) {
1217 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1218 free_str_list(&sensu_tags_arr);
1219 if (sensu_tags == NULL) {
1225 } /* }}} int sensu_config */
1227 void module_register(void)
1229 plugin_register_complex_config("write_sensu", sensu_config);
1232 /* vim: set sw=8 sts=8 ts=8 noet : */