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<OID> 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<OID>:       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 = 1000000;
 
     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;
+                    }
+                    if (excl_oid_idx >= 0) {
+/*
+                        printf("Skipping over reply oid: ");
+                        print_description(vars->name, vars->name_length, width);
+*/
+                        /*
+                         * Check if last variable, and if so, save for next request.  
+                         */
+                        if (vars->next_variable == NULL) {
+                            memmove(name, vars->name,
+                                    vars->name_length * sizeof(oid));
+                            name_length = vars->name_length;
+                        }
+                        continue; /* skip this variable */
+                    }
                     numprinted++;
                     print_variable(vars->name, vars->name_length, vars);
                     if ((vars->type != SNMP_ENDOFMIBVIEW) &&
------------------------------------------------------------------------------
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
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to