您的位置:首页 > 编程语言 > C#

基于C#的SNMP编程(使用SnmpSharpNet)

2014-04-28 10:13 411 查看
基于C#的SNMP编程(使用SnmpSharpNet)

以前从来没接触过snmp之类的东西,感觉网上的资料也很少,经过痛苦的摸索开始了解一些东西了。

首先我们得了解一些基本概念,snmp、MIB之类的各种,在百度百科里看看就懂了。

因为我要做的是一个网络管理网页,所以我就选择了用snmpsharpnet, 那样我就可以写在asp.net的项目里面了。接下来记录一下步骤:

1. 下载一个包(从这个网址http://www.snmpsharpnet.com/点击download就行了)
2. 然后配置自己计算机的snmp服务
打开“控制面板”-“程序和功能”-“打开或者关闭windows功能”。勾选“简单网络管理协议”然后确定。



打开“控制面板”-“管理工具”-“服务”,找到SNMP Service, 双击打开。



配置安全选项卡,在“接受的社区名称”里面添加一个public,在下面“接受来自下列主机的SNMP包”里面加入localhost和你想访问的那台电脑的IP,不过你得保证那台电脑也开启了SNMP服务,并且接受来自你的snmp包,同时UDP 161端口没有被防火墙给挡住。(这里我写的是自己的IP,昨天晚上试过别人的ip,别人那边配置好了的话是可以用的),如何打开UDP
161端口可以参加下列网址:http://www.cnblogs.com/vipsoft/archive/2012/05/02/2478847.html



3.建立一个ASP.NET的项目。
打开VS2010,新建网站,asp.net空网站



4. 在解决方案浏览器里面单击项目名,点击添加引用,找到之前下载的那个包的位置添加对那个包的引用,不截图了自己试试就知道了。完成后你就可以试试了。
比如:
我添加了一个页面叫test.aspx
在里面加入了两个button和一个textbox
<div class="workspace">
<asp:TextBox ID="txtContent" TextMode="MultiLine" runat="server" Height="180px" Width="404px">	</asp:TextBox>
</div>
<div class="buttemspace">
<asp:Button ID="Button1" runat="server" Text="获取指定机器信息" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="监控磁盘的空间大小"
onclick="Button2_Click" />
</div>
在test.aspx.cs里面添加了两个事件:本代码是参照博客http://www.cnblogs.com/wuhuacong/archive/2011/01/10/1931975.html写的,非原创。如果你想试试的话请把下面代码中的IP地址换成你自己的或者你配置好的别人的IP。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using SnmpSharpNet;
using System.Net;

public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
OctetString community = new OctetString("public");

AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver1;
IpAddress agent = new IpAddress("222.26.188.191");

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

// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr
pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName

// Make SNMP request
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);

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
this.txtContent.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
// Reply variables are returned in the same order as they were added
//  to the VbList
this.txtContent.Text += string.Format("sysDescr({0}) ({1}): {2} \r\n",
result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
result.Pdu.VbList[0].Value.ToString());
this.txtContent.Text += string.Format("sysObjectID({0}) ({1}): {2} \r\n",
result.Pdu.VbList[1].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
result.Pdu.VbList[1].Value.ToString());
this.txtContent.Text += string.Format("sysUpTime({0}) ({1}): {2} \r\n",
result.Pdu.VbList[2].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
result.Pdu.VbList[2].Value.ToString());
this.txtContent.Text += string.Format("sysContact({0}) ({1}): {2} \r\n",
result.Pdu.VbList[3].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type),
result.Pdu.VbList[3].Value.ToString());
this.txtContent.Text += string.Format("sysName({0}) ({1}): {2} \r\n",
result.Pdu.VbList[4].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type),
result.Pdu.VbList[4].Value.ToString());
}
}
else
{
this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n");
}
target.Dispose();
}
protected void Button2_Click(object sender, System.EventArgs e)
{
double[] diskstorage1, diskstorage2;

diskstorage1 = snmpget("127.0.0.1", "public", 1);
this.txtContent.Text += Environment.NewLine;
diskstorage2 = snmpget("222.26.188.191", "public", 2);
//foreach (double dou in diskstorage1)
//{
//    this.txtContent.Text += string.Format("{0}  GB \r\n", dou.ToString("f2"));
//}
}

double[] snmpget(string ipaddress, string comname, int i)
{

double cvolumn = 0, dvolumn = 0, cvolunmn1 = 0, dvolumn1 = 0, cvolumn2 = 0, dvolumn2 = 0;
double[] backup = new double[6];
// SNMP community name
OctetString community = new OctetString(comname);

// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 1 (or 2)
param.Version = (int)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(ipaddress);

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

// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
//区分两台服务器的硬盘mib代码
if (i == 2)
{
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.1"); //硬盘C盘簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.2"); //硬盘D盘簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.1");//硬盘C盘已用簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.2");//硬盘D盘已用簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.1");//硬盘C盘分配单元
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.2");//硬盘D盘分配单元
}
else if (i == 1)
{
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.1"); //硬盘C盘簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.2"); //硬盘D盘簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.1");//硬盘C盘已用簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.2");//硬盘D盘已用簇数
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.1");//硬盘C盘分配单元
pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.2");//硬盘D盘分配单元
}

SnmpV1Packet result = new SnmpV1Packet();

// Make SNMP request
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
this.txtContent.Text += string.Format("Error in SNMP reply. Error {0} index {1} \r\n",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
// Reply variables are returned in the same order as they were added
//  to the VbList

int a = int.Parse(result.Pdu.VbList[0].Value.ToString());
int b = int.Parse(result.Pdu.VbList[1].Value.ToString());
int c = int.Parse(result.Pdu.VbList[2].Value.ToString());
int d = int.Parse(result.Pdu.VbList[3].Value.ToString());
int num1 = int.Parse(result.Pdu.VbList[4].Value.ToString());
int num2 = int.Parse(result.Pdu.VbList[5].Value.ToString());
cvolumn = (double)a * num1 / 1024 / 1024 / 1024;
dvolumn = (double)b * num2 / 1024 / 1024 / 1024;
cvolunmn1 = (double)c * num1 / 1024 / 1024 / 1024;
dvolumn1 = (double)d * num2 / 1024 / 1024 / 1024;
cvolumn2 = cvolumn - cvolunmn1;
dvolumn2 = dvolumn - dvolumn1;
backup[0] = cvolumn;
backup[1] = dvolumn;
backup[2] = cvolunmn1;
backup[3] = dvolumn1;
backup[4] = cvolumn2;
backup[5] = dvolumn2;
this.txtContent.Text += string.Format("c盘空间{0} GB \r\n", cvolumn.ToString("f2"));
this.txtContent.Text += string.Format("d盘空间{0} GB \r\n", dvolumn.ToString("f2"));
this.txtContent.Text += string.Format("c盘已用空间{0} GB \r\n", cvolunmn1.ToString("f2"));
this.txtContent.Text += string.Format("d盘已用空间{0} GB \r\n", dvolumn1.ToString("f2"));
this.txtContent.Text += string.Format("c盘剩余空间{0} GB \r\n", cvolumn2.ToString("f2"));
this.txtContent.Text += string.Format("d盘剩余空间{0} GB \r\n", dvolumn2.ToString("f2"));
}
}
else
{
this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n");
}
target.Close();

return backup;
}

}
运行一下,点击第一个button“获取指定机器信息”会显示



然后点第二个button"监控磁盘大小"会显示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