基于SNMP在网络监控中的应用1_OLT设备的监控

     SNMP 是应用在 IP 网络管理网络节点(服务器、OLT、路由器、交换机等)的一种标准协议,属于应用层协议,通过 SNMP 可以获得设备的主要运行参数信息,帮助管理员解决出现的问题,我用到的开发工具包括C#、JavaScript、Echarts等。

    本次介绍如何监控OLT设备的主要工作参数指标,包括上行口状态、板卡cpu利用率、内存利用率和温度等,其他需要监控的指标自己可以摸索。

OLT设备的oid信息由于保密的需要做了隐藏
public string zxAnOltsysName = "1.3.6.1.2.1.1.5.0";  //C320设备名称
public string zxAnOltsysDescr = "1.3.6.1.2.1.1.1.0";  //C320设备描述
public string zxAnOltGei1Status = "1.3.6.1.2.1.2.2.*";   //C320上行口
public string zxAnCardOperStatus = "1.3.6.1.4.1.3902.*";//块卡状态
public string zxAnCardCpuLoad = "1.3.6.1.4.1.3902.*";//板卡cpu利用率
public string zxAnCardMemUsage = "1.3.6.1.4.1.3902.*";//内存利用率
public string zxAnOltTemperature = "1.3.6.1.4.1.3902.*"; //温度
public string zxAnOltOnuAddress = "1.3.6.1.4.1.3902.*"; //带的ONUmac

根据oid调用getOIDValue()函数获得对应的参数值。

 public Dictionary<string, string> getOIDValue(string host, string singleoid, string mcmtsCommunity)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                OctetString community = new OctetString(mcmtsCommunity);
                AgentParameters param = new AgentParameters(community);
                param.Version = SnmpVersion.Ver2;
                IpAddress agent = new IpAddress(host);
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 1000, 1);
                Pdu pdu = new Pdu(PduType.Get);
                if (!singleoid.Contains("SNMP No-Such-Instance"))
                {
                    pdu.VbList.Add(singleoid);
                    SnmpV2Packet result = (SnmpV2Packet)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)
                        {
                            for (int i = 0; i < result.Pdu.VbList.Count; i++)
                            {
                                dic.Add(result.Pdu.VbList[i].Oid.ToString(), result.Pdu.VbList[i].Value.ToString());
                            }
                        }
                    }
                    target.Close();
                }
                else
                {
                    dic = null;
                }
            }
            catch (Exception e)
            {
                dic = null;
            }
            return dic;
        }
 public Dictionary<string, string> getWalkValueV2(string host, string irootOid, string mcmtsCommunity)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                OctetString community = new OctetString(mcmtsCommunity);
                AgentParameters param = new AgentParameters(community);
                param.Version = SnmpVersion.Ver2;
                IpAddress agent = new IpAddress(host);
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 1000, 1);

                // Define Oid that is the root of the MIB
                //  tree you wish to retrieve
                Oid rootOid = new Oid(irootOid); // ifDescr
                Oid lastOid = (Oid)rootOid.Clone();
                Pdu pdu = new Pdu(PduType.GetBulk);
                pdu.NonRepeaters = 0;
                pdu.MaxRepetitions = 5;
                while (lastOid != null)
                {
                    if (pdu.RequestId != 0)
                    {
                        pdu.RequestId += 1;
                    }
                    // Clear Oids from the Pdu class.
                    pdu.VbList.Clear();
                    // Initialize request PDU with the last retrieved Oid
                    pdu.VbList.Add(lastOid);
                    // Make SNMP request
                    SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
                    // You should catch exceptions in the Request if using in real application.

                    // 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);
                            lastOid = null;
                            break;
                        }
                        else
                        {
                            // Walk through returned variable bindings
                            foreach (Vb v in result.Pdu.VbList)
                            {
                                // Check that retrieved Oid is "child" of the root OID
                                if (rootOid.IsRootOf(v.Oid))
                                {
                                    //Console.WriteLine("{0} ({1}): {2}",v.Oid.ToString(),SnmpConstants.GetTypeName(v.Value.Type),v.Value.ToString());
                                    dic.Add(v.Oid.ToString(), v.Value.ToString());
                                    if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                        lastOid = null;
                                    else
                                        lastOid = v.Oid;
                                }
                                else
                                {
                                    // we have reached the end of the requested
                                    // MIB tree. Set lastOid to null and exit loop
                                    lastOid = null;
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No response received from SNMP agent.");
                    }
                }
                target.Close();
            }
            catch (Exception e)
            {
                dic = null;
            }
            return dic;
        }

把获得的参数值利用页面进行展示,如果发现指标异常,通过微信平台进行相应的报警,让维修人员尽快处理(由于时间关系,下一步在写相关的内容)

 

效果图

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

消灭倭寇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值