diff options
author | Stef Walter <stef@memberwebs.com> | 2006-03-30 00:54:39 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2006-03-30 00:54:39 +0000 |
commit | c62d4bcfb91cedf6b53db77a3cc15b2411342520 (patch) | |
tree | 0345c892fe4da58259d559f692f92a5b66076a5a /module/bsnmp-regex.c | |
parent | 1a8556d49b78ba9e2f366944183326efc027213d (diff) |
variable and expire support
Diffstat (limited to 'module/bsnmp-regex.c')
-rw-r--r-- | module/bsnmp-regex.c | 78 |
1 files changed, 60 insertions, 18 deletions
diff --git a/module/bsnmp-regex.c b/module/bsnmp-regex.c index 3696820..c0aa252 100644 --- a/module/bsnmp-regex.c +++ b/module/bsnmp-regex.c @@ -63,6 +63,7 @@ #define DEFAULT_CONFIG CONF_PREFIX "/rrdbot" #define DEFAULT_SOCKET "/var/run/snmp-regex.sock" +#define DEFAULT_EXPIRES 0 /* our module handle */ static struct lmodule *module; @@ -86,6 +87,7 @@ struct data_entry { int type; char *descr; + uint64_t expires; #ifdef WITH_PCRE pcre *regex; @@ -109,6 +111,7 @@ static uint32_t entry_count = 0; /* configuration */ static u_char *regex_config = NULL; static char *config_memory = NULL; +static uint64_t option_expires = DEFAULT_EXPIRES; /* The socket to read from */ static u_char *regex_sock = NULL; @@ -717,6 +720,9 @@ config_entry (struct data_entry *data, char *name, char *regex, /* Replacement data */ data->result = result; + /* Options */ + data->expires = option_expires; + return 0; } @@ -781,6 +787,26 @@ config_line (char *name, char *value, int line) return 0; } +static void +config_var (char *name, char *value, int line) +{ + char *t2; + int i; + + /* The expires variable */ + if (strcmp (name, "expire") == 0 || strcmp (name, "expires") == 0) { + + i = strtol (value, &t2, 10); + if (i <= 0 || *t2) + emsg ("invalid value for '%s' variable. ignoring: %s", name, value); + else + option_expires = i * 100; + return; + } + + emsg ("unknown or invalid variable. ignoring: %s", name); +} + static int config_read () { @@ -832,6 +858,7 @@ config_parse () char* p; char* t; int line = 0; + int var = 0; int r; config_free_all (); @@ -858,24 +885,33 @@ config_parse () if (!*t || *t == '#') continue; - t = strchr (t, ':'); - if (t == NULL) { + t = t + strcspn (t, ":="); + if (!*t) { emsg ("invalid config line: %s", p); return -1; } + /* Equal sign denotes a variable */ + var = (*t == '='); *t = 0; t++; - /* Pass of the name and value */ - r = config_line (strtrim (p), strtrim (t), line); - if (r < 0) { + /* Pass variables to the appropriate place */ + if (var) { + config_var (strtrim (p), strtrim (t), line); - /* If -2 was returned, no error message was printed */ - if (r == -2) - emsg ("[line %d] invalid configuration file", line); + /* And config lines to the appropriate place */ + } else { - return -1; + r = config_line (strtrim (p), strtrim (t), line); + if (r < 0) { + + /* If -2 was returned, no error message was printed */ + if (r == -2) + emsg ("[line %d] invalid configuration file", line); + + return -1; + } } } @@ -1009,6 +1045,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; + int expired = 0; switch (op) { case SNMP_OP_GETNEXT: @@ -1038,6 +1075,15 @@ op_regexentry (struct snmp_context *ctx, struct snmp_value *value, break; } + /* Figure out if we're expired or not */ + ticks = getcurrticks (); + if (ticks == 0) + return SNMP_ERR_GENERR; + if (data->expires && data->last_update) { + if (ticks > (data->last_update + data->expires)) + expired = 1; + } + switch (which) { case LEAF_regexIndex: value->v.integer = data->index; @@ -1047,23 +1093,19 @@ op_regexentry (struct snmp_context *ctx, struct snmp_value *value, return (string_get (value, data->descr, -1)); case LEAF_regexLast: - if (data->last_update) { - ticks = getcurrticks (); - if (ticks == 0) - return SNMP_ERR_GENERR; + if (data->last_update && !expired) value->v.uint32 = (ticks - data->last_update); - } else { + else value->v.uint32 = 0; - } break; case LEAF_regexInteger: - value->v.uint32 = data->value_int; + value->v.uint32 = expired ? 0 : data->value_int; break; case LEAF_regexValue: - return (string_get (value, data->value_str ? data->value_str : "", -1)); - + return (string_get (value, (data->value_str && !expired) ? + data->value_str : "", -1)); default: ASSERT(0); break; |