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
67 if (size < 0) { return -1; }
68 // alloc with size plus 1 for `\0'
69 *str = (char *) malloc(size + 1);
70 // return -1 to be compliant
71 // if pointer is `NULL'
72 if (NULL == *str) { return -1; }
73 // format string with original
74 // variadic arguments and set new size
75 size = vsprintf(*str, fmt, args);
79 static int my_asprintf(char **str, const char *fmt, ...) {
82 // init variadic argumens
84 // format and get size
85 size = my_vasprintf(str, fmt, args);
100 char *event_service_prefix;
101 struct str_list metric_handlers;
102 struct str_list notification_handlers;
105 pthread_mutex_t lock;
109 _Bool always_append_ds;
114 struct addrinfo *res;
118 static char *sensu_tags = NULL;
119 static char **sensu_attrs = NULL;
120 static size_t sensu_attrs_num;
122 static int add_str_to_list(struct str_list *strs,
123 const char *str_to_add) /* {{{ */
125 char **old_strs_ptr = strs->strs;
126 char *newstr = strdup(str_to_add);
127 if (newstr == NULL) {
128 ERROR("write_sensu plugin: Unable to alloc memory");
131 strs->strs = realloc(strs->strs, sizeof(char *) *(strs->nb_strs + 1));
132 if (strs->strs == NULL) {
133 strs->strs = old_strs_ptr;
135 ERROR("write_sensu plugin: Unable to alloc memory");
138 strs->strs[strs->nb_strs] = newstr;
142 /* }}} int add_str_to_list */
144 static void free_str_list(struct str_list *strs) /* {{{ */
146 for (int i=0; i<strs->nb_strs; i++)
150 /* }}} void free_str_list */
152 static int sensu_connect(struct sensu_host *host) /* {{{ */
158 // Resolve the target if we haven't done already
159 if (!(host->flags & F_READY)) {
160 memset(&service, 0, sizeof(service));
163 node = (host->node != NULL) ? host->node : SENSU_HOST;
164 service = (host->service != NULL) ? host->service : SENSU_PORT;
166 struct addrinfo ai_hints = {
167 .ai_family = AF_INET,
168 .ai_flags = AI_ADDRCONFIG,
169 .ai_socktype = SOCK_STREAM
172 if ((e = getaddrinfo(node, service, &ai_hints, &(host->res))) != 0) {
173 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s",
174 node, gai_strerror(e));
177 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s",
179 host->flags |= F_READY;
182 struct linger so_linger;
184 for (struct addrinfo *ai = host->res; ai != NULL; ai = ai->ai_next) {
186 if ((host->s = socket(ai->ai_family,
188 ai->ai_protocol)) == -1) {
192 // Set very low close() lingering
193 so_linger.l_onoff = 1;
194 so_linger.l_linger = 3;
195 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) != 0)
196 WARNING("write_sensu plugin: failed to set socket close() lingering");
198 set_sock_opts(host->s);
200 // connect the socket
201 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
206 DEBUG("write_sensu plugin: connected");
211 WARNING("write_sensu plugin: Unable to connect to sensu client");
215 } /* }}} int sensu_connect */
217 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
223 } /* }}} void sensu_close_socket */
225 static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */
228 char *ret_str = NULL;
230 if (list->nb_strs == 0) {
231 ret_str = malloc(sizeof(char));
232 if (ret_str == NULL) {
233 ERROR("write_sensu plugin: Unable to alloc memory");
239 res = my_asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
241 ERROR("write_sensu plugin: Unable to alloc memory");
245 for (int i=1; i<list->nb_strs; i++) {
246 res = my_asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
249 ERROR("write_sensu plugin: Unable to alloc memory");
254 res = my_asprintf(&ret_str, "%s]", temp_str);
257 ERROR("write_sensu plugin: Unable to alloc memory");
262 } /* }}} char *build_json_str_list*/
264 static int sensu_format_name2(char *ret, int ret_len,
265 const char *hostname,
266 const char *plugin, const char *plugin_instance,
267 const char *type, const char *type_instance,
268 const char *separator)
274 buffer_size = (size_t) ret_len;
276 #define APPEND(str) do { \
277 size_t l = strlen (str); \
278 if (l >= buffer_size) \
280 memcpy (buffer, (str), l); \
281 buffer += l; buffer_size -= l; \
284 assert (plugin != NULL);
285 assert (type != NULL);
290 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
293 APPEND (plugin_instance);
297 if ((type_instance != NULL) && (type_instance[0] != 0))
300 APPEND (type_instance);
302 assert (buffer_size > 0);
307 } /* int sensu_format_name2 */
309 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
311 int len=strlen(orig_name);
312 for (int 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];
337 // First part of the JSON string
338 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
340 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
341 if (handlers_str == NULL) {
342 ERROR("write_sensu plugin: Unable to alloc memory");
346 // incorporate the handlers
347 if (strlen(handlers_str) == 0) {
349 ret_str = strdup(part1);
350 if (ret_str == NULL) {
351 ERROR("write_sensu plugin: Unable to alloc memory");
356 res = my_asprintf(&ret_str, "%s, %s", part1, handlers_str);
359 ERROR("write_sensu plugin: Unable to alloc memory");
364 // incorporate the plugin name information
365 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
368 ERROR("write_sensu plugin: Unable to alloc memory");
373 // incorporate the plugin type
374 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
377 ERROR("write_sensu plugin: Unable to alloc memory");
382 // incorporate the plugin instance if any
383 if (vl->plugin_instance[0] != 0) {
384 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
387 ERROR("write_sensu plugin: Unable to alloc memory");
393 // incorporate the plugin type instance if any
394 if (vl->type_instance[0] != 0) {
395 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
398 ERROR("write_sensu plugin: Unable to alloc memory");
404 // incorporate the data source type
405 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
406 char ds_type[DATA_MAX_NAME_LEN];
407 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
408 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
411 ERROR("write_sensu plugin: Unable to alloc memory");
416 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
419 ERROR("write_sensu plugin: Unable to alloc memory");
425 // incorporate the data source name
426 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
429 ERROR("write_sensu plugin: Unable to alloc memory");
434 // incorporate the data source index
436 char ds_index[DATA_MAX_NAME_LEN];
437 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
438 res = my_asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
441 ERROR("write_sensu plugin: Unable to alloc memory");
447 // add key value attributes from config if any
448 for (size_t i = 0; i < sensu_attrs_num; i += 2) {
449 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
452 ERROR("write_sensu plugin: Unable to alloc memory");
458 // incorporate sensu tags from config if any
459 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
460 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
463 ERROR("write_sensu plugin: Unable to alloc memory");
469 // calculate the value and set to a string
470 if (ds->ds[index].type == DS_TYPE_GAUGE) {
471 res = my_asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
474 ERROR("write_sensu plugin: Unable to alloc memory");
477 } else if (rates != NULL) {
478 res = my_asprintf(&value_str, GAUGE_FORMAT, rates[index]);
481 ERROR("write_sensu plugin: Unable to alloc memory");
485 if (ds->ds[index].type == DS_TYPE_DERIVE) {
486 res = my_asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
489 ERROR("write_sensu plugin: Unable to alloc memory");
493 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
494 res = my_asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
497 ERROR("write_sensu plugin: Unable to alloc memory");
502 res = my_asprintf(&value_str, "%llu", vl->values[index].counter);
505 ERROR("write_sensu plugin: Unable to alloc memory");
511 // Generate the full service name
512 sensu_format_name2(name_buffer, sizeof(name_buffer),
513 vl->host, vl->plugin, vl->plugin_instance,
514 vl->type, vl->type_instance, host->separator);
515 if (host->always_append_ds || (ds->ds_num > 1)) {
516 if (host->event_service_prefix == NULL)
517 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
518 name_buffer, ds->ds[index].name);
520 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
521 host->event_service_prefix, name_buffer, ds->ds[index].name);
523 if (host->event_service_prefix == NULL)
524 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
526 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
527 host->event_service_prefix, name_buffer);
530 // Replace collectd sensor name reserved characters so that time series DB is happy
531 in_place_replace_sensu_name_reserved(service_buffer);
533 // finalize the buffer by setting the output and closing curly bracket
534 res = my_asprintf(&temp_str, "%s, \"output\": \"%s %s %lld\"}\n",ret_str, service_buffer, value_str, (long long)CDTIME_T_TO_TIME_T(vl->time));
538 ERROR("write_sensu plugin: Unable to alloc memory");
543 DEBUG("write_sensu plugin: Successfully created json for metric: "
544 "host = \"%s\", service = \"%s\"",
545 vl->host, service_buffer);
547 } /* }}} char *sensu_value_to_json */
550 * Uses replace_str2() implementation from
551 * http://creativeandcritical.net/str-replace-c/
552 * copyright (c) Laird Shaw, under public domain.
554 static char *replace_str(const char *str, const char *old, /* {{{ */
559 size_t oldlen = strlen(old);
560 size_t count = strlen(new);
562 size_t newlen = count;
563 int samesize = (oldlen == newlen);
566 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
568 /* This is undefined if p - str > PTRDIFF_MAX */
569 retlen = p - str + strlen(p) + count * (newlen - oldlen);
571 retlen = strlen(str);
573 ret = calloc(1, retlen + 1);
576 // added to original: not optimized, but keeps valgrind happy.
581 /* If the old and new strings are different lengths - in other
582 * words we have already iterated through with strstr above,
583 * and thus we know how many times we need to call it - then we
584 * can avoid the final (potentially lengthy) call to strstr,
585 * which we already know is going to return NULL, by
586 * decrementing and checking count.
588 if (!samesize && !count--)
590 /* Otherwise i.e. when the old and new strings are the same
591 * length, and we don't know how many times to call strstr,
592 * we must check for a NULL return here (we check it in any
593 * event, to avoid further conditions, and because there's
594 * no harm done with the check even when the old and new
595 * strings are different lengths).
597 if ((q = strstr(p, old)) == NULL)
599 /* This is undefined if q - p > PTRDIFF_MAX */
603 memcpy(r, new, newlen);
607 strncpy(r, p, strlen(p));
610 } /* }}} char *replace_str */
612 static char *replace_json_reserved(const char *message) /* {{{ */
614 char *msg = replace_str(message, "\\", "\\\\");
616 ERROR("write_sensu plugin: Unable to alloc memory");
619 char *tmp = replace_str(msg, "\"", "\\\"");
622 ERROR("write_sensu plugin: Unable to alloc memory");
625 msg = replace_str(tmp, "\n", "\\\n");
628 ERROR("write_sensu plugin: Unable to alloc memory");
632 } /* }}} char *replace_json_reserved */
634 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
635 notification_t const *n)
637 char service_buffer[6 * DATA_MAX_NAME_LEN];
638 char const *severity;
644 // add the severity/status
645 switch (n->severity) {
651 severity = "WARNING";
655 severity = "CRITICAL";
659 severity = "UNKNOWN";
662 res = my_asprintf(&temp_str, "{\"status\": %d", status);
664 ERROR("write_sensu plugin: Unable to alloc memory");
669 // incorporate the timestamp
670 res = my_asprintf(&temp_str, "%s, \"timestamp\": %lld", ret_str, (long long)CDTIME_T_TO_TIME_T(n->time));
673 ERROR("write_sensu plugin: Unable to alloc memory");
678 char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
679 if (handlers_str == NULL) {
680 ERROR("write_sensu plugin: Unable to alloc memory");
684 // incorporate the handlers
685 if (strlen(handlers_str) != 0) {
686 res = my_asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
690 ERROR("write_sensu plugin: Unable to alloc memory");
698 // incorporate the plugin name information if any
699 if (n->plugin[0] != 0) {
700 res = my_asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
703 ERROR("write_sensu plugin: Unable to alloc memory");
709 // incorporate the plugin type if any
710 if (n->type[0] != 0) {
711 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
714 ERROR("write_sensu plugin: Unable to alloc memory");
720 // incorporate the plugin instance if any
721 if (n->plugin_instance[0] != 0) {
722 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
725 ERROR("write_sensu plugin: Unable to alloc memory");
731 // incorporate the plugin type instance if any
732 if (n->type_instance[0] != 0) {
733 res = my_asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
736 ERROR("write_sensu plugin: Unable to alloc memory");
742 // add key value attributes from config if any
743 for (i = 0; i < sensu_attrs_num; i += 2) {
744 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
747 ERROR("write_sensu plugin: Unable to alloc memory");
753 // incorporate sensu tags from config if any
754 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
755 res = my_asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
758 ERROR("write_sensu plugin: Unable to alloc memory");
764 // incorporate the service name
765 sensu_format_name2(service_buffer, sizeof(service_buffer),
766 /* host */ "", n->plugin, n->plugin_instance,
767 n->type, n->type_instance, host->separator);
768 // replace sensu event name chars that are considered illegal
769 in_place_replace_sensu_name_reserved(service_buffer);
770 res = my_asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
773 ERROR("write_sensu plugin: Unable to alloc memory");
778 // incorporate the check output
779 if (n->message[0] != 0) {
780 char *msg = replace_json_reserved(n->message);
782 ERROR("write_sensu plugin: Unable to alloc memory");
786 res = my_asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
790 ERROR("write_sensu plugin: Unable to alloc memory");
796 // Pull in values from threshold and add extra attributes
797 for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
798 if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) {
799 res = my_asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
802 ERROR("write_sensu plugin: Unable to alloc memory");
807 if (meta->type == NM_TYPE_STRING) {
808 res = my_asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
811 ERROR("write_sensu plugin: Unable to alloc memory");
818 // close the curly bracket
819 res = my_asprintf(&temp_str, "%s}\n", ret_str);
822 ERROR("write_sensu plugin: Unable to alloc memory");
827 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
828 "host = \"%s\", service = \"%s\", state = \"%s\"",
829 n->host, service_buffer, severity);
831 } /* }}} char *sensu_notification_to_json */
833 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
838 status = sensu_connect(host);
842 buffer_len = strlen(msg);
844 status = (int) swrite(host->s, msg, buffer_len);
845 sensu_close_socket(host);
849 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
850 (host->node != NULL) ? host->node : SENSU_HOST,
851 (host->service != NULL) ? host->service : SENSU_PORT,
852 sstrerror(errno, errbuf, sizeof(errbuf)));
857 } /* }}} int sensu_send_msg */
860 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
864 status = sensu_send_msg(host, msg);
866 host->flags &= ~F_READY;
867 if (host->res != NULL) {
868 freeaddrinfo(host->res);
875 } /* }}} int sensu_send */
878 static int sensu_write(const data_set_t *ds, /* {{{ */
879 const value_list_t *vl,
883 int statuses[vl->values_len];
884 struct sensu_host *host = ud->data;
885 gauge_t *rates = NULL;
888 pthread_mutex_lock(&host->lock);
889 memset(statuses, 0, vl->values_len * sizeof(*statuses));
891 if (host->store_rates) {
892 rates = uc_get_rate(ds, vl);
894 ERROR("write_sensu plugin: uc_get_rate failed.");
895 pthread_mutex_unlock(&host->lock);
899 for (size_t i = 0; i < vl->values_len; i++) {
900 msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]);
903 pthread_mutex_unlock(&host->lock);
906 status = sensu_send(host, msg);
909 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
910 pthread_mutex_unlock(&host->lock);
916 pthread_mutex_unlock(&host->lock);
918 } /* }}} int sensu_write */
920 static int sensu_notification(const notification_t *n, user_data_t *ud) /* {{{ */
923 struct sensu_host *host = ud->data;
926 pthread_mutex_lock(&host->lock);
928 msg = sensu_notification_to_json(host, n);
930 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);
941 } /* }}} int sensu_notification */
943 static void sensu_free(void *p) /* {{{ */
945 struct sensu_host *host = p;
950 pthread_mutex_lock(&host->lock);
952 host->reference_count--;
953 if (host->reference_count > 0) {
954 pthread_mutex_unlock(&host->lock);
958 sensu_close_socket(host);
959 if (host->res != NULL) {
960 freeaddrinfo(host->res);
963 sfree(host->service);
964 sfree(host->event_service_prefix);
967 sfree(host->separator);
968 free_str_list(&(host->metric_handlers));
969 free_str_list(&(host->notification_handlers));
970 pthread_mutex_destroy(&host->lock);
972 } /* }}} void sensu_free */
975 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
977 struct sensu_host *host = NULL;
979 oconfig_item_t *child;
980 char callback_name[DATA_MAX_NAME_LEN];
982 if ((host = calloc(1, sizeof(*host))) == NULL) {
983 ERROR("write_sensu plugin: calloc failed.");
986 pthread_mutex_init(&host->lock, NULL);
987 host->reference_count = 1;
989 host->service = NULL;
990 host->notifications = 0;
992 host->store_rates = 1;
993 host->always_append_ds = 0;
994 host->metric_handlers.nb_strs = 0;
995 host->metric_handlers.strs = NULL;
996 host->notification_handlers.nb_strs = 0;
997 host->notification_handlers.strs = NULL;
998 host->separator = strdup("/");
999 if (host->separator == NULL) {
1000 ERROR("write_sensu plugin: Unable to alloc memory");
1005 status = cf_util_get_string(ci, &host->name);
1007 WARNING("write_sensu plugin: Required host name is missing.");
1012 for (int i = 0; i < ci->children_num; i++) {
1013 child = &ci->children[i];
1016 if (strcasecmp("Host", child->key) == 0) {
1017 status = cf_util_get_string(child, &host->node);
1020 } else if (strcasecmp("Notifications", child->key) == 0) {
1021 status = cf_util_get_boolean(child, &host->notifications);
1024 } else if (strcasecmp("Metrics", child->key) == 0) {
1025 status = cf_util_get_boolean(child, &host->metrics);
1028 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
1029 status = cf_util_get_string(child, &host->event_service_prefix);
1032 } else if (strcasecmp("Separator", child->key) == 0) {
1033 status = cf_util_get_string(child, &host->separator);
1036 } else if (strcasecmp("MetricHandler", child->key) == 0) {
1037 char *temp_str = NULL;
1038 status = cf_util_get_string(child, &temp_str);
1041 status = add_str_to_list(&(host->metric_handlers), temp_str);
1045 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1046 char *temp_str = NULL;
1047 status = cf_util_get_string(child, &temp_str);
1050 status = add_str_to_list(&(host->notification_handlers), temp_str);
1054 } else if (strcasecmp("Port", child->key) == 0) {
1055 status = cf_util_get_service(child, &host->service);
1057 ERROR("write_sensu plugin: Invalid argument "
1058 "configured for the \"Port\" "
1062 } else if (strcasecmp("StoreRates", child->key) == 0) {
1063 status = cf_util_get_boolean(child, &host->store_rates);
1066 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1067 status = cf_util_get_boolean(child,
1068 &host->always_append_ds);
1072 WARNING("write_sensu plugin: ignoring unknown config "
1073 "option: \"%s\"", child->key);
1081 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1083 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. Giving up.");
1087 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1089 WARNING("write_sensu plugin: notifications enabled but no NotificationHandler defined. Giving up.");
1093 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1094 WARNING("write_sensu plugin: NotificationHandler given so forcing notifications to be enabled");
1095 host->notifications = 1;
1098 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1099 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be enabled");
1103 if (!(host->notifications || host->metrics)) {
1104 WARNING("write_sensu plugin: neither metrics nor notifications enabled. Giving up.");
1109 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1113 .free_func = sensu_free
1116 pthread_mutex_lock(&host->lock);
1118 if (host->metrics) {
1119 status = plugin_register_write(callback_name, sensu_write, &ud);
1121 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1122 "failed with status %i.",
1123 callback_name, status);
1125 host->reference_count++;
1128 if (host->notifications) {
1129 status = plugin_register_notification(callback_name, sensu_notification, &ud);
1131 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1132 "failed with status %i.",
1133 callback_name, status);
1135 host->reference_count++;
1138 if (host->reference_count <= 1) {
1139 /* Both callbacks failed => free memory.
1140 * We need to unlock here, because sensu_free() will lock.
1141 * This is not a race condition, because we're the only one
1142 * holding a reference. */
1143 pthread_mutex_unlock(&host->lock);
1148 host->reference_count--;
1149 pthread_mutex_unlock(&host->lock);
1152 } /* }}} int sensu_config_node */
1154 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1156 oconfig_item_t *child;
1158 struct str_list sensu_tags_arr;
1160 sensu_tags_arr.nb_strs = 0;
1161 sensu_tags_arr.strs = NULL;
1163 for (int i = 0; i < ci->children_num; i++) {
1164 child = &ci->children[i];
1166 if (strcasecmp("Node", child->key) == 0) {
1167 sensu_config_node(child);
1168 } else if (strcasecmp(child->key, "attribute") == 0) {
1169 if (child->values_num != 2) {
1170 WARNING("sensu attributes need both a key and a value.");
1173 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1174 child->values[1].type != OCONFIG_TYPE_STRING) {
1175 WARNING("sensu attribute needs string arguments.");
1179 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string);
1180 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string);
1182 DEBUG("write_sensu plugin: New attribute: %s => %s",
1183 child->values[0].value.string,
1184 child->values[1].value.string);
1185 } else if (strcasecmp(child->key, "tag") == 0) {
1187 status = cf_util_get_string(child, &tmp);
1191 status = add_str_to_list(&sensu_tags_arr, tmp);
1195 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1197 WARNING("write_sensu plugin: Ignoring unknown "
1198 "configuration option \"%s\" at top level.",
1202 if (sensu_tags_arr.nb_strs > 0) {
1204 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1205 free_str_list(&sensu_tags_arr);
1206 if (sensu_tags == NULL) {
1207 ERROR("write_sensu plugin: Unable to alloc memory");
1212 } /* }}} int sensu_config */
1214 void module_register(void)
1216 plugin_register_complex_config("write_sensu", sensu_config);
1219 /* vim: set sw=8 sts=8 ts=8 noet : */