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;
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) /* {{{ */
179 char *ret_str = NULL;
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");
197 for (i=1; i<list->nb_strs; i++) {
198 res = asprintf(&ret_str, "%s, \"%s\"", temp_str, list->strs[i]);
201 ERROR("write_sensu plugin: Unable to alloc memory");
206 res = asprintf(&ret_str, "%s]", temp_str);
209 ERROR("write_sensu plugin: Unable to alloc memory");
214 } /* }}} char *build_json_str_list*/
216 static int sensu_format_name2(char *ret, int ret_len,
217 const char *hostname,
218 const char *plugin, const char *plugin_instance,
219 const char *type, const char *type_instance,
220 const char *separator)
226 buffer_size = (size_t) ret_len;
228 #define APPEND(str) do { \
229 size_t l = strlen (str); \
230 if (l >= buffer_size) \
232 memcpy (buffer, (str), l); \
233 buffer += l; buffer_size -= l; \
236 assert (plugin != NULL);
237 assert (type != NULL);
242 if ((plugin_instance != NULL) && (plugin_instance[0] != 0))
245 APPEND (plugin_instance);
249 if ((type_instance != NULL) && (type_instance[0] != 0))
252 APPEND (type_instance);
254 assert (buffer_size > 0);
259 } /* int sensu_format_name2 */
261 static void in_place_replace_sensu_name_reserved(char *orig_name) /* {{{ */
264 int len=strlen(orig_name);
265 for (i=0; i<len; i++) {
266 // some plugins like ipmi generate special characters in metric name
267 switch(orig_name[i]) {
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;
273 case '+': orig_name[i] = '_'; break;
276 } /* }}} char *replace_sensu_name_reserved */
278 static char *sensu_value_to_json(struct sensu_host const *host, /* {{{ */
279 data_set_t const *ds,
280 value_list_t const *vl, size_t index,
281 gauge_t const *rates,
284 char name_buffer[5 * DATA_MAX_NAME_LEN];
285 char service_buffer[6 * DATA_MAX_NAME_LEN];
291 // First part of the JSON string
292 const char *part1 = "{\"name\": \"collectd\", \"type\": \"metric\"";
294 char *handlers_str = build_json_str_list("handlers", &(host->metric_handlers));
295 if (handlers_str == NULL) {
296 ERROR("write_sensu plugin: Unable to alloc memory");
300 // incorporate the handlers
301 if (strlen(handlers_str) == 0) {
303 ret_str = strdup(part1);
304 if (ret_str == NULL) {
305 ERROR("write_sensu plugin: Unable to alloc memory");
310 res = asprintf(&ret_str, "%s, %s", part1, handlers_str);
313 ERROR("write_sensu plugin: Unable to alloc memory");
318 // incorporate the plugin name information
319 res = asprintf(&temp_str, "%s, \"collectd_plugin\": \"%s\"", ret_str, vl->plugin);
322 ERROR("write_sensu plugin: Unable to alloc memory");
327 // incorporate the plugin type
328 res = asprintf(&temp_str, "%s, \"collectd_plugin_type\": \"%s\"", ret_str, vl->type);
331 ERROR("write_sensu plugin: Unable to alloc memory");
336 // incorporate the plugin instance if any
337 if (vl->plugin_instance[0] != 0) {
338 res = asprintf(&temp_str, "%s, \"collectd_plugin_instance\": \"%s\"", ret_str, vl->plugin_instance);
341 ERROR("write_sensu plugin: Unable to alloc memory");
347 // incorporate the plugin type instance if any
348 if (vl->type_instance[0] != 0) {
349 res = asprintf(&temp_str, "%s, \"collectd_plugin_type_instance\": \"%s\"", ret_str, vl->type_instance);
352 ERROR("write_sensu plugin: Unable to alloc memory");
358 // incorporate the data source type
359 if ((ds->ds[index].type != DS_TYPE_GAUGE) && (rates != NULL)) {
360 char ds_type[DATA_MAX_NAME_LEN];
361 ssnprintf (ds_type, sizeof (ds_type), "%s:rate", DS_TYPE_TO_STRING(ds->ds[index].type));
362 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, ds_type);
365 ERROR("write_sensu plugin: Unable to alloc memory");
370 res = asprintf(&temp_str, "%s, \"collectd_data_source_type\": \"%s\"", ret_str, DS_TYPE_TO_STRING(ds->ds[index].type));
373 ERROR("write_sensu plugin: Unable to alloc memory");
379 // incorporate the data source name
380 res = asprintf(&temp_str, "%s, \"collectd_data_source_name\": \"%s\"", ret_str, ds->ds[index].name);
383 ERROR("write_sensu plugin: Unable to alloc memory");
388 // incorporate the data source index
390 char ds_index[DATA_MAX_NAME_LEN];
391 ssnprintf (ds_index, sizeof (ds_index), "%zu", index);
392 res = asprintf(&temp_str, "%s, \"collectd_data_source_index\": %s", ret_str, ds_index);
395 ERROR("write_sensu plugin: Unable to alloc memory");
401 // add key value attributes from config if any
402 for (i = 0; i < sensu_attrs_num; i += 2) {
403 res = asprintf(&temp_str, "%s, \"%s\": \"%s\"", ret_str, sensu_attrs[i], sensu_attrs[i+1]);
406 ERROR("write_sensu plugin: Unable to alloc memory");
412 // incorporate sensu tags from config if any
413 if ((sensu_tags != NULL) && (strlen(sensu_tags) != 0)) {
414 res = asprintf(&temp_str, "%s, %s", ret_str, sensu_tags);
417 ERROR("write_sensu plugin: Unable to alloc memory");
423 // calculate the value and set to a string
424 if (ds->ds[index].type == DS_TYPE_GAUGE) {
425 res = asprintf(&value_str, GAUGE_FORMAT, vl->values[index].gauge);
428 ERROR("write_sensu plugin: Unable to alloc memory");
431 } else if (rates != NULL) {
432 res = asprintf(&value_str, GAUGE_FORMAT, rates[index]);
435 ERROR("write_sensu plugin: Unable to alloc memory");
439 if (ds->ds[index].type == DS_TYPE_DERIVE) {
440 res = asprintf(&value_str, "%"PRIi64, vl->values[index].derive);
443 ERROR("write_sensu plugin: Unable to alloc memory");
447 else if (ds->ds[index].type == DS_TYPE_ABSOLUTE) {
448 res = asprintf(&value_str, "%"PRIu64, vl->values[index].absolute);
451 ERROR("write_sensu plugin: Unable to alloc memory");
456 res = asprintf(&value_str, "%llu", vl->values[index].counter);
459 ERROR("write_sensu plugin: Unable to alloc memory");
465 // Generate the full service name
466 sensu_format_name2(name_buffer, sizeof(name_buffer),
467 vl->host, vl->plugin, vl->plugin_instance,
468 vl->type, vl->type_instance, host->separator);
469 if (host->always_append_ds || (ds->ds_num > 1)) {
470 if (host->event_service_prefix == NULL)
471 ssnprintf(service_buffer, sizeof(service_buffer), "%s.%s",
472 name_buffer, ds->ds[index].name);
474 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s.%s",
475 host->event_service_prefix, name_buffer, ds->ds[index].name);
477 if (host->event_service_prefix == NULL)
478 sstrncpy(service_buffer, name_buffer, sizeof(service_buffer));
480 ssnprintf(service_buffer, sizeof(service_buffer), "%s%s",
481 host->event_service_prefix, name_buffer);
484 // Replace collectd sensor name reserved characters so that time series DB is happy
485 in_place_replace_sensu_name_reserved(service_buffer);
487 // finalize the buffer by setting the output and closing curly bracket
488 res = asprintf(&temp_str, "%s, \"output\": \"%s %s %ld\"}\n", ret_str, service_buffer, value_str, CDTIME_T_TO_TIME_T(vl->time));
492 ERROR("write_sensu plugin: Unable to alloc memory");
497 DEBUG("write_sensu plugin: Successfully created json for metric: "
498 "host = \"%s\", service = \"%s\"",
499 vl->host, service_buffer);
501 } /* }}} char *sensu_value_to_json */
504 * Uses replace_str2() implementation from
505 * http://creativeandcritical.net/str-replace-c/
506 * copyright (c) Laird Shaw, under public domain.
508 static char *replace_str(const char *str, const char *old, /* {{{ */
513 size_t oldlen = strlen(old);
514 size_t count = strlen(new);
516 size_t newlen = count;
517 int samesize = (oldlen == newlen);
520 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
522 /* This is undefined if p - str > PTRDIFF_MAX */
523 retlen = p - str + strlen(p) + count * (newlen - oldlen);
525 retlen = strlen(str);
527 ret = calloc(1, retlen + 1);
530 // added to original: not optimized, but keeps valgrind happy.
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 < 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 : */