www.pudn.com > SNMPDLL_src.zip > mainClass.cs
////////////////////////////////////////////////////////////////////////////////
//
// SNMPDLL (2006-02-08 09:30:00)
//
// Copyright (c) 2005 Olivier Griffet
//
// This library is based on the work of Marek Malowidzki and Malcolm Crowe
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using Org.Snmp.Snmp_pp;
using RFC1157;
namespace SNMPDll
{
///
/// This class allow you to create a MIB
///
public class Mib
{
#region init variable + decl variable + constructor
private Mgmt myMib;
private Hashtable htOIDName; //key=OID value = fullName; key=name value = OID; key = fullName value = OID
public Mib()
{
myMib = new Mgmt();
htOIDName = new Hashtable();
}
#endregion
#region public method
#region get Or exist OID - name - fullName
///
/// Try to see if an OID exists into the loaded mib
///
/// oid string
/// true if exist else false
public bool existOID(string OID)
{
if (this.getFullName(OID) != "")return true;
else return false;
}
///
/// Try to see if a fullName exists into the loaded mib
///
/// fullName of an oid
/// true if exist else false
public bool existFullName(string fullName)
{
if (this.getOID(fullName) != "")return true;
else return false;
}
///
/// Try to find the simple name of the OID (last part of the name)
///
/// OID
/// the last part of the full name or emptyString if the OID does not exist
public string getSimpleName(string OID)
{
if (this.htOIDName.Contains(OID))
{
string[] s = ((string)htOIDName[OID]).Split('.');
return s[s.Length-1];
}
else
{
string[] s = this.getFullName(OID).Split('.');
return s[s.Length-1];
}
}
///
/// Look after OID from a simple name.
///
/// simple name (not the fullName path
/// Return the OID or return empty string if the name does not exist
public string getOIDFromSimpleName(string name)
{
if (!this.htOIDName.Contains(name)) return (string)this.htOIDName[name];
//Parcourir tout et si trouvé, balancer information
Console.WriteLine("Walk through the MIB to get the information");
walk();
if (!this.htOIDName.Contains(name)) return (string)this.htOIDName[name];
else return "";
}
///
/// Return the OID of the fullname given path
///
///
///
public string getOID(string fullName)
{
try
{
if (!this.htOIDName.Contains(fullName)) return (string)this.htOIDName[fullName];
else this.htOIDName.Add(fullName,this.myMib.giveOID(fullName));
return (string)this.htOIDName[fullName];
}
catch //Does not exist OID with the full name given
{
return "";
}
}
///
/// Look after the full name from the OID
///
///
///
public string getFullName(string OID)
{
try
{
if (this.htOIDName.Contains(OID)) return (string)this.htOIDName[OID];
else this.htOIDName.Add(OID,this.myMib.getFullNameFromOID(OID));
return (string)this.htOIDName[OID];
}
catch //Does not exist OID with the full name given
{
return "";
}
}
#endregion
///
/// Look if a SNMPObject has a description, if not return emptyString
///
///
///
public string getDescription(SNMPObject mySNMPObject)
{
return mySNMPObject.getDescription();
}
///
/// Method used on mib.dll
///
///
///
public string getDescription(string OID)
{
return this.myMib.getDescription(OID);
}
#region load mib, add OID
///
/// try to add an OID with his name to the MIB. ! the OID parent must exsit
///
/// OID to add
/// name of the OID
/// return an exception if the OID parent does not already exist into the MIB or if the OID already exist with an different name
public void addOIDName(string OID, string name)
{
string[] s = OID.Split('.');
string OIDParent = "";
for (int j=0;j
/// Load a mib file
///
///
public void loadMib(string fileName)
{
myMib.loadFile(fileName);
}
///
/// Load all mib file of a directory
///
///
public void loadDirectoryMib(string directoryName)
{
DirectoryInfo dir = new DirectoryInfo(directoryName);
FileInfo[] list = dir.GetFiles("*.mib");
foreach (FileInfo f in list)
{
this.loadMib(f.FullName);
Console.WriteLine("Load " + f.FullName);
}
}
#endregion
///
/// Display to the Console the result of the mib
///
public void walk()
{
//Private part
Console.WriteLine("Walk thru the private part of the MIB");
giveAllKids("private");
//Mgmt part
Console.WriteLine("Walk through the Mgmt part of the MIB");
giveAllKids("mgmt");
}
///
/// Write the result into a file
///
///
public void walk(string fileName)
{
StreamWriter sw = new StreamWriter(fileName);
//Private part
Console.WriteLine("Private part");
giveAllKids("private",sw);
//Mgmt part
Console.WriteLine("Mgmt part");
giveAllKids("mgmt",sw);
}
#endregion
#region private part
#region private method for the walk method
///
/// Procédure récursive
///
///
private void giveAllKids(string nameParent, StreamWriter sw)
{
string[] result = giveKids(nameParent);
for (int j=0;j
/// RECURSIVE PROCEDURE
///
///
private void giveAllKids(string nameParent)
{
string[] result = giveKids(nameParent);
for (int j=0;j
/// Constructeur où le paramètre est toujours un OID
///
///
public SNMPObject(string OID)
{
this.OID = OID;
//Mettre la description
}
///
/// Constructeur où l'ID est soit un OID soit un full name qui décrit l'ID
///
///
///
public SNMPObject(string ID, Mib _mib)
{
//Repérer si l'ID donné est un OID ou un fullName
if (char.IsDigit(ID,0))
{
this.OID = ID;
}
else
{
this.OID = _mib.getOID(ID);
}
//Mettre la description
this.description = _mib.getDescription(OID);
this.fullName = _mib.getFullName(OID);
string aaa;
}
///
/// Get the value of the SNMPObject
///
/// the SNMPAgent where you wanna take the value
///
/// (string)ht["value"] = value of the snmp request of the SNMPObject
/// (string)ht["type"] = type of the snmp request of the SNMPObject
///
public Hashtable getValue(SNMPAgent myAgent)
{
Hashtable ht = myAgent.getValue(this);
myValue = (string)ht["value"];
myType = (string)ht["type"];
return ht;
}
///
/// Get the value of the SNMPObject
///
/// the SNMPAgent where you wanna take the value
/// the value cast to a string
public string getSimpleValue(SNMPAgent myAgent)
{
if (myValue == null)
{
Hashtable ht = myAgent.getValue(this);
myValue = (string)ht["value"];
myType = (string)ht["type"];
}
return myValue;
}
public string getTypeString(SNMPAgent myAgent)
{
if (myType == null)
{
Hashtable ht = myAgent.getValue(this);
myValue = (string)ht["value"];
myType = (string)ht["type"];
}
return myType;
}
public string getTypeString()
{
if (myType == null)
{
throw new Exception("You must first defined a SNMPAgent");
}
else return myType;
}
public SNMPOIDType getType(SNMPAgent myAgent)
{
if (myType == null)
{
this.getTypeString(myAgent);
}
return this.getType();
}
public SNMPOIDType getType()
{
if (myType == null)
{
throw new Exception("You must first defined a SNMPAgent");
}
string aaa = SNMPOIDType.OctetString.ToString();
if (myType == SNMPOIDType.Counter32.ToString()) return SNMPOIDType.Counter32;
else if (myType == SNMPOIDType.Gauge32.ToString()) return SNMPOIDType.Gauge32;
else if (myType == SNMPOIDType.Int.ToString()) return SNMPOIDType.Int;
else if (myType == SNMPOIDType.IpAddress.ToString()) return SNMPOIDType.IpAddress;
else if (myType == SNMPOIDType.OctetString.ToString()) return SNMPOIDType.OctetString;
else if (myType == SNMPOIDType.Oid.ToString()) return SNMPOIDType.Oid;
else if (myType == SNMPOIDType.TimeTicks.ToString()) return SNMPOIDType.TimeTicks;
else throw new Exception("Type not found...");
}
public string getFullName()
{
if (fullName == null)
{
throw new Exception("You must first defined the MIB");
}
return fullName;
}
public string getFullName(Mib _mib)
{
if (fullName == "")
{
fullName = _mib.getFullName(this.OID);
}
return fullName;
}
public string getOID()
{
return OID;
}
public string getDescription()
{
return description;
}
}
#endregion
#region SNMPAgent
///
/// The SNMPagent is the server/computer you ask a SNMP request
///
public class SNMPAgent
{
string IPAddress = "";
string communityRead = "";
string communityWrite = "";
///
/// Construtor. Create a SNMP agent
///
/// The IPAddress of the agent
/// The read community string of the agent
/// The write community string of the agent
public SNMPAgent(string IPAddress, string communityRead, string communityWrite)
{
this.IPAddress = IPAddress;
this.communityRead = communityRead;
this.communityWrite = communityWrite;
}
///
/// Construtor. Create a SNMP agent
///
/// The IPAddress of the agent
public SNMPAgent(string IPAddress)
{
this.IPAddress = IPAddress;
this.communityRead = "public";
this.communityWrite = "public";
}
///
/// Walk thru the tree from the SNMPObject given in argument
///
///
public void walk(SNMPObject mySNMPOID)
{
string [] args = new string[7];
args[0] = "walk";
args[1] = this.getIPAddress();
args[2] = "-o";
//args[3] = this.OID + ".0";
args[3] = mySNMPOID.getOID();
args[4] = "-Dl0"; //don't make debug
args[5] = "-c" + this.getCommunityRead(); //community read
args[6] = "-C" + this.getCommunityWrite(); //community write
//launch request
Manager.makeOrder(args);
}
///
/// Get the value of the SNMPObject
///
/// the SNMPObject we want the value
///
/// (string)ht["value"] = value of the snmp request of the SNMPObject
/// (string)ht["type"] = type of the snmp request of the SNMPObject
///
public Hashtable getValue(SNMPObject mySNMPObject)
{
string [] args = new string[7];
args[0] = "get";
args[1] = this.getIPAddress();
args[2] = "-o";
//args[3] = this.OID + ".0";
args[3] = mySNMPObject.getOID();
args[4] = "-Dl0"; //don't make debug
args[5] = "-c" + this.getCommunityRead(); //community read
args[6] = "-C" + this.getCommunityWrite(); //community write
//lancer la requête
Hashtable htResult = Manager.makeOrder(args);
htResult.Add("value",(string)((Hashtable)htResult[1])["value"]);
htResult.Add("type",(string)((Hashtable)htResult[1])["type"]);
return htResult;
}
///
/// Get the value of the SNMPObjects given in argument
///
/// the SNMPObjects you wanna take the value
///
/// htResult[1] = hashtable containing the value/type of the snmp request for the SNMPObject[0]
/// htResult[2] = hashtable containing the value/type of the snmp request for the SNMPObject[1]
/// (string)((Hashtable)ht[1])["value"] = value of the snmp request for the SNMPObject[0]
/// (string)((Hashtable)ht[1])["type"] = type of the snmp request for the SNMPObject[0]
///
public Hashtable getValues(SNMPObject[] SNMPObjects)
{
int nbrArgs = 5 + 2 * SNMPObjects.Length;
string [] args = new string[nbrArgs];
args[0] = "get";
args[1] = this.getIPAddress();
args[2] = "-Dl0"; //don't make debug
args[3] = "-c" + this.getCommunityRead(); //community read
args[4] = "-C" + this.getCommunityWrite(); //community write
int i = 5;
foreach(SNMPObject mySNMPObject in SNMPObjects)
{
args[i] = "-o";
args[i+1] = mySNMPObject.getOID();
i = i + 2;
}
//lancer la requête
Hashtable htResult = Manager.makeOrder(args);
return htResult;
}
///
/// Get the value of the OID deduced by the fullName of the OID and the mib given in argument
///
/// full name of the oid's
/// a mib
///
public Hashtable getValues(string[] fullNameArray, Mib myMib)
{
SNMPObject[] mySNMPObjects = new SNMPObject[fullNameArray.Length];
int i = 0;
foreach(string fullName in fullNameArray)
{
mySNMPObjects[i] = new SNMPObject(myMib.getOID(fullName));
i++;
}
return this.getValues(mySNMPObjects);
}
///
/// Get the value of the OID deduced by the full name of the OID and and the mib given in argument
///
///
///
///
public Hashtable getValue(string fullName, Mib myMib)
{
return this.getValue(new SNMPObject(myMib.getOID(fullName)));
}
public void setValue(SNMPObject mySNMPObject, SNMPOIDType myType, string myValue)
{
string [] args = new string[9];
args[0] = "set";
args[1] = this.IPAddress;
args[2] = "-o";
args[3] = mySNMPObject.getOID();
args[4] = "-Dl0";
args[5] = "-c" + this.communityRead;
args[6] = "-C" + this.communityWrite;
args[7] = "-V" + myValue;
args[8] = "-T" + myType.ToString();
Manager.makeOrder(args);
}
public void setValue(SNMPObject mySNMPObject, SNMPOIDType myType, int myValue)
{
this.setValue(mySNMPObject, myType, myValue.ToString());
}
public void setValue(SNMPObject mySNMPObject, SNMPOIDType myType, TimeSpan myValue)
{
this.setValue(mySNMPObject, myType, myValue.Days.ToString() + " days " + myValue.Hours + ":" + myValue.Minutes + ":" + myValue.Seconds);
}
public string getIPAddress() {return IPAddress;}
public string getCommunityRead() {return communityRead;}
public string getCommunityWrite() {return communityWrite;}
}
#endregion
#region SNMPOIDType
public enum SNMPOIDType
{
OctetString,
Int,
Counter32,
TimeTicks,
Gauge32,
IpAddress,
Oid
}
#endregion
}