2 * collectd - src/write_sensu.c
3 * Copyright (C) 2015 Fabrice A. Marie
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Fabrice A. Marie <fabrice at kibinlabs.com>
30 #include "configfile.h"
31 #include "utils_cache.h"
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
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;
119 hints.ai_flags |= AI_ADDRCONFIG;
122 node = (host->node != NULL) ? host->node : SENSU_HOST;
123 service = (host->service != NULL) ? host->service : SENSU_PORT;
125 if ((e = getaddrinfo(node, service, &hints, &(host->res))) != 0) {
126 ERROR("write_sensu plugin: Unable to resolve host \"%s\": %s",
127 node, gai_strerror(e));
130 DEBUG("write_sensu plugin: successfully resolved host/port: %s/%s",
132 host->flags |= F_READY;
135 struct linger so_linger;
137 for (ai = host->res; ai != NULL; ai = ai->ai_next) {
139 if ((host->s = socket(ai->ai_family,
141 ai->ai_protocol)) == -1) {
145 // Set very low close() lingering
146 so_linger.l_onoff = 1;
147 so_linger.l_linger = 3;
148 if (setsockopt(host->s, SOL_SOCKET, SO_LINGER, &so_linger, sizeof so_linger) != 0)
149 WARNING("write_sensu plugin: failed to set socket close() lingering");
151 // connect the socket
152 if (connect(host->s, ai->ai_addr, ai->ai_addrlen) != 0) {
157 DEBUG("write_sensu plugin: connected");
162 WARNING("write_sensu plugin: Unable to connect to sensu client");
166 } /* }}} int sensu_connect */
168 static void sensu_close_socket(struct sensu_host *host) /* {{{ */
174 } /* }}} void sensu_close_socket */
176 static char *build_json_str_list(const char *tag, struct str_list const *list) /* {{{ */
182 if (list->nb_strs == 0) {
183 ret_str = malloc(sizeof(char));
184 if (ret_str == NULL) {
185 ERROR("write_sensu plugin: Unable to alloc memory");
191 res = asprintf(&temp_str, "\"%s\": [\"%s\"", tag, list->strs[0]);
193 ERROR("write_sensu plugin: Unable to alloc memory");
196 for (i=1; i<list->nb_strs; i++) {
197 res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
200 ERROR("write_sensu plugin: Unable to alloc memory");
205 res = asprintf(&ret_str, "%s]", temp_str);
208 ERROR("write_sensu plugin: Unable to alloc memory");
213 } /* }}} char *build_json_str_list*/
215 static int sensu_format_name2(char *ret, int ret_len,
216 const char *hostname,
217 const char *plugin, const char *plugin_instance,
218 const char *type, const char *type_instance,
219 const char *separator)
225 buffer_size = (size_t) ret_len;
227 #define APPEND(str) do { \
228 size_t l = strlen (str); \
229 if (l >= buffer_size) \
231 memcpy (buffer, (str), l); \
232 buffer += l; buffer_size -= l; \
235 assert (plugin != NULL);
236 assert (type != NULL);
241 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
244 APPEND (plugin_instance);
248 if ((type_instance != NULL) && (type_instance[0] != 0))
251 APPEND (type_instance);
253 assert (buffer_size > 0);
258 } /* int sensu_format_name2 */
260 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
263 int len=strlen(orig_name);
264 for (i=0; i<len; i++) {
265 // some plugins like ipmi generate special characters in metric name
266 switch(orig_name[i]) {
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;
272 case '+': orig_name[i] = '_'; break;
275 } /* }}} char *replace_sensu_name_reserved */
277 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
278 data_set_t const *ds,
279 value_list_t const *vl, size_t index,
280 gauge_t const *rates,
283 char name_buffer[5 * DATA_MAX_NAME_LEN];
284 char service_buffer[6 * DATA_MAX_NAME_LEN];
290 // First part of the JSON string
291 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
293 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
294 if (handlers_str == NULL) {
295 ERROR("write_sensu plugin: Unable to alloc memory");
299 // incorporate the handlers
300 if (strlen(handlers_str) == 0) {
302 ret_str = strdup(part1);
303 if (ret_str == NULL) {
304 ERROR("write_sensu plugin: Unable to alloc memory");
309 res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
312 ERROR("write_sensu plugin: Unable to alloc memory");
317 // incorporate the plugin name information
318 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
321 ERROR("write_sensu plugin: Unable to alloc memory");
326 // incorporate the plugin type
327 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
330 ERROR("write_sensu plugin: Unable to alloc memory");
335 // incorporate the plugin instance if any
336 if (vl->plugin_instance[0] != 0) {
337 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
340 ERROR("write_sensu plugin: Unable to alloc memory");
346 // incorporate the plugin type instance if any
347 if (vl->type_instance[0] != 0) {
348 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
351 ERROR("write_sensu plugin: Unable to alloc memory");
357 // incorporate the data source type
358 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
359 char ds_type[DATA_MAX_NAME_LEN];
360 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
361 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
364 ERROR("write_sensu plugin: Unable to alloc memory");
369 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
372 ERROR("write_sensu plugin: Unable to alloc memory");
378 // incorporate the data source name
379 res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
382 ERROR("write_sensu plugin: Unable to alloc memory");
387 // incorporate the data source index
389 char ds_index[DATA_MAX_NAME_LEN];
390 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
391 res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
394 ERROR("write_sensu plugin: Unable to alloc memory");
400 // add key value attributes from config if any
401 for (i = 0; i < sensu_attrs_num; i += 2) {
402 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
405 ERROR("write_sensu plugin: Unable to alloc memory");
411 // incorporate sensu tags from config if any
412 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
413 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
416 ERROR("write_sensu plugin: Unable to alloc memory");
422 // calculate the value and set to a string
423 if (ds->ds[index].type == DS_TYPE_GAUGE) {
424 res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
427 ERROR("write_sensu plugin: Unable to alloc memory");
430 } else if (rates != NULL) {
431 res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
434 ERROR("write_sensu plugin: Unable to alloc memory");
438 if (ds->ds[index].type == DS_TYPE_DERIVE) {
439 res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
442 ERROR("write_sensu plugin: Unable to alloc memory");
446 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
447 res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
450 ERROR("write_sensu plugin: Unable to alloc memory");
455 res = asprintf(&value_str, "%llu", vl->values[index].counter);
458 ERROR("write_sensu plugin: Unable to alloc memory");
464 // Generate the full service name
465 sensu_format_name2(name_buffer, sizeof(name_buffer),
466 vl->host, vl->plugin, vl->plugin_instance,
467 vl->type, vl->type_instance, host->separator);
468 if (host->always_append_ds || (ds->ds_num > 1)) {
469 if (host->event_service_prefix == NULL)
470 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
471 name_buffer, ds->ds[index].name);
473 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
474 host->event_service_prefix, name_buffer, ds->ds[index].name);
476 if (host->event_service_prefix == NULL)
477 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
479 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
480 host->event_service_prefix, name_buffer);
483 // Replace collectd sensor name reserved characters so that time series DB is happy
484 in_place_replace_sensu_name_reserved(service_buffer);
486 // finalize the buffer by setting the output and closing curly bracket
487 res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
491 ERROR("write_sensu plugin: Unable to alloc memory");
496 DEBUG("write_sensu plugin: Successfully created json for metric: "
497 "host = \"%s\", service = \"%s\"",
498 vl->host, service_buffer);
500 } /* }}} char *sensu_value_to_json */
503 * Uses replace_str2() implementation from
504 * http://creativeandcritical.net/str-replace-c/
505 * copyright (c) Laird Shaw, under public domain.
507 static char *replace_str(const char *str, const char *old, /* {{{ */
512 size_t oldlen = strlen(old);
513 size_t count = strlen(new);
515 size_t newlen = count;
516 int samesize = (oldlen == newlen);
519 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
521 /* This is undefined if p - str > PTRDIFF_MAX */
522 retlen = p - str + strlen(p) + count * (newlen - oldlen);
524 retlen = strlen(str);
526 ret = malloc(retlen + 1);
529 // added to original: not optimized, but keeps valgrind happy.
530 memset(ret, 0, retlen + 1);
535 /* If the old and new strings are different lengths - in other
536 * words we have already iterated through with strstr above,
537 * and thus we know how many times we need to call it - then we
538 * can avoid the final (potentially lengthy) call to strstr,
539 * which we already know is going to return NULL, by
540 * decrementing and checking count.
542 if (!samesize && !count--)
544 /* Otherwise i.e. when the old and new strings are the same
545 * length, and we don't know how many times to call strstr,
546 * we must check for a NULL return here (we check it in any
547 * event, to avoid further conditions, and because there's
548 * no harm done with the check even when the old and new
549 * strings are different lengths).
551 if ((q = strstr(p, old)) == NULL)
553 /* This is undefined if q - p > PTRDIFF_MAX */
557 memcpy(r, new, newlen);
561 strncpy(r, p, strlen(p));
564 } /* }}} char *replace_str */
566 static char *replace_json_reserved(const char *message) /* {{{ */
568 char *msg = replace_str(message, "\\", "\\\\");
570 ERROR("write_sensu plugin: Unable to alloc memory");
573 char *tmp = replace_str(msg, "\"", "\\\"");
576 ERROR("write_sensu plugin: Unable to alloc memory");
579 msg = replace_str(tmp, "\n", "\\\n");
582 ERROR("write_sensu plugin: Unable to alloc memory");
586 } /* }}} char *replace_json_reserved */
588 static char *sensu_notification_to_json(struct sensu_host *host, /* {{{ */
589 notification_t const *n)
591 char service_buffer[6 * DATA_MAX_NAME_LEN];
592 char const *severity;
593 notification_meta_t *meta;
599 // add the severity/status
600 switch (n->severity) {
606 severity = "WARNING";
610 severity = "CRITICAL";
614 severity = "UNKNOWN";
617 res = asprintf(&temp_str, "{\"status\": %d", status);
619 ERROR("write_sensu plugin: Unable to alloc memory");
624 // incorporate the timestamp
625 res = asprintf(&temp_str, "%s, \"timestamp\": %ld", ret_str, CDTIME_T_TO_TIME_T(n->time));
628 ERROR("write_sensu plugin: Unable to alloc memory");
633 char *handlers_str = build_json_str_list("handlers", &(host->notification_handlers));
634 if (handlers_str == NULL) {
635 ERROR("write_sensu plugin: Unable to alloc memory");
639 // incorporate the handlers
640 if (strlen(handlers_str) != 0) {
641 res = asprintf(&temp_str, "%s, %s", ret_str, handlers_str);
645 ERROR("write_sensu plugin: Unable to alloc memory");
653 // incorporate the plugin name information if any
654 if (n->plugin[0] != 0) {
655 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, n->plugin);
658 ERROR("write_sensu plugin: Unable to alloc memory");
664 // incorporate the plugin type if any
665 if (n->type[0] != 0) {
666 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, n->type);
669 ERROR("write_sensu plugin: Unable to alloc memory");
675 // incorporate the plugin instance if any
676 if (n->plugin_instance[0] != 0) {
677 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, n->plugin_instance);
680 ERROR("write_sensu plugin: Unable to alloc memory");
686 // incorporate the plugin type instance if any
687 if (n->type_instance[0] != 0) {
688 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, n->type_instance);
691 ERROR("write_sensu plugin: Unable to alloc memory");
697 // add key value attributes from config if any
698 for (i = 0; i < sensu_attrs_num; i += 2) {
699 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
702 ERROR("write_sensu plugin: Unable to alloc memory");
708 // incorporate sensu tags from config if any
709 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
710 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
713 ERROR("write_sensu plugin: Unable to alloc memory");
719 // incorporate the service name
720 sensu_format_name2(service_buffer, sizeof(service_buffer),
721 /* host */ "", n->plugin, n->plugin_instance,
722 n->type, n->type_instance, host->separator);
723 // replace sensu event name chars that are considered illegal
724 in_place_replace_sensu_name_reserved(service_buffer);
725 res = asprintf(&temp_str, "%s, \"name\": \"%s\"", ret_str, &service_buffer[1]);
728 ERROR("write_sensu plugin: Unable to alloc memory");
733 // incorporate the check output
734 if (n->message[0] != 0) {
735 char *msg = replace_json_reserved(n->message);
737 ERROR("write_sensu plugin: Unable to alloc memory");
741 res = asprintf(&temp_str, "%s, \"output\": \"%s - %s\"", ret_str, severity, msg);
745 ERROR("write_sensu plugin: Unable to alloc memory");
751 // Pull in values from threshold and add extra attributes
752 for (meta = n->meta; meta != NULL; meta = meta->next) {
753 if (strcasecmp("CurrentValue", meta->name) == 0 && meta->type == NM_TYPE_DOUBLE) {
754 res = asprintf(&temp_str, "%s, \"current_value\": \"%.8f\"", ret_str, meta->nm_value.nm_double);
757 ERROR("write_sensu plugin: Unable to alloc memory");
762 if (meta->type == NM_TYPE_STRING) {
763 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, meta->name, meta->nm_value.nm_string);
766 ERROR("write_sensu plugin: Unable to alloc memory");
773 // close the curly bracket
774 res = asprintf(&temp_str, "%s}\n", ret_str);
777 ERROR("write_sensu plugin: Unable to alloc memory");
782 DEBUG("write_sensu plugin: Successfully created JSON for notification: "
783 "host = \"%s\", service = \"%s\", state = \"%s\"",
784 n->host, service_buffer, severity);
786 } /* }}} char *sensu_notification_to_json */
788 static int sensu_send_msg(struct sensu_host *host, const char *msg) /* {{{ */
793 status = sensu_connect(host);
797 buffer_len = strlen(msg);
799 status = (int) swrite(host->s, msg, buffer_len);
800 sensu_close_socket(host);
804 ERROR("write_sensu plugin: Sending to Sensu at %s:%s failed: %s",
805 (host->node != NULL) ? host->node : SENSU_HOST,
806 (host->service != NULL) ? host->service : SENSU_PORT,
807 sstrerror(errno, errbuf, sizeof(errbuf)));
812 } /* }}} int sensu_send_msg */
815 static int sensu_send(struct sensu_host *host, char const *msg) /* {{{ */
819 status = sensu_send_msg(host, msg);
821 host->flags &= ~F_READY;
822 if (host->res != NULL) {
823 freeaddrinfo(host->res);
830 } /* }}} int sensu_send */
833 static int sensu_write(const data_set_t *ds, /* {{{ */
834 const value_list_t *vl,
838 int statuses[vl->values_len];
839 struct sensu_host *host = ud->data;
840 gauge_t *rates = NULL;
844 pthread_mutex_lock(&host->lock);
845 memset(statuses, 0, vl->values_len * sizeof(*statuses));
847 if (host->store_rates) {
848 rates = uc_get_rate(ds, vl);
850 ERROR("write_sensu plugin: uc_get_rate failed.");
851 pthread_mutex_unlock(&host->lock);
855 for (i = 0; i < (size_t) vl->values_len; i++) {
856 msg = sensu_value_to_json(host, ds, vl, (int) i, rates, statuses[i]);
859 pthread_mutex_unlock(&host->lock);
862 status = sensu_send(host, msg);
865 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
866 pthread_mutex_unlock(&host->lock);
872 pthread_mutex_unlock(&host->lock);
874 } /* }}} int sensu_write */
876 static int sensu_notification(const notification_t *n, user_data_t *ud) /* {{{ */
879 struct sensu_host *host = ud->data;
882 pthread_mutex_lock(&host->lock);
884 msg = sensu_notification_to_json(host, n);
886 pthread_mutex_unlock(&host->lock);
890 status = sensu_send(host, msg);
893 ERROR("write_sensu plugin: sensu_send failed with status %i", status);
894 pthread_mutex_unlock(&host->lock);
897 } /* }}} int sensu_notification */
899 static void sensu_free(void *p) /* {{{ */
901 struct sensu_host *host = p;
906 pthread_mutex_lock(&host->lock);
908 host->reference_count--;
909 if (host->reference_count > 0) {
910 pthread_mutex_unlock(&host->lock);
914 sensu_close_socket(host);
915 if (host->res != NULL) {
916 freeaddrinfo(host->res);
919 sfree(host->service);
920 sfree(host->event_service_prefix);
923 sfree(host->separator);
924 free_str_list(&(host->metric_handlers));
925 free_str_list(&(host->notification_handlers));
926 pthread_mutex_destroy(&host->lock);
928 } /* }}} void sensu_free */
931 static int sensu_config_node(oconfig_item_t *ci) /* {{{ */
933 struct sensu_host *host = NULL;
936 oconfig_item_t *child;
937 char callback_name[DATA_MAX_NAME_LEN];
940 if ((host = calloc(1, sizeof(*host))) == NULL) {
941 ERROR("write_sensu plugin: calloc failed.");
944 pthread_mutex_init(&host->lock, NULL);
945 host->reference_count = 1;
947 host->service = NULL;
948 host->notifications = 0;
950 host->store_rates = 1;
951 host->always_append_ds = 0;
952 host->metric_handlers.nb_strs = 0;
953 host->metric_handlers.strs = NULL;
954 host->notification_handlers.nb_strs = 0;
955 host->notification_handlers.strs = NULL;
956 host->separator = strdup("/");
957 if (host->separator == NULL) {
958 ERROR("write_sensu plugin: Unable to alloc memory");
963 status = cf_util_get_string(ci, &host->name);
965 WARNING("write_sensu plugin: Required host name is missing.");
970 for (i = 0; i < ci->children_num; i++) {
971 child = &ci->children[i];
974 if (strcasecmp("Host", child->key) == 0) {
975 status = cf_util_get_string(child, &host->node);
978 } else if (strcasecmp("Notifications", child->key) == 0) {
979 status = cf_util_get_boolean(child, &host->notifications);
982 } else if (strcasecmp("Metrics", child->key) == 0) {
983 status = cf_util_get_boolean(child, &host->metrics);
986 } else if (strcasecmp("EventServicePrefix", child->key) == 0) {
987 status = cf_util_get_string(child, &host->event_service_prefix);
990 } else if (strcasecmp("Separator", child->key) == 0) {
991 status = cf_util_get_string(child, &host->separator);
994 } else if (strcasecmp("MetricHandler", child->key) == 0) {
995 char *temp_str = NULL;
996 status = cf_util_get_string(child, &temp_str);
999 status = add_str_to_list(&(host->metric_handlers), temp_str);
1003 } else if (strcasecmp("NotificationHandler", child->key) == 0) {
1004 char *temp_str = NULL;
1005 status = cf_util_get_string(child, &temp_str);
1008 status = add_str_to_list(&(host->notification_handlers), temp_str);
1012 } else if (strcasecmp("Port", child->key) == 0) {
1013 status = cf_util_get_service(child, &host->service);
1015 ERROR("write_sensu plugin: Invalid argument "
1016 "configured for the \"Port\" "
1020 } else if (strcasecmp("StoreRates", child->key) == 0) {
1021 status = cf_util_get_boolean(child, &host->store_rates);
1024 } else if (strcasecmp("AlwaysAppendDS", child->key) == 0) {
1025 status = cf_util_get_boolean(child,
1026 &host->always_append_ds);
1030 WARNING("write_sensu plugin: ignoring unknown config "
1031 "option: \"%s\"", child->key);
1039 if (host->metrics && (host->metric_handlers.nb_strs == 0)) {
1041 WARNING("write_sensu plugin: metrics enabled but no MetricHandler defined. Giving up.");
1045 if (host->notifications && (host->notification_handlers.nb_strs == 0)) {
1047 WARNING("write_sensu plugin: notifications enabled but no NotificationHandler defined. Giving up.");
1051 if ((host->notification_handlers.nb_strs > 0) && (host->notifications == 0)) {
1052 WARNING("write_sensu plugin: NotificationHandler given so forcing notifications to be enabled");
1053 host->notifications = 1;
1056 if ((host->metric_handlers.nb_strs > 0) && (host->metrics == 0)) {
1057 WARNING("write_sensu plugin: MetricHandler given so forcing metrics to be enabled");
1061 if (!(host->notifications || host->metrics)) {
1062 WARNING("write_sensu plugin: neither metrics nor notifications enabled. Giving up.");
1067 ssnprintf(callback_name, sizeof(callback_name), "write_sensu/%s", host->name);
1069 ud.free_func = sensu_free;
1071 pthread_mutex_lock(&host->lock);
1073 if (host->metrics) {
1074 status = plugin_register_write(callback_name, sensu_write, &ud);
1076 WARNING("write_sensu plugin: plugin_register_write (\"%s\") "
1077 "failed with status %i.",
1078 callback_name, status);
1080 host->reference_count++;
1083 if (host->notifications) {
1084 status = plugin_register_notification(callback_name, sensu_notification, &ud);
1086 WARNING("write_sensu plugin: plugin_register_notification (\"%s\") "
1087 "failed with status %i.",
1088 callback_name, status);
1090 host->reference_count++;
1093 if (host->reference_count <= 1) {
1094 /* Both callbacks failed => free memory.
1095 * We need to unlock here, because sensu_free() will lock.
1096 * This is not a race condition, because we're the only one
1097 * holding a reference. */
1098 pthread_mutex_unlock(&host->lock);
1103 host->reference_count--;
1104 pthread_mutex_unlock(&host->lock);
1107 } /* }}} int sensu_config_node */
1109 static int sensu_config(oconfig_item_t *ci) /* {{{ */
1112 oconfig_item_t *child;
1114 struct str_list sensu_tags_arr;
1116 sensu_tags_arr.nb_strs = 0;
1117 sensu_tags_arr.strs = NULL;
1119 for (i = 0; i < ci->children_num; i++) {
1120 child = &ci->children[i];
1122 if (strcasecmp("Node", child->key) == 0) {
1123 sensu_config_node(child);
1124 } else if (strcasecmp(child->key, "attribute") == 0) {
1125 if (child->values_num != 2) {
1126 WARNING("sensu attributes need both a key and a value.");
1129 if (child->values[0].type != OCONFIG_TYPE_STRING ||
1130 child->values[1].type != OCONFIG_TYPE_STRING) {
1131 WARNING("sensu attribute needs string arguments.");
1135 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[0].value.string);
1136 strarray_add(&sensu_attrs, &sensu_attrs_num, child->values[1].value.string);
1138 DEBUG("write_sensu plugin: New attribute: %s => %s",
1139 child->values[0].value.string,
1140 child->values[1].value.string);
1141 } else if (strcasecmp(child->key, "tag") == 0) {
1143 status = cf_util_get_string(child, &tmp);
1147 status = add_str_to_list(&sensu_tags_arr, tmp);
1151 DEBUG("write_sensu plugin: Got tag: %s", tmp);
1153 WARNING("write_sensu plugin: Ignoring unknown "
1154 "configuration option \"%s\" at top level.",
1158 if (sensu_tags_arr.nb_strs > 0) {
1160 sensu_tags = build_json_str_list("tags", &sensu_tags_arr);
1161 free_str_list(&sensu_tags_arr);
1162 if (sensu_tags == NULL) {
1163 ERROR("write_sensu plugin: Unable to alloc memory");
1168 } /* }}} int sensu_config */
1170 void module_register(void)
1172 plugin_register_complex_config("write_sensu", sensu_config);
1175 /* vim: set sw=8 sts=8 ts=8 noet : */