我通过 c# 中的 snmpsharpnet 查询 Synology NAS 并获得以下 UPS 电池值:
OID:1.3.6.1.4.1.6574.4.3.1.1.0 类型:不透明数据:9F 78 04 42 C8 00 00
但是,它应该是一个浮点值 => 100.00
电压相同:
OID:1.3.6.1.4.1.6574.4.4.1.1.0 类型:不透明数据:9F 78 04 43 65 00 00
浮点值 => 230.00
我怎样才能获得价值?
我的代码:
// SNMP community name
OctetString communityo = new OctetString(community);
// Define agent parameters class
AgentParameters param = new AgentParameters(communityo);
// Set SNMP version to 1 (or 2)
param.Version = SnmpVersion.Ver1;
// Construct the agent address object
// IpAddress class is easy to use here because
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
IpAddress agent = new IpAddress(host);
// Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
// Pdu class used for all 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 is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
// Reply variables are returned in the same order as they were added
// to the 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 from SNMP agent.");
}
target.Close();