SNMP to check system uptime

Actually, check SNMP service uptime.

private static void sysuptime(string host, string snmp_community, out TimeSpan? uptime)
{
    UdpTarget target = new UdpTarget(IPAddress.Parse(host));
            
    Pdu pdu = new Pdu(PduType.Get);
    pdu.VbList.Add("1.3.6.1.2.1.1.3.0");
    AgentParameters param = new AgentParameters(SnmpVersion.Ver2, new OctetString(snmp_community));
            
    try
    {
        SnmpV2Packet packet = (SnmpV2Packet)target.Request(pdu, param);
        AsnType asnType = packet.Pdu.VbList["1.3.6.1.2.1.1.3.0"].Value;

        uptime = (TimeSpan)(asnType as TimeTicks);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Data.ToString());
        uptime = new TimeSpan(0);
    }
}

Besides, you have to setting snmp_community in SNMP service.
snmp

Leave a Reply

Your email address will not be published. Required fields are marked *