由于项目的需要,需要使用SNMP来进行相关的开发,需要在我的程序中利用SNMP的Trap协议给指定的系统程序发送相关的设备数据信息, 使得其系统能够监控到设备的最新信息以及状态,对方只是提供了一个Java的例子,而我的程序是C#开发的,因此对这方面的知识进行了解学习,完成了相关的功能,本文有感于此,对SNMP方面做了一些开发总结,以求树碑到此一游,独乐不如众乐,邀兴趣之士猎奇探秘.

首先介绍标题的几个问题,SNMP是什么,snmpsharpnet又是什么,开发能解决什么问题?

SNMP是什么呢?

简单来说,SNMP主要就是一种特定的网络管理协议,用来获取设备相关信息的一种一些,复杂来讲,就很讲究了,我不在长篇大量介绍重复的内容,需要了解可以参考下面几篇博文:

snmp是什么?

SNMP介紹及命令

SNMP基础简介

SNMP协议学习

snmpsharpnet又是什么呢?

snmpsharpnet是基于C#开发一个开源组件,主要是为了方便使用SNMP协议而生,支持各种SNMP各种版本、各种指令的一个优秀组件。其官方网站是:http://www.snmpsharpnet.com/,里面有很多C#使用例子及相关介绍。

有人也应用它来做了一些小应用,如博客:c#开发snmp应用

这类开发能解决什么问题呢?

简单来讲,可以开发相关设备管理及监控的程序。

为了使用SNMP来进行调试及开发,需要在操作系统中安装SNMP服务(默认系统没有安装),安装和普通的安装Window组件一样的步骤,选定简单网络管理协议组件即可,安装后,在服务中启动SNMP服务即可,如下所示:

虽然启动了服务,不过要顺利访问,还需要对系统的社区名称及可以访问的IP地址做配置,否则一样无法取得计算机的相关设备信息,配置对话框是双击服务,打开属性对话框进行配置即可。

          

利用SNMPSharpNet组件来开发SNMP服务应用,省却了很多复杂的组装操作,非常方便,如获取指定机器的基本信息,可以通过

private void button1_Click(object sender, EventArgs e)

        {
            // SNMP community name
            OctetString community = new OctetString("public");

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

// 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();
        }

运行后可以显示指定机器的基本信息,如下所示:

sysDescr(1.3.6.1.2.1.1.1.0) (OctetString): Hardware: x86 Family 6 Model 26 Stepping 5 AT/AT COMPATIBLE - Software: Windows Version 5.2 (Build 3790 Multiprocessor Free) 
sysObjectID(1.3.6.1.2.1.1.2.0) (ObjectId): 1.3.6.1.4.1.311.1.1.3.1.2 
sysUpTime(1.3.6.1.2.1.1.3.0) (TimeTicks): 46d 4h 14m 2s 320ms 
sysContact(1.3.6.1.2.1.1.4.0) (OctetString):  
sysName(1.3.6.1.2.1.1.5.0) (OctetString): TCC-TX 

又例如我们可以通过SNMP命令来监控磁盘的空间大小,例子代码如下所示:

        private void button7_Click(object sender, EventArgs e)
        {
            double[] diskstorage1, diskstorage2;

diskstorage1 = snmpget("127.0.0.1", "public", 1);
            this.txtContent.Text += Environment.NewLine;
            diskstorage2 = snmpget("192.168.101.81", "gci_RW", 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.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
                    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;
        }

出来的结果就是显示各计算机的磁盘信息,如下所示:

c盘空间97.66 GB 
d盘空间73.25 GB 
c盘已用空间36.61 GB 
d盘已用空间31.00 GB 
c盘剩余空间61.05 GB 
d盘剩余空间42.25 GB

c盘空间48.83 GB 
d盘空间57.19 GB 
c盘已用空间1.70 GB 
d盘已用空间6.68 GB 
c盘剩余空间47.13 GB 
d盘剩余空间50.51 GB

另外利用SNMP可以发送约定的Trap协议到指定的计算机上,从而实现两个计算机上的交互操作,Trap协议可以发送多种数据类型,如字符类型、整形、日期类型、OID类型等信息,发送Trap协议比较简单,如下所示:

int i = 0;

        private void button8_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++;
        }

通过接受数据的控制台,我们能可以查看到Trap协议接受到的情况,如下所示:

由于SNMP方面的应用应该很少人涉及到,因此对多数人来说,还是比较神秘的东西,本文抛砖引玉,希望与大家一起讨论学习。

本文转自博客园伍华聪的博客,原文链接:利用C#开发基于snmpsharpnet基础的SNMP开发应用,如需转载请自行联系原博主。

