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>
41 #define SENSU_HOST "localhost"
42 #define SENSU_PORT "3030"
51 char *event_service_prefix;
52 struct str_list metric_handlers;
53 struct str_list notification_handlers;
60 _Bool always_append_ds;
69 static char *sensu_tags = NULL;
70 static char **sensu_attrs = NULL;
71 static size_t sensu_attrs_num;
73 static int add_str_to_list(struct str_list *strs,
74 const char *str_to_add) /* {{{ */
76 char **old_strs_ptr = strs->strs;
77 char *newstr = strdup(str_to_add);
79 ERROR("write_sensu plugin: Unable to alloc memory");
82 strs->strs = realloc(strs->strs, sizeof(char *) *(strs->nb_strs + 1));
83 if (strs->strs == NULL) {
84 strs->strs = old_strs_ptr;
86 ERROR("write_sensu plugin: Unable to alloc memory");
89 strs->strs[strs->nb_strs] = newstr;
93 /* }}} int add_str_to_list */
95 static void free_str_list(struct str_list *strs) /* {{{ */
98 for (i=0; i<strs->nb_strs; i++)
102 /* }}} void free_str_list */
104 static int sensu_connect(struct sensu_host *host) /* {{{ */
107 struct addrinfo *ai, hints;
111 // Resolve the target if we haven't done already
112 if (!(host->flags & F_READY)) {
113 memset(&hints, 0, sizeof(hints));
114 memset(&service, 0, sizeof(service));
116 hints.ai_family = AF_INET;
117 hints.ai_socktype = SOCK_STREAM;
118 hints.ai_flags = AI_ADDRCONFIG;
120 node = (host->node != NULL) ? host->node : SENSU_HOST;
121 service = (host->service != NULL) ? host->service : SENSU_PORT;
123 if ((e = getaddrinfo(node, service, &hints, &(host->res))) != 0) {
124 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s",
125 node, gai_strerror(e));
128 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s",
130 host->flags |= F_READY;
133 struct linger so_linger;
135 for (ai = host->res; ai != NULL; ai = ai->ai_next) {
137 if ((host->s = socket(ai->ai_family,
139 ai->ai_protocol)) == -1) {
143 // Set very low close() lingering
144 so_linger.l_onoff = 1;
145 so_linger.l_linger = 3;
146 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) != 0)
147 WARNING("write_sensu plugin: failed to set socket close() lingering");
149 // connect the socket
150 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
155 DEBUG("write_sensu plugin: connected");
160 WARNING("write_sensu plugin: Unable to connect to sensu client");
164 } /* }}} int sensu_connect */
166 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
172 } /* }}} void sensu_close_socket */
174 static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */
177 char *ret_str = NULL;
180 if (list->nb_strs == 0) {
181 ret_str = malloc(sizeof(char));
182 if (ret_str == NULL) {
183 ERROR("write_sensu plugin: Unable to alloc memory");
189 res = asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
191 ERROR("write_sensu plugin: Unable to alloc memory");
195 for (i=1; i<list->nb_strs; i++) {
196 res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
199 ERROR("write_sensu plugin: Unable to alloc memory");
204 res = asprintf(&ret_str, "%s]", temp_str);
207 ERROR("write_sensu plugin: Unable to alloc memory");
212 } /* }}} char *build_json_str_list*/
214 static int sensu_format_name2(char *ret, int ret_len,
215 const char *hostname,
216 const char *plugin, const char *plugin_instance,
217 const char *type, const char *type_instance,
218 const char *separator)
224 buffer_size = (size_t) ret_len;
226 #define APPEND(str) do { \
227 size_t l = strlen (str); \
228 if (l >= buffer_size) \
230 memcpy (buffer, (str), l); \
231 buffer += l; buffer_size -= l; \
234 assert (plugin != NULL);
235 assert (type != NULL);
240 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
243 APPEND (plugin_instance);
247 if ((type_instance != NULL) && (type_instance[0] != 0))
250 APPEND (type_instance);
252 assert (buffer_size > 0);
257 } /* int sensu_format_name2 */
259 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
262 int len=strlen(orig_name);
263 for (i=0; i<len; i++) {
264 // some plugins like ipmi generate special characters in metric name
265 switch(orig_name[i]) {
266 case '(': orig_name[i] = '_'; break;
267 case ')': orig_name[i] = '_'; break;
268 case ' ': orig_name[i] = '_'; break;
269 case '"': orig_name[i] = '_'; break;
270 case '\'': orig_name[i] = '_'; break;
271 case '+': orig_name[i] = '_'; break;
274 } /* }}} char *replace_sensu_name_reserved */
276 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
277 data_set_t const *ds,
278 value_list_t const *vl, size_t index,
279 gauge_t const *rates,
282 char name_buffer[5 * DATA_MAX_NAME_LEN];
283 char service_buffer[6 * DATA_MAX_NAME_LEN];
289 // First part of the JSON string
290 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
292 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
293 if (handlers_str == NULL) {
294 ERROR("write_sensu plugin: Unable to alloc memory");
298 // incorporate the handlers
299 if (strlen(handlers_str) == 0) {
301 ret_str = strdup(part1);
302 if (ret_str == NULL) {
303 ERROR("write_sensu plugin: Unable to alloc memory");
308 res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
311 ERROR("write_sensu plugin: Unable to alloc memory");
316 // incorporate the plugin name information
317 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
320 ERROR("write_sensu plugin: Unable to alloc memory");
325 // incorporate the plugin type
326 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
329 ERROR("write_sensu plugin: Unable to alloc memory");
334 // incorporate the plugin instance if any
335 if (vl->plugin_instance[0] != 0) {
336 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
339 ERROR("write_sensu plugin: Unable to alloc memory");
345 // incorporate the plugin type instance if any
346 if (vl->type_instance[0] != 0) {
347 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
350 ERROR("write_sensu plugin: Unable to alloc memory");
356 // incorporate the data source type
357 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
358 char ds_type[DATA_MAX_NAME_LEN];
359 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
360 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
363 ERROR("write_sensu plugin: Unable to alloc memory");
368 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
371 ERROR("write_sensu plugin: Unable to alloc memory");
377 // incorporate the data source name
378 res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
381 ERROR("write_sensu plugin: Unable to alloc memory");
386 // incorporate the data source index
388 char ds_index[DATA_MAX_NAME_LEN];
389 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
390 res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
393 ERROR("write_sensu plugin: Unable to alloc memory");
399 // add key value attributes from config if any
400 for (i = 0; i < sensu_attrs_num; i += 2) {
401 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
404 ERROR("write_sensu plugin: Unable to alloc memory");
410 // incorporate sensu tags from config if any
411 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
412 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
415 ERROR("write_sensu plugin: Unable to alloc memory");
421 // calculate the value and set to a string
422 if (ds->ds[index].type == DS_TYPE_GAUGE) {
423 res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
426 ERROR("write_sensu plugin: Unable to alloc memory");
429 } else if (rates != NULL) {
430 res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
433 ERROR("write_sensu plugin: Unable to alloc memory");
437 if (ds->ds[index].type == DS_TYPE_DERIVE) {
438 res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
441 ERROR("write_sensu plugin: Unable to alloc memory");
445 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
446 res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
449 ERROR("write_sensu plugin: Unable to alloc memory");
454 res = asprintf(&value_str, "%llu", vl->values[index].counter);
457 ERROR("write_sensu plugin: Unable to alloc memory");
463 // Generate the full service name
464 sensu_format_name2(name_buffer, sizeof(name_buffer),
465 vl->host, vl->plugin, vl->plugin_instance,
466 vl->type, vl->type_instance, host->separator);
467 if (host->always_append_ds || (ds->ds_num > 1)) {
468 if (host->event_service_prefix == NULL)
469 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
470 name_buffer, ds->ds[index].name);
472 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
473 host->event_service_prefix, name_buffer, ds->ds[index].name);
475 if (host->event_service_prefix == NULL)
476 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
478 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
479 host->event_service_prefix, name_buffer);
482 // Replace collectd sensor name reserved characters so that time series DB is happy
483 in_place_replace_sensu_name_reserved(service_buffer);
485 // finalize the buffer by setting the output and closing curly bracket
486 res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
490 ERROR("write_sensu plugin: Unable to alloc memory");
495 DEBUG("write_sensu plugin: Successfully created json for metric: "
496 "host = \"%s\", service = \"%s\"",
497 vl->host, service_buffer);
499 } /* }}} char *sensu_value_to_json */
502 * Uses replace_str2() implementation from
503 * http://creativeandcritical.net/str-replace-c/
504 * copyright (c) Laird Shaw, under public domain.
506 static char *replace_str(const char *str, const char *old, /* {{{ */
511 size_t oldlen = strlen(old);
512 size_t count = strlen(new);
514 size_t newlen = count;
515 int samesize = (oldlen == newlen);
518 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
520 /* This is undefined if p - str > PTRDIFF_MAX */
521 retlen = p - str + strlen(p) + count * (newlen - oldlen);
523 retlen = strlen(str);
525 ret = calloc(1, retlen + 1);
528 // added to original: not optimized, but keeps valgrind happy.
533 /* If the old and new strings are different lengths - in other
534 * words we have already iterated through with strstr above,
535 * and thus we know how many times we need to call it - then we
536 * can avoid the final (potentially lengthy) call to strstr,
537 * which we already know is going to return NULL, by
538 * decrementing and checking count.
540 if (!samesize && !count--)
542 /* Otherwise i.e. when the old and new strings are the same
543 * length, and we don't know how many times to call strstr,
544 * we must check for a NULL return here (we check it in any
545 * event, to avoid further conditions, and because there's
546 * no harm done with the check even when the old and new
547 * strings are different lengths).
549 if ((q = strstr(p, old)) == NULL)
551 /* This is undefined if q - p > PTRDIFF_MAX */
555 memcpy(r, new, newlen);
559 strncpy(r, p, strlen(p));
562 } /* }}} char *replace_str */
564 static char *replace_json_reserved(const char *message) /* {{{ */
566 char *msg = replace_str(message, "\\", "\\\\");
568 ERROR("write_sensu plugin: Unable to alloc memory");
571 char *tmp = replace_str(msg, "\"", "\\\"");
574 ERROR("write_sensu plugin: Unable to alloc memory");
577 msg = replace_str(tmp, "\n", "\\\n");
580 ERROR("write_sensu plugin: Unable to alloc memory");
584 } /* }}} char *replace_json_reserved */
586 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
587 notification_t const *n)
589 char service_buffer[6 * DATA_MAX_NAME_LEN];
590 char const *severity;
591 notification_meta_t *meta;
597 // add the severity/status
598 switch (n->severity) {
604 severity = "WARNING";
608 severity = "CRITICAL";
612 severity = "UNKNOWN";
615 res = asprintf(&temp_str, "{\"status\": %d", status);
617 ERROR("write_sensu plugin: Unable to alloc memory");
622 // incorporate the timestamp
623 res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
626 ERROR("write_sensu plugin: Unable to alloc memory");
631 char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
632 if (handlers_str == NULL) {
633 ERROR("write_sensu plugin: Unable to alloc memory");
637 // incorporate the handlers
638 if (strlen(handlers_str) != 0) {
639 res = asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
643 ERROR("write_sensu plugin: Unable to alloc memory");
651 // incorporate the plugin name information if any
652 if (n->plugin[0] != 0) {
653 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
656 ERROR("write_sensu plugin: Unable to alloc memory");
662 // incorporate the plugin type if any
663 if (n->type[0] != 0) {
664 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
667 ERROR("write_sensu plugin: Unable to alloc memory");
673 // incorporate the plugin instance if any
674 if (n->plugin_instance[0] != 0) {
675 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
678 ERROR("write_sensu plugin: Unable to alloc memory");
684 // incorporate the plugin type instance if any
685 if (n->type_instance[0] != 0) {
686 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
689 ERROR("write_sensu plugin: Unable to alloc memory");
695 // add key value attributes from config if any
696 for (i = 0; i < sensu_attrs_num; i += 2) {
697 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
700 ERROR("write_sensu plugin: Unable to alloc memory");
706 // incorporate sensu tags from config if any
707 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
708 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
711 ERROR("write_sensu plugin: Unable to alloc memory");
717 // incorporate the service name
718 sensu_format_name2(service_buffer, sizeof(service_buffer),
719 /* host */ "", n->plugin, n->plugin_instance,
720 n->type, n->type_instance, host->separator);
721 // replace sensu event name chars that are considered illegal
722 in_place_replace_sensu_name_reserved(service_buffer);
723 res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
726 ERROR("write_sensu plugin: Unable to alloc memory");
731 // incorporate the check output
732 if (n->message[0] != 0) {
733 char *msg = replace_json_reserved(n->message);
735 ERROR("write_sensu plugin: Unable to alloc memory");
739 res = asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
743 ERROR("write_sensu plugin: Unable to alloc memory");
749 // Pull in values from threshold and add extra attributes
750 for (meta = n->meta; meta != NULL; meta = meta->next) {
751 if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) {
752 res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
755 ERROR("write_sensu plugin: Unable to alloc memory");
760 if (meta->type == NM_TYPE_STRING) {
761 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
764 ERROR("write_sensu plugin: Unable to alloc memory");
771 // close the curly bracket
772 res = asprintf(&temp_str, "%s}\n", ret_str);
775 ERROR("write_sensu plugin: Unable to alloc memory");
780 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
781 "host = \"%s\", service = \"%s\", state = \"%s\"",
782 n->host, service_buffer, severity);
784 } /* }}} char *sensu_notification_to_json */
786 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
791 status = sensu_connect(host);
795 buffer_len = strlen(msg);
797 status = (int) swrite(host->s, msg, buffer_len);
798 sensu_close_socket(host);
802 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
803 (host->node != NULL) ? host->node : SENSU_HOST,
804 (host->service != NULL) ? host->service : SENSU_PORT,
805 sstrerror(errno, errbuf, sizeof(errbuf)));
810 } /* }}} int sensu_send_msg */
813 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
817 status = sensu_send_msg(host, msg);
819 host->flags &= ~F_READY;
820 if (host->res != NULL) {
821 freeaddrinfo(host->res);
828 } /* }}} int sensu_send */
831 static int sensu_write(const data_set_t *ds, /* {{{ */
832 const value_list_t *vl,
836 int statuses[vl->values_len];
837 struct sensu_host *host = ud->data;
838 gauge_t *rates = NULL;
842 pthread_mutex_lock(&host->lock);
843 memset(statuses, 0, vl->values_len * sizeof(*statuses));
845 if (host->store_rates) {
846 rates = uc_get_rate(ds, vl);
848 ERROR("write_sensu plugin: uc_get_rate failed.");
849 pthread_mutex_unlock(&host->lock);
853 for (i = 0; i < vl->values_len; i++) {
854 msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]);
857 pthread_mutex_unlock(&host->lock);
860 status = sensu_send(host, msg);
863 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
864 pthread_mutex_unlock(&host->lock);
870 pthread_mutex_unlock(&host->lock);
872 } /* }}} int sensu_write */
874 static int sensu_notification(const notification_t *n, user_data_t *ud) /* {{{ */
877 struct sensu_host *host = ud->data;
880 pthread_mutex_lock(&host->lock);
882 msg = sensu_notification_to_json(host, n);
884 pthread_mutex_unlock(&host->lock);
888 status = sensu_send(host, msg);
891 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
892 pthread_mutex_unlock(&host->lock);
895 } /* }}} int sensu_notification */
897 static void sensu_free(void *p) /* {{{ */
899 struct sensu_host *host = p;
904 pthread_mutex_lock(&host->lock);
906 host->reference_count--;
907 if (host->reference_count > 0) {
908 pthread_mutex_unlock(&host->lock);
912 sensu_close_socket(host);
913 if (host->res != NULL) {
914 freeaddrinfo(host->res);
917 sfree(host->service);
918 sfree(host->event_service_prefix);
921 sfree(host->separator);
922 free_str_list(&(host->metric_handlers));
923 free_str_list(&(host->notification_handlers));
924 pthread_mutex_destroy(&host->lock);
926 } /* }}} void sensu_free */
929 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
931 struct sensu_host *host = NULL;
934 oconfig_item_t *child;
935 char callback_name[DATA_MAX_NAME_LEN];
938 if ((host = calloc(1, sizeof(*host))) == NULL) {
939 ERROR("write_sensu plugin: calloc failed.");
942 pthread_mutex_init(&host->lock, NULL);
943 host->reference_count = 1;
945 host->service = NULL;
946 host->notifications = 0;
948 host->store_rates = 1;
949 host->always_append_ds = 0;
950 host->metric_handlers.nb_strs = 0;
951 host->metric_handlers.strs = NULL;
952 host->notification_handlers.nb_strs = 0;
953 host->notification_handlers.strs = NULL;
954 host->separator = strdup("/");
955 if (host->separator == NULL) {
956 ERROR("write_sensu plugin: Unable to alloc memory");
961 status = cf_util_get_string(ci, &host->name);
963 WARNING("write_sensu plugin: Required host name is missing.");
968 for (i = 0; i < ci->children_num; i++) {
969 child = &ci->children[i];
972 if (strcasecmp("Host", child->key) == 0) {
973 status = cf_util_get_string(child, &host->node);
976 } else if (strcasecmp("Notifications", child->key) == 0) {
977 status = cf_util_get_boolean(child, &host->notifications);
980 } else if (strcasecmp("Metrics", child->key) == 0) {
981 status = cf_util_get_boolean(child, &host->metrics);
984 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
985 status = cf_util_get_string(child, &host->event_service_prefix);
988 } else if (strcasecmp("Separator", child->key) == 0) {
989 status = cf_util_get_string(child, &host->separator);
992 } else if (strcasecmp("MetricHandler", child->key) == 0) {
993 char *temp_str = NULL;
994 status = cf_util_get_string(child, &temp_str);
997 status = add_str_to_list(&(host->metric_handlers), temp_str);
1001 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1002 char *temp_str = NULL;
1003 status = cf_util_get_string(child, &temp_str);
1006 status = add_str_to_list(&(host->notification_handlers), temp_str);
1010 } else if (strcasecmp("Port", child->key) == 0) {
1011 status = cf_util_get_service(child, &host->service);
1013 ERROR("write_sensu plugin: Invalid argument "
1014 "configured for the \"Port\" "
1018 } else if (strcasecmp("StoreRates", child->key) == 0) {
1019 status = cf_util_get_boolean(child, &host->store_rates);
1022 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1023 status = cf_util_get_boolean(child,
1024 &host->always_append_ds);
1028 WARNING("write_sensu plugin: ignoring unknown config "
1029 "option: \"%s\"", child->key);
1037 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1039 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. Giving up.");
1043 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1045 WARNING("write_sensu plugin: notifications enabled but no NotificationHandler defined. Giving up.");
1049 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1050 WARNING("write_sensu plugin: NotificationHandler given so forcing notifications to be enabled");
1051 host->notifications = 1;
1054 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1055 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be enabled");
1059 if (!(host->notifications || host->metrics)) {
1060 WARNING("write_sensu plugin: neither metrics nor notifications enabled. Giving up.");
1065 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1067 ud.free_func = sensu_free;
1069 pthread_mutex_lock(&host->lock);
1071 if (host->metrics) {
1072 status = plugin_register_write(callback_name, sensu_write, &ud);
1074 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1075 "failed with status %i.",
1076 callback_name, status);
1078 host->reference_count++;
1081 if (host->notifications) {
1082 status = plugin_register_notification(callback_name, sensu_notification, &ud);
1084 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1085 "failed with status %i.",
1086 callback_name, status);
1088 host->reference_count++;
1091 if (host->reference_count <= 1) {
1092 /* Both callbacks failed => free memory.
1093 * We need to unlock here, because sensu_free() will lock.
1094 * This is not a race condition, because we're the only one
1095 * holding a reference. */
1096 pthread_mutex_unlock(&host->lock);
1101 host->reference_count--;
1102 pthread_mutex_unlock(&host->lock);
1105 } /* }}} int sensu_config_node */
1107 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1110 oconfig_item_t *child;
1112 struct str_list sensu_tags_arr;
1114 sensu_tags_arr.nb_strs = 0;
1115 sensu_tags_arr.strs = NULL;
1117 for (i = 0; i < ci->children_num; i++) {
1118 child = &ci->children[i];
1120 if (strcasecmp("Node", child->key) == 0) {
1121 sensu_config_node(child);
1122 } else if (strcasecmp(child->key, "attribute") == 0) {
1123 if (child->values_num != 2) {
1124 WARNING("sensu attributes need both a key and a value.");
1127 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1128 child->values[1].type != OCONFIG_TYPE_STRING) {
1129 WARNING("sensu attribute needs string arguments.");
1133 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string);
1134 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string);
1136 DEBUG("write_sensu plugin: New attribute: %s => %s",
1137 child->values[0].value.string,
1138 child->values[1].value.string);
1139 } else if (strcasecmp(child->key, "tag") == 0) {
1141 status = cf_util_get_string(child, &tmp);
1145 status = add_str_to_list(&sensu_tags_arr, tmp);
1149 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1151 WARNING("write_sensu plugin: Ignoring unknown "
1152 "configuration option \"%s\" at top level.",
1156 if (sensu_tags_arr.nb_strs > 0) {
1158 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1159 free_str_list(&sensu_tags_arr);
1160 if (sensu_tags == NULL) {
1161 ERROR("write_sensu plugin: Unable to alloc memory");
1166 } /* }}} int sensu_config */
1168 void module_register(void)
1170 plugin_register_complex_config("write_sensu", sensu_config);
1173 /* vim: set sw=8 sts=8 ts=8 noet : */