Asynchronicity & Net-⁠SNMP AgentX protocol

Vincent Bernat

One of the best ways to add SNMP sup­port to an ap­pli­ca­tion is the use of AgentX pro­to­col: an ap­pli­ca­tion can act as a sub­agent and han­dle re­quests from a mas­ter agent us­ing AgentX pro­to­col. The most wide­spread im­ple­men­ta­tion for such a task is the one from Net-⁠SNMP.

TL;DR

An ap­pli­ca­tion asyn­chronously lever­ag­ing Net-⁠SNMP AgentX im­ple­men­ta­tion has many op­por­tu­ni­ties to get stuck when the mas­ter agent be­comes tem­po­rar­ily un­avail­able. A mostly ef­fi­cient work­around is to ded­i­cate a mas­ter agent for AgentX and del­e­gate MIB han­dling to a subagent.

Pinging the master agent#

Net-⁠SNMP uses an event-based model. It also pro­vides some syn­chro­nous func­tions but they are built on top of the asyn­chro­nous ones. It is shipped with its own event loop. There­fore, Net-⁠SNMP AgentX pro­to­col im­ple­men­ta­tion seems a fine choice to add SNMP sup­port to an event-based pro­gram, for ex­am­ple in Keepalived, a VRRP dae­mon and a mon­i­tor­ing dae­mon for LVS clusters.

Un­for­tu­nately, an AgentX sub­agent is ex­pected to check, at a reg­u­lar in­ter­val, if the mas­ter agent is alive. This is han­dled au­to­mat­i­cally with Net-⁠SNMP im­ple­men­ta­tion as long as you call run_​alarms() func­tion which will call agentx_​check_​session():

/*
 * check a session validity for connectivity to the master agent.  If
 * not functioning, close and start attempts to reopen the session
 */
void
agentx_check_session(unsigned int clientreg, void *clientarg)
{
    /* […] */
    DEBUGMSGTL(("agentx/subagent", "checking status of session %p\n", ss));

    if (!agentx_send_ping(ss)) {
        snmp_log(LOG_WARNING,
                 "AgentX master agent failed to respond to ping.  Attempting to re-register.\n");
        /*
         * master agent disappeared?  Try and re-register.
         * close first, just to be sure .
         */
        agentx_unregister_callbacks(ss);
        agentx_close_session(ss, AGENTX_CLOSE_TIMEOUT);
        /* […] */
        if (main_session != NULL) {
            /* […] */
            main_session = NULL;
            agentx_reopen_session(0, NULL);
        }
        else {
            snmp_close(main_session);
            main_session = NULL;
        }
    } else {
        DEBUGMSGTL(("agentx/subagent", "session %p responded to ping\n",
                    ss));
    }
}

This piece of code will end up call­ing (di­rectly or in­di­rectly) the fol­low­ing func­tions1 which are us­ing agentx_​synch_​response():

  • agentx_​send_ping()
  • agentx_​open_​session()
  • agentx_​reopen_​session()
  • agentx_​close_​session()
  • agentx_​register()
  • agentx_​unregister()
  • agentx_​register_index()
  • agentx_​unregister_index()
  • agentx_​add_​agentcaps()
  • agentx_​remove_​agentcaps()

agentx_​synch_​response() will syn­chronously wait for an an­swer from the mas­ter agent. Your event-based pro­gram will just sit here do­ing noth­ing while this hap­pens. For ex­am­ple, keepalived may be un­able to send VRRP probes re­sult­ing in some se­ri­ous havoc in your cluster.

Func­tions pre­fixed by agentx_ are not part of Net-⁠SNMP API and could there­fore be rewrit­ten to do their work asyn­chronously. Un­for­tu­nately, talk is cheap. More­over, there ex­ist other deep paths that will block a sub­agent. For ex­am­ple, the low-level con­nec­tion to the mas­ter agent (us­ing TCP or a Unix socket, but not with UDP) will use a block­ing connect() call. It seems quite dif­fi­cult to fix this.

