markabrahams / node-net-snmp

JavaScript implementation of the Simple Network Management Protocol (SNMP)
206 stars 97 forks source link

Agentx command handler function async and error #250

Closed ivanbaj closed 5 months ago

ivanbaj commented 5 months ago

I have two questions abut this text: handler (optional) - an optional callback function, which is called before the request to the MIB is made. This could update the MIB value(s) handled by this provider. If not given, the values are simply returned from (or set in) the MIB without any other processing. The callback function takes a MibRequest instance, which has a done() function. This must be called when finished processing the request. To signal an error, give a single error object in the form of {errorStatus: <status>}, where <status> is a value from ErrorStatus e.g. {errorStatus: snmp.ErrorStatus.GeneralError}. The MibRequest also has an oid field with the instance OID being operated on, and an operation field with the request type from snmp.PduType. If the MibRequest is for a SetRequest PDU, then variables setValue and setType contain the value and type received in the SetRequest varbind.

  1. If I am processing an async action can I call MibRequest .done() after the async operation has finished? My test shows that the handler function does not cause the snmp set esponse to wait.
  2. The text above says to use errorStatus but the code is expecting {type: , value: }
    
                    responseVarbind = {
                        oid: mibRequest.oid,
                        type: error.type || ObjectType.Null,
                        value: error.value || null
                    };```

setting the errorStatus did nothing for me. What is the correct call for done() when there is an error?

markabrahams commented 5 months ago

Hi @ivanbaj!

For (1), yes you can call .done() after an async operation fine, and responses will wait. For set operations, understand that the AgentX master will typically send your subagent three PDUs (TestSet, CommitSet, CleanupSet), and your handler function will be called once for each of these PDUs. The MIB change only takes effect after the second PDU (CommitSet). You may want to act only on the CommitSet message (check for mibRequest.operation === AgentXPduType.CommitSet in your handler)

For (2), this is a bug, as the error status feature had not been implemented for the AgentX subagent. I've fixed this now and published in version 3.11.1 of the npm.

ivanbaj commented 5 months ago

Thank you sooo much!