Recipes for extending Net-⁠SNMP

Vincent Bernat

SNMP stands for Sim­ple Net­work Man­age­ment Pro­to­col. It al­lows a man­ager to query in­for­ma­tion from an agent. A pop­u­lar use of SNMP is to re­trieve net­work in­ter­face coun­ters to plot a band­width graph. While the “S” in SNMP stands for sim­ple, SNMP can be quite dif­fi­cult to deal with. How­ever, it is still the de facto stan­dard for re­triev­ing met­rics in a het­ero­ge­neous en­vi­ron­ment. Net-⁠SNMP is a suite of SNMP ap­pli­ca­tions in­clud­ing an agent. Out of the box, this agent ex­ports a lot of in­for­ma­tion but if some­thing is miss­ing, there are sev­eral ways to ex­tend it. Which ones?

TL;DR

In my opin­ion, ex­tend­ing Net-⁠SNMP should be done with ei­ther of these meth­ods: us­ing extend di­rec­tive, us­ing pass_​persist di­rec­tive, and us­ing AgentX protocol.

SNMP in a nutshell#

The vari­ables ac­ces­si­ble with SNMP through the agent are or­ga­nized in a tree. Each vari­able is typed and as­so­ci­ated to an ob­ject iden­ti­fier (or OID). For ex­am­ple, .1.3.6.1.2.1.31.1.1.1.1.2 is the OID of the name of the sec­ond net­work card.

Usu­ally, SNMP is op­er­ated over UDP, port 161. For the pur­pose of this ar­ti­cle, we con­sider a man­ager can is­sue three kinds of requests:

GET
Re­trieve a variable.
GETNEXT
Re­trieve the next vari­able, in lex­i­cal or­der of their OID. This op­er­a­tion en­ables an SNMP man­ager to walk through all avail­able vari­ables, for ex­am­ple to dis­cover the list of in­ter­faces of an agent. This op­er­a­tion is what makes an SNMP agent dif­fi­cult to im­ple­ment but this is also a crit­i­cal feature.
SET
Re­quest the mod­i­fi­ca­tion of a variable.

Since such an OID may be dif­fi­cult to han­dle by hu­mans, avail­able vari­ables are de­scribed by a MIB mod­ule which is a file that should be parsed to get a map­ping be­tween OID and vari­able names and types. For ex­am­ple, the above OID can also be re­ferred as IF-MIB::​ifName.2. These MIB mod­ules are de­scribed us­ing a sub­set of ASN.1 de­fined in RFC 2578.

A MIB mod­ule al­lows one to group sev­eral vari­ables into a con­cep­tual ta­ble. For ex­am­ple, IF-MIB::​ifDescr, IF-MIB::​ifType, IF-MIB::​ifSpeed are col­umns of IF-MIB::​ifTable. Each row is as­so­ci­ated to an in­dex. In the case of IF-MIB::​ifTable, it is the in­ter­face in­dex. Net-⁠SNMP in­cludes snmptable al­low­ing one to dis­play such a ta­ble in a nat­u­ral manner:1

$ snmptable localhost IF-MIB::ifTable
SNMP table: IF-MIB::ifTable

 ifIndex ifDescr           ifType ifMtu   ifSpeed
       1      lo softwareLoopback 16436  10000000
       2    eth0   ethernetCsmacd  1500 100000000
       3    eth1   ethernetCsmacd  1500  10000000
       4     br0   ethernetCsmacd  1500         0

Vari­ables in­side a ta­ble are called colum­nar ob­jects while out­side ones are scalar ob­jects. SNMP, as a pro­to­col, does not care about this dis­tinc­tion since it does not know the con­cept of MIB modules.

Other use­ful tools from Net-⁠SNMP in­clude snmpget to get a spe­cific vari­able, snmpwalk to dis­cover avail­able vari­ables (with GETNEXT op­er­a­tion) from an OID and snmptranslate to trans­late be­tween ob­ject names and OID.