利用C#开发基于snmpsharpnet基础的SNMP开发应用相关推荐

  1. 利用Vitis开发基于ZCU106的神经网络加速器(一)——Vitis概述及XRT编译

    前言 毕设要用到Xilinx家的ZCU106这块板子,了解到最近Xilinx统一了Vivado,XilinxSDK,并集成了常用开源IP核,推出了Vitis统一软件平台,使我们不再需要关注底层的Ver ...

  2. 【Vaadin教程】利用IDEA开发基于Vaadin网络聊天室程序

    为什么80%的码农都做不了架构师?>>>    利用Vaadin,我们可以轻松的开发出丰富的web界面,像写Swing一样写GUI,感觉什么extjs之类的弱爆了!下面是我做的一个网 ...

  3. 利用Matlab开发基于XSENS Mtw传感器模块的行人室内实时定位系统的相关

    后面将会用MarkDown来进行完全整理,当前正在开发阶段,只是会把学到的相关知识当做笔记保留下来. 1. cellfun函数,将函数用于元数组中的每个cell中 C={1:100, [2;4;10] ...

  4. android opencv 获取小图在大图的坐标_Android开发—基于OpenCV实现相机实时图像识别跟踪...

    利用OpenCV实现实时图像识别和图像跟踪 图像识别 什么是图像识别 图像识别,是指利用计算机对图像进行处理.分析和理解,以识别各种不同模式的目标和对像的技术.根据观测到的图像,对其中的物体分辨其类别 ...

  5. 前端开发工程化探讨--基础篇(长文)

    转载自UC资深前端工程师张云龙的github 喂喂喂,那个切图的,把页面写好就发给研发工程师套模板吧. 你好,切图仔. 不知道你的团队如何定义前端开发,据我所知,时至今日仍然有很多团队会把前端开发归类 ...

  6. snmp 获取设备类型_SNMP开发系列(三)SNMP Agent的实现

    SNMP在IT运营.网络设备管理.通信网元管理.物联网上应用广泛.以下章节将分析Linux/pSos等嵌入式环境下SNMP Agent流程特点及使用嵌入式设计SNMP Agent的技术细节(其中涉及到 ...

  7. OpenGL开发之旅基础知识介绍

    最近由于手机项目中需要用到OpenGL ES的知识,所以这段时间正在研究OpenGL的相关知识.因为OpenGL ES是OpenGL的剪裁版本,所以我直接从OpenGL入手,然后再去看OpenGL E ...

  8. 客户端序列码生成_Django REST Framework教程(2): 序列化器介绍及开发基于函数视图的API...

    在上篇文章中,我们已经介绍了为什么要学习DRF,什么是序列化以及什么是符合RESTful规范的Web API.在本篇文章中我们将以博客为例,使用DRF提供的序列化器(Serializers类)开发两个 ...

  9. 如何用GameMakerStudio开发基于物理引擎的平台游戏 | Lynda教程 中文字幕

    GameMakerStudio教程之如何用GML开发基于物理引擎的平台游戏 | Lynda教程 中文字幕 Building a Physics-Based Platformer in GameMake ...

最新文章

  1. Java反射以及应用
  2. 【数据库(二)】嵌套子查询
  3. catch里面不想做任何处理_Java 如何优雅处理 Exception? 看完这 9 个示例你秒懂
  4. bocketmq 多个消费者同时_菜鸟开建“海南跨境物流枢纽”让自贸港消费者海购最快当日达...
  5. react dispatch_React测试的那些事(三) React Hook 测试实例
  6. 初等数论及其应用——中国剩余定理
  7. 最长上升子序列——动态规划
  8. 搜索引擎的十大秘密(收藏)
  9. ANSIBLE---变量
  10. docker 搭建指定版本的cas_Docker搭建-生成SpringBoot项目脚手架-各版本
  11. sqlalchemy mysql_小窍门:为MySQL和Pandas准备的SQLAlchemy
  12. Windows as a Service(4)——使用Intune管理Windows10更新
  13. Android Eclipseproject开发中的常见调试问题(二)android.os.NetworkOnMainThreadException 异常的解决的方法...
  14. matlab随机数函数小结
  15. sql注入python编程_Python编写SQL注入工具(2)
  16. 北京大学计算机语言学,北京大学计算语言学教育部重点实验室
  17. nfc卡模式与标准模式_解析目前NFC具有的三种工作模式
  18. 配置aconda_重装windows系统后配置Anaconda
  19. oracle大数据量查询超时排查
  20. 机器学习及SparkMLlib简介

热门文章

  1. html输入地址提示错误,高德地图开发之输入框内伴随地址的输入,动态给出地址选择提示...
  2. jquery设置表单元素只读_jquery设置元素readonly和disabled(checkbox只读)
  3. 如何HTML中输入正确格式,以HTML格式输入样式
  4. 安卓设置原生alert设置圆角_每个月流量都超额?安卓、苹果用户可以尝试更改这些设置...
  5. python从csv中读取数据打印到屏幕上-从Python中的CSV文件读取数据
  6. njx如何实现负载均衡_使用Nginx实现负载均衡
  7. Spring mvc 异常处理
  8. CentOS7系统服务管理systemctl
  9. 在linux环境下模拟实现简单命令解释器_git bash 竟然不支持 tree 命令
  10. 专有网络 VPC > 快速入门 > 网络规划