Here are some (par­tial) work­arounds:

  • Call agentx_​check_​session() less of­ten. The de­fault in­ter­val is 15 sec­onds. Dis­abling it com­pletely or us­ing a very high value is not ad­vised be­cause this func­tion is the only way to reestab­lish a con­nec­tion to the mas­ter agent when it is, for ex­am­ple, restarted. Here is how to change the de­lay to 120 seconds:

    netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
                       NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL, 120);
    
  • Do not call agentx_​check_​session() if we know the mas­ter agent is alive. For ex­am­ple, if it just sent us a new re­quest, no need to ping it. I have writ­ten a patch im­ple­ment­ing such a work­around. The ABI is left un­touched, there­fore, you only need to re­com­pile Net-⁠SNMP li­braries.

  • Use a lower time­out and no retry. By de­fault, the time­out for syn­chro­nous re­quests is 1 sec­ond and a failed re­quest will be re­tried 5 times. Be­cause sev­eral syn­chro­nous func­tions are used in a row, you may be trapped for as long as 30 sec­onds with these set­tings. Here is how2 to dis­able the retry mech­a­nism and to set the time­out to a very low value:

    static int
    snmp_setup_session_cb(int majorID, int minorID,
                  void *serverarg, void *clientarg)
    {
        netsnmp_session *sess = serverarg;
        sess->timeout = ONE_SEC / 3;
        sess->retries = 0;
        return 0;
    }
    
    void some_init_function()
    {
        /* […] */
        snmp_register_callback(SNMP_CALLBACK_LIBRARY,
           SNMP_CALLBACK_SESSION_INIT,
           snmp_setup_session_cb, NULL);
        /* […] */
    }
    

Unresponsive master agent#

Why is the mas­ter agent un­re­spon­sive? snmpd is also an event-based pro­gram and, trust me, there are a lot of places where it will be­come un­re­spon­sive. Here are two examples:

  • When us­ing the pass or the pass_​persist di­rec­tive, Net-⁠SNMP will block un­til the del­e­gated com­mand out­puts a com­plete line.

    /* var_extensible_pass() in agent/mibgroup/ucd-snmp/pass.c */
    /*
     * valid call.  Exec and get output
     */
    if ((fd = get_exec_output(passthru)) != -1) {
        file = fdopen(fd, "r");
        if (fgets(buf, sizeof(buf), file) == NULL) {
            fclose(file);
            wait_on_exec(passthru);
            /* […] */
            continue;
        }
        /* […] */
        fclose(file);
        wait_on_exec(passthru);
        /* […] */
    }
    
  • DISMAN-PING-MIB im­ple­men­ta­tion will just block while ping­ing the re­mote host. If the host is down and you have re­quested 5 probes with a time­out of 1 sec­ond, snmpd will be un­re­spon­sive for 5 sec­onds. A sim­i­lar prob­lem ex­ists with DISMAN-TRACEROUTE-MIB.

Minimal master agent#

More and more code is run­ning in the main agent. You can­not ex­pect it to not block. But we could add a min­i­mal mas­ter agent to the mix: it will han­dle only a few MIB mod­ules by it­self while most MIB mod­ules will be del­e­gated to a com­plete agent run­ning as a sub­agent. Your own sub­agent will con­nect to the min­i­mal mas­ter agent.

Schema showing the use of a minimal master agent
Adding a minimal master agent and turning snmpd into a subagent

It is quite easy to turn snmpd into a sub­agent: just use the -X flag. We should also dis­able un­wanted MIB mod­ules for each agent with the -I flag. For the min­i­mal mas­ter agent, we only en­able some mod­ules. All other mod­ules3 will be served by snmpd act­ing as a subagent.

$ MODS="snmp_mib,sysORTable,usmConf,usmStats,usmUser,vacm_conf,vacm_context,vacm_vars"
$ snmpd -Lsd -Lf /dev/null -u snmp -g snmp \
>   -C -c /etc/snmp/snmpd.master.conf \
>   -p /var/run/snmpd.master.pid \
>   -I $MODS
$ snmpd -Lsd -Lf /dev/null -u snmp -g snmp \
>   -p /var/run/snmpd.pid -X \
>   -I -$MODS

Ac­cess con­trol con­fig­u­ra­tion should be placed in /etc/​snmp/​snmpd.​master.​conf, as well as master agentx di­rec­tive. Other di­rec­tives (like pass, pass_​persist, load, sysname, …) in /etc/​snmp/​snmpd.conf.

The min­i­mal mas­ter agent is very un­likely to block and while the root cause is still there, it is now very much mit­i­gated. You may ex­tend this so­lu­tion by splic­ing snmpd into sev­eral sub­agents: one han­dling safe mod­ules, one han­dling pass and pass_​persist stuff and a last one for un­safe mod­ules (like DISMAN-PING-MIB).