From 8508efd140d64efc676237f6ec9792c65d108aac Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 14 Jul 2008 11:49:06 +0200 Subject: [PATCH] snmp plugin: Parse strings according to the data source type. Some broken SNMP implementations returns numbers as a string, e. g. a temperature could would be returned as "19.2 C". This change uses the data source type, i. e. `counter' or `gauge' to convert that string into an integer or a floating point number using strtoll or strtod. In case of an error (string-pointer is NULL or no conversion could take place) either zero or NAN is returned. --- src/snmp.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/snmp.c b/src/snmp.c index d19493df..05612576 100644 --- a/src/snmp.c +++ b/src/snmp.c @@ -717,13 +717,42 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type, temp += (uint32_t) vl->val.counter64->low; DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp); } + else if (vl->type == ASN_OCTET_STR) + { + /* We'll handle this later.. */ + } else { WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type); defined = 0; } - if (type == DS_TYPE_COUNTER) + if (vl->type == ASN_OCTET_STR) + { + char *string; + char *endptr; + + string = (char *) vl->val.string; + endptr = NULL; + + if (string != NULL) + { + if (type == DS_TYPE_COUNTER) + ret.counter = (counter_t) strtoll (string, &endptr, /* base = */ 0); + else if (type == DS_TYPE_GAUGE) + ret.gauge = (gauge_t) strtod (string, &endptr); + } + + /* Check if an error occurred */ + if ((string == NULL) || (endptr == string)) + { + if (type == DS_TYPE_COUNTER) + ret.counter = 0; + else if (type == DS_TYPE_GAUGE) + ret.gauge = NAN; + } + } + else if (type == DS_TYPE_COUNTER) { ret.counter = temp; } -- 2.30.2