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>
32 #include "configfile.h"
33 #include "utils_cache.h"
34 #include <arpa/inet.h>
42 #define SENSU_HOST "localhost"
43 #define SENSU_PORT "3030"
52 char *event_service_prefix;
53 struct str_list metric_handlers;
54 struct str_list notification_handlers;
61 _Bool always_append_ds;
70 static char *sensu_tags = NULL;
71 static char **sensu_attrs = NULL;
72 static size_t sensu_attrs_num;
74 static int add_str_to_list(struct str_list *strs,
75 const char *str_to_add) /* {{{ */
77 char **old_strs_ptr = strs->strs;
78 char *newstr = strdup(str_to_add);
80 ERROR("write_sensu plugin: Unable to alloc memory");
83 strs->strs = realloc(strs->strs, sizeof(char *) *(strs->nb_strs + 1));
84 if (strs->strs == NULL) {
85 strs->strs = old_strs_ptr;
87 ERROR("write_sensu plugin: Unable to alloc memory");
90 strs->strs[strs->nb_strs] = newstr;
94 /* }}} int add_str_to_list */
96 static void free_str_list(struct str_list *strs) /* {{{ */
99 for (i=0; i<strs->nb_strs; i++)
103 /* }}} void free_str_list */
105 static int sensu_connect(struct sensu_host *host) /* {{{ */
108 struct addrinfo *ai, hints;
112 // Resolve the target if we haven't done already
113 if (!(host->flags & F_READY)) {
114 memset(&hints, 0, sizeof(hints));
115 memset(&service, 0, sizeof(service));
117 hints.ai_family = AF_INET;
118 hints.ai_socktype = SOCK_STREAM;
120 hints.ai_flags |= AI_ADDRCONFIG;
123 node = (host->node != NULL) ? host->node : SENSU_HOST;
124 service = (host->service != NULL) ? host->service : SENSU_PORT;
126 if ((e = getaddrinfo(node, service, &hints, &(host->res))) != 0) {
127 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s",
128 node, gai_strerror(e));
131 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s",
133 host->flags |= F_READY;
136 struct linger so_linger;
138 for (ai = host->res; ai != NULL; ai = ai->ai_next) {
140 if ((host->s = socket(ai->ai_family,
142 ai->ai_protocol)) == -1) {
146 // Set very low close() lingering
147 so_linger.l_onoff = 1;
148 so_linger.l_linger = 3;
149 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) != 0)
150 WARNING("write_sensu plugin: failed to set socket close() lingering");
152 // connect the socket
153 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
158 DEBUG("write_sensu plugin: connected");
163 WARNING("write_sensu plugin: Unable to connect to sensu client");
167 } /* }}} int sensu_connect */
169 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
175 } /* }}} void sensu_close_socket */
177 static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */
180 char *ret_str = NULL;
183 if (list->nb_strs == 0) {
184 ret_str = malloc(sizeof(char));
185 if (ret_str == NULL) {
186 ERROR("write_sensu plugin: Unable to alloc memory");
192 res = asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
194 ERROR("write_sensu plugin: Unable to alloc memory");
198 for (i=1; i<list->nb_strs; i++) {
199 res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
202 ERROR("write_sensu plugin: Unable to alloc memory");
207 res = asprintf(&ret_str, "%s]", temp_str);
210 ERROR("write_sensu plugin: Unable to alloc memory");
215 } /* }}} char *build_json_str_list*/
217 static int sensu_format_name2(char *ret, int ret_len,
218 const char *hostname,
219 const char *plugin, const char *plugin_instance,
220 const char *type, const char *type_instance,
221 const char *separator)
227 buffer_size = (size_t) ret_len;
229 #define APPEND(str) do { \
230 size_t l = strlen (str); \
231 if (l >= buffer_size) \
233 memcpy (buffer, (str), l); \
234 buffer += l; buffer_size -= l; \
237 assert (plugin != NULL);
238 assert (type != NULL);
243 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
246 APPEND (plugin_instance);
250 if ((type_instance != NULL) && (type_instance[0] != 0))
253 APPEND (type_instance);
255 assert (buffer_size > 0);
260 } /* int sensu_format_name2 */
262 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
265 int len=strlen(orig_name);
266 for (i=0; i<len; i++) {
267 // some plugins like ipmi generate special characters in metric name
268 switch(orig_name[i]) {
269 case '(': orig_name[i] = '_'; break;
270 case ')': orig_name[i] = '_'; break;
271 case ' ': orig_name[i] = '_'; break;
272 case '"': orig_name[i] = '_'; break;
273 case '\'': orig_name[i] = '_'; break;
274 case '+': orig_name[i] = '_'; break;
277 } /* }}} char *replace_sensu_name_reserved */
279 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
280 data_set_t const *ds,
281 value_list_t const *vl, size_t index,
282 gauge_t const *rates,
285 char name_buffer[5 * DATA_MAX_NAME_LEN];
286 char service_buffer[6 * DATA_MAX_NAME_LEN];
292 // First part of the JSON string
293 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
295 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
296 if (handlers_str == NULL) {
297 ERROR("write_sensu plugin: Unable to alloc memory");
301 // incorporate the handlers
302 if (strlen(handlers_str) == 0) {
304 ret_str = strdup(part1);
305 if (ret_str == NULL) {
306 ERROR("write_sensu plugin: Unable to alloc memory");
311 res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
314 ERROR("write_sensu plugin: Unable to alloc memory");
319 // incorporate the plugin name information
320 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
323 ERROR("write_sensu plugin: Unable to alloc memory");
328 // incorporate the plugin type
329 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
332 ERROR("write_sensu plugin: Unable to alloc memory");
337 // incorporate the plugin instance if any
338 if (vl->plugin_instance[0] != 0) {
339 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
342 ERROR("write_sensu plugin: Unable to alloc memory");
348 // incorporate the plugin type instance if any
349 if (vl->type_instance[0] != 0) {
350 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
353 ERROR("write_sensu plugin: Unable to alloc memory");
359 // incorporate the data source type
360 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
361 char ds_type[DATA_MAX_NAME_LEN];
362 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
363 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
366 ERROR("write_sensu plugin: Unable to alloc memory");
371 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
374 ERROR("write_sensu plugin: Unable to alloc memory");
380 // incorporate the data source name
381 res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
384 ERROR("write_sensu plugin: Unable to alloc memory");
389 // incorporate the data source index
391 char ds_index[DATA_MAX_NAME_LEN];
392 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
393 res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
396 ERROR("write_sensu plugin: Unable to alloc memory");
402 // add key value attributes from config if any
403 for (i = 0; i < sensu_attrs_num; i += 2) {
404 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
407 ERROR("write_sensu plugin: Unable to alloc memory");
413 // incorporate sensu tags from config if any
414 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
415 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
418 ERROR("write_sensu plugin: Unable to alloc memory");
424 // calculate the value and set to a string
425 if (ds->ds[index].type == DS_TYPE_GAUGE) {
426 res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
429 ERROR("write_sensu plugin: Unable to alloc memory");
432 } else if (rates != NULL) {
433 res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
436 ERROR("write_sensu plugin: Unable to alloc memory");
440 if (ds->ds[index].type == DS_TYPE_DERIVE) {
441 res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
444 ERROR("write_sensu plugin: Unable to alloc memory");
448 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
449 res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
452 ERROR("write_sensu plugin: Unable to alloc memory");
457 res = asprintf(&value_str, "%llu", vl->values[index].counter);
460 ERROR("write_sensu plugin: Unable to alloc memory");
466 // Generate the full service name
467 sensu_format_name2(name_buffer, sizeof(name_buffer),
468 vl->host, vl->plugin, vl->plugin_instance,
469 vl->type, vl->type_instance, host->separator);
470 if (host->always_append_ds || (ds->ds_num > 1)) {
471 if (host->event_service_prefix == NULL)
472 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
473 name_buffer, ds->ds[index].name);
475 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
476 host->event_service_prefix, name_buffer, ds->ds[index].name);
478 if (host->event_service_prefix == NULL)
479 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
481 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
482 host->event_service_prefix, name_buffer);
485 // Replace collectd sensor name reserved characters so that time series DB is happy
486 in_place_replace_sensu_name_reserved(service_buffer);
488 // finalize the buffer by setting the output and closing curly bracket
489 res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
493 ERROR("write_sensu plugin: Unable to alloc memory");
498 DEBUG("write_sensu plugin: Successfully created json for metric: "
499 "host = \"%s\", service = \"%s\"",
500 vl->host, service_buffer);
502 } /* }}} char *sensu_value_to_json */
505 * Uses replace_str2() implementation from
506 * http://creativeandcritical.net/str-replace-c/
507 * copyright (c) Laird Shaw, under public domain.
509 static char *replace_str(const char *str, const char *old, /* {{{ */
514 size_t oldlen = strlen(old);
515 size_t count = strlen(new);
517 size_t newlen = count;
518 int samesize = (oldlen == newlen);
521 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
523 /* This is undefined if p - str > PTRDIFF_MAX */
524 retlen = p - str + strlen(p) + count * (newlen - oldlen);
526 retlen = strlen(str);
528 ret = calloc(1, retlen + 1);
531 // added to original: not optimized, but keeps valgrind happy.
536 /* If the old and new strings are different lengths - in other
537 * words we have already iterated through with strstr above,
538 * and thus we know how many times we need to call it - then we
539 * can avoid the final (potentially lengthy) call to strstr,
540 * which we already know is going to return NULL, by
541 * decrementing and checking count.
543 if (!samesize && !count--)
545 /* Otherwise i.e. when the old and new strings are the same
546 * length, and we don't know how many times to call strstr,
547 * we must check for a NULL return here (we check it in any
548 * event, to avoid further conditions, and because there's
549 * no harm done with the check even when the old and new
550 * strings are different lengths).
552 if ((q = strstr(p, old)) == NULL)
554 /* This is undefined if q - p > PTRDIFF_MAX */
558 memcpy(r, new, newlen);
562 strncpy(r, p, strlen(p));
565 } /* }}} char *replace_str */
567 static char *replace_json_reserved(const char *message) /* {{{ */
569 char *msg = replace_str(message, "\\", "\\\\");
571 ERROR("write_sensu plugin: Unable to alloc memory");
574 char *tmp = replace_str(msg, "\"", "\\\"");
577 ERROR("write_sensu plugin: Unable to alloc memory");
580 msg = replace_str(tmp, "\n", "\\\n");
583 ERROR("write_sensu plugin: Unable to alloc memory");
587 } /* }}} char *replace_json_reserved */
589 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
590 notification_t const *n)
592 char service_buffer[6 * DATA_MAX_NAME_LEN];
593 char const *severity;
594 notification_meta_t *meta;
600 // add the severity/status
601 switch (n->severity) {
607 severity = "WARNING";
611 severity = "CRITICAL";
615 severity = "UNKNOWN";
618 res = asprintf(&temp_str, "{\"status\": %d", status);
620 ERROR("write_sensu plugin: Unable to alloc memory");
625 // incorporate the timestamp
626 res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
629 ERROR("write_sensu plugin: Unable to alloc memory");
634 char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
635 if (handlers_str == NULL) {
636 ERROR("write_sensu plugin: Unable to alloc memory");
640 // incorporate the handlers
641 if (strlen(handlers_str) != 0) {
642 res = asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
646 ERROR("write_sensu plugin: Unable to alloc memory");
654 // incorporate the plugin name information if any
655 if (n->plugin[0] != 0) {
656 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
659 ERROR("write_sensu plugin: Unable to alloc memory");
665 // incorporate the plugin type if any
666 if (n->type[0] != 0) {
667 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
670 ERROR("write_sensu plugin: Unable to alloc memory");
676 // incorporate the plugin instance if any
677 if (n->plugin_instance[0] != 0) {
678 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
681 ERROR("write_sensu plugin: Unable to alloc memory");
687 // incorporate the plugin type instance if any
688 if (n->type_instance[0] != 0) {
689 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
692 ERROR("write_sensu plugin: Unable to alloc memory");
698 // add key value attributes from config if any
699 for (i = 0; i < sensu_attrs_num; i += 2) {
700 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
703 ERROR("write_sensu plugin: Unable to alloc memory");
709 // incorporate sensu tags from config if any
710 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
711 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
714 ERROR("write_sensu plugin: Unable to alloc memory");
720 // incorporate the service name
721 sensu_format_name2(service_buffer, sizeof(service_buffer),
722 /* host */ "", n->plugin, n->plugin_instance,
723 n->type, n->type_instance, host->separator);
724 // replace sensu event name chars that are considered illegal
725 in_place_replace_sensu_name_reserved(service_buffer);
726 res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
729 ERROR("write_sensu plugin: Unable to alloc memory");
734 // incorporate the check output
735 if (n->message[0] != 0) {
736 char *msg = replace_json_reserved(n->message);
738 ERROR("write_sensu plugin: Unable to alloc memory");
742 res = asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
746 ERROR("write_sensu plugin: Unable to alloc memory");
752 // Pull in values from threshold and add extra attributes
753 for (meta = n->meta; meta != NULL; meta = meta->next) {
754 if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) {
755 res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
758 ERROR("write_sensu plugin: Unable to alloc memory");
763 if (meta->type == NM_TYPE_STRING) {
764 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
767 ERROR("write_sensu plugin: Unable to alloc memory");
774 // close the curly bracket
775 res = asprintf(&temp_str, "%s}\n", ret_str);
778 ERROR("write_sensu plugin: Unable to alloc memory");
783 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
784 "host = \"%s\", service = \"%s\", state = \"%s\"",
785 n->host, service_buffer, severity);
787 } /* }}} char *sensu_notification_to_json */
789 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
794 status = sensu_connect(host);
798 buffer_len = strlen(msg);
800 status = (int) swrite(host->s, msg, buffer_len);
801 sensu_close_socket(host);
805 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
806 (host->node != NULL) ? host->node : SENSU_HOST,
807 (host->service != NULL) ? host->service : SENSU_PORT,
808 sstrerror(errno, errbuf, sizeof(errbuf)));
813 } /* }}} int sensu_send_msg */
816 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
820 status = sensu_send_msg(host, msg);
822 host->flags &= ~F_READY;
823 if (host->res != NULL) {
824 freeaddrinfo(host->res);
831 } /* }}} int sensu_send */
834 static int sensu_write(const data_set_t *ds, /* {{{ */
835 const value_list_t *vl,
839 int statuses[vl->values_len];
840 struct sensu_host *host = ud->data;
841 gauge_t *rates = NULL;
845 pthread_mutex_lock(&host->lock);
846 memset(statuses, 0, vl->values_len * sizeof(*statuses));
848 if (host->store_rates) {
849 rates = uc_get_rate(ds, vl);
851 ERROR("write_sensu plugin: uc_get_rate failed.");
852 pthread_mutex_unlock(&host->lock);
856 for (i = 0; i < vl->values_len; i++) {
857 msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]);
860 pthread_mutex_unlock(&host->lock);
863 status = sensu_send(host, msg);
866 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
867 pthread_mutex_unlock(&host->lock);
873 pthread_mutex_unlock(&host->lock);
875 } /* }}} int sensu_write */
877 static int sensu_notification(const notification_t *n, user_data_t *ud) /* {{{ */
880 struct sensu_host *host = ud->data;
883 pthread_mutex_lock(&host->lock);
885 msg = sensu_notification_to_json(host, n);
887 pthread_mutex_unlock(&host->lock);
891 status = sensu_send(host, msg);
894 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
895 pthread_mutex_unlock(&host->lock);
898 } /* }}} int sensu_notification */
900 static void sensu_free(void *p) /* {{{ */
902 struct sensu_host *host = p;
907 pthread_mutex_lock(&host->lock);
909 host->reference_count--;
910 if (host->reference_count > 0) {
911 pthread_mutex_unlock(&host->lock);
915 sensu_close_socket(host);
916 if (host->res != NULL) {
917 freeaddrinfo(host->res);
920 sfree(host->service);
921 sfree(host->event_service_prefix);
924 sfree(host->separator);
925 free_str_list(&(host->metric_handlers));
926 free_str_list(&(host->notification_handlers));
927 pthread_mutex_destroy(&host->lock);
929 } /* }}} void sensu_free */
932 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
934 struct sensu_host *host = NULL;
937 oconfig_item_t *child;
938 char callback_name[DATA_MAX_NAME_LEN];
941 if ((host = calloc(1, sizeof(*host))) == NULL) {
942 ERROR("write_sensu plugin: calloc failed.");
945 pthread_mutex_init(&host->lock, NULL);
946 host->reference_count = 1;
948 host->service = NULL;
949 host->notifications = 0;
951 host->store_rates = 1;
952 host->always_append_ds = 0;
953 host->metric_handlers.nb_strs = 0;
954 host->metric_handlers.strs = NULL;
955 host->notification_handlers.nb_strs = 0;
956 host->notification_handlers.strs = NULL;
957 host->separator = strdup("/");
958 if (host->separator == NULL) {
959 ERROR("write_sensu plugin: Unable to alloc memory");
964 status = cf_util_get_string(ci, &host->name);
966 WARNING("write_sensu plugin: Required host name is missing.");
971 for (i = 0; i < ci->children_num; i++) {
972 child = &ci->children[i];
975 if (strcasecmp("Host", child->key) == 0) {
976 status = cf_util_get_string(child, &host->node);
979 } else if (strcasecmp("Notifications", child->key) == 0) {
980 status = cf_util_get_boolean(child, &host->notifications);
983 } else if (strcasecmp("Metrics", child->key) == 0) {
984 status = cf_util_get_boolean(child, &host->metrics);
987 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
988 status = cf_util_get_string(child, &host->event_service_prefix);
991 } else if (strcasecmp("Separator", child->key) == 0) {
992 status = cf_util_get_string(child, &host->separator);
995 } else if (strcasecmp("MetricHandler", child->key) == 0) {
996 char *temp_str = NULL;
997 status = cf_util_get_string(child, &temp_str);
1000 status = add_str_to_list(&(host->metric_handlers), temp_str);
1004 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1005 char *temp_str = NULL;
1006 status = cf_util_get_string(child, &temp_str);
1009 status = add_str_to_list(&(host->notification_handlers), temp_str);
1013 } else if (strcasecmp("Port", child->key) == 0) {
1014 status = cf_util_get_service(child, &host->service);
1016 ERROR("write_sensu plugin: Invalid argument "
1017 "configured for the \"Port\" "
1021 } else if (strcasecmp("StoreRates", child->key) == 0) {
1022 status = cf_util_get_boolean(child, &host->store_rates);
1025 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1026 status = cf_util_get_boolean(child,
1027 &host->always_append_ds);
1031 WARNING("write_sensu plugin: ignoring unknown config "
1032 "option: \"%s\"", child->key);
1040 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1042 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. Giving up.");
1046 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1048 WARNING("write_sensu plugin: notifications enabled but no NotificationHandler defined. Giving up.");
1052 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1053 WARNING("write_sensu plugin: NotificationHandler given so forcing notifications to be enabled");
1054 host->notifications = 1;
1057 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1058 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be enabled");
1062 if (!(host->notifications || host->metrics)) {
1063 WARNING("write_sensu plugin: neither metrics nor notifications enabled. Giving up.");
1068 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1070 ud.free_func = sensu_free;
1072 pthread_mutex_lock(&host->lock);
1074 if (host->metrics) {
1075 status = plugin_register_write(callback_name, sensu_write, &ud);
1077 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1078 "failed with status %i.",
1079 callback_name, status);
1081 host->reference_count++;
1084 if (host->notifications) {
1085 status = plugin_register_notification(callback_name, sensu_notification, &ud);
1087 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1088 "failed with status %i.",
1089 callback_name, status);
1091 host->reference_count++;
1094 if (host->reference_count <= 1) {
1095 /* Both callbacks failed => free memory.
1096 * We need to unlock here, because sensu_free() will lock.
1097 * This is not a race condition, because we're the only one
1098 * holding a reference. */
1099 pthread_mutex_unlock(&host->lock);
1104 host->reference_count--;
1105 pthread_mutex_unlock(&host->lock);
1108 } /* }}} int sensu_config_node */
1110 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1113 oconfig_item_t *child;
1115 struct str_list sensu_tags_arr;
1117 sensu_tags_arr.nb_strs = 0;
1118 sensu_tags_arr.strs = NULL;
1120 for (i = 0; i < ci->children_num; i++) {
1121 child = &ci->children[i];
1123 if (strcasecmp("Node", child->key) == 0) {
1124 sensu_config_node(child);
1125 } else if (strcasecmp(child->key, "attribute") == 0) {
1126 if (child->values_num != 2) {
1127 WARNING("sensu attributes need both a key and a value.");
1130 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1131 child->values[1].type != OCONFIG_TYPE_STRING) {
1132 WARNING("sensu attribute needs string arguments.");
1136 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string);
1137 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string);
1139 DEBUG("write_sensu plugin: New attribute: %s => %s",
1140 child->values[0].value.string,
1141 child->values[1].value.string);
1142 } else if (strcasecmp(child->key, "tag") == 0) {
1144 status = cf_util_get_string(child, &tmp);
1148 status = add_str_to_list(&sensu_tags_arr, tmp);
1152 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1154 WARNING("write_sensu plugin: Ignoring unknown "
1155 "configuration option \"%s\" at top level.",
1159 if (sensu_tags_arr.nb_strs > 0) {
1161 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1162 free_str_list(&sensu_tags_arr);
1163 if (sensu_tags == NULL) {
1164 ERROR("write_sensu plugin: Unable to alloc memory");
1169 } /* }}} int sensu_config */
1171 void module_register(void)
1173 plugin_register_complex_config("write_sensu", sensu_config);
1176 /* vim: set sw=8 sts=8 ts=8 noet : */