How can I respond to a SNMP monitoring system as an Agent using snmpSharpNet

2.1k Views Asked by At

I want to build an app working like a SNMP device (such as switches etc) to monitor some items using snmp monitoring apps (like solarwinds, zabbix etc)

I use SNMPsharpNet component and successfully receive Get message, But I cannot respond to message, Look here:

UdpTarget target = new UdpTarget((IPAddress)new IpAddress(_peerIP.Address),162,5000,3);

nmpV2Packet pkt = new SnmpV2Packet();
try
{
    pkt.decode(_inbuffer, inlen);
}

pkt.Pdu.VbList.RemoveAt(0);

pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Micromoje")); //sysDescr
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.2.1.1.0")); //sysObjectID
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); //sysUpTime
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("DCU Nodes")); //sysContact
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("DCU Managing")); //sysName

AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString("public"));

SnmpV2Packet response = new SnmpV2Packet("public");
response = target.Request(pkt.Pdu, aparam) as SnmpV2Packet;

when I use this code I receive the error message 'Request has reached maximum retries.'

then I tried this code:

pkt.Pdu.VbList.RemoveAt(0);

pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Micromoje")); //sysDescr
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.2.1.1.0")); //sysObjectID
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); //sysUpTime
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("DCU Nodes")); //sysContact
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("DCU Managing")); //sysName   


SnmpV2Packet response = new SnmpV2Packet("public"); //= target.Request(pkt.Pdu, aparam) as SnmpV2Packet;
response.Pdu.SetVbList(pkt.Pdu.VbList);
response.Pdu.Type = PduType.Set;

try
{
    byte[] buf = response.encode();
    _socket.SendTo(buf, (EndPoint)_peerIP);
}

when I use this code I receive the error message 'does not respond with the supplied read/write community string' at monitoring system side

Finally I cannot connect my app as a SNMP device and test connections fails, please help me,

1

There are 1 best solutions below

0
On

You need to answer with the same RequestId. Also the type should be PduType.Response. I updated your code:

int requestId = pkt.Pdu.RequestId;
pkt.Pdu.VbList.RemoveAt(0);

pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Micromoje")); //sysDescr
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.2.1.1.0")); //sysObjectID
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); //sysUpTime
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("DCU Nodes")); //sysContact
pkt.Pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.5.0"), new OctetString("DCU Managing")); //sysName   


SnmpV2Packet response = new SnmpV2Packet("public"); //= target.Request(pkt.Pdu, param) as SnmpV2Packet;
response.Pdu.SetVbList(pkt.Pdu.VbList);
response.Pdu.Type = PduType.Response;
response.Pdu.RequestId = requestId;

try
{
    byte[] buf = response.encode();
    _socket.SendTo(buf, (EndPoint)_peerIP);
}