Reputation: 11
I have to find the network flow through a particular port in a cisco 2960 switch. For this I have to write a code using SNMP and C# only. I am already in the middle of the project and I would like to add this feature too in my project. I have referred from the particular sit and am using the same library files to do it.SNMP Sharp
If anyone know the proper mib to find the network information/status to find the info, please help.
Ok.. This was the question I asked, which I later successfully implemented. So here I am putting up my code for those who are looking for this.
Here is the link which I referred.... 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();
}
}
}
Upvotes: 1
Views: 1388