[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: snmpconf A non-trivial policyAction example



At 12:52 AM 11/9/00 -0800, Steve Waldbusser wrote:

Enough RMON2 background, here's the script:

In my never-ending crusade to introduce a native string type, let me 
rewrite the above script using such a native type based on the following 
assumptions:

         a quoted string is of type "string"
         Strings can be assigned using the '=' operator
         Strings can be concatenated using the '+' infix
                 operator (as in C++)
         Using the + operator to add an integer to a string
                 converts the integer to a string first.

Below is the original script interleaved with my revisions


 >    char oid[STRINGLEN], value[STRINGLEN];

/* The declarations of oid and value are not necessary in the revised example,
  * but for exposition, here is what they would look like:
  */
string oid, value;

 >    int index, pdu;
 >
 >    pdu = 0; /* Use PDU #0 */
 >    if (!(index = setRowStatus("hlMatrixControlStatus.*", 20)))
 >        return; /* couldn't find a free entry */

The above code would be unchanged.

 >    sprintf(oid, "hlMatrixControlDataSource.%d", index);
 >    sprintf(value, "ifIndex.%d", $1);
 >    writeVarbind(pdu, 0, oid, TYPE_OBJECT_IDENTIFIER, value, 0);

/* No need to pass a length argument */
writeVarbind(pdu, 0, "hlMatrixControlDataSource." + index,
              TYPE_OBJECT_IDENTIFIER, "ifIndex." + $1);

 >    sprintf(oid, "hlMatrixControlNlMaxDesiredEntries.%d", index);
 >    writeVarbind(pdu, 1, oid, TYPE_INTEGER, "1000", 0);
 >
 >    sprintf(oid, "hlMatrixControlAlMaxDesiredEntries.%d", index);
 >    writeVarbind(pdu, 2, oid, TYPE_INTEGER, "1000", 0);
 >
 >    sprintf(oid, "hlMatrixControlOwner.%d", index);
 >    writeVarbind(pdu, 3, oid, TYPE_OCTET_STRING, "policy manager", 14);
 >
 >    sprintf(oid, "hlMatrixControlStatus.%d", index);
 >    writeVarbind(pdu, 4, oid, TYPE_INTEGER, "1", 0);


writeVarbind(pdu, 1,
                    "hlMatrixControlNlMaxDesiredEntries." + index,
                    TYPE_INTEGER, "1000");
writeVarbind(pdu, 2,
                    "hlMatrixControlAlMaxDesiredEntries." + index,
                    TYPE_INTEGER, "1000");
writeVarbind(pdu, 3, "hlMatrixControlOwner." + index,
                    TYPE_OCTET_STRING, "policy manager");
writeVarbind(pdu, 4,
                    "hlMatrixControlStatus." + index,TYPE_INTEGER,
                    "1");

 >    snmpsend(0, 5, OP_SET);

/* The a snmpsend line could be unchanged, although I
  * would prefer the following:
  */
snmpset(pdu, 5);

- - - - - -
The above example does not fully show the benefits of the string type. An 
example using get would be more interesting from the string standpoint. 
Here is an off-the-cuff fragment:

Using character arrays (no string type):
=========================================

char oid[STRINGLEN], value[STRINGLEN];
int index, pdu, valueLen;

pdu = 0; /* Use PDU #0 */

sprintf(oid, "hlMatrixControlDataSource.%d", index);
writeVarbind(pdu, 0, oid, TYPE_OBJECT_IDENTIFIER, "", 0);
snmpsend(pdu, 1, OP_GET);

/* Error if retrieved oid or value don't fit in buffer */
readVarbind(pdu, 0, oid, TYPE_OBJECT_IDENTIFIER,
                   value, &valueLen);

=========================================================
Using a built-in string type:
=============================

string value, oid;
int index, pdu;

pdu = 0; /* Use PDU #0 */

sprintf(oid, "hlMatrixControlDataSource.%d", index);
writeVarbind(pdu, 0, "hlMatrixControlDataSource." + index,
                    TYPE_OBJECT_IDENTIFIER, "");
snmpsend(pdu, 1, OP_GET); /* or snmpget(pdu, 1); */

/* oid and value will grow to required length */
value = readVarbind(pdu, 0, &oid);

The above example is basic, but with enough code, the simplification 
provided by the string type becomes more compelling. Also, the reduced 
chance of error results in reduced need for error handling, which is always 
a big win.

There are also several other simplifications possible once strings are part 
of the language. For example, the readVarbind function could be split and 
simplified, avoiding the need for declaring dummy variables to hold unused 
results:

oid = readVarbindOid(pdu, 0);
value = readVarbindValue(pdu, 0);

Note that either of the above two calls could be eliminated if one or the 
other result is not needed.


- Pablo
---------------------------------------------------------------------
Pablo Halpern                                    phalpern@newview.org
http://www.halpernwightsoftware.com