X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Ftarget_notification.c;h=f83a904ac07f6f73d9d7530e2d01e3dfa3c07178;hp=21c071ed422f36d625f44366ace457add857f46e;hb=7111bb6df7628edce3a8e538b386fbe27633a191;hpb=f0d468892e1c173b226f739be48291981473b4fb diff --git a/src/target_notification.c b/src/target_notification.c index 21c071ed..f83a904a 100644 --- a/src/target_notification.c +++ b/src/target_notification.c @@ -31,124 +31,116 @@ #include "utils_cache.h" #include "utils_subst.h" -struct tn_data_s -{ +struct tn_data_s { int severity; char *message; }; typedef struct tn_data_s tn_data_t; -static int tn_config_add_severity (tn_data_t *data, /* {{{ */ - const oconfig_item_t *ci) -{ - if ((ci->values_num != 1) - || (ci->values[0].type != OCONFIG_TYPE_STRING)) - { - ERROR ("Target `notification': The `%s' option requires exactly one string " - "argument.", ci->key); - return (-1); +static int tn_config_add_severity(tn_data_t *data, /* {{{ */ + const oconfig_item_t *ci) { + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { + ERROR("Target `notification': The `%s' option requires exactly one string " + "argument.", + ci->key); + return -1; } - if ((strcasecmp ("FAILURE", ci->values[0].value.string) == 0) - || (strcasecmp ("CRITICAL", ci->values[0].value.string) == 0)) + if ((strcasecmp("FAILURE", ci->values[0].value.string) == 0) || + (strcasecmp("CRITICAL", ci->values[0].value.string) == 0)) data->severity = NOTIF_FAILURE; - else if ((strcasecmp ("WARNING", ci->values[0].value.string) == 0) - || (strcasecmp ("WARN", ci->values[0].value.string) == 0)) + else if ((strcasecmp("WARNING", ci->values[0].value.string) == 0) || + (strcasecmp("WARN", ci->values[0].value.string) == 0)) data->severity = NOTIF_WARNING; - else if (strcasecmp ("OKAY", ci->values[0].value.string) == 0) + else if (strcasecmp("OKAY", ci->values[0].value.string) == 0) data->severity = NOTIF_OKAY; - else - { - WARNING ("Target `notification': Unknown severity `%s'. " - "Will use `FAILURE' instead.", - ci->values[0].value.string); + else { + WARNING("Target `notification': Unknown severity `%s'. " + "Will use `FAILURE' instead.", + ci->values[0].value.string); data->severity = NOTIF_FAILURE; } - return (0); + return 0; } /* }}} int tn_config_add_severity */ -static int tn_config_add_string (char **dest, /* {{{ */ - const oconfig_item_t *ci) -{ +static int tn_config_add_string(char **dest, /* {{{ */ + const oconfig_item_t *ci) { char *temp; if (dest == NULL) - return (-EINVAL); - - if ((ci->values_num != 1) - || (ci->values[0].type != OCONFIG_TYPE_STRING)) - { - ERROR ("Target `notification': The `%s' option requires exactly one string " - "argument.", ci->key); - return (-1); + return -EINVAL; + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { + ERROR("Target `notification': The `%s' option requires exactly one string " + "argument.", + ci->key); + return -1; } - if (ci->values[0].value.string[0] == 0) - { - ERROR ("Target `notification': The `%s' option does not accept empty strings.", + if (ci->values[0].value.string[0] == 0) { + ERROR( + "Target `notification': The `%s' option does not accept empty strings.", ci->key); - return (-1); + return -1; } - temp = sstrdup (ci->values[0].value.string); - if (temp == NULL) - { - ERROR ("tn_config_add_string: sstrdup failed."); - return (-1); + temp = sstrdup(ci->values[0].value.string); + if (temp == NULL) { + ERROR("tn_config_add_string: sstrdup failed."); + return -1; } - free (*dest); + free(*dest); *dest = temp; - return (0); + return 0; } /* }}} int tn_config_add_string */ -static int tn_destroy (void **user_data) /* {{{ */ +static int tn_destroy(void **user_data) /* {{{ */ { tn_data_t *data; if (user_data == NULL) - return (-EINVAL); + return -EINVAL; data = *user_data; if (data == NULL) - return (0); + return 0; - sfree (data->message); - sfree (data); + sfree(data->message); + sfree(data); - return (0); + return 0; } /* }}} int tn_destroy */ -static int tn_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ +static int tn_create(const oconfig_item_t *ci, void **user_data) /* {{{ */ { tn_data_t *data; int status; - data = calloc (1, sizeof (*data)); - if (data == NULL) - { - ERROR ("tn_create: calloc failed."); - return (-ENOMEM); + data = calloc(1, sizeof(*data)); + if (data == NULL) { + ERROR("tn_create: calloc failed."); + return -ENOMEM; } data->message = NULL; data->severity = 0; status = 0; - for (int i = 0; i < ci->children_num; i++) - { + for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - if (strcasecmp ("Message", child->key) == 0) - status = tn_config_add_string (&data->message, child); - else if (strcasecmp ("Severity", child->key) == 0) - status = tn_config_add_severity (data, child); - else - { - ERROR ("Target `notification': The `%s' configuration option is not understood " - "and will be ignored.", child->key); + if (strcasecmp("Message", child->key) == 0) + status = tn_config_add_string(&data->message, child); + else if (strcasecmp("Severity", child->key) == 0) + status = tn_config_add_severity(data, child); + else { + ERROR("Target `notification': The `%s' configuration option is not " + "understood " + "and will be ignored.", + child->key); status = 0; } @@ -157,94 +149,85 @@ static int tn_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ } /* Additional sanity-checking */ - while (status == 0) - { - if ((data->severity != NOTIF_FAILURE) - && (data->severity != NOTIF_WARNING) - && (data->severity != NOTIF_OKAY)) - { - DEBUG ("Target `notification': Setting " - "the default severity `WARNING'."); + while (status == 0) { + if ((data->severity != NOTIF_FAILURE) && + (data->severity != NOTIF_WARNING) && (data->severity != NOTIF_OKAY)) { + DEBUG("Target `notification': Setting " + "the default severity `WARNING'."); data->severity = NOTIF_WARNING; } - if (data->message == NULL) - { - ERROR ("Target `notification': No `Message' option has been specified. " - "Without it, the `Notification' target is useless."); + if (data->message == NULL) { + ERROR("Target `notification': No `Message' option has been specified. " + "Without it, the `Notification' target is useless."); status = -1; } break; } - if (status != 0) - { - tn_destroy ((void *) &data); - return (status); + if (status != 0) { + tn_destroy((void *)&data); + return status; } *user_data = data; - return (0); + return 0; } /* }}} int tn_create */ -static int tn_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */ - notification_meta_t __attribute__((unused)) **meta, void **user_data) -{ +static int tn_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */ + notification_meta_t __attribute__((unused)) * *meta, + void **user_data) { tn_data_t *data; - notification_t n = { 0 }; + notification_t n = {0}; char temp[NOTIF_MAX_MSG_LEN]; gauge_t *rates; int rates_failed; if ((ds == NULL) || (vl == NULL) || (user_data == NULL)) - return (-EINVAL); + return -EINVAL; data = *user_data; - if (data == NULL) - { - ERROR ("Target `notification': Invoke: `data' is NULL."); - return (-EINVAL); + if (data == NULL) { + ERROR("Target `notification': Invoke: `data' is NULL."); + return -EINVAL; } /* Initialize the structure. */ n.severity = data->severity; - n.time = cdtime (); - sstrncpy (n.message, data->message, sizeof (n.message)); - sstrncpy (n.host, vl->host, sizeof (n.host)); - sstrncpy (n.plugin, vl->plugin, sizeof (n.plugin)); - sstrncpy (n.plugin_instance, vl->plugin_instance, - sizeof (n.plugin_instance)); - sstrncpy (n.type, vl->type, sizeof (n.type)); - sstrncpy (n.type_instance, vl->type_instance, - sizeof (n.type_instance)); + n.time = cdtime(); + sstrncpy(n.message, data->message, sizeof(n.message)); + sstrncpy(n.host, vl->host, sizeof(n.host)); + sstrncpy(n.plugin, vl->plugin, sizeof(n.plugin)); + sstrncpy(n.plugin_instance, vl->plugin_instance, sizeof(n.plugin_instance)); + sstrncpy(n.type, vl->type, sizeof(n.type)); + sstrncpy(n.type_instance, vl->type_instance, sizeof(n.type_instance)); n.meta = NULL; -#define REPLACE_FIELD(t,v) \ - if (subst_string (temp, sizeof (temp), n.message, t, v) != NULL) \ - sstrncpy (n.message, temp, sizeof (n.message)); - REPLACE_FIELD ("%{host}", n.host); - REPLACE_FIELD ("%{plugin}", n.plugin); - REPLACE_FIELD ("%{plugin_instance}", n.plugin_instance); - REPLACE_FIELD ("%{type}", n.type); - REPLACE_FIELD ("%{type_instance}", n.type_instance); +#define REPLACE_FIELD(t, v) \ + if (subst_string(temp, sizeof(temp), n.message, t, v) != NULL) \ + sstrncpy(n.message, temp, sizeof(n.message)); + REPLACE_FIELD("%{host}", n.host); + REPLACE_FIELD("%{plugin}", n.plugin); + REPLACE_FIELD("%{plugin_instance}", n.plugin_instance); + REPLACE_FIELD("%{type}", n.type); + REPLACE_FIELD("%{type_instance}", n.type_instance); rates_failed = 0; rates = NULL; - for (size_t i = 0; i < ds->ds_num; i++) - { + for (size_t i = 0; i < ds->ds_num; i++) { char template[DATA_MAX_NAME_LEN]; char value_str[DATA_MAX_NAME_LEN]; - ssnprintf (template, sizeof (template), "%%{ds:%s}", ds->ds[i].name); + const char *format = "%%{ds:%.*s}"; + snprintf(template, sizeof(template), format, + DATA_MAX_NAME_LEN - strlen(format), ds->ds[i].name); - if (ds->ds[i].type != DS_TYPE_GAUGE) - { - if ((rates == NULL) && (rates_failed == 0)) - { - rates = uc_get_rate (ds, vl); + if (ds->ds[i].type != DS_TYPE_GAUGE) { + if ((rates == NULL) && (rates_failed == 0)) { + rates = uc_get_rate(ds, vl); if (rates == NULL) rates_failed = 1; } @@ -252,35 +235,30 @@ static int tn_invoke (const data_set_t *ds, value_list_t *vl, /* {{{ */ /* If this is a gauge value, use the current value. */ if (ds->ds[i].type == DS_TYPE_GAUGE) - ssnprintf (value_str, sizeof (value_str), - GAUGE_FORMAT, (double) vl->values[i].gauge); + snprintf(value_str, sizeof(value_str), GAUGE_FORMAT, + (double)vl->values[i].gauge); /* If it's a counter, try to use the current rate. This may fail, if the * value has been renamed. */ else if (rates != NULL) - ssnprintf (value_str, sizeof (value_str), - GAUGE_FORMAT, (double) rates[i]); + snprintf(value_str, sizeof(value_str), GAUGE_FORMAT, (double)rates[i]); /* Since we don't know any better, use the string `unknown'. */ else - sstrncpy (value_str, "unknown", sizeof (value_str)); + sstrncpy(value_str, "unknown", sizeof(value_str)); - REPLACE_FIELD (template, value_str); + REPLACE_FIELD(template, value_str); } - sfree (rates); + sfree(rates); - plugin_dispatch_notification (&n); + plugin_dispatch_notification(&n); - return (FC_TARGET_CONTINUE); + return FC_TARGET_CONTINUE; } /* }}} int tn_invoke */ -void module_register (void) -{ - target_proc_t tproc = { 0 }; +void module_register(void) { + target_proc_t tproc = {0}; - tproc.create = tn_create; - tproc.destroy = tn_destroy; - tproc.invoke = tn_invoke; - fc_register_target ("notification", tproc); + tproc.create = tn_create; + tproc.destroy = tn_destroy; + tproc.invoke = tn_invoke; + fc_register_target("notification", tproc); } /* module_register */ - -/* vim: set sw=2 sts=2 tw=78 et fdm=marker : */ -