pief / python-netsnmpagent

Allows to write net-snmp subagents in Python.
GNU Lesser General Public License v3.0
68 stars 39 forks source link

getRegistered(context) throws exception when OctetString is DateTime #44

Open paulo-erichsen opened 4 years ago

paulo-erichsen commented 4 years ago

I'm setting an OctetString as follows as an SNMP DateTime object

I'm not sure if the way I'm setting the DateTime is correct for SNMP, but when I do a snmpwalk, I'm able to see the variable just fine. Except the program blows up when I call dump_registered()

setup

import struct
import time

# setup the agent as in simple_agent.py example, then

time_millisecs = 1577836800000

# convert milliseconds to an OctetString
value = time.gmtime(time_millisecs / 1000)
value = struct.pack(
                            ">HBBBBBB",
                            value.tm_year,
                            value.tm_mon,
                            value.tm_mday,
                            value.tm_hour,
                            value.tm_min,
                            value.tm_sec,
                            0,
                        )
new_row.setRowCell(2, agent.OctetString(value))

# the above is fine, but it then blows up when the following is called
dump_registered(agent)

stacktrace

Traceback (most recent call last):
  File "./snmp_agent.py", line 465, in <module>
    main()
  File "./snmp_agent.py", line 435, in main
    dump_registered(agent)
  File "./snmp_agent.py", line 342, in dump_registered
    variables = agent.getRegistered(context)
  File "~/my_venv/lib/python3.8/site-packages/netsnmpagent.py", line 949, in getRegistered
    "value": snmpobj.value()
  File "~/my_venv/lib/python3.8/site-packages/netsnmpagent.py", line 895, in value
    retdict[indices][int(data.contents.column)] = u(ctypes.string_at(data.contents.data.string, data.contents.data_len))
  File "~/my_venv/lib/python3.8/site-packages/netsnmpagent.py", line 51, in u
    return s if isinstance("Test", bytes) else s.decode(locale.getpreferredencoding())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 1: invalid continuation byte
Exception ignored in: <module 'threading' from '/usr/lib/python3.8/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 1388, in _shutdown
    lock.acquire()
KeyboardInterrupt: 
* Cleaning up...
kgraefe commented 4 years ago

This has been addressed in https://github.com/pief/python-netsnmpagent/pull/40.

@pief You may want to make a new release? The latest release is quite behind the master branch.