aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2012-02-16 23:18:27 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2012-02-16 23:32:04 +0200
commitcc8e335a68ab8232837515ba3f91b42f7dc7b9e9 (patch)
tree9d3336ff48d0ab594e808c3576ecd9978a5881e9
parent1de41968fc735f701f3a0cf2d1d05023a00b7971 (diff)
downloadslb-cc8e335a68ab8232837515ba3f91b42f7dc7b9e9.tar.gz
slb-cc8e335a68ab8232837515ba3f91b42f7dc7b9e9.tar.bz2
Fix printing of oid values.
Don't print type prefixes, don't enclose octet strings in double quotes. * src/slb.c (slb_snmp_setup): Set NETSNMP_DS_LIB_QUICK_PRINT to 1. * src/slb.h (slb_format_cache): New struct. (SLB_FORMAT_CACHE_NULL): New define. (slb_format_cache_init,slb_format_oid_value): New protos. (slb_assertion) <buf,bufsize>: Remove. (slb_assertion) <cache>: New member. * src/snmploop.c (slb_format_cache_init) (slb_format_oid_value): New functions. (assertion_test): Call slb_format_oid_value. (slb_table_grow): Likewise. (slb_table_grow): Free temporary buffer after use. * doc/config.texi: Update.
-rw-r--r--doc/config.texi6
-rw-r--r--src/slb.c4
-rw-r--r--src/slb.h16
-rw-r--r--src/snmploop.c73
4 files changed, 70 insertions, 29 deletions
diff --git a/doc/config.texi b/doc/config.texi
index 91f21d5..e39819f 100644
--- a/doc/config.texi
+++ b/doc/config.texi
@@ -975,6 +975,7 @@ If this statement is absent, the default expression will be used
is reported.
@end deffn
+@anchor{table}
@deffn {Config: server} table name base-oid
Define a slice in an SNMP table. The slice will be named @var{name}
and will contain all MIBs under @var{base-oid}.
@@ -1111,10 +1112,11 @@ interface 3), it would be wise to ensure that the 3rd row refers to
the interface in question (say @samp{eth1}):
@example
-assert "IF-MIB::ifDescr.3" eq "STRING: eth1";
+assert "IF-MIB::ifDescr.3" eq "eth1";
@end example
-Notice, that @var{pattern} must include the data type.
+See also @ref{table, table slices}, for a more elegant way of
+achieving this.
@end deffn
@node output
diff --git a/src/slb.c b/src/slb.c
index 04146ce..51e4d92 100644
--- a/src/slb.c
+++ b/src/slb.c
@@ -122,6 +122,10 @@ slb_snmp_setup()
exit(EX_SOFTWARE);
}
snmp_enable_calllog();
+ /* This informs value formatting functions that only the value
+ itself should be printed, without the type prefix and quotes */
+ netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
+ NETSNMP_DS_LIB_QUICK_PRINT, 1);
}
diff --git a/src/slb.h b/src/slb.h
index 7277dbe..5e3b04e 100644
--- a/src/slb.h
+++ b/src/slb.h
@@ -297,7 +297,20 @@ int eval_expression(struct slb_expression *expr,
struct slb_node *copy_node(struct slb_node *node, struct grecs_symtab *vartab);
void slb_node_state_init(struct slb_node_state *ns);
+
+struct slb_format_cache {
+ unsigned char *cache_buffer;
+ size_t cache_bufsize;
+};
+
+#define SLB_FORMAT_CACHE_NULL { NULL, 0 }
+void slb_format_cache_init(struct slb_format_cache *cache);
+char *slb_format_oid_value(oid *oid, size_t len, struct variable_list *vp,
+ struct slb_format_cache *cache);
+
+
+
#define SLB_ASSERT_NE 0x01
struct slb_assertion {
@@ -307,8 +320,7 @@ struct slb_assertion {
char *value;
char *text;
grecs_locus_t locus;
- unsigned char *buf;
- size_t bufsize;
+ struct slb_format_cache cache;
};
struct grecs_symtab *assertion_symtab_create(void);
diff --git a/src/snmploop.c b/src/snmploop.c
index f7663f2..84e30fe 100644
--- a/src/snmploop.c
+++ b/src/snmploop.c
@@ -29,6 +29,43 @@ srvid(struct slb_server *srv)
return srv->id ? srv->id : srv->peer;
}
+void
+slb_format_cache_init(struct slb_format_cache *cache)
+{
+ memset(cache, 0, sizeof(*cache));
+}
+
+char *
+slb_format_oid_value(oid *oid, size_t len, struct variable_list *vp,
+ struct slb_format_cache *cache)
+{
+ struct slb_format_cache tcache;
+ size_t out_len = 0;
+ int rc;
+
+ if (!cache) {
+ slb_format_cache_init(&tcache);
+ cache = &tcache;
+ }
+
+ if (cache->cache_bufsize == 0) {
+ /* Initial allocation */
+ cache->cache_bufsize = 16;
+ cache->cache_buffer = grecs_malloc(cache->cache_bufsize);
+ }
+ if (!sprint_realloc_value(&cache->cache_buffer, &cache->cache_bufsize,
+ &out_len, 1, oid, len, vp))
+ return NULL;
+ if (vp->type == ASN_OCTET_STR &&
+ cache->cache_buffer[0] == '"' &&
+ cache->cache_buffer[out_len-1] == '"') {
+ memmove(cache->cache_buffer, cache->cache_buffer + 1,
+ out_len-2);
+ cache->cache_buffer[out_len-2] = 0;
+ }
+ return (char*) cache->cache_buffer;
+}
+
#define E_VARINST_OK 0
#define E_VARINST_UNSUPPORTED 1
#define E_VARINST_OVERFLOW 2
@@ -92,16 +129,12 @@ assertion_test(struct slb_assertion *ap, struct variable_list *vp)
{
size_t out_len = 0;
int rc;
+ char *buf;
- if (ap->bufsize == 0) {
- /* Initial allocation */
- ap->bufsize = 16;
- ap->buf = grecs_malloc(ap->bufsize);
- }
- if (!sprint_realloc_value(&ap->buf, &ap->bufsize, &out_len, 1, ap->name,
- ap->length, vp))
+ buf = slb_format_oid_value(ap->name, ap->length, vp, &ap->cache);
+ if (!buf)
return 1;
- rc = strcmp(ap->value, (char*) ap->buf);
+ rc = strcmp(ap->value, buf);
if (ap->flags & SLB_ASSERT_NE)
rc = !rc;
return rc;
@@ -126,7 +159,7 @@ process_result(struct slb_server *srv, struct snmp_pdu *pdu)
ap->locus.beg.file, ap->locus.beg.line,
srvid(srv),
ap->text,
- ap->buf);
+ ap->cache.cache_buffer);
return;
}
@@ -181,24 +214,13 @@ process_result(struct slb_server *srv, struct snmp_pdu *pdu)
static int
slb_table_grow(struct slb_table *tab, struct variable_list *vp)
{
- size_t out_len = 0;
- size_t bufsize = 16;
- unsigned char *buf = grecs_malloc(bufsize);
+ char *buf;
struct slb_idxnum *ent;
int install = 1;
- int rc;
- int ovf;
-
- /* This is needed by sprint_realloc_value.
- Very unfortunate. */
- netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
- NETSNMP_DS_LIB_QUICK_PRINT, 1);
- rc = sprint_realloc_value(&buf, &bufsize, &out_len,
- 1, vp->name, vp->name_length, vp);
- /* On the other hand, assertions assume data type prefix */
- netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
- NETSNMP_DS_LIB_QUICK_PRINT, 0);
- if (rc == 0) {
+ int ovf, out_len;
+
+ buf = slb_format_oid_value(vp->name, vp->name_length, vp, NULL);
+ if (!buf) {
logmsg(LOG_ERR,
"cannot format variable: %s",
snmp_api_errstring(snmp_errno));
@@ -206,6 +228,7 @@ slb_table_grow(struct slb_table *tab, struct variable_list *vp)
}
ent = idxnum_lookupz(tab->tab_idx, buf, &install);
+ free(buf);
if (ent->idxnum_strval) {
ent->idxnum_strsiz = 16;
ent->idxnum_strval = grecs_malloc(ent->idxnum_strsiz);

Return to:

Send suggestions and report system problems to the System administrator.