Float variables not working with python-agentx

I just found another bug in current versions of the python-agentx. If, in your agent, you do something like this:

cpuTemp = 24.5
axd.RegisterVar("cpuTemp", cpuTemp)

you will hit Internal error in type switching error messages.

Since this error message is not declared inside agentx.py it must come from the SNMP libraries themselves, as can be quickly confirmed via:

$ for FILE in /usr/lib64/libnetsnmp*so; do echo $FILE; strings $FILE | grep "type switching"; done
/usr/lib64/libnetsnmpagent.so
/usr/lib64/libnetsnmphelpers.so
/usr/lib64/libnetsnmpmibs.so
/usr/lib64/libnetsnmp.so
Internal error in type switching
/usr/lib64/libnetsnmptrapd.so

A quick Google search shows that the error message comes from snmp_client.c‘s snmp_set_var_value() function, namely the codeblock at its end:

    default:
        snmp_log(LOG_ERR,"Internal error in type switching\n");
        snmp_set_detail("Internal error in type switching\n");
       return (1);
    }

    return 0;

Obviously, the default code path should not be taken. The correct code block lies a few lines above:

    case ASN_OPAQUE_FLOAT:
        if (largeval) {
            snmp_log(LOG_ERR,"bad size for opaque float (%d)\n",
                     (int)vars->val_len);
            return (1);
        }
        vars->val_len = sizeof(float);
        memmove(vars->val.floatVal, value, vars->val_len);
        break;

This is what should trigger — if the variable type were ASN_OPAQUE_FLAOT. So apparantly it is not this type. A look inside agentx.py‘s RequestObject.SetValue() method reveals:

		elif type(value) == float:
			otype = ASN_APP_FLOAT
			value = ctypes.pointer(ctypes.c_float(value))

So there is the bug. ASN_APP_FLOAT should be ASN_OPAQUE_FLOAT.

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

Leave a comment