Making some object R/W - SNMP

This is a discussion on Making some object R/W - SNMP ; I've am now starting to modify my agent that I've used net-snmp to write for some R/W objects. Up to now everything has been informational and read-only. Some objects are configuration objects so it makes since to allow an NMS ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: Making some object R/W

  1. Making some object R/W

    I've am now starting to modify my agent that I've used net-snmp
    to write for some R/W objects. Up to now everything has been
    informational and read-only. Some objects are configuration
    objects so it makes since to allow an NMS to modify them. Below
    is one of my objects. My skeleton was genertated with mib2c in ucd-snmp
    I've moved to net-snmp but have not redone all my code to
    what seems like the possible new format that mib2c spits out.

    Can someone tell me what I need to change to make this
    object setable by the private community?

    -- Snippet From Mib --
    opsSrenaPortName OBJECT-TYPE
    SYNTAX DisplayString
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION
    "The configured name for this port."

    ::= { opsSrenaPortEntry 2 }

    -- End Snippet From Mib --


    -- snippet from C code --

    #define OPSSRENAPORTNAME 10
    { OPSSRENAPORTNAME , ASN_OCTET_STR , RONLY , var_opsSrenaPortTable, 5,
    { 1,1,7,1,2 } },

    /**
    * Human readable name for the port
    */
    case OPSSRENAPORTNAME:
    {

    ptr = c->port[index-1].name;

    if(*ptr)
    strncpy(string, ptr, sizeof(string));
    else
    strncpy(string, "none", sizeof(string));

    relcfgptr(c);

    *var_len = strlen(string);
    return (unsigned char *) string;
    }

    --- End Snippet --

  2. Re: Making some object R/W

    mib2c created the following skeleton:


    int
    write_opsSrenaPortName(int action,
    u_char *var_val,
    u_char var_val_type,
    size_t var_val_len,
    u_char *statP,
    oid *name,
    size_t name_len)
    {
    static unsigned char string[SPRINT_MAX_LEN];
    int size;


    switch ( action ) {
    case RESERVE1:
    if (var_val_type != ASN_OCTET_STR){
    fprintf(stderr, "write to opsSrenaPortName not
    ASN_OCTET_STR\n");
    return SNMP_ERR_WRONGTYPE;
    }
    if (var_val_len > sizeof(string)){
    fprintf(stderr,"write to opsSrenaPortName: bad length\n");
    return SNMP_ERR_WRONGLENGTH;
    }
    break;


    case RESERVE2:
    size = var_val_len;
    string = (char *) var_val;


    break;


    case FREE:
    /* Release any resources that have been allocated */
    break;


    case ACTION:
    /* The variable has been stored in string for
    you to use, and you have just been asked to do something with
    it. Note that anything done here must be reversable in the
    UNDO case */
    break;

    case UNDO:
    /* Back out any changes made in the ACTION case */
    break;


    case COMMIT:
    /* Things are working well, so it's now safe to make the change
    permanently. Make sure that anything done here can't fail! */
    break;
    }
    return SNMP_ERR_NOERROR;
    }


    My quesiton is that where can I get the index number from
    within that function. I enter the port table with an index
    number but it appears write is called without that index number
    I'm curious about the action, undo, and commit parameters.
    Allowing the user to send a new name will modify the config
    of the device. So and snmpset will immediately modify config.
    Shoud I wait for a commit command to write it? If I do,
    where do I store the new string when ACTION is called? Maybe
    a static variable within the function and then put that
    value in config when commit is called? Seeing those options
    leads me to believe I need some form of persistent storeage
    for all these objects taht are writeable and only modify
    config with commit is called?

    Thanks,
    Chris


  3. Re: Making some object R/W

    Chris Fowler wrote:

    > Can someone tell me what I need to change to make this
    > object setable by the private community?


    Well, first it won't typically be the community "private".
    It'll be whatever community you configure to allow write access.
    (And I'd suggest you don't use "private")


    > -- Snippet From Mib --
    > opsSrenaPortName OBJECT-TYPE
    > SYNTAX DisplayString
    > MAX-ACCESS read-only ==> read-write


    (or possibly read-create if you want to be able to add new rows
    at run-time).


    > STATUS current
    > DESCRIPTION
    > "The configured name for this port."
    >
    > ::= { opsSrenaPortEntry 2 }
    >
    > -- End Snippet From Mib --
    >
    >
    > -- snippet from C code --
    >
    > #define OPSSRENAPORTNAME 10
    > { OPSSRENAPORTNAME , ASN_OCTET_STR , RONLY , var_opsSrenaPortTable,
    > { 5,{ 1,1,7,1,2 } },


    RONLY -> RWRITE


    > /**
    > * Human readable name for the port
    > */
    > case OPSSRENAPORTNAME:
    > {
    >
    > ptr = c->port[index-1].name;
    >
    > if(*ptr)
    > strncpy(string, ptr, sizeof(string));
    > else
    > strncpy(string, "none", sizeof(string));
    >
    > relcfgptr(c);


    Add
    *write_method = write_PortName

    >
    > *var_len = strlen(string);
    > return (unsigned char *) string;
    > }
    >
    > --- End Snippet --


    Then craft yourself a routine 'write_PortName'.
    See the tutorial at
    http://www.net-snmp.org/tutorial/agent/08-writing.html

    for more details of what this should do.

    Dave

  4. Re: Making some object R/W

    Dave Shield wrote:

    > Then craft yourself a routine 'write_PortName'.
    > See the tutorial at
    > http://www.net-snmp.org/tutorial/agent/08-writing.html
    >
    > for more details of what this should do.
    >
    > Dave


    That page is making more sense. Thanks.

  5. Re: Making some object R/W

    Below is the command that I'm using to set the variable.
    Do I need to specify a comitt or undo option on the
    command line?

    snmpset -c private 192.168.2.231 \
    OUTPOST-SRENA-MIB:psSrenaAudit.0 i 1

  6. Re: Making some object R/W

    Chris Fowler wrote:

    > My quesiton is that where can I get the index number from
    > within that function.


    Pull it out of the 'name' field

    The new API has rather better support for this sort of thing,
    but doesn't have quite the same rambling documentation as the
    older API. You pays your money & you takes your choice....

    (I'm assuming your other questions have been sort-of answered
    by the "extending the agent" document)

    Dave

  7. Re: Making some object R/W

    Chris Fowler wrote:

    > The document is great.



    Why thank you, kind Sir.


    > Is there an SNMP_ERR_EINVAL equivalent
    > so I can return an error to the caller if I receive an
    > invalid argument?


    Not as such - you're limited to the set of SNMP-defined
    error cases. This does include a general-purpose
    SNMP_ERR_GENERR, which might be suitable.

    Depends on exactly what you mean by "an invalid argument".

    Dave

  8. Re: Making some object R/W

    Chris Fowler wrote:

    > Below is the command that I'm using to set the variable.
    > Do I need to specify a comitt or undo option on the
    > command line?


    No.
    The multi-pass handling is purely internal to the agent.
    The client just specifies what it wants changed, and
    gets told Yeay or Nay.

    Dave

  9. Re: Making some object R/W

    Dave Shield wrote:

    > Chris Fowler wrote:
    >
    >> The document is great.

    >
    >
    > Why thank you, kind Sir.
    >
    >
    >> Is there an SNMP_ERR_EINVAL equivalent
    >> so I can return an error to the caller if I receive an
    >> invalid argument?

    >
    > Not as such - you're limited to the set of SNMP-defined
    > error cases. This does include a general-purpose
    > SNMP_ERR_GENERR, which might be suitable.
    >
    > Depends on exactly what you mean by "an invalid argument".
    >
    > Dave


    And Invalid Argument would be if I receive the a 5 and I was
    expecting 1-4. I would want to spit back an error number
    instead of trying to aply that number to config.

  10. Re: Making some object R/W

    Chris Fowler wrote:

    > And Invalid Argument would be if I receive the a 5 and I was
    > expecting 1-4.


    In which case, Juergen has listed the appropriate errors
    to use - probably 'wrongValue' (or maybe 'inconsistentValue')
    in this case.

    I wasn't sure whether you meant this, or something going
    wrong at a lower level (such as receiving a NULL parameter
    in one of the handler routines).

    Dave

+ Reply to Thread