c# - snmpsharpnet Opaque Float -
i query synlogy nas via snmpsharpnet in c# , following value ups battery:
oid: 1.3.6.1.4.1.6574.4.3.1.1.0 type: opaque data: 9f 78 04 42 c8 00 00
however, should float value => 100.00
same @ voltage:
oid: 1.3.6.1.4.1.6574.4.4.1.1.0 type: opaque data: 9f 78 04 43 65 00 00
float value => 230.00
how can value?
my code:
// snmp community name octetstring communityo = new octetstring(community); // define agent parameters class agentparameters param = new agentparameters(communityo); // set snmp version 1 (or 2) param.version = snmpversion.ver1; // construct agent address object // ipaddress class easy use here because // try resolve constructor parameter if doesn't // parse ip address ipaddress agent = new ipaddress(host); // construct target udptarget target = new udptarget((ipaddress)agent, 161, 2000, 1); // pdu class used requests pdu pdu = new pdu(pdutype.get); pdu.vblist.add(batteryoid); // pdu.vblist.add(voltageoid); // pdu.vblist.add(statusoid); // // make snmp request snmpv1packet result = (snmpv1packet)target.request(pdu, param); // if result null agent didn't reply or couldn't parse reply. if (result != null) { // errorstatus other 0 error returned // agent - see snmpconstants error definitions if (result.pdu.errorstatus != 0) { // agent reported error request console.writeline("error in snmp reply. error {0} index {1}", result.pdu.errorstatus, result.pdu.errorindex); } else { // reply variables returned in same order added // vblist console.writeline("battery ({0}) ({1}): {2}", result.pdu.vblist[0].oid.tostring(), snmpconstants.gettypename(result.pdu.vblist[0].value.type), result.pdu.vblist[0].value.tostring()); console.writeline("voltage ({0}) ({1}): {2}", result.pdu.vblist[1].oid.tostring(), snmpconstants.gettypename(result.pdu.vblist[1].value.type), result.pdu.vblist[1].value.tostring()); console.writeline("status ({0}) ({1}): {2}", result.pdu.vblist[2].oid.tostring(), snmpconstants.gettypename(result.pdu.vblist[2].value.type), result.pdu.vblist[2].value.tostring()); } } else { console.writeline("no response received snmp agent."); } target.close();
opaque data snmp means value field ber-encoded asn.1 data - it's encoded in same language snmp queries encoded in.
for quick background asn.1 standard modeling data - defines how structure data in standard way such structure can understood other programs. asn.1 not system encoding structure in bits , bytes - job of either ber or der encoding standards.
the data have - 9f 78 04 42 c8 00 00
- ber-encoded data. ber data separated 3 parts: type
, length
, , value
.
if disassemble bytes using template, come 9f78
type; 04
length, , 42c80000
value.
according of netsnmp's documentation, 'type' code of 9f78
, in ber, means "floattype". looks promising.
the next byte length byte, 04
in case. means have 4-byte float in value.
lastly, have value code 42c80000
. according rfc 6340, value interpreted standard ieee 754 32-bit float. ieee 754 used standard encoding floating point values bits - it's computers , programming languages use. c#'s float
type defined ieee 754 32-bit single precision floating point value, , double
ieee 754 64-bit double precision floating point value, instance
if use handy-dandy online converter convert raw bytes floating point, find out 42c80000
treated ieee 754 32-bit float value '100.0'. voila, have value.
now have made sense of data, lets figure out how code deal it.
the values these devices should in same format, can assume start 9f78 04
. should check value got contains bytes, chop them off leaving 4 remaining bytes storing floating point value. need convert bytes float.
thankfully, .net has class - bitconverter
:
private static void bytestosingle() { // remember 0x42c80000 has 0x42 @ highest byte, 00 @ lowest byte. byte[] snmpdata = new byte[] { 0x00, 0x00, 0xc8, 0x42 }; single floatval = bitconverter.tosingle( snmpdata, 0 ); // prints "100.0". console.writeline( floatval ); }
Comments
Post a Comment