c#snmpでのOID値によるVALUE値の取得
6490 ワード
using System;
using System.Collections.Generic;
using System.Text;
using SnmpSharpNet;
using System.Net;
namespace BankProject
{
class SnmpUtil
{
private SimpleSnmp snmp = null;
private Dictionary<Oid, AsnType> result = null;
public SnmpUtil()
{
}
public SnmpUtil(string host, string community)
{
host = host == "" ? ReadyAppConfig.ConfigManager("snmp_host") : host;
community = community == "" ? ReadyAppConfig.ConfigManager("snmp_community") : community;
snmp = new SimpleSnmp(host, community);
}
private bool isValid()
{
bool bol = true;
if (!snmp.Valid)
{
bol = false;
return bol;
}
return bol;
}
private Dictionary<Oid,AsnType> SnmpClient(string oid)
{
result = snmp.Walk(SnmpVersion.Ver1, oid);
return result;
}
public List<object> excuteInit(SnmpDao oid)
{
List<object> list = null;
if(isValid())
{
Dictionary<Oid,AsnType> result=SnmpClient(oid.Oid);
if(result!=null)
{
list = new List<object>();
foreach (KeyValuePair<Oid, AsnType> entry in result)
{
string ss = string.Format("{0}|{1}|{2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type),
entry.Value.ToString());
list.Add(ss);
}
}
}
return list;
}
}
}
SNMPSHARPNETサードパーティDLLファイルをオンラインでダウンロードし、プロジェクトに導入し、既存のAPIに基づいてテストを行う.
以下は私のDAOファイルで、OID値を保存するために使用します.
using System;
using System.Collections.Generic;
using System.Text;
namespace BankProject
{
class SnmpDao
{
private string oid;
public string Oid
{
get { return oid; }
set { oid = value; }
}
}
}
WINFORMを建てて、テストします
private void snmpCLientGet()
{
SimpleSnmp snmp = new SimpleSnmp(host, community);
if (!snmp.Valid)
{
Console.WriteLine("SNMP agent host name/ip address is invalid.");
return;
}
//Dictionary<Oid, AsnType> result = snmp.Walk(SnmpVersion.Ver1, "1.3.6.1.2.1.25.2.3.1.4.2");
Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1, new string[] { ".1.3.6.1.2.1.25.3.2.1.3.1" });
if (result == null)
{
Console.WriteLine("No results received.");
return;
}
foreach (KeyValuePair<Oid, AsnType> kvp in result)
{
string s=string.Format("{0}= {1}: {2}", kvp.Key.ToString(), SnmpConstants.GetTypeName(kvp.Value.Type), kvp.Value.ToString());
richTextBox1.Text =richTextBox1.Text+"
"+s;
}
}
private void snmpClientWalk()
{
SimpleSnmp snmp = new SimpleSnmp(host, community);
Dictionary<Oid, AsnType> result = snmp.Walk(SnmpVersion.Ver1, ".1.3.6.1.2.1.25.3.5.1.1");
if (result == null)
{
Console.WriteLine("Request failed.");
}
else
{
foreach (KeyValuePair<Oid, AsnType> entry in result)
{
string ss = string.Format("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type),
entry.Value.ToString());
richTextBox1.Text = richTextBox1.Text + "
" + ss;
}
}
}
private void runInfo_Click(object sender, EventArgs e)
{
//string func = System.Reflection.MethodBase.GetCurrentMethod().Name+"()";
//this.panel3.Controls.Add(new TestControl());
//snmpClientWalk();
//snmpCLientGet();
SnmpDao sd = new SnmpDao();
sd.Oid = ".1.3.6.1.2.1.25.3.5.1.1";
SnmpUtil snmp = new SnmpUtil("","");
List<object> list=snmp.excuteInit(sd);
foreach(string s in list)
{
richTextBox1.Text = richTextBox1.Text + "
" + s;
}
}
private void snmpSetPdu()
{
string community = "private";
SimpleSnmp snmp = new SimpleSnmp(host, community);
List<Vb> vblist = new List<Vb>();
Oid setOid = new Oid("");
OctetString setValue = new OctetString("");
vblist.Add(new Vb(setOid,setValue));
Dictionary<Oid, AsnType> result = snmp.Set(SnmpVersion.Ver1, vblist.ToArray());
if(result==null)
{
Console.WriteLine(" ");
}
else
{
Console.WriteLine(" ");
}
}
テストに成功しました
しかし、なぜあるOIDはGETでしか入手できないのか、あるOIDはWALKでしか入手できないのか、理解できないのか、どの神様が私に説明してくれて、ありがとうございました.