RE: Newbie question: writing a simple agent

[email protected]
Newsgroups gmane.network.net-snmp.user
Message-ID <[email protected]>
Hello Graeme,

sorry for bothering you again.

I have tried to merge your code with MFD code. I am obviously doing
something wrong. When I run snmpwalk on the agent I can see that
agent_check_and_process(1) in the main loop finishes but the handler does
not get called.

I get error message from the agent:

Connection from UDP: [127.0.0.1]:-32767

I assume this means this request was not handled by the agent.

I do not know if netsnmp_register_handler(reg) is successful, it returns
0. I was googling but I did not find meaning of this value.

Did I miss something in my main()? I am attaching short code.

Thank you and please do net get angry with me for so many mails.

Milan

----


#include "agent.h"
#include "agent_error.h"

static int keep_running;

void stop_server(UNUSED int a) {
    keep_running = 0;
}

/* Ippc function which will be called for all OIDs below the root one
registered above */
int IppcOidHandler(UNUSED netsnmp_mib_handler *handler,
                   UNUSED netsnmp_handler_registration *reginfo,
                   UNUSED netsnmp_agent_request_info *reqinfo,
                   netsnmp_request_info *requests)
{
  int ret = SNMP_ERR_NOERROR;

  printf("Received SNMP request");
  for (unsigned i = 0; i < requests->range_end_len; i++) {
      printf("%d.", (int)requests->range_end[i]);
  }
  printf("\n");

  switch(reqinfo->mode)
  {
    case MODE_GET:
         /* Call some other code to process the 'requests' argument */
         //ret = myData->getData( requests );
         break;
    case MODE_GETNEXT:
         /* Call some other code to process the 'requests' argument */
         //ret = myData->getNextData( requests );
         break;
    default:
         /* we should never get here in my code */
         //iRet = SNMP_ERR_GENERR;
         break;
  }
  return 1; //ret;
}

int main (UNUSED int argc, UNUSED char **argv) {
  netsnmp_mib_handler *handler;
  netsnmp_handler_registration *reg;

  static oid root_oid[] =  { 1, 3, 6 };

  snmp_enable_stderrlog();

  SOCK_STARTUP;

  init_agent("ippc agent");

  init_snmp("ippc agent");

  init_master_agent();  /* open the port to listen on (defaults to
udp:161) */

  keep_running = 1;
  signal(SIGTERM, stop_server);
  signal(SIGINT, stop_server);

  handler = netsnmp_create_handler("IppcOIDHandler", &IppcOidHandler );

  if (handler) {
      reg = netsnmp_handler_registration_create( "IppcHandler",
                                                 handler,
                                                 root_oid,
                                                 OID_LENGTH(root_oid),
                                                 0);

      printf("returned=%d\n", netsnmp_register_handler(reg));
   }
   else {
      exit(1);
   }


  while(keep_running) {
    agent_check_and_process(1); /* 0 == don't block */
    printf("mainloop\n");
  }

  /* at shutdown time */
  netsnmp_unregister_handler(reg);
  snmp_shutdown("ippc agent");
  SOCK_CLEANUP;

  return 0;
}




----


