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

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料