diff options
author | Stef Walter <stef@memberwebs.com> | 2008-04-16 15:46:15 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2008-04-16 15:46:15 +0000 |
commit | 8856ed0bd7582cfae8e6c8b4db7a0d6255e93813 (patch) | |
tree | 61067503db9618d5f21e5f9287408173dc90cb3d | |
parent | c99b288ae66e97ae59ac65c82d98c42d7b63c355 (diff) |
Support parsing strings into integers and floats.
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | daemon/poll-engine.c | 50 |
2 files changed, 52 insertions, 2 deletions
@@ -1,8 +1,10 @@ -0.9 ????? +0.9 [2008-04-16] - Support failover between multiple agents. - Support table queries by looking up table indexes. - Major refactoring of internals. - Warn and ignore MIBs that we don't recognize. + - Support parsing strings into integers and floats + 0.8 [2007-07-16] - Fix assertion when reallocating SNMP request memory diff --git a/daemon/poll-engine.c b/daemon/poll-engine.c index 119c8b1..e628382 100644 --- a/daemon/poll-engine.c +++ b/daemon/poll-engine.c @@ -40,6 +40,7 @@ #include <sys/types.h> #include <sys/socket.h> +#include <ctype.h> #include <errno.h> #include <err.h> @@ -163,6 +164,48 @@ finish_poll (rb_poller *poll, mstime when) poll->polling = 0; } +static int +parse_string_value (struct snmp_value *value, rb_item *item) +{ + char buf[256]; + char *t, *b; + + ASSERT (value); + ASSERT (value->syntax == SNMP_SYNTAX_OCTETSTRING); + ASSERT (item); + + if(value->v.octetstring.len >= sizeof(buf)) + return 0; + + memset(buf, 0, sizeof(buf)); + strncpy(buf, (char*)value->v.octetstring.octets, value->v.octetstring.len); + + /* Remove leading spaces */ + b = buf; + while(isspace(*b)) + ++b; + + /* Cannot parse empty strings */ + if(!*b) + return 0; + + /* Try to parse the string into an integer */ + item->v.i_value = strtoll(b, &t, 10); + if(!*t || isspace(*t)) { + item->vtype = VALUE_REAL; + return 1; + } + + /* Try to parse the string into a floating point */ + item->v.f_value = strtod(b, &t); + if(!*t || isspace(*t)) { + item->vtype = VALUE_FLOAT; + return 1; + } + + return 0; +} + static void field_response (int request, int code, struct snmp_value *value, void *arg) { @@ -200,9 +243,14 @@ field_response (int request, int code, struct snmp_value *value, void *arg) item->vtype = VALUE_REAL; break; case SNMP_SYNTAX_OCTETSTRING: + if (!parse_string_value(value, item)) + msg = "snmp server returned non numeric value for field: %s"; + break; case SNMP_SYNTAX_OID: + msg = "snmp server returned a oid value for field: %s"; + break; case SNMP_SYNTAX_IPADDRESS: - msg = "snmp server returned non numeric value for field: %s"; + msg = "snmp server returned a ip address value for field: %s"; break; case SNMP_SYNTAX_NOSUCHOBJECT: case SNMP_SYNTAX_NOSUCHINSTANCE: |