Re: Problems registering AgentX (in C)

Bill Fenner <[email protected]>
Newsgroups gmane.network.net-snmp.user
Message-ID <CAF4SogapOwuX5LkEhMMUNN7M3uMFT7cHhSOmjpPSAH43e1A+tg@mail.gmail.com>
On Wed, Aug 12, 2015 at 6:47 AM, Ulrich Windl <
[email protected]> wrote:

> Hi again!
>
> Even after adding "snmp_set_do_debugging(1);" at the beginning of my
> program, I have no real clue; the last lines I get are these:
> ---
> trace: snmp_sess_select_info(): snmp_api.c, 5862:
> sess_select: for all sessions: 5 3
> sess_select: next alarm 0.-1020349360 sec
> sess_select: blocking:no session requests or alarms.
> ---
>
> I'm wondering: How does main() automatically process the options in the
> tutorial ("% ./mysubagent -f -Lo -x  tcp:localhost:1705")? The
> example-daemon.c has no special commands for that.
>

I built my main around the one that "net-snmp-config --compile-subagent"
creates for you.  It has lots of useful command-line parsing, including
config files, debugging, forking, etc. You can add the "--norm" argument to
"net-snmp-config --compile-subagent" and it will say "leaving the temporary
code file: arglebargle.c" and then you can rename it and build it yourself.

>
> I found one problem: I missed to add 'init_snmp("testagent");', but even
> then it didn't work.
>
> Found the most important problem: Once I put the local config file
> (testagent.conf) into /etc/snmp, the agent became much more alive!
>
> So the final question is: Is there a way to tell your subagent to locate
> the config file in the current directory? If you need to put it into
> /etc/snmp, you need root rights for development, and having a local,
> non-priviledged snmp Daemon for testing makes no sense.
>

If you use the command line parsing suggested above, you can run it with
"-c whatever.conf".  If you don't want to do that, you can specify the
config file inside your main() with
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,NETSNMP_DS_LIB_OPTIONALCONFIG,
"whatever.conf");

  Bill


