This commit is contained in:
Wolfgang Hottgenroth 2015-10-08 17:22:29 +02:00
commit d6a5489ef8
23 changed files with 411 additions and 0 deletions

0
.hgignore Normal file
View File

22
Snmp2Mysql.sln Normal file
View File

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Snmp2Mysql", "Snmp2Mysql\Snmp2Mysql.csproj", "{CD44AA11-6B4E-461A-94BE-006D1EA7CB34}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CD44AA11-6B4E-461A-94BE-006D1EA7CB34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD44AA11-6B4E-461A-94BE-006D1EA7CB34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD44AA11-6B4E-461A-94BE-006D1EA7CB34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD44AA11-6B4E-461A-94BE-006D1EA7CB34}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

BIN
Snmp2Mysql.v12.suo Normal file

Binary file not shown.

6
Snmp2Mysql/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

84
Snmp2Mysql/Program.cs Normal file
View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using SnmpSharpNet;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data;
namespace Snmp2Mysql
{
class Program
{
static void Main(string[] args)
{
SnmpGetter snmpGetter = new SnmpGetter("public", "127.0.0.1");
snmpGetter.AddOid(1, "1.3.6.1.2.1.1.1.0");
snmpGetter.AddOid(2, "1.3.6.1.2.1.1.3.0");
SnmpGetterResultProvider res = snmpGetter.Exec();
foreach (SnmpGetterResult r in res)
{
Console.WriteLine("{0} {1} {2}: {3}", r.Index, r.Oid, r.Type, r.Value);
}
MySqlConnection conn = null;
try
{
// Create Connection String from Input data
string myConnectionString = "SERVER=localhost;" +
"DATABASE=testdb;" +
"UID=testuser;" +
"PASSWORD=test123;";
conn = new MySqlConnection(myConnectionString);
conn.Open();
if (conn.State == ConnectionState.Open)
{
Console.WriteLine("Connection Established");
string stmt = "SELECT * FROM testtable";
MySqlCommand cmd = new MySqlCommand(stmt, conn);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0} {1}", reader[0], reader[1]);
}
reader.Close();
}
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine("MySQL Exception: {0}", ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Unknown Exception: {0}", ex.Message);
}
finally
{
if (conn != null)
{
conn.Close();
conn = null;
}
}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die mit einer Assembly verknüpft sind.
[assembly: AssemblyTitle("Snmp2Mysql")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("KROHNE Messtechnik")]
[assembly: AssemblyProduct("Snmp2Mysql")]
[assembly: AssemblyCopyright("Copyright © KROHNE Messtechnik 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("6c87bd47-7522-436f-b7b6-fe5674e8cbbd")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CD44AA11-6B4E-461A-94BE-006D1EA7CB34}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Snmp2Mysql</RootNamespace>
<AssemblyName>Snmp2Mysql</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MySql.Data, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>bin\Debug\MySql.Data.dll</HintPath>
</Reference>
<Reference Include="SnmpSharpNet, Version=0.9.4.0, Culture=neutral, PublicKeyToken=b2181aa3b9571feb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>bin\Debug\SnmpSharpNet.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SnmpGetter.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

168
Snmp2Mysql/SnmpGetter.cs Normal file
View File

@ -0,0 +1,168 @@
using SnmpSharpNet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Snmp2Mysql
{
class SnmpGetterException : Exception
{
public SnmpGetterException(string msg) : base(msg) {}
public SnmpGetterException(string msg, Exception rootCause) : base(msg, rootCause) { }
}
class SnmpGetterResult
{
private Object mIndex;
private string mValue;
private string mType;
private string mOid;
public SnmpGetterResult(Object index, string value, string type, string oid)
{
mIndex = index;
mValue = value;
mType = type;
mOid = oid;
}
public Object Index
{
get { return mIndex; }
}
public string Value
{
get { return mValue; }
}
public string Type
{
get { return mType; }
}
public string Oid
{
get { return mOid; }
}
}
class SnmpGetterResultProvider : IEnumerable<SnmpGetterResult>
{
private ArrayList mIndexes;
private SnmpV2Packet mPacket;
public SnmpGetterResultProvider(ArrayList indexes, SnmpV2Packet packet)
{
mIndexes = indexes;
mPacket = packet;
}
IEnumerator<SnmpGetterResult> IEnumerable<SnmpGetterResult>.GetEnumerator()
{
int idx = 0;
while (true)
{
if (idx == mIndexes.Count)
{
yield break;
}
else
{
SnmpGetterResult r = new SnmpGetterResult(mIndexes[idx],
mPacket.Pdu.VbList[idx].Value.ToString(),
SnmpConstants.GetTypeName(mPacket.Pdu.VbList[idx].Value.Type),
mPacket.Pdu.VbList[idx].Oid.ToString());
idx++;
yield return r;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new Exception("not supported");
}
}
class SnmpGetter
{
private OctetString mCommunity;
private IpAddress mHost;
private AgentParameters mParams;
private UdpTarget mTarget = null;
private Pdu mPdu;
private ArrayList mVbIndexes;
private SnmpV2Packet mPacket;
public SnmpGetter(string community, string ipAddress)
{
mCommunity = new OctetString(community);
mHost = new IpAddress(ipAddress);
mParams = new AgentParameters(mCommunity);
mParams.Version = SnmpVersion.Ver2;
mTarget = new UdpTarget((IPAddress)mHost, 161, 2000, 1);
mPdu = new Pdu(PduType.Get);
mVbIndexes = new ArrayList();
}
public void Close()
{
if (mTarget != null)
{
mTarget.Close();
mTarget = null;
}
}
~SnmpGetter()
{
Close();
}
public void AddOid(Object index, string oid)
{
mVbIndexes.Add(index);
mPdu.VbList.Add(oid);
}
public SnmpGetterResultProvider Exec()
{
try
{
mPacket = (SnmpV2Packet)mTarget.Request(mPdu, mParams);
}
catch (SnmpNetworkException e)
{
throw new SnmpGetterException(e.Message, e);
}
if (mPacket != null)
{
if (mPacket.Pdu.ErrorStatus != 0)
{
string msg = String.Format("Error in SNMP reply. Error {0} index {1}",
mPacket.Pdu.ErrorStatus,
mPacket.Pdu.ErrorIndex);
Close();
throw new SnmpGetterException(msg);
}
else
{
return new SnmpGetterResultProvider(mVbIndexes, mPacket);
}
}
else
{
Close();
throw new SnmpGetterException("No response received from SNMP agent");
}
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

Binary file not shown.

View File

@ -0,0 +1,5 @@
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bin\Debug\Snmp2Mysql.exe.config
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bin\Debug\Snmp2Mysql.exe
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bin\Debug\Snmp2Mysql.pdb
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\obj\Debug\Snmp2Mysql.exe
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\obj\Debug\Snmp2Mysql.pdb

Binary file not shown.

Binary file not shown.