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 "configfile.h"
34 #include "utils_cache.h"
35 #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) /* {{{ */
98 for (int i=0; i<strs->nb_strs; i++)
102 /* }}} void free_str_list */
104 static int sensu_connect(struct sensu_host *host) /* {{{ */
110 // Resolve the target if we haven't done already
111 if (!(host->flags & F_READY)) {
112 memset(&service, 0, sizeof(service));
115 node = (host->node != NULL) ? host->node : SENSU_HOST;
116 service = (host->service != NULL) ? host->service : SENSU_PORT;
118 struct addrinfo ai_hints = {
119 .ai_family = AF_INET,
120 .ai_flags = AI_ADDRCONFIG,
121 .ai_socktype = SOCK_STREAM
124 if ((e = getaddrinfo(node, service, &ai_hints, &(host->res))) != 0) {
125 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s",
126 node, gai_strerror(e));
129 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s",
131 host->flags |= F_READY;
134 struct linger so_linger;
136 for (struct addrinfo *ai = host->res; ai != NULL; ai = ai->ai_next) {
138 if ((host->s = socket(ai->ai_family,
140 ai->ai_protocol)) == -1) {
144 // Set very low close() lingering
145 so_linger.l_onoff = 1;
146 so_linger.l_linger = 3;
147 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) != 0)
148 WARNING("write_sensu plugin: failed to set socket close() lingering");
150 // connect the socket
151 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
156 DEBUG("write_sensu plugin: connected");
161 WARNING("write_sensu plugin: Unable to connect to sensu client");
165 } /* }}} int sensu_connect */
167 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
173 } /* }}} void sensu_close_socket */
175 static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */
178 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 (int 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) /* {{{ */
261 int len=strlen(orig_name);
262 for (int i=0; i<len; i++) {
263 // some plugins like ipmi generate special characters in metric name
264 switch(orig_name[i]) {
265 case '(': orig_name[i] = '_'; break;
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;
273 } /* }}} char *replace_sensu_name_reserved */
275 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
276 data_set_t const *ds,
277 value_list_t const *vl, size_t index,
278 gauge_t const *rates,
281 char name_buffer[5 * DATA_MAX_NAME_LEN];
282 char service_buffer[6 * DATA_MAX_NAME_LEN];
287 // First part of the JSON string
288 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
290 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
291 if (handlers_str == NULL) {
292 ERROR("write_sensu plugin: Unable to alloc memory");
296 // incorporate the handlers
297 if (strlen(handlers_str) == 0) {
299 ret_str = strdup(part1);
300 if (ret_str == NULL) {
301 ERROR("write_sensu plugin: Unable to alloc memory");
306 res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
309 ERROR("write_sensu plugin: Unable to alloc memory");
314 // incorporate the plugin name information
315 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
318 ERROR("write_sensu plugin: Unable to alloc memory");
323 // incorporate the plugin type
324 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
327 ERROR("write_sensu plugin: Unable to alloc memory");
332 // incorporate the plugin instance if any
333 if (vl->plugin_instance[0] != 0) {
334 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
337 ERROR("write_sensu plugin: Unable to alloc memory");
343 // incorporate the plugin type instance if any
344 if (vl->type_instance[0] != 0) {
345 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
348 ERROR("write_sensu plugin: Unable to alloc memory");
354 // incorporate the data source type
355 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
356 char ds_type[DATA_MAX_NAME_LEN];
357 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
358 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
361 ERROR("write_sensu plugin: Unable to alloc memory");
366 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
369 ERROR("write_sensu plugin: Unable to alloc memory");
375 // incorporate the data source name
376 res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
379 ERROR("write_sensu plugin: Unable to alloc memory");
384 // incorporate the data source index
386 char ds_index[DATA_MAX_NAME_LEN];
387 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
388 res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
391 ERROR("write_sensu plugin: Unable to alloc memory");
397 // add key value attributes from config if any
398 for (size_t i = 0; i < sensu_attrs_num; i += 2) {
399 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
402 ERROR("write_sensu plugin: Unable to alloc memory");
408 // incorporate sensu tags from config if any
409 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
410 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
413 ERROR("write_sensu plugin: Unable to alloc memory");
419 // calculate the value and set to a string
420 if (ds->ds[index].type == DS_TYPE_GAUGE) {
421 res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
424 ERROR("write_sensu plugin: Unable to alloc memory");
427 } else if (rates != NULL) {
428 res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
431 ERROR("write_sensu plugin: Unable to alloc memory");
435 if (ds->ds[index].type == DS_TYPE_DERIVE) {
436 res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
439 ERROR("write_sensu plugin: Unable to alloc memory");
443 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
444 res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
447 ERROR("write_sensu plugin: Unable to alloc memory");
452 res = asprintf(&value_str, "%llu", vl->values[index].counter);
455 ERROR("write_sensu plugin: Unable to alloc memory");
461 // Generate the full service name
462 sensu_format_name2(name_buffer, sizeof(name_buffer),
463 vl->host, vl->plugin, vl->plugin_instance,
464 vl->type, vl->type_instance, host->separator);
465 if (host->always_append_ds || (ds->ds_num > 1)) {
466 if (host->event_service_prefix == NULL)
467 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
468 name_buffer, ds->ds[index].name);
470 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
471 host->event_service_prefix, name_buffer, ds->ds[index].name);
473 if (host->event_service_prefix == NULL)
474 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
476 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
477 host->event_service_prefix, name_buffer);
480 // Replace collectd sensor name reserved characters so that time series DB is happy
481 in_place_replace_sensu_name_reserved(service_buffer);
483 // finalize the buffer by setting the output and closing curly bracket
484 res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
488 ERROR("write_sensu plugin: Unable to alloc memory");
493 DEBUG("write_sensu plugin: Successfully created json for metric: "
494 "host = \"%s\", service = \"%s\"",
495 vl->host, service_buffer);
497 } /* }}} char *sensu_value_to_json */
500 * Uses replace_str2() implementation from
501 * http://creativeandcritical.net/str-replace-c/
502 * copyright (c) Laird Shaw, under public domain.
504 static char *replace_str(const char *str, const char *old, /* {{{ */
509 size_t oldlen = strlen(old);
510 size_t count = strlen(new);
512 size_t newlen = count;
513 int samesize = (oldlen == newlen);
516 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
518 /* This is undefined if p - str > PTRDIFF_MAX */
519 retlen = p - str + strlen(p) + count * (newlen - oldlen);
521 retlen = strlen(str);
523 ret = calloc(1, retlen + 1);
526 // added to original: not optimized, but keeps valgrind happy.
531 /* If the old and new strings are different lengths - in other
532 * words we have already iterated through with strstr above,
533 * and thus we know how many times we need to call it - then we
534 * can avoid the final (potentially lengthy) call to strstr,
535 * which we already know is going to return NULL, by
536 * decrementing and checking count.
538 if (!samesize && !count--)
540 /* Otherwise i.e. when the old and new strings are the same
541 * length, and we don't know how many times to call strstr,
542 * we must check for a NULL return here (we check it in any
543 * event, to avoid further conditions, and because there's
544 * no harm done with the check even when the old and new
545 * strings are different lengths).
547 if ((q = strstr(p, old)) == NULL)
549 /* This is undefined if q - p > PTRDIFF_MAX */
553 memcpy(r, new, newlen);
557 strncpy(r, p, strlen(p));
560 } /* }}} char *replace_str */
562 static char *replace_json_reserved(const char *message) /* {{{ */
564 char *msg = replace_str(message, "\\", "\\\\");
566 ERROR("write_sensu plugin: Unable to alloc memory");
569 char *tmp = replace_str(msg, "\"", "\\\"");
572 ERROR("write_sensu plugin: Unable to alloc memory");
575 msg = replace_str(tmp, "\n", "\\\n");
578 ERROR("write_sensu plugin: Unable to alloc memory");
582 } /* }}} char *replace_json_reserved */
584 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
585 notification_t const *n)
587 char service_buffer[6 * DATA_MAX_NAME_LEN];
588 char const *severity;
594 // add the severity/status
595 switch (n->severity) {
601 severity = "WARNING";
605 severity = "CRITICAL";
609 severity = "UNKNOWN";
612 res = asprintf(&temp_str, "{\"status\": %d", status);
614 ERROR("write_sensu plugin: Unable to alloc memory");
619 // incorporate the timestamp
620 res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
623 ERROR("write_sensu plugin: Unable to alloc memory");
628 char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
629 if (handlers_str == NULL) {
630 ERROR("write_sensu plugin: Unable to alloc memory");
634 // incorporate the handlers
635 if (strlen(handlers_str) != 0) {
636 res = asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
640 ERROR("write_sensu plugin: Unable to alloc memory");
648 // incorporate the plugin name information if any
649 if (n->plugin[0] != 0) {
650 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
653 ERROR("write_sensu plugin: Unable to alloc memory");
659 // incorporate the plugin type if any
660 if (n->type[0] != 0) {
661 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
664 ERROR("write_sensu plugin: Unable to alloc memory");
670 // incorporate the plugin instance if any
671 if (n->plugin_instance[0] != 0) {
672 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
675 ERROR("write_sensu plugin: Unable to alloc memory");
681 // incorporate the plugin type instance if any
682 if (n->type_instance[0] != 0) {
683 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
686 ERROR("write_sensu plugin: Unable to alloc memory");
692 // add key value attributes from config if any
693 for (i = 0; i < sensu_attrs_num; i += 2) {
694 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
697 ERROR("write_sensu plugin: Unable to alloc memory");
703 // incorporate sensu tags from config if any
704 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
705 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
708 ERROR("write_sensu plugin: Unable to alloc memory");
714 // incorporate the service name
715 sensu_format_name2(service_buffer, sizeof(service_buffer),
716 /* host */ "", n->plugin, n->plugin_instance,
717 n->type, n->type_instance, host->separator);
718 // replace sensu event name chars that are considered illegal
719 in_place_replace_sensu_name_reserved(service_buffer);
720 res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
723 ERROR("write_sensu plugin: Unable to alloc memory");
728 // incorporate the check output
729 if (n->message[0] != 0) {
730 char *msg = replace_json_reserved(n->message);
732 ERROR("write_sensu plugin: Unable to alloc memory");
736 res = asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
740 ERROR("write_sensu plugin: Unable to alloc memory");
746 // Pull in values from threshold and add extra attributes
747 for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
748 if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) {
749 res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
752 ERROR("write_sensu plugin: Unable to alloc memory");
757 if (meta->type == NM_TYPE_STRING) {
758 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
761 ERROR("write_sensu plugin: Unable to alloc memory");
768 // close the curly bracket
769 res = asprintf(&temp_str, "%s}\n", ret_str);
772 ERROR("write_sensu plugin: Unable to alloc memory");
777 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
778 "host = \"%s\", service = \"%s\", state = \"%s\"",
779 n->host, service_buffer, severity);
781 } /* }}} char *sensu_notification_to_json */
783 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
788 status = sensu_connect(host);
792 buffer_len = strlen(msg);
794 status = (int) swrite(host->s, msg, buffer_len);
795 sensu_close_socket(host);
799 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
800 (host->node != NULL) ? host->node : SENSU_HOST,
801 (host->service != NULL) ? host->service : SENSU_PORT,
802 sstrerror(errno, errbuf, sizeof(errbuf)));
807 } /* }}} int sensu_send_msg */
810 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
814 status = sensu_send_msg(host, msg);
816 host->flags &= ~F_READY;
817 if (host->res != NULL) {
818 freeaddrinfo(host->res);
825 } /* }}} int sensu_send */
828 static int sensu_write(const data_set_t *ds, /* {{{ */
829 const value_list_t *vl,
833 int statuses[vl->values_len];
834 struct sensu_host *host = ud->data;
835 gauge_t *rates = NULL;
838 pthread_mutex_lock(&host->lock);
839 memset(statuses, 0, vl->values_len * sizeof(*statuses));
841 if (host->store_rates) {
842 rates = uc_get_rate(ds, vl);
844 ERROR("write_sensu plugin: uc_get_rate failed.");
845 pthread_mutex_unlock(&host->lock);
849 for (size_t i = 0; i < vl->values_len; i++) {
850 msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]);
853 pthread_mutex_unlock(&host->lock);
856 status = sensu_send(host, msg);
859 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
860 pthread_mutex_unlock(&host->lock);
866 pthread_mutex_unlock(&host->lock);
868 } /* }}} int sensu_write */
870 static int sensu_notification(const notification_t *n, user_data_t *ud) /* {{{ */
873 struct sensu_host *host = ud->data;
876 pthread_mutex_lock(&host->lock);
878 msg = sensu_notification_to_json(host, n);
880 pthread_mutex_unlock(&host->lock);
884 status = sensu_send(host, msg);
887 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
888 pthread_mutex_unlock(&host->lock);
891 } /* }}} int sensu_notification */
893 static void sensu_free(void *p) /* {{{ */
895 struct sensu_host *host = p;
900 pthread_mutex_lock(&host->lock);
902 host->reference_count--;
903 if (host->reference_count > 0) {
904 pthread_mutex_unlock(&host->lock);
908 sensu_close_socket(host);
909 if (host->res != NULL) {
910 freeaddrinfo(host->res);
913 sfree(host->service);
914 sfree(host->event_service_prefix);
917 sfree(host->separator);
918 free_str_list(&(host->metric_handlers));
919 free_str_list(&(host->notification_handlers));
920 pthread_mutex_destroy(&host->lock);
922 } /* }}} void sensu_free */
925 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
927 struct sensu_host *host = NULL;
929 oconfig_item_t *child;
930 char callback_name[DATA_MAX_NAME_LEN];
933 if ((host = calloc(1, sizeof(*host))) == NULL) {
934 ERROR("write_sensu plugin: calloc failed.");
937 pthread_mutex_init(&host->lock, NULL);
938 host->reference_count = 1;
940 host->service = NULL;
941 host->notifications = 0;
943 host->store_rates = 1;
944 host->always_append_ds = 0;
945 host->metric_handlers.nb_strs = 0;
946 host->metric_handlers.strs = NULL;
947 host->notification_handlers.nb_strs = 0;
948 host->notification_handlers.strs = NULL;
949 host->separator = strdup("/");
950 if (host->separator == NULL) {
951 ERROR("write_sensu plugin: Unable to alloc memory");
956 status = cf_util_get_string(ci, &host->name);
958 WARNING("write_sensu plugin: Required host name is missing.");
963 for (int i = 0; i < ci->children_num; i++) {
964 child = &ci->children[i];
967 if (strcasecmp("Host", child->key) == 0) {
968 status = cf_util_get_string(child, &host->node);
971 } else if (strcasecmp("Notifications", child->key) == 0) {
972 status = cf_util_get_boolean(child, &host->notifications);
975 } else if (strcasecmp("Metrics", child->key) == 0) {
976 status = cf_util_get_boolean(child, &host->metrics);
979 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
980 status = cf_util_get_string(child, &host->event_service_prefix);
983 } else if (strcasecmp("Separator", child->key) == 0) {
984 status = cf_util_get_string(child, &host->separator);
987 } else if (strcasecmp("MetricHandler", child->key) == 0) {
988 char *temp_str = NULL;
989 status = cf_util_get_string(child, &temp_str);
992 status = add_str_to_list(&(host->metric_handlers), temp_str);
996 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
997 char *temp_str = NULL;
998 status = cf_util_get_string(child, &temp_str);
1001 status = add_str_to_list(&(host->notification_handlers), temp_str);
1005 } else if (strcasecmp("Port", child->key) == 0) {
1006 status = cf_util_get_service(child, &host->service);
1008 ERROR("write_sensu plugin: Invalid argument "
1009 "configured for the \"Port\" "
1013 } else if (strcasecmp("StoreRates", child->key) == 0) {
1014 status = cf_util_get_boolean(child, &host->store_rates);
1017 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1018 status = cf_util_get_boolean(child,
1019 &host->always_append_ds);
1023 WARNING("write_sensu plugin: ignoring unknown config "
1024 "option: \"%s\"", child->key);
1032 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1034 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. Giving up.");
1038 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1040 WARNING("write_sensu plugin: notifications enabled but no NotificationHandler defined. Giving up.");
1044 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1045 WARNING("write_sensu plugin: NotificationHandler given so forcing notifications to be enabled");
1046 host->notifications = 1;
1049 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1050 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be enabled");
1054 if (!(host->notifications || host->metrics)) {
1055 WARNING("write_sensu plugin: neither metrics nor notifications enabled. Giving up.");
1060 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1062 ud.free_func = sensu_free;
1064 pthread_mutex_lock(&host->lock);
1066 if (host->metrics) {
1067 status = plugin_register_write(callback_name, sensu_write, &ud);
1069 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1070 "failed with status %i.",
1071 callback_name, status);
1073 host->reference_count++;
1076 if (host->notifications) {
1077 status = plugin_register_notification(callback_name, sensu_notification, &ud);
1079 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1080 "failed with status %i.",
1081 callback_name, status);
1083 host->reference_count++;
1086 if (host->reference_count <= 1) {
1087 /* Both callbacks failed => free memory.
1088 * We need to unlock here, because sensu_free() will lock.
1089 * This is not a race condition, because we're the only one
1090 * holding a reference. */
1091 pthread_mutex_unlock(&host->lock);
1096 host->reference_count--;
1097 pthread_mutex_unlock(&host->lock);
1100 } /* }}} int sensu_config_node */
1102 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1104 oconfig_item_t *child;
1106 struct str_list sensu_tags_arr;
1108 sensu_tags_arr.nb_strs = 0;
1109 sensu_tags_arr.strs = NULL;
1111 for (int i = 0; i < ci->children_num; i++) {
1112 child = &ci->children[i];
1114 if (strcasecmp("Node", child->key) == 0) {
1115 sensu_config_node(child);
1116 } else if (strcasecmp(child->key, "attribute") == 0) {
1117 if (child->values_num != 2) {
1118 WARNING("sensu attributes need both a key and a value.");
1121 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1122 child->values[1].type != OCONFIG_TYPE_STRING) {
1123 WARNING("sensu attribute needs string arguments.");
1127 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string);
1128 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string);
1130 DEBUG("write_sensu plugin: New attribute: %s => %s",
1131 child->values[0].value.string,
1132 child->values[1].value.string);
1133 } else if (strcasecmp(child->key, "tag") == 0) {
1135 status = cf_util_get_string(child, &tmp);
1139 status = add_str_to_list(&sensu_tags_arr, tmp);
1143 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1145 WARNING("write_sensu plugin: Ignoring unknown "
1146 "configuration option \"%s\" at top level.",
1150 if (sensu_tags_arr.nb_strs > 0) {
1152 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1153 free_str_list(&sensu_tags_arr);
1154 if (sensu_tags == NULL) {
1155 ERROR("write_sensu plugin: Unable to alloc memory");
1160 } /* }}} int sensu_config */
1162 void module_register(void)
1164 plugin_register_complex_config("write_sensu", sensu_config);
1167 /* vim: set sw=8 sts=8 ts=8 noet : */