如何使用SNMP c#获取网络流经端口的带宽

时间:2016-07-15 10:07:30

标签: c# networking port snmp snmpsharpnet

我必须通过cisco 2960交换机中的特定端口找到网络流。为此,我必须使用SNMP和C#编写代码。我已经在项目的中间,我想在我的项目中添加此功能。我已经从特定的sit和am使用相同的库文件进行了参考。SNMP Sharp

如果有人知道正确的mib找到网络信息/状态以查找信息,请提供帮助。

好的..这就是我提出的问题,我后来成功实施了这个问题。所以我在这里为那些正在寻找这个的人提供我的代码。

这是我提到的链接.... Cisco

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SnmpSharpNet;
using System.Net;

namespace ConsoleApplication10
{
    public static class Bandwidth
    {
        public static string  bandwidth()
        {
            Console.WriteLine("\n\n --------------Bandwidth----------------");
            string strMsg = "";
            // SNMP community name
            OctetString community = new OctetString("publicrw");

            // Define agent parameters class
            AgentParameters param = new AgentParameters(community);
            // Set SNMP version to 1
            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("172.17.0.203");

            // Construct target
            UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);


            //SnmpV2Packet pkt = ifspeed();
            //foreach (Vb v in pkt.Pdu.VbList)
            //{
            //    if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
            //    {
            //        // end of mib reached
            //        return strMsg;
            //    }
            //    else
            //    {
            //        // process Vb value
            //    }
            //}  


            Pdu pdu = new Pdu(PduType.Get);
            pdu.VbList.Add("1.3.6.1.2.1.2.2.1.10.10101");//Input Octect 
            pdu.VbList.Add("1.3.6.1.2.1.2.2.1.16.10101");//Output Octect
            pdu.VbList.Add("1.3.6.1.2.1.2.2.1.5.10101");//If Speed

            // 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
                {
                    Console.WriteLine("sysDescr({0}) ({1}): {2}",
                        result.Pdu.VbList[0].Oid.ToString(),
                        SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
                        result.Pdu.VbList[0].Value.ToString());

                    //Giving values to a variable
                    int a = Convert.ToInt32(result.Pdu.VbList[0].Value.ToString());
                    int b = Convert.ToInt32(result.Pdu.VbList[1].Value.ToString());
                    int c = Convert.ToInt32(result.Pdu.VbList[2].Value.ToString());

                    // Calculating IfSpeed
                    int d = (((b - a) / 60));

                    //Calculting Utilization
                    Console.WriteLine("Input utilization:"+ ((a*8) / (d)));
                    Console.WriteLine("Output utilization:" + ((b * 8) / (d)));
                }
            }

            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }

            return strMsg;
            target.Close();


        }
    }
}

0 个答案:

没有答案