一、获取指定机器信息
private void button1_Click(object sender, EventArgs e)
{
txtContent.Clear();
txtContent.ForeColor=System.Drawing.Color.Red;
OctetString community = new OctetString("public");
AgentParameters param = new AgentParameters(community);
param.Version = SnmpVersion.Ver1;
IpAddress agent = new IpAddress("127.0.0.1");// 127.0.0.1 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 获取机器名
pdu.VbList.Add(".1.3.6.1.2.1.1.6.0"); //SysLocation 机器所在的位置
// 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 + "\r\n");
}
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());
this.txtContent.Text += string.Format("SysLocation({0}) ({1}): {2} \r\n", result.Pdu.VbList[5].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[5].Value.Type), result.Pdu.VbList[5].Value.ToString());
}
}
else
{
this.txtContent.Text += string.Format("No response received from SNMP agent. \r\n");
}
target.Dispose();
}
二、监控磁盘的空间大小
private void button2_Click(object sender, EventArgs e)
{
txtContent.Clear();
txtContent.ForeColor = System.Drawing.Color.Green;
double[] diskstorage1, diskstorage2;
diskstorage1 = snmpget("127.0.0.1", "public", 1);//这是自己的电脑配置
this.txtContent.Text += Environment.NewLine;
diskstorage2 = snmpget("第二台机器ip", "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 SNMP回发了一个错误. 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;
}
三、已发送约定的trap协议到指定的计算机上
int i = 0;
private void button3_Click(object sender, EventArgs e)
{
//TrapAgent agent = new TrapAgent();
//VbCollection col = new VbCollection();
连接状态 设备连接状态(0:通信正常 1:通信故障)
工作温度
告警描述
//string desc = string.Format("测试Trap内容");
//col.Add(new Oid(".1.3.6.1.4.1.22014.99.2.1.6.2.1.1.1"), new Integer32(0));
//col.Add(new Oid(".1.3.6.1.4.1.22014.99.2.1.6.2.1.1.2"), new Integer32(30));
//col.Add(new Oid(".1.3.6.1.4.1.22014.99.2.1.6.2.4.1.1"), new OctetString(Encoding.Default.GetBytes(desc)));
Send the trap to the localhost port 162
//string hostIp = "127.0.0.1";
//string community = "public";
//agent.SendV2Trap(new IpAddress(hostIp), 162, community,13433, new Oid(".1.3.6.1.6.3.1.1.5"), col);
//i++;
}
四、通过OID获取机器上的信息
1.版本1
private void button4_Click(object sender, EventArgs e)
{
txtContent.Clear();
txtContent.ForeColor = System.Drawing.Color.YellowGreen;
OctetString community = new OctetString("public");
// Define agent parameters class 定义代理参数类
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 1 将SNMP版本设置为1
param.Version = SnmpVersion.Ver1;
// Construct the agent address object 构造代理地址对象
// IpAddress class is easy to use here because /IpAddress类在这里很容易使用
// it will try to resolve constructor parameter if it doesn't //如果没有,它将尝试解析构造函数参数
// parse to an IP address//解析到IP地址 127.0.0.1
IpAddress agent = new IpAddress("127.0.0.1");
// Construct target 构建目标
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
// Define Oid that is the root of the MIB 定义Oid,它是MIB的根
// tree you wish to retrieve 您希望检索的树。
Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr 如果备注说明
// This Oid represents last Oid returned by 这个Oid表示最后返回的Oid
// the SNMP agent -SNMP代理
Oid lastOid = (Oid)rootOid.Clone();
// Pdu class used for all requests Pdu类用于所有请求。
Pdu pdu = new Pdu(PduType.GetNext);
// Loop through results 遍历结果
while (lastOid != null)
{
// When Pdu class is first constructed, RequestId is set to a random value当第一次构造Pdu类时,RequestId被设置为一个随机值
// that needs to be incremented on subsequent requests made using the 这需要在使用的后续请求中增加
// same instance of the Pdu class. Pdu类的相同实例。
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
// Clear Oids from the Pdu class. 清除Pdu类中的oid。
pdu.VbList.Clear();
//Initialize request PDU with the last retrieved Oid 使用最后检索到的Oid初始化请求PDU
pdu.VbList.Add(lastOid);
// Make SNMP request 请求
SnmpV1Packet result = (SnmpV1Packet)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 Error状态为0,则返回错误
// the Agent - see SnmpConstants for error definitions 代理—错误定义请参见snmpconstant
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}", result.Pdu.ErrorStatus,result.Pdu.ErrorIndex);
// 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检查检索到的Oid是根Oid的“子”
if (rootOid.IsRootOf(v.Oid))
{
this.txtContent.Text += string.Format("{0} --({1}): --{2}--", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString() + "\r\n");
//Console.WriteLine("{0} ({1}): {2}",v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type),v.Value.ToString());
lastOid = v.Oid;
}
else
{
// we have reached the end of the requested
// MIB tree. Set lastOid to null and exit loop MIB树。将老Oid设置为null并退出循环
lastOid = null;
}
}
}
}
else
{
this.txtContent.Text += string.Format("No response received from SNMP agent.-SNMP代理没有收到响应");
//Console.WriteLine("No response received from SNMP agent.-SNMP代理没有收到响应");
}
}
target.Close();
}
2.版本2,3
private void button6_Click(object sender, EventArgs e)
{
txtContent.Clear();
txtContent.ForeColor = System.Drawing.Color.Green;
// SNMP community name 社区
OctetString community = new OctetString("public");
// Define agent parameters class 定义代理参数类
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)
//将SNMP版本设置为2 (GET-BULK只与SNMP ver 2和3兼容)
param.Version = SnmpVersion.Ver2;
//Construct the agent address object 构造代理地址对象
//IpAddress class is easy to use here because IpAddress 类在这里很容易使用,因为
//it will try to resolve constructor parameter if it doesn't 如果没有,它将尝试解析构造函数参数
//parse to an IP address 解析到IP地址
IpAddress agent = new IpAddress("127.0.0.1");
// Construct target 构建目标
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
//Define Oid that is the root of the MIB;定义Oid,它是MIB的根。
//tree you wish to retrieve 你想要找回的树
Oid rootOid = new Oid("1.3.6.1.2.1.2.1"); // ifNumber 如果备注说明
//获取设备上的vlan列表,查找查找vtpVlanIfIndex(OID: 1.3.6.1.4.1.9.9.46.1.3.1.1.18)
//将上面的 Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr中换成
//Oid rootOid = new Oid("1.3.6.1.4.1.9.9.46"); // vlain id 好像是这个,需要一个个试一下,待修改,这个OID不在mib2中,用mib浏览器是找不到的,而在mib2上面一层,SMI树中
//使用此OID,得到的结果是 “端口”--》“vlan id”,eg:2 --》123
// This Oid represents last Oid returned by 这个Oid表示最后返回的Oid
// the SNMP agent 代理
Oid lastOid = (Oid)rootOid.Clone();
// Pdu class used for all requests Pdu类用于所有的请求
Pdu pdu = new Pdu(PduType.GetBulk);
// In this example, set NonRepeaters value to 0 在本例中,将非中继器值设置为0
pdu.NonRepeaters = 0;
//MaxRepetitions tells the agent how many Oid/Value pairs to return
//max重复告诉代理要返回多少个oid/值对
//in the response. 响应
pdu.MaxRepetitions = 5;
// Loop through results
while (lastOid != null)
{
// When Pdu class is first constructed, RequestId is set to 0
//当第一次构造Pdu类时,RequestId被设置为0
//and during encoding id will be set to the random value
//在编码期间,id将被设置为随机值
//for subsequent requests, id will be set to a value that
//对于后续的请求,id将被设置为一个值
//needs to be incremented to have unique request ids for each
//需要对每一个都有唯一的请求id
//packet 包 分组
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
// Clear Oids from the Pdu class. //清除Pdu类的oid。
pdu.VbList.Clear();
//Initialize request PDU with the last retrieved Oid
//使用最后检索到的Oid初始化请求PDU
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 // error status other然后0是一个错误返回
//the Agent - see SnmpConstants for error definitions 代理-请参阅snmp常量的错误定义。
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}", result.Pdu.ErrorStatus, result.Pdu.ErrorIndex);
//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 遍历返回的变量绑定
for (int i = 0; i < result.Pdu.VbList.Count; i++)
{
this.txtContent.Text += string.Format(result.Pdu.VbList[i].ToString() + "\r\n");
}
//this.txtContent.Text += string.Format(result.Pdu.VbList+ "\r\n");
foreach (Vb v in result.Pdu.VbList)
{
//Check that retrieved Oid is "child" of the root OID 检查检索到的Oid是根Oid的“子”
if (rootOid.IsRootOf(v.Oid))
{ // 1.3.6.1.2.1.2.2.1.2. 193 (OctetString): pon-olt3/1
this.txtContent.Text += string.Format("{0}-- ({1}): --{2}-", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString() + "\r\n");
// Console.WriteLine("{0} ({1}): {2}",v.Oid.ToString(),SnmpConstants.GetTypeName(v.Value.Type),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 MIB树。将lastOid设置为null并退出循环
lastOid = null;
}
}
}
}
else
{
this.txtContent.Text += string.Format("No response received from SNMP agent.");
//Console.WriteLine("No response received from SNMP agent.");
}
}
target.Close();
}
五,关于相关的OID可在网上查找暂不列举