ruby-snmp
ruby-snmp copied to clipboard
DateAndTime format is unprintable
I'm not sure whether this should be something handled by this library. I'm also not very familiar with ASN.1, so forgive any inaccuracies here.
An object can have a syntax of DateAndTime
, which is defined as a macro in SNMPv2-TC. If you receive a trap with one of these objects and do something like varbind.value.to_s, you receive an exception such as Encoding::UndefinedConversionError: ""\xDE"" from ASCII-8BIT to UTF-8
. The macro defines a DISPLAY-HINT 2d-1d-1d,1d:1d:1d.1d,1a1d:1d
for displaying the string. I expect to_s
to convert the raw octet string to this format.
You could try something like this (not tested):
module SNMP
class OctetString
def to_date
dt = self.unpack("ncccccc")
sprintf "%04d-%02d-%02dT%02d:%02d:%02d", *dt
end
end
end
The SNMP library doesn't know anything about syntax definitions or display hints in MIBs, so it's up to the user to interpret these kinds of non-printable strings.
I can do something like that. Is this something the library should know about? That is, do SNMP libraries in other languages take care of this? If so, I'll dive in and figure out how to add it.
So, I looked into this. It looks like I'll have to implement a Ruby SMI library, which this could then use to format things according to the MIB files. That's quite a lot and I don't think I'll ever get around to that. It will be simpler to write a new Logstash snmptrap plugin that uses net-snmp.
Okay, the net-snmp ffi library doesn't have anything for listening for notifications. It doesn't even wrap the necessary functions, so it is also a bit of work to get that going.
The proper solution here would be to implement an SMI library and use that to read the MIB, get display hints, and use them to format the file.
I think the dumbest way I can make this library work if I extend the MIB yaml to include a syntax string. We can then add syntax formatters one-by-one as needed. The first one would be a formatter for DateAndTime.
A slightly less-dumb way would be to have the MIB yaml also include display hints. We would parse those display hints and use them to format the values.
What do you think of this, @hallidave?