$ snmpget localhost IF-MIB::ifDescr.2
IF-MIB::ifDescr.2 = STRING: eth0
$ snmpwalk localhost IF-MIB::ifDescr
IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: eth0
IF-MIB::ifDescr.3 = STRING: eth1
IF-MIB::ifDescr.4 = STRING: br0
$ snmpwalk -On public localhost .1.3.6.1.2.1.2.2.1.2
.1.3.6.1.2.1.2.2.1.2.1 = STRING: lo
.1.3.6.1.2.1.2.2.1.2.2 = STRING: eth0
.1.3.6.1.2.1.2.2.1.2.3 = STRING: eth1
.1.3.6.1.2.1.2.2.1.2.4 = STRING: br0
$ snmptranslate -On IF-MIB::ifDescr.3
.1.3.6.1.2.1.2.2.1.2.3

Extending Net-⁠SNMP#

ethtool -S al­lows us to ac­cess var­i­ous sta­tis­tics from a net­work card, like the num­ber of octets re­ceived and trans­mit­ted or the num­ber of col­li­sions. While some of these sta­tis­tics are de­fined in IF-MIB, in RMON-MIB and in EtherLike-MIB, some of them are not avail­able and very spe­cific. There­fore, we would like to ex­port these sta­tis­tics, un­mod­i­fied, through SNMP.

To the best of my knowl­edge, no MIB mod­ule ex­ists for such a us­age. The first task is to write one. It can be called ETHTOOL-MIB. It only con­tains one ta­ble in­dexed by the in­ter­face in­dex and the name of the statis­tic. Here is the ex­pected output:

$ snmpwalk localhost ETHTOOL-MIB::ethtoolStat.2
ethtoolStat.2.'align_errors' = Counter64: 0
ethtoolStat.2.'broadcast' = Counter64: 25
ethtoolStat.2.'multicast' = Counter64: 345
ethtoolStat.2.'rx_errors' = Counter64: 0
ethtoolStat.2.'rx_missed' = Counter64: 0
ethtoolStat.2.'rx_packets' = Counter64: 262011
ethtoolStat.2.'tx_aborted' = Counter64: 0
ethtoolStat.2.'tx_errors' = Counter64: 0
ethtoolStat.2.'tx_multi_collisions' = Counter64: 0
ethtoolStat.2.'tx_packets' = Counter64: 296170
ethtoolStat.2.'tx_single_collisions' = Counter64: 0
ethtoolStat.2.'tx_underrun' = Counter64: 0
ethtoolStat.2.'unicast' = Counter64: 261641

I have coded var­i­ous im­ple­men­ta­tions of this MIB mod­ule and made them avail­able in a Git repository.

With arbitrary commands#

The extend di­rec­tive al­lows the agent to ex­e­cute an ar­bi­trary com­mand and to pro­vide its out­put and sta­tus through NET-SNMP-EXTEND-MIB mod­ule. If we want to ex­port the sta­tis­tics for eth0, we could add some­thing like this in snmpd.​conf, us­ing a cus­tom script to for­mat the out­put of ethtool -S in a way that is more suit­able for our use:

# Export eth0 statistics
extend eth0-names  /full/path/to/ethtool-stats eth0 names
extend eth0-values /full/path/to/ethtool-stats eth0 values

The re­sult can be re­trieved through NET-SNMP-EXTEND-MIB::‌nsExtendOutput2Table:

$ snmpwalk localhost NET-SNMP-EXTEND-MIB::nsExtendOutput2Table
nsExtendOutLine."eth0-names".1 = STRING: tx_packets
nsExtendOutLine."eth0-names".2 = STRING: rx_packets
nsExtendOutLine."eth0-names".3 = STRING: tx_errors
nsExtendOutLine."eth0-names".4 = STRING: rx_errors
nsExtendOutLine."eth0-names".5 = STRING: rx_missed
nsExtendOutLine."eth0-names".6 = STRING: align_errors
nsExtendOutLine."eth0-names".7 = STRING: tx_single_collisions
nsExtendOutLine."eth0-names".8 = STRING: tx_multi_collisions
nsExtendOutLine."eth0-values".1 = STRING: 246309
nsExtendOutLine."eth0-values".2 = STRING: 223941
nsExtendOutLine."eth0-values".3 = STRING: 0
nsExtendOutLine."eth0-values".4 = STRING: 0
nsExtendOutLine."eth0-values".5 = STRING: 0
nsExtendOutLine."eth0-values".6 = STRING: 0
nsExtendOutLine."eth0-values".7 = STRING: 0
nsExtendOutLine."eth0-values".8 = STRING: 0

It is also pos­si­ble to con­fig­ure such a com­mand us­ing SET re­quests. For ex­am­ple, if we want to add sta­tis­tics for eth2, we could is­sue the fol­low­ing command:

$ snmpset -m +NET-SNMP-EXTEND-MIB localhost \
>    'nsExtendStatus."eth2-names"'  = createAndGo \
>    'nsExtendCommand."eth2-names"' = /full/path/to/ethtool-stats \
>    'nsExtendArgs."eth2-names"'    = 'eth2 names' \
>    'nsExtendStatus."eth2-values"'  = createAndGo \
>    'nsExtendCommand."eth2-values"' = /full/path/to/ethtool-stats \
>    'nsExtendArgs."eth2-values"'    = 'eth2 values'
nsExtendStatus."eth2-names" = INTEGER: createAndGo(4)
nsExtendCommand."eth2-names" = STRING: /full/path/to/ethtool-stats
nsExtendArgs."eth2-names" = STRING: eth2 names
nsExtendStatus."eth2-values" = INTEGER: createAndGo(4)
nsExtendCommand."eth2-values" = STRING: /full/path/to/ethtool-stats
nsExtendArgs."eth2-values" = STRING: eth2 values
$ snmpgetnext -m +NET-SNMP-EXTEND-MIB localhost \
>    'nsExtendOutLine."eth2-names"'
nsExtendOutLine."eth2-names".1 = STRING: tx_packets

This can be very con­ve­nient but also a huge se­cu­rity risk. You should dis­able this func­tion­al­ity or en­able it only for some SNMPv3 user.

Us­ing extend does not al­low one to im­ple­ment an ar­bi­trary MIB mod­ule. If the cache is not dis­abled, extend is a good so­lu­tion for var­i­ous sim­ple needs, like pro­vid­ing the con­tent of a one-line file or the out­put of some Na­gios plugin. How­ever, ex­port­ing any­thing tab­u­lar, like in­ter­face sta­tis­tics, is not a good fit.

With pass-through scripts#

Net-⁠SNMP al­lows one to ex­tend the agent by del­e­gat­ing some sub-tree to a script de­fined by the pass_​persist di­rec­tive. Such a script will re­ceive re­quests on its stan­dard in­put with a very sim­ple line-ori­ented pro­to­col de­scribed in the man­ual page of snmpd.conf. Here is an ex­am­ple of in­teraction:

→  PING
←  PONG
→  get
→  .1.3.6.1.4.1.39178.100.1.1.1.2.2.109.117.108.116.105.99.97.115.116
←  .1.3.6.1.4.1.39178.100.1.1.1.2.2.109.117.108.116.105.99.97.115.116
←  counter64
←  223941
→  getnext
→  .1.3.6.1.4.1.39178.100.1.1.1.2.2
←  .1.3.6.1.4.1.39178.100.1.1.1.2.2.97.108.105.103.110.95.101.114
←  counter64
←  0

The pro­to­col is sim­ple enough to im­ple­ment it from scratch in any script­ing lan­guage. How­ever, in Perl, you may pre­fer to use SNMP::‌ExtensionSNMP::‌PassPersist ex­ten­sion from Sébastien Aperghis-Tra­moni. You can find the com­plete ex­am­ple on GitHub.

use SNMP::Extension::PassPersist;
my $extsnmp = SNMP::Extension::PassPersist->new(
    backend_collect => \&update_tree
    );
$extsnmp->run;

sub update_tree {
    my @interfaces = </sys/class/net/*>;
    foreach my $interface (@interfaces) {
        $interface =~ s/^.*\///;
        open(ETHTOOL, "ethtool -S $interface 2>/dev/null |") or next;
        while (<ETHTOOL>) {
            /^\s+(\w+): (\d+)$/ or next;
            my $name  = $1;
            my $value = int($2);
            my $oid   = oid_compute($interface, $name);
            $extsnmp->add_oid_entry($oid, "counter", $value);
        }
        close(ETHTOOL);
    }
}

With Python, snmp-passper­sist mod­ule, from Nico­las Ag­ius, pro­vides a sim­i­lar in­ter­face. Look at the com­plete ex­am­ple on GitHub.

pass_​persist al­lows you to im­ple­ment an ar­bi­trary MIB mod­ule with hon­est per­for­mance. Here are some im­por­tant things to know about this di­rective:

  • Un­less you have a re­cent ver­sion of Net-⁠SNMP, 64-bit types are con­verted to 32-bit equiv­a­lents. The coun­ters may over­flow quickly.
  • While han­dling a re­quest, you are not al­lowed to pause (for ex­am­ple, to fetch a re­mote in­for­ma­tion): the agent would be­come un­re­spon­sive. If you need slow-to-get data or data from the agent, grab them in a sep­a­rate thread and en­sure you have them be­forehand.

With AgentX protocol#

AgentX is a pro­to­col de­fined in RFC 2741 al­low­ing a mas­ter agent to be ex­tended by in­de­pen­dent sub-agents. For snmpd to be­come a mas­ter agent, you just need to add master agentx in snmpd.​conf. Here are some of the ben­e­fits of an AgentX sub-agent over pass_​persist:

  1. No con­fig­u­ra­tion is needed for the mas­ter agent to ac­cept an ad­di­tional sub-agent. A sub-agent reg­is­ters to the mas­ter agent the MIB mod­ules (or part of them) it wants to take care of.
  2. A sub-agent is de­cou­pled from the mas­ter agent. It can run with a dif­fer­ent iden­tity or be in­te­grated into an­other dae­mon to ex­port its in­ter­nal met­rics, send traps or al­low re­mote con­fig­u­ra­tion through SNMP.
  3. AgentX pro­to­col can be car­ried over TCP. Sub-agents can there­fore run on a for­eign host or in a jailed en­vi­ronment.
  4. 64-bit types are fully sup­ported. Traps are also supported.

Perl and Python#

Net-⁠SNMP pro­vides a Perl mod­ule, Net-⁠SNMP::​agent, to write a sub-agent us­ing AgentX pro­to­col. This mod­ule is not as con­ve­nient as the mod­ule for pass_​persist de­scribed above since it does not pro­vide a con­ve­nient layer to put re­quired in­for­ma­tion into some data­s­tore. It mim­ics the avail­able C API. Here are a few ex­am­ples of its use:

There is no sim­i­lar bind­ing for Python but there is an agentx ex­ten­sion us­ing ctypes. It pro­vides a more high-level view. I did not write an ex­am­ple for this one but you can look at the ex­am­ple pro­vided by the author.

C with “new” API#

To write a sub-agent in C, you have to choose be­tween two API:

  1. the API in­her­ited from UCD-SNMP also known as the “tra­di­tional” API;
  2. the new API de­vel­oped for Net-⁠SNMP 5.x.

With both API, a han­dler is reg­is­tered to take care of one or sev­eral sub-trees. With the tra­di­tional API, the han­dler is in­voked with very lit­tle in­for­ma­tion. It is then up to you to lo­cate the ap­pro­pri­ate vari­able by map­ping the in­dex part of the OID to the ob­jects you want to ex­port. It is quite sim­ple for scalar ob­jects but colum­nar ob­jects with com­plex in­dexes are a pain with such an API.

The new API pro­vides var­i­ous helpers. Some of them al­low you to copy ob­jects into an ar­ray or a list and let Net-⁠SNMP li­brary do the hard work of serv­ing them. An­other helper will pro­vide meth­ods to serve ob­jects with­out putting them in a spe­cial struc­ture but by pro­vid­ing an it­er­a­tor. Some of these helpers can also help you to cache some variables.

On top of this new API, Net-⁠SNMP comes with mib2c, a tool that will help you con­vert a MIB mod­ule into C code: af­ter a few ques­tions, you will get a skele­ton C code you will have to com­plete with the logic to re­trieve real ob­jects. A gen­er­ated file also pro­vides di­rec­tions on what to do (with some magic com­mand to have a step-by-step cookbook).

I was able to im­ple­ment ETHTOOL-MIB by just run­ning it through mib2c, choos­ing the ta­ble con­tainer helper with cache and adding some code in ethtoolStatTable_​data_​access.c. You can look at the re­sult on GitHub. Here is a stripped-down ver­sion2 of what has been modified:

/* Socket for ioctl */
skfd = socket(AF_INET, SOCK_DGRAM, 0);

/* Iterate through all interfaces */
getifaddrs(&ifap);
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
    /* Grab statistics name and values */
    strcpy(ifr.ifr_name, ifa->ifa_name);
    drvinfo.cmd         = ETHTOOL_GDRVINFO;
    ifr.ifr_data        = (caddr_t) &drvinfo;
    ioctl(skfd, SIOCETHTOOL, &ifr);
    n_stats             = drvinfo.n_stats;
    strings->cmd        = ETHTOOL_GSTRINGS;
    strings->string_set = ETH_SS_STATS;
    strings->len        = n_stats;
    ifr.ifr_data        = (caddr_t) strings;
    ioctl(skfd, SIOCETHTOOL, &ifr);
    stats->cmd          = ETHTOOL_GSTATS;
    stats->n_stats      = n_stats;
    ifr.ifr_data        = (caddr_t) stats;
    ioctl(skfd, SIOCETHTOOL, &ifr);

    ifIndex = if_nametoindex(ifa->ifa_name);

    /* Iterate through statistics */
    for (i = 0; i < n_stats; i++) {

        /* Declare the appropriate index */
        strncpy(ethtoolStatName,
                (char *)&strings->data[i * ETH_GSTRING_LEN],
                ETH_GSTRING_LEN);
        ethtoolStatName[sizeof(ethtoolStatName) - 1] = '\0';
        ethtoolStatName_len = strlen(ethtoolStatName);

        /* Append this new variable to the container. */
        rowreq_ctx = ethtoolStatTable_allocate_rowreq_ctx();
        ethtoolStatTable_indexes_set(rowreq_ctx,
                    ifIndex,
                    ethtoolStatName, ethtoolStatName_len);
        rowreq_ctx->data.ethtoolStat.high = stats->data[i] >> 32;
        rowreq_ctx->data.ethtoolStat.low = (uint32_t)stats->data[i];

        CONTAINER_INSERT(container, rowreq_ctx);
    }
}

See? It is pretty easy. I have only enu­mer­ated all the ob­jects I wanted to ex­pose. I didn’t even have to con­vert sta­tis­tics names to an OID, mib2c built a ethtoolStatTable_​indexes_​set() for this pur­pose. OpenHPI project pro­vides an ex­ten­sive doc­u­men­ta­tion to write a sub-agent this way.

Now, on the down­side, to im­ple­ment a sin­gle ta­ble, I have 14 gen­er­ated files whose code style is usu­ally un­fit to in­te­grate into an ex­ist­ing project: some peo­ple, my­self in­cluded, do not like au­to­mat­i­cally gen­er­ated code, all the more when it is so ver­bose. The so­lu­tion would be to use the new API with­out mib2c; un­for­tu­nately, while the doc­u­men­ta­tion ex­ists, all pro­vided ex­am­ples rely ex­clu­sively on mib2c tool. Un­less you are able to grasp the new API with­out it, I would re­strict its use to two cases:

  • Writ­ing a new mod­ule for in­clu­sion into Net-⁠SNMP. Since most mod­ules are now writ­ten with the help of mib2c, there is no cod­ing style problem.
  • Writ­ing a stand­alone sub-agent for some MIB. As a stand­alone project, you won’t run into cod­ing style prob­lems either.

For other uses, let’s have a look at the tra­di­tional API.

C with “traditional” API#

The tra­di­tional API is way sim­pler and even if it is quite old, it is eas­ier to find as­so­ci­ated ex­am­ples. Here is how to ini­tial­ize the agent, again, in a stripped-down ver­sion (the com­plete ver­sion is on GitHub):

static oid ethtool_oid[] = {1, 3, 6, 1, 4, 1, 39178, 100, 1};
static struct variable3 ethtool_vars[] = {
  {1, ASN_COUNTER64, RONLY, ethtool_stat, 3, {1, 1, 2}}
};

int main()
{
  netsnmp_enable_subagent();
  snmp_disable_log();
  snmp_enable_stderrlog();
  init_agent("ethtoolAgent");
  REGISTER_MIB("ethtoolStatTable", ethtool_vars,
               variable3, ethtool_oid);
  init_snmp("ethtoolAgent");
  while (1)
    agent_check_and_process(1);
}

The REGISTER_​MIB() macro is used to reg­is­ter the pro­vided root OID (ethtool_​oid) to the mas­ter agent. It also reg­is­ters han­dlers as­so­ci­ated to var­i­ous sub-OID through the ethtool_​vars[] ar­ray. Each mem­ber of this ar­ray fea­tures the fol­low­ing fields:

  • magic is an in­te­ger al­low­ing one to dis­crim­i­nate OID han­dled by the same han­dler. For a ta­ble, this al­lows one to han­dle sev­eral col­umns with the same han­dler since they share a com­mon index.
  • type is the type of the vari­able that will be returned.
  • acl tells if the ob­ject is read-only (RONLY) or read-write (RWRITE).
  • findVar is the han­dler as­so­ci­ated to the ob­ject. Its task will be to find the ap­pro­pri­ate ob­ject and re­turn its value.
  • The two last fields are the rel­a­tive OID this en­try is re­spon­si­ble for along with its length.

The pro­to­type of ethtool_​stat() han­dler func­tion is the following:

static u_char*
ethtool_stat(struct variable *vp, oid *name, size_t *length,
             int exact, size_t *var_len, WriteMethod **write_method);

This func­tion should re­turn the value of the re­quested vari­able or NULL if no ap­pro­pri­ate ob­ject was found. The value can be stat­i­cally stored since it will be copied. Here is the de­scrip­tion of the ar­guments:

  • struct variable *vp is the struc­ture the han­dler was as­so­ci­ated with, ex­cept that vp->name is now a full OID, not a rel­a­tive one. You can ac­cess vp->magic to de­ter­mine which col­umn or which scalar to han­dle if this han­dler is com­mon to sev­eral objects.
  • oid *name and size_​t *length de­scribe the OID re­quested. If you want to re­turn an­other OID (for a GETNEXT re­quest for ex­am­ple), you can mod­ify them. name points to an OID ar­ray large enough to fit any OID you want to return.
  • int exact tells if the re­quested OID should be ex­actly matched (case of GET or SET) or not (GETNEXT).
  • size_​t *var_​len is used to tell the size of the re­turned object.
  • WriteMethod **write_​method is only used if the vari­able is mod­i­fi­able. When a SET re­quest is re­ceived, the API will call the han­dler with exact set to 1 and ex­pects to get a pointer to the func­tion that will han­dle the write op­eration.3

To im­ple­ment this func­tion, you are on your own. When re­ly­ing on ex­ter­nally fetched data, you need to han­dle a cache your­self. I have used a red-black tree for this pur­pose (with code stolen from OpenBSD). This also en­ables an easy im­ple­men­ta­tion of the GETNEXT op­eration.

Here are three projects us­ing this API to pro­vide SNMP support:

  • Keepalived is a VRRP dae­mon and a mon­i­tor­ing dae­mon for LVS clus­ters. I have added a com­plete SNMP sup­port. The most in­ter­est­ing files are core/​snmp.c, check/​check_​snmp.c and vrrp/​vrrp_​snmp.c. It fea­tures a tight in­te­gra­tion into Keepalived event loop that should be re­us­able by other projects. It in­cludes read-only sup­port of all data struc­tures (con­fig­u­ra­tion, state and sta­tis­tics), abil­ity to send traps on events and a write sup­port for some values.

  • lldpd is an im­ple­men­ta­tion of 802.1AB (LLDP) which is a pro­to­col al­low­ing an equip­ment to ad­ver­tise it­self to its neigh­bors on Eth­er­net level. 802.1AB comes with a huge MIB mod­ule and many ex­ten­sions. lldpd pro­vides a fairly com­plete read-only im­ple­men­ta­tion of this MIB mod­ule. Some ta­bles have com­plex in­dexes. Have a look at agent.c. There is cur­rently no write sup­port and no traps but lldpd fea­tures priv­i­lege sep­a­ra­tion and some code al­lows it to still use a Unix socket to speak with the mas­ter agent.

  • As­ter­isk, an open source PBX, in­cludes an AgentX sub-agent al­low­ing one to grab some met­rics. Look at res/​snmp/​agent.c.

Up­date (2012-03)

If it is crit­i­cal for your project to not block, you should be aware that us­ing Net-⁠SNMP im­ple­men­ta­tion of AgentX has some is­sues. Have a look at my other post on this topic.

Other solutions#

The man­ual page for snmpd.conf ex­plores other ways to ex­tend Net-⁠SNMP agent un­der the sec­tion “Ex­tend­ing agent func­tionality”:

  • exec and sh are dep­re­cated. Use extend instead.
  • pass is a bit sim­pler than pass_​persist. The com­mand is run once for each re­quest. Such a com­mand can eas­ily be turned into a pass_​persist script. More­over, Net-⁠SNMP will not an­swer any re­quest while a pass com­mand is running.
  • proxy redi­rects re­quests to an­other SNMP agent. Un­less you have some code that can only act as a com­plete SNMP agent, it is bet­ter to use AgentX in­stead. For pro­grams us­ing Net-⁠SNMP API, turn­ing an agent into an AgentX sub-agent is just one line of code. Since a sub-agent in­volves less code than a com­plete agent, it is more ef­fi­cient and more reliable.
  • smux has been su­per­seded by AgentX.
  • perl and dlmod al­low one to load and ex­e­cute ex­ter­nal code in­side the run­ning agent. Un­less you have im­por­tant per­for­mance re­quire­ments, it is bet­ter to use a sub-agent in­stead: if some bug hap­pens, only the sub-agent will be af­fected, not the mas­ter agent. A sub-agent sticks more closely to the Unix phi­los­o­phy: “write pro­grams that do one thing and do it well.” How­ever, Net-⁠SNMP FAQ says:

Im­ple­ment­ing the mod­ule in C within the main agent (di­rectly or via dlmod) is prob­a­bly the most ef­fi­cient and re­li­able, closely fol­lowed by em­bed­ded perl (or python) ex­ten­sions. These have the ad­van­tage of min­i­mal over­heads be­tween the code im­ple­ment­ing the MIB mod­ule, and the agent frame­work, and no in­ter-process com­mu­ni­ca­tion issues.