diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bsnmp-regex.c | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/src/bsnmp-regex.c b/src/bsnmp-regex.c index 72b12b2..091fbf8 100644 --- a/src/bsnmp-regex.c +++ b/src/bsnmp-regex.c @@ -4,6 +4,7 @@ #include <sys/queue.h> #include <sys/select.h> #include <sys/stat.h> +#include <sys/time.h> #include <syslog.h> #include <unistd.h> #include <stdarg.h> @@ -122,6 +123,23 @@ strtrim (char* data) return data; } +static uint64_t +getcurrticks () +{ + struct timeval tp; + uint64_t t; + + /* Update tick count */ + if (gettimeofday (&tp, NULL) < 0) { + emsg ("couldn't get current time: %s", strerror (errno)); + return 0; + } + + t = (((uint64_t)tp.tv_sec) * 100) + (((uint64_t)tp.tv_usec) / 10000); + return t; +} + + /* ----------------------------------------------------------------------------- * MATCHING */ @@ -129,7 +147,40 @@ strtrim (char* data) static int process_match (struct data_entry *data, char *result) { -fprintf (stderr, "match for %s: %s\n", data->descr, result ? result : "MATCH"); + char *t; + int i; + + switch(data->type) { + case TYPE_COUNTER: + data->value++; + break; + case TYPE_INTEGER: + if (!result) { + emsg ("match, but no result data for '%s'", data->descr); + return -1; + } + + i = strtol (result, &t, 10); + if (*t) { + emsg ("invalid integer for '%s': %s", data->descr, result); + return -1; + } + + data->value = i; + break; + + default: + ASSERT(0); + break; + } + + data->last_update = getcurrticks (); + + /* + fprintf(stderr, "updated '%s' to value '%lld' at '%lld'\n", + data->descr, data->value, data->last_update); + */ + return 0; } @@ -225,8 +276,6 @@ process_log (char *line, int len) process_match (data, result); free (result); } - - fprintf (stderr, "log line: %s\n", line); } /* ----------------------------------------------------------------------------- @@ -727,6 +776,7 @@ op_regexentry (struct snmp_context *ctx, struct snmp_value *value, { asn_subid_t which = value->var.subs[sub - 1]; struct data_entry *data; + uint64_t ticks; switch (op) { case SNMP_OP_GETNEXT: @@ -765,7 +815,10 @@ op_regexentry (struct snmp_context *ctx, struct snmp_value *value, return (string_get (value, data->descr, -1)); case LEAF_regexLast: - value->v.uint32 = data->last_update; + ticks = getcurrticks (); + if (ticks == 0) + return SNMP_ERR_GENERR; + value->v.uint32 = (ticks - data->last_update); break; case LEAF_regexValue: |