> Regards,
> Ulrich
>
> >>> "Ulrich Windl" <[email protected]> schrieb am
> 12.08.2015 um
> 12:01 in Nachricht <[email protected]>:
> > Hi!
> >
> > I got absolute zero replies to my message, so I created an isolated test
> > case in the mean-time. The test case looks like this:
> >
> > static  volatile int    stop_requests = 0;      /* count stop requests */
> >
> > /* main */
> > int     main(int argc, char *argv[])
> > {
> >         int             result  = 0;
> >
> >         /*BEGIN SNMP*/
> >         /* prepare for AgentX subagent */
> >         if ( netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
> >                                     NETSNMP_DS_AGENT_ROLE, 1)
> >              != SNMPERR_SUCCESS )
> >         {
> >                 result = 2;
> >         }
> >         SOCK_STARTUP;           /* initialize TCP/IP, if necessary */
> >         init_agent("testagent");        /* initialize the agent library
> */
> >         init_IOTWatchMIB();     /* initialize MIB module */
> >         while ( stop_requests == 0 ) {
> >                 agent_check_and_process(1); /* 0 == don't block */
> >         }
> >
> >         /* at shutdown time */
> >         snmp_shutdown("testagent");
> >         SOCK_CLEANUP;
> >         /*END SNMP*/
> > quit:   return(result);
> > }
> >
> > When running the testagent with ltrace, I see the handlers are
> registered,
> > and the test agent waits for data:
> >
> > ---
> > SNMP> ltrace ./testagent
> > __libc_start_main(0x400f40, 1, 0x7ffc0e2bb4e8, 0x4011b0, 0x4011a0
> <unfinished
> > ...>
> > netsnmp_ds_set_boolean(1, 1, 1, 0, 0x7fd165f0f320) = 0
> > init_agent(0x4012a4, 0x7fd1663d1f40, 0, 1, 0x7fd165f0f320) = 0
> > snmp_get_do_debugging(1, 1, 0, 1, 0x2965736c61667c) = 0
> > netsnmp_create_handler_registration(0x40134d, 0x401040, 0x602120, 13, 1)
> =
> > 0x6071a0
> > netsnmp_register_scalar(0x6071a0, 0x602188, 0, 1, 1000) = 0
> > netsnmp_create_handler_registration(0x40135f, 0x400fd0, 0x6020a0, 13, 1)
> =
> > 0x607d40
> > netsnmp_register_scalar(0x607d40, 0x602108, 0, 2, 1000) = 0
> > agent_check_and_process(1, 0x7fd166a33049, 0, 0, 0x608050
> > ---
> >
> > However If I query the OIDs registered, no data are returned ("There is
> no
> > such variable name in this MIB.")
> >
> > The init Module looks like this:
> >
> > /** Initializes the IOTWatchMIB module */
> > void
> > init_IOTWatchMIB(void)
> > {
> >     static oid      giveItABetterName_oid[] =
> >         { 1, 3, 6, 1, 4, 1, 8072, 9999, 9999, 1000, 1, 1, 1 };
> >     static oid      giveThisANameToo_oid[] =
> >         { 1, 3, 6, 1, 4, 1, 8072, 9999, 9999, 1000, 1, 1, 2 };
> >
> >     DEBUGMSGTL(("IOTWatchMIB", "Initializing\n"));
> >
> >     netsnmp_register_scalar(netsnmp_create_handler_registration
> >                             ("giveItABetterName",
> handle_giveItABetterName,
> >                              giveItABetterName_oid,
> >                              OID_LENGTH(giveItABetterName_oid),
> >                              HANDLER_CAN_RONLY));
> >     netsnmp_register_scalar(netsnmp_create_handler_registration
> >                             ("giveThisANameToo", handle_giveThisANameToo,
> >                              giveThisANameToo_oid,
> >                              OID_LENGTH(giveThisANameToo_oid),
> >                              HANDLER_CAN_RONLY));
> > }
> >
> > (The handlers both should cause an abort if called (to help debugging)):
> >
> >     switch (reqinfo->mode) {
> >
> >     case MODE_GET:
> >         snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
> >                                  (u_char *) 0 /* XXX: a pointer to the
> > scalar's data */
> >                                  , 1 /* XXX: the length of the data in
> bytes
> > */ );
> >         break;
> >
> > I have absolutely no idea how to debug this. While debugging I'm running
> > snmpd in user mode as suggested in the tutorial:
> > /usr/sbin/snmpd -f -Lo -C --rwcommunity=public --master=agentx \
> > --agentXSocket=tcp:localhost:1705 udp:1161 -Dagentx
> >
> > I fail to see where my testagent connects to the SNMP socket, nor do I
> see
> > that my config file is opened. The select() is on a pipe, but I cannot
> see
> > any other process in strace:
> > ---
> > 2819  munmap(0x7f84066bd000, 4096)      = 0
> > 2819  gettimeofday({1439373523, 620867}, NULL) = 0
> > 2819  pipe([3, 4])                      = 0
> > 2819  gettimeofday({1439373523, 621388}, NULL) = 0
> > 2819  pipe([5, 6])                      = 0
> > 2819  select(6, [3 5], NULL, NULL, NULL) = ? ERESTARTNOHAND (To be
> > restarted)
> > 2819  --- SIGINT (Interrupt) @ 0 (0) ---
> > 2819  +++ killed by SIGINT +++
> > ---
> >
> > Any ideas?
> >
> > Regards,
> > Ulrich
> >
> >>>> Ulrich Windl schrieb am 05.08.2015 um 10:42 in Nachricht
> <55C1CC7D.CAD : 161
> > :
> > 60728>:
> >> Hello!
> >>
> >> I had reported problems with registering an AgentX subagent in Perl (no
> >> error, but doesn't work). Finally I found the bug that prevented it from
> >> working.
> >>
> >> Now I have a C program that uses multiple threads to retrieve and
> monitor
> >> some values. I tried to make this program an AgentX subagent by adding
> the
> >> code from the C coding example. Unfortunately the same things as in Perl
> >> happen: The agent does not connect, and there is no error. When trying
> to
> >> access any of the registered OIDs, an error is returned. None of the
> > handlers
> >> is ever called.
> >>
> >> What I'd like to have eventually is that one thread takes care of all
> the
> >> SNMP handling.
> >>
> >> From the example I guess that the API is too high-level (init_agent()):
> It's
> >> hard to see which steps are actually executed, and what the results of
> these
> >
> >> steps are.
> >>
> >> What could the problem be, and how could I debug it?
> >>
> >> The essential code fragemnt looks like this:
> >>                 /*BEGIN SNMP*/
> >>                 /* prepare for AgentX subagent */
> >>                 if ( netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
> >>                                             NETSNMP_DS_AGENT_ROLE, 1)
> >>                      != SNMPERR_SUCCESS )
> >>                 {
> >>                         /* report problem */
> >>                 }
> >>                 SOCK_STARTUP;           /* initialize TCP/IP, if
> necessary
> >> */
> >>                 init_agent("iotwatch"); /* initialize the agent library
> */
> >>                 init_IOTWatchMIB();     /* initialize MIB module */
> >>                 while ( stop_requests == 0 ) {
> >>                         agent_check_and_process(1); /* 0 == don't block
> */
> >>                 }
> >>
> >>                 /* at shutdown time */
> >>                 snmp_shutdown("iotwatch");
> >>                 SOCK_CLEANUP;
> >>                 /*END SNMP*/
> >>
> >> Regards,
> >> Ulrich
> >>
> >>
> >
> >
> >
> >
> >
> ------------------------------------------------------------------------------
> > _______________________________________________
> > 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
>
>
>
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> 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
>

------------------------------------------------------------------------------

_______________________________________________
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.