From aec55dfa7f94fd4ff99cec0767e6ffed93a21339 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 19 Feb 2009 09:48:14 +0100 Subject: [PATCH] powerdns, utils_cmd_putval, utils_db_query: Use parse_value(). Besides cleaning up the code a little, this also adds more verbose error messages. --- src/common.c | 2 +- src/common.h | 2 +- src/powerdns.c | 27 ++++----------------- src/utils_cmd_putval.c | 11 +++++---- src/utils_db_query.c | 53 ++++++++++++++---------------------------- 5 files changed, 29 insertions(+), 66 deletions(-) diff --git a/src/common.c b/src/common.c index 914857c8..c4994a2c 100644 --- a/src/common.c +++ b/src/common.c @@ -790,7 +790,7 @@ int parse_identifier (char *str, char **ret_host, return (0); } /* int parse_identifier */ -int parse_value (char *value, value_t *ret_value, const data_source_t ds) +int parse_value (const char *value, value_t *ret_value, const data_source_t ds) { char *endptr = NULL; diff --git a/src/common.h b/src/common.h index 2bdc8949..5b9f02cc 100644 --- a/src/common.h +++ b/src/common.h @@ -230,7 +230,7 @@ int format_name (char *ret, int ret_len, int parse_identifier (char *str, char **ret_host, char **ret_plugin, char **ret_plugin_instance, char **ret_type, char **ret_type_instance); -int parse_value (char *value, value_t *ret_value, const data_source_t ds); +int parse_value (const char *value, value_t *ret_value, const data_source_t ds); int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds); #if !HAVE_GETPWNAM_R diff --git a/src/powerdns.c b/src/powerdns.c index 164137ba..beb49fbb 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -289,30 +289,11 @@ static void submit (const char *plugin_instance, /* {{{ */ return; } - if (ds->ds[0].type == DS_TYPE_GAUGE) + if (0 != parse_value (value, &values[0], ds->ds[0])) { - char *endptr = NULL; - - values[0].gauge = strtod (value, &endptr); - - if (endptr == value) - { - ERROR ("powerdns plugin: Cannot convert `%s' " - "to a floating point number.", value); - return; - } - } - else - { - char *endptr = NULL; - - values[0].counter = strtoll (value, &endptr, 0); - if (endptr == value) - { - ERROR ("powerdns plugin: Cannot convert `%s' " - "to an integer number.", value); - return; - } + ERROR ("powerdns plugin: Cannot convert `%s' " + "to a number.", value); + return; } vl.values = values; diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index 639b8ab1..be09185e 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -65,12 +65,13 @@ static int dispatch_values (const data_set_t *ds, value_list_t *vl, break; } - if (strcmp (ptr, "U") == 0) + if ((strcmp (ptr, "U") == 0) && (ds->ds[i].type == DS_TYPE_GAUGE)) vl->values[i].gauge = NAN; - else if (ds->ds[i].type == DS_TYPE_COUNTER) - vl->values[i].counter = atoll (ptr); - else if (ds->ds[i].type == DS_TYPE_GAUGE) - vl->values[i].gauge = atof (ptr); + else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i])) + { + print_to_socket (fh, "-1 Failed to parse value `%s'.", ptr); + return (-1); + } i++; } /* while (strtok_r) */ diff --git a/src/utils_db_query.c b/src/utils_db_query.c index c2897c7c..5531b25f 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -197,7 +197,7 @@ static int udb_legacy_result_handle_result (udb_result_t *r, /* {{{ */ { value_list_t vl = VALUE_LIST_INIT; value_t value; - char *endptr; + char *value_str; assert (r->legacy_mode == 1); assert (r->ds != NULL); @@ -206,23 +206,14 @@ static int udb_legacy_result_handle_result (udb_result_t *r, /* {{{ */ vl.values = &value; vl.values_len = 1; - endptr = NULL; - errno = 0; - if (r->ds->ds[0].type == DS_TYPE_COUNTER) - vl.values[0].counter = (counter_t) strtoll (column_values[r->legacy_position], - &endptr, /* base = */ 0); - else if (r->ds->ds[0].type == DS_TYPE_GAUGE) - vl.values[0].gauge = (gauge_t) strtod (column_values[r->legacy_position], - &endptr); - else - errno = EINVAL; - - if ((endptr == column_values[r->legacy_position]) || (errno != 0)) + value_str = column_values[r->legacy_position]; + if (0 != parse_value (value_str, &vl.values[0], r->ds->ds[0])) { - WARNING ("db query utils: udb_result_submit: Parsing `%s' as %s failed.", - column_values[r->legacy_position], + ERROR ("db query utils: udb_legacy_result_handle_result: " + "Parsing `%s' as %s failed.", value_str, (r->ds->ds[0].type == DS_TYPE_COUNTER) ? "counter" : "gauge"); - vl.values[0].gauge = NAN; + errno = EINVAL; + return (-1); } sstrncpy (vl.host, q->host, sizeof (vl.host)); @@ -351,7 +342,7 @@ static int udb_legacy_result_create (const char *query_name, /* {{{ */ /* * Result private functions */ -static void udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ +static int udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ { value_list_t vl = VALUE_LIST_INIT; size_t i; @@ -365,30 +356,21 @@ static void udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ if (vl.values == NULL) { ERROR ("db query utils: malloc failed."); - return; + return (-1); } vl.values_len = r->ds->ds_num; for (i = 0; i < r->values_num; i++) { - char *endptr; - - endptr = NULL; - errno = 0; - if (r->ds->ds[i].type == DS_TYPE_COUNTER) - vl.values[i].counter = (counter_t) strtoll (r->values_buffer[i], - &endptr, /* base = */ 0); - else if (r->ds->ds[i].type == DS_TYPE_GAUGE) - vl.values[i].gauge = (gauge_t) strtod (r->values_buffer[i], &endptr); - else - errno = EINVAL; + char *value_str = r->values_buffer[i]; - if ((endptr == r->values_buffer[i]) || (errno != 0)) + if (0 != parse_value (value_str, &vl.values[i], r->ds->ds[i])) { - WARNING ("db query utils: udb_result_submit: Parsing `%s' as %s failed.", - r->values_buffer[i], + ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.", + value_str, (r->ds->ds[i].type == DS_TYPE_COUNTER) ? "counter" : "gauge"); - vl.values[i].gauge = NAN; + errno = EINVAL; + return (-1); } } @@ -430,6 +412,7 @@ static void udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */ plugin_dispatch_values (&vl); sfree (vl.values); + return (0); } /* }}} void udb_result_submit */ static void udb_result_finish_result (udb_result_t *r) /* {{{ */ @@ -468,9 +451,7 @@ static int udb_result_handle_result (udb_result_t *r, /* {{{ */ for (i = 0; i < r->values_num; i++) r->values_buffer[i] = column_values[r->values_pos[i]]; - udb_result_submit (r, q); - - return (0); + return udb_result_submit (r, q); } /* }}} int udb_result_handle_result */ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */ -- 2.30.2