[PATCH] Exclude (Sub-)Subtree from snmpbulkwalk

2017-08-25 Thread michael-dev
When using snmpbulkwalk to fetch all OIDs from my Aruba Switch, 
snmpbulkwalk complains about non-increasing OIDs.

Ignoring using -Cc makes snmpbulkwalk loop forever.

Therefore I'd like to ignore a specific (Sub-)Subtree that cannot be 
walked.


This patch adds -Ce cmdline option to snmpbulkwalk, which makes 
snmpbulkwalk skip the subtree induced by this OID.
The cmdline option can be used multiple times to skip multiple 
sub-trees.


Using this option, I could easly walk all (other) SNMP oids on the 
switch. I uses this to identify some mapping between ssh configuration 
commands and SNMP related objects.


This patch does not update snmpwalk (I didn't need it). It also does not 
update the related man page, as I'm not fluent with editing man pages.


Regards,
M. Braun--- a/apps/snmpbulkwalk.c
+++ b/apps/snmpbulkwalk.c
@@ -72,6 +72,8 @@
 oid objid_mib[] = { 1, 3, 6, 1, 2, 1 };
 int numprinted = 0;
 int reps = 10, non_reps = 0;
+char**  excl_oids_str = NULL;
+size_t  num_excl_oids = 0;
 
 void
 usage(void)
@@ -83,6 +85,8 @@
 fprintf(stderr,
 "  -C APPOPTS\t\tSet various application specific behaviours:\n");
 fprintf(stderr,
+"\t\t\t  e:   skip over excluded block\n");
+fprintf(stderr,
 "\t\t\t  c:   do not check returned OIDs are increasing\n");
 fprintf(stderr,
 "\t\t\t  i:   include given OIDs in the search range\n");
@@ -124,6 +128,13 @@
 case 'C':
 while (*optarg) {
 switch (*optarg++) {
+case 'e':
+		num_excl_oids++;
+		excl_oids_str = realloc(excl_oids_str, sizeof(*excl_oids_str) * num_excl_oids);
+		if (!excl_oids_str)
+			exit(1);
+		excl_oids_str[num_excl_oids - 1] = optarg;
+		return;
 case 'c':
 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
@@ -187,6 +198,11 @@
 int status = STAT_ERROR;
 int check;
 int exitval = 0;
+int i;
+int excl_oid_idx;
+oid **excl_oids = NULL;
+size_t  *excl_oids_len = NULL;
+int width = 100;
 
 netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "includeRequested",
 			   NETSNMP_DS_APPLICATION_ID, 
@@ -233,6 +249,26 @@
 rootlen = sizeof(objid_mib) / sizeof(oid);
 }
 
+/*
+ * parse exclude oid strings
+ */
+excl_oids_len = realloc(excl_oids_len, sizeof(*excl_oids_len) * num_excl_oids);
+if (!excl_oids_len && num_excl_oids > 0)
+ exit(1);
+excl_oids = realloc(excl_oids, sizeof(*excl_oids) * num_excl_oids);
+if (!excl_oids && num_excl_oids > 0)
+exit(1);
+for (excl_oid_idx = 0; excl_oid_idx < num_excl_oids; excl_oid_idx++) {
+excl_oids_len[excl_oid_idx] = MAX_OID_LEN;
+excl_oids[excl_oid_idx] = malloc(sizeof(**excl_oids) * MAX_OID_LEN);
+if (!excl_oids[excl_oid_idx])
+   exit(1);
+if (snmp_parse_oid(excl_oids_str[excl_oid_idx], excl_oids[excl_oid_idx], &excl_oids_len[excl_oid_idx]) == NULL) {
+   snmp_perror(excl_oids_str[excl_oid_idx]);
+   exit(1);
+}
+}
+
 SOCK_STARTUP;
 
 /*
@@ -264,6 +300,28 @@
 }
 
 while (running) {
+excl_oid_idx = -1;
+for(i = 0; i < num_excl_oids; i++) {
+if ((name_length < excl_oids_len[i])
+|| (memcmp(excl_oids[i], name, excl_oids_len[i] * sizeof(oid)) != 0))
+continue; // not in excluded subtree, continue to next exclude rule
+excl_oid_idx = i;
+break;
+}
+if (excl_oid_idx >= 0) {
+/* has been excluded, so skip over */
+memmove(name, excl_oids[excl_oid_idx], excl_oids_len[excl_oid_idx] * sizeof(oid));
+name_length = excl_oids_len[i];
+while (name_length < MAX_OID_LEN) {
+name[name_length] = MAX_SUBID;
+name_length++;
+}
+/*  Alternatively: instead of using MAX_SUBID do
+name[name_length-1]++;
+snmp_get_and_print(ss, root, rootlen);
+*/
+}
+
 /*
  * create PDU for GETBULK request and add object name to request 
  */
@@ -292,6 +350,31 @@
 running = 0;
 continue;
 }
+excl_oid_idx = -1;
+for(i = 0; i < num_excl_oids; i++) {
+if ((vars->name_length < excl_oids_len[i])
+|| (memcmp(excl_oids[i], vars->name, excl_oids_len[i] * sizeof(oid))
+!= 0))
+continue; // not in excluded subtree, continue to next exclude rule
+/* has been excluded */
+excl_oid_idx = i;
+break;
+}
+

Re: [netsnmp-coders] [PATCH] Exclude (Sub-)Subtree from snmpbulkwalk

2017-08-26 Thread michael-dev

Am 25.08.2017 20:09, schrieb Jeff Gehlbach:

On 08/25/2017 12:22 PM, michael-dev wrote:

When using snmpbulkwalk to fetch all OIDs from my Aruba Switch,
snmpbulkwalk complains about non-increasing OIDs.
Ignoring using -Cc makes snmpbulkwalk loop forever.


I hope you will also report this problem to Aruba as a bug. It would be
a service to everybody who deals with SNMP agents for a living.


I've filed a ticket.

Regards,
M. Braun

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders