python-agentx: Registering variables repeatedly

All good things come in threes… consider the following code snippet that uses python-agentx:

axd.RegisterVar("a", 0)
axd.RegisterVar("b", 1)

snmpwalk will show both a and b correctly. Now consider the following code:


axd.RegisterVar("a", 0)
axd.RegisterVar("b", 1)
axd.RegisterVar("a", 2)

snmpwalk will show a only. Which is a bug. Why?

python-agentx internally maintains “next OID” pointers with each registered variable, so it can tell clients using the SNMP “Get Next” command about the successor for a given OID. However, it always assumed that variables have not been registered before. Thus, the third RegisterVar() call above, while not adding a second a variable (not possible, because a Dictionary data structure is used underneath which enforces uniqueness among its keys), overwrote the first a‘s “next OID” setting with the special value =None=. Thus, no reference to b any more.

Obviously, the fix is to distinguish whether a variable has already been registered or not. In the first case, only its value needs updated, in the latter case the existing code.

You think registering a variable twice is a strange idea? Actually it’s not. Consider an API using stackable plugins: the first plugin registers a variable with a value “Unknown”, because it leaves the actual data retrieval to a more specialized second plugin, which registers the variable again, but with a proper value. Since the python-agentx API does not distinguish between registering and updating, RegisterVar() needs the extension outlined above.

The patch can be found at Sourceforge Bug report #3519098.

Leave a comment