On Wed, August 8, 2007 4:10 pm, Graeme Wilson wrote:
> Hi Milan,
>
>
> No worries about the questions.
>
>
> For my subagent (binary/executable) I doctored an existing 'main' that I
> had from when I'd created a subagent with mib2c - its just like your
> example below: - init_agent
> - etc etc
> - register your handler here
> - loop (agent_check_and_process)
> - etc etc
>
>
> Cheers
> Graeme
>
>
>
>> -----Original Message-----
>> From: [email protected] [mailto:[email protected]]
>> Sent: 08 August 2007 14:47
>> To: Graeme Wilson
>> Cc: [email protected]
>> Subject: RE: Newbie question: writing a simple agent
>>
>>
>> Hello Graeme,
>>
>>
>> I went through your code and it seems it is exactly what I need.
>>
>>
>> I am just confused about one thing: how does it plug into snmpd?
>>
>>
>> I understand that I need to do in agent main() registration
>> (and un-registration at agent finish)  but what do I actually
>> generate?
>>
>> Should I create a binary like in MFD example and loop using
>> agent_check_and_process() or snmp_select_info() or should I create some
>> kind of .so that does mere regsitration and finishes?
>>
>> In the former case the scenario is
>> - init_agent
>> - init_snmp
>> - init_master_agent();
>> - register handler
>> - loop
>> - un-register handler
>> - snmp_shutdown
>>
>>
>> or am I missing something?
>>
>> Sorry for asking you newbie trivial questions :-)
>>
>>
>> Thanks.
>>
>>
>> Milan
>>
>>
>>
>>
>>
>>
>> On Wed, August 8, 2007 11:11 am, Graeme Wilson wrote:
>>
>>>
>>
>>> Hi,
>>>
>>>
>>>
>>> I have done something similar recently (I provide some sample code
>>> below - but note that ultimately everything is still done within
>>>
>> the agent).
>>> My Agent simply registers the root OID of the MIB it will
>>>
>> handle and
>>> some other function knows all about the data and the specific OIDs
>>> that the Agent handles. I just registered a generic handler for the
>>> root oid and
>> then when it
>>> is called, the handler make appropriate calls to handle the
>> request.
>>> I guess in your case you could then request the data (if
>>>
>> any) from the
>>> process/thread that holds the Data and OIDS.
>>>
>>> Hope that helps.
>>> Graeme
>>> -------------
>>> /* In your main/initialise function */
>>> netsnmp_mib_handler* handler = netsnmp_create_handler(
>>> "GenericOIDHandler", &genericOidHandler );
>>>
>>>
>>>
>>> netsnmp_handler_registration* reg =
>>> netsnmp_handler_registration_create( "GenericHandler",
>>> handler, root_oid, OID_LENGTH(root_oid), 0 );
>>>
>>> netsnmp_register_handler( reg );
>>>
>>>
>>>
>>> /* Generic function which will be called for all OIDs below
>>>
>> the root
>>> one registered above */ int genericOidHandler( netsnmp_mib_handler
>>> *handler, netsnmp_handler_registration *reginfo,
>>> netsnmp_agent_request_info *reqinfo, netsnmp_request_info
>> *requests )
>>
>>> { int ret = SNMP_ERR_NOERROR;
>>>
>>>
>>> switch(reqinfo->mode) { case MODE_GET: /* Call some other code to
>>> process the 'requests' argument */ ret = myData->getData( requests );
>>> break; case MODE_GETNEXT: /* Call some other code to process the
>>> 'requests'
>>> argument */ ret = myData->getNextData( requests ); break; default: /* we
>>> should never get here in my code */ iRet = SNMP_ERR_GENERR; break; }
>>> return ret; }
>>>
>>>
>>>
>>>
>>>> -----Original Message-----
>>>> From: [email protected]
>>>> [mailto:[email protected]] On Behalf Of
>>>> [email protected]
>>>> Sent: 08 August 2007 06:51
>>>> To: Mike Ayers
>>>> Cc: [email protected]
>>>> Subject: RE: Newbie question: writing a simple agent
>>>>
>>>>
>>>>
>>>> Hello Mike,
>>>>
>>>>
>>>>
>>>> thanks for replying to my question.
>>>>
>>>> I understand that a standard approach is to register the
>>>>
>> agent for a
>>>> range of OISs and to let the agent to be OID aware while the other
>>>> process should take care of the underlaying data.
>>>>
>>>> However, I was asked to write an agent that would NOT be
>>>>
>> OID-aware.
>>
>>>> All it would know it is responsible for a range of OIDs without
>>>> knowing any details. It would forward complete OIDs to the other
>>>> process that would take care about OID
>> disassembly/translation, data
>>>> manipulation etc.
>>>>
>>>> I am wondering if this is possible within the Net SNMP API, at all.
>>>>
>>>>
>>>>
>>>> If so, where should I start?
>>>>
>>>>
>>>>
>>>> Thanks.
>>>>
>>>>
>>>>
>>>> Milan
>>>>
>>>>
>>>>
>>>> On Tue, August 7, 2007 11:44 pm, Mike Ayers wrote:
>>>>
>>>>
>>>>>
>>>>
>>>>>> From: [email protected]
>>>>>> [mailto:[email protected]] On
>>>>>>
>> Behalf Of
>>
>>>>>> [email protected]
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>> The agent itself will not be aware of individual OIDs and their
>>>>>>  meaning. This will be done by the other process that also
>>>> holds the
>>>>>> actual data.
>>>>>
>>>>> This is a bit of a design problem as stated.  The (sub)agent is
>>>>> responsible for managing its OID range, and therefore it is not
>>>>> possible to have a subagent completely unaware of the OIDs.
>>>> This does
>>>>
>>>>
>>>>> not, however, mean that the subagent needs to be aware of
>> the data
>>>>> underlying the MIB.
>>>>>
>>>>> A common paradigm is to have the subagent register for
>>>>>
>> the range of
>>>>> values (table, MIB, whatever) that it will manage, and
>> have the OP
>>>>> (other
>>>>> process) manage the data within the range.  The agent does OID
>>>>> assembly/disassembly (i.e. breaking OIDs into range part
>>>> and specific
>>>>> part), possibly reformat the specific part to make it more
>>>>> understandable to the OP (thus only the subagent need be
>>>> MIB aware).
>>>>
>>>>
>>>>> The OP returns the values for the requested data, then
>>>>>
>> the subagent
>>>>> formats and assembles the response.
>>>>>
>>>>>
>>>>> HTH,
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Mike
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>> ---------------------------------------------------------------------
>>
>>>> -
>>>>
>>>>
>>>>> ---  This SF.net email is sponsored by: Splunk Inc.
>>>>> Still grepping through log files to find problems?  Stop.
>>>>> Now Search log events and configuration files using AJAX
>>>>>
>>>>>
>>>> and a browser.
>>>>> Download your FREE copy of Splunk now >>  http://get.splunk.com/
>>>>> _______________________________________________
>>>>> Net-snmp-users mailing list
>>>>> [email protected]
>>>>> Please see the following page to unsubscribe or change
>>>>>
>>>>>
>>>> other options:
>>>>> https://lists.sourceforge.net/lists/listinfo/net-snmp-users
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --------------------------------------------------------------
>>>> -----------
>>>> This SF.net email is sponsored by: Splunk Inc.
>>>> Still grepping through log files to find problems?  Stop.
>>>> Now Search log events and configuration files using AJAX and a
>>>> browser. Download your FREE copy of Splunk now >>
>>>> http://get.splunk.com/
>>>> _______________________________________________
>>>> Net-snmp-users mailing list
>>>> [email protected]
>>>> Please see the following page to unsubscribe or change
>>>>
>> other options:
>>>> https://lists.sourceforge.net/lists/listinfo/net-snmp-users
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>> ----------------------------------------------------------------------
>>
>>> ---  This SF.net email is sponsored by: Splunk Inc.
>>> Still grepping through log files to find problems?  Stop.
>>> Now Search log events and configuration files using AJAX
>>>
>> and a browser.
>>> Download your FREE copy of Splunk now >>  http://get.splunk.com/
>>> _______________________________________________
>>> Net-snmp-users mailing list
>>> [email protected]
>>> Please see the following page to unsubscribe or change
>>>
>> other options:
>>> https://lists.sourceforge.net/lists/listinfo/net-snmp-users
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>
>



-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.