当前位置:编程学习 > C#/ASP.NET >>

snmpsharpnet类库开发问题

近日在开发一个项目,也不算很复杂,就是利用snmp协议来获取服务器的硬盘空间,然后进行实时监控,在网上找了一下,发现一个比较好用的库snmpsharpnet-7-5,写的代码如下,但是出现了一个问题,首先调用的Ip地址172.29.14.98没有问题,但程序运行到下一个IP172.29.13.2地址就出现异常,显示连接超数,但是互换一下IP地址还是前面的iP可以,后面的Ip不行,只调用一次函数两个IP地址都是可以的,就是有先后顺序就不行,我也不知道是什么原因,使用udp协议进行通信,应该不涉及端口的释放,而且已经使用了close函数进行关闭连接,有没有哪位高人使用过这个库,可以指导一下,不胜感激!!!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SnmpSharpNet;
using System.Net;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Threading;



namespace snmp
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] diskstorage1,diskstorage2;
            
            diskstorage1 = snmpget("172.29.14.98", "public", 1);
            
            diskstorage2 = snmpget("172.29.13.2", "pubilc",2);
            foreach (double dou in diskstorage2)
            Console.WriteLine(dou.ToString());
            
        }
    
        
        
static 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.2"); //硬盘C盘簇数
                pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.5.3"); //硬盘D盘簇数
                pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.2");//硬盘C盘已用簇数
                pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.6.3");//硬盘D盘已用簇数
                pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.2");//硬盘C盘分配单元
                pdu.VbList.Add("1.3.6.1.2.1.25.2.3.1.4.3");//硬盘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
                    Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                        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;
                    Console.WriteLine("c盘空间{0}", cvolumn.ToString());
                    Console.WriteLine("d盘空间{0}", dvolumn.ToString());
                    Console.WriteLine("c盘已用空间{0}", cvolunmn1.ToString());
                    Console.WriteLine("d盘已用空间{0}", dvolumn1.ToString());
                    Console.WriteLine("c盘剩余空间{0}", cvolumn2.ToString());
                    Console.WriteLine("d盘剩余空间{0}", dvolumn2.ToString());
                   
                }
            }
            else
            {
                Console.WriteLine("No response received from SNMP agent.");
            }
             target.Close();
          
            
            return backup;
            
        }
    
    }
   
}


--------------------编程问答-------------------- 自己顶一下 --------------------编程问答-------------------- 自己再顶一下,大家讨论一下阿,关于类库我有源代码,如果需要我可以发上来,大家互相探讨学习 --------------------编程问答-------------------- 学习了。 --------------------编程问答-------------------- 没用过
http://hi.baidu.com/zhouzaibao/blog/item/cd0f5401ef5cf8de277fb590.html --------------------编程问答-------------------- 有项目管理经验的.NET开发的朋友,加上限500人的QQ群28720769,一起交流。 --------------------编程问答-------------------- 回复wuyq11:
那个网页我已经看过,我也曾经参考过,但是那个问题还是不能解决,但还是谢谢你 --------------------编程问答-------------------- 路过看两眼 --------------------编程问答-------------------- 最近用SNMPSharpNET开发也碰到了类似问题,帮顶一下! --------------------编程问答-------------------- --------------------编程问答-------------------- 路过帮顶回复太快,请先休息一下! 
--------------------编程问答-------------------- 有机会可以试试看
http://sharpsnmplib.codeplex.com --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主解决了没? --------------------编程问答-------------------- target.Close(); 改成 target.Dispose(); --------------------编程问答-------------------- 请问怎么样可以将SnmpSharpNet.dll导入C#工程呢?直接添加引用会报错,不知这个dll是什么语言看法的啊,需要将这个dll先注册成为COM组件吗?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,