summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2006-03-11 20:02:52 +0000
committerStef Walter <stef@memberwebs.com>2006-03-11 20:02:52 +0000
commita50303c1cb7a4f6b3dd3b78aa2ee076ef0a758e8 (patch)
treeb0cc1fc24ee9e44588562afbd839d66192431e9e
parent5f88ca4b71e02bfba1c708d6ca54cc720020dfa7 (diff)
Add tallying support for data
-rw-r--r--src/bsnmp-regex.c61
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: