changes
This commit is contained in:
Binary file not shown.
216
Snmp2Mysql/DatabaseLink.cs
Normal file
216
Snmp2Mysql/DatabaseLink.cs
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
using System.Data;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Snmp2Mysql
|
||||||
|
{
|
||||||
|
class DatabaseLinkException : Exception
|
||||||
|
{
|
||||||
|
public DatabaseLinkException(string msg) : base(msg) { }
|
||||||
|
public DatabaseLinkException(string msg, Exception rootCause) : base(msg, rootCause) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
abstract class DatabaseConnectionHelper : IDisposable
|
||||||
|
{
|
||||||
|
protected string mDbConnStr;
|
||||||
|
protected MySqlConnection mConn;
|
||||||
|
|
||||||
|
public DatabaseConnectionHelper(string dbConnStr)
|
||||||
|
{
|
||||||
|
mDbConnStr = dbConnStr;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
mConn = new MySqlConnection(mDbConnStr);
|
||||||
|
mConn.Open();
|
||||||
|
if (mConn.State != ConnectionState.Open)
|
||||||
|
{
|
||||||
|
throw new DatabaseLinkException("unable to open database connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (MySql.Data.MySqlClient.MySqlException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("MySQL Exception: {0}", ex.Message);
|
||||||
|
throw new DatabaseLinkException("failed to connect", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (mConn != null)
|
||||||
|
{
|
||||||
|
mConn.Close();
|
||||||
|
mConn = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
~DatabaseConnectionHelper()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceDataItemTuple
|
||||||
|
{
|
||||||
|
private int mId;
|
||||||
|
private string mOid;
|
||||||
|
|
||||||
|
public DeviceDataItemTuple(int id, string oid)
|
||||||
|
{
|
||||||
|
mId = id;
|
||||||
|
mOid = oid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Id
|
||||||
|
{
|
||||||
|
get { return mId; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Oid
|
||||||
|
{
|
||||||
|
get { return mOid; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeviceDataItemProvider : DatabaseConnectionHelper, IEnumerable<DeviceDataItemTuple>
|
||||||
|
{
|
||||||
|
private int mDevice;
|
||||||
|
|
||||||
|
public DeviceDataItemProvider(string dbConnStr, int device) : base(dbConnStr)
|
||||||
|
{
|
||||||
|
mDevice = device;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
throw new Exception("not supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator<DeviceDataItemTuple> IEnumerable<DeviceDataItemTuple>.GetEnumerator()
|
||||||
|
{
|
||||||
|
string stmt = "SELECT dedi.id, di.oid FROM devicedataitem_t dedi, dataitem_t di WHERE dedi.device = " + mDevice + " AND dedi.dataitem = di.id";
|
||||||
|
MySqlCommand cmd = new MySqlCommand(stmt, mConn);
|
||||||
|
|
||||||
|
using (MySqlDataReader reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
// Console.WriteLine("{0} {1}", reader[0], reader[1]);
|
||||||
|
DeviceDataItemTuple r = new DeviceDataItemTuple((int)reader[0], (string)reader[1]);
|
||||||
|
yield return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceTuple
|
||||||
|
{
|
||||||
|
private DeviceDataItemProvider mDeviceDataItemProvider;
|
||||||
|
private string mDeviceAddress;
|
||||||
|
private string mCommunity;
|
||||||
|
|
||||||
|
public DeviceTuple(DeviceDataItemProvider deviceDataItemProvider, string deviceAddress, string community)
|
||||||
|
{
|
||||||
|
mDeviceDataItemProvider = deviceDataItemProvider;
|
||||||
|
mDeviceAddress = deviceAddress;
|
||||||
|
mCommunity = community;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDataItemProvider DeviceDataItemProvider
|
||||||
|
{
|
||||||
|
get { return mDeviceDataItemProvider; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string DeviceAddress
|
||||||
|
{
|
||||||
|
get { return mDeviceAddress; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Community
|
||||||
|
{
|
||||||
|
get { return mCommunity; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceProvider : DatabaseConnectionHelper, IEnumerable<DeviceTuple>
|
||||||
|
{
|
||||||
|
public DeviceProvider(string dbConnStr) : base(dbConnStr) { }
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
throw new Exception("not supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator<DeviceTuple> IEnumerable<DeviceTuple>.GetEnumerator()
|
||||||
|
{
|
||||||
|
string stmt = "SELECT id, address, community FROM device_t";
|
||||||
|
MySqlCommand cmd = new MySqlCommand(stmt, mConn);
|
||||||
|
|
||||||
|
using (MySqlDataReader reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
// Console.WriteLine("{0} {1} {2}", reader[0], reader[1], reader[2]);
|
||||||
|
int deviceId = (int)reader[0];
|
||||||
|
string deviceAddress = (string)reader[1];
|
||||||
|
string community = (string)reader[2];
|
||||||
|
DeviceDataItemProvider deviceDataItemProvider = new DeviceDataItemProvider(mDbConnStr, deviceId);
|
||||||
|
DeviceTuple r = new DeviceTuple(deviceDataItemProvider, deviceAddress, community);
|
||||||
|
yield return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DataCollector : DatabaseConnectionHelper
|
||||||
|
{
|
||||||
|
public DataCollector(string dbConnStr) : base(dbConnStr) { }
|
||||||
|
|
||||||
|
public void add(int id, string value)
|
||||||
|
{
|
||||||
|
string stmt = "INSERT INTO collecteddata_t (devicedataitem, value) VALUES(" + id + ", '" + value + "')";
|
||||||
|
MySqlCommand cmd = new MySqlCommand(stmt, mConn);
|
||||||
|
cmd.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseLink
|
||||||
|
{
|
||||||
|
private string mDbConnStr;
|
||||||
|
|
||||||
|
public DatabaseLink(string dbConnStr)
|
||||||
|
{
|
||||||
|
mDbConnStr = dbConnStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceProvider DeviceProvider
|
||||||
|
{
|
||||||
|
get { return new DeviceProvider(mDbConnStr); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataCollector DataCollector
|
||||||
|
{
|
||||||
|
get { return new DataCollector(mDbConnStr); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,11 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net;
|
|
||||||
using SnmpSharpNet;
|
|
||||||
using System.Collections;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
using System.Data;
|
|
||||||
|
|
||||||
namespace Snmp2Mysql
|
namespace Snmp2Mysql
|
||||||
{
|
{
|
||||||
@ -17,68 +12,38 @@ namespace Snmp2Mysql
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
SnmpGetter snmpGetter = new SnmpGetter("public", "172.16.2.1");
|
string dbConnStr = "SERVER=localhost;" +
|
||||||
snmpGetter.AddOid(1, "1.3.6.1.2.1.1.1.0");
|
"DATABASE=statsdb;" +
|
||||||
snmpGetter.AddOid(2, "1.3.6.1.2.1.1.3.0");
|
"UID=statsuser;" +
|
||||||
SnmpGetterResultProvider res = snmpGetter.Exec();
|
"PASSWORD=test123;";
|
||||||
|
DatabaseLink dbLink = new DatabaseLink(dbConnStr);
|
||||||
|
using (DataCollector dc = dbLink.DataCollector)
|
||||||
|
using (DeviceProvider dp = dbLink.DeviceProvider)
|
||||||
|
{
|
||||||
|
foreach (DeviceTuple dt in dp)
|
||||||
|
{
|
||||||
|
// Console.WriteLine("dt: {0}, {1}", dt.DeviceAddress, dt.Community);
|
||||||
|
|
||||||
|
using (SnmpGetter snmpGetter = new SnmpGetter(dt.Community, dt.DeviceAddress))
|
||||||
|
{
|
||||||
|
using (DeviceDataItemProvider ddip = dt.DeviceDataItemProvider)
|
||||||
|
{
|
||||||
|
foreach (DeviceDataItemTuple ddit in dt.DeviceDataItemProvider)
|
||||||
|
{
|
||||||
|
// Console.WriteLine(" ddit: {0}, {1}", ddit.Id, ddit.Oid);
|
||||||
|
snmpGetter.AddOid(ddit.Id, ddit.Oid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SnmpGetterResultProvider res = snmpGetter.Exec();
|
||||||
foreach (SnmpGetterResult r in res)
|
foreach (SnmpGetterResult r in res)
|
||||||
{
|
{
|
||||||
Console.WriteLine("{0} {1} {2}: {3}", r.Index, r.Oid, r.Type, r.Value);
|
Console.WriteLine("{0} {1} {2}: {3}", r.Index, r.Oid, r.Type, r.Value);
|
||||||
}
|
dc.add((int)r.Index, 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,7 @@
|
|||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="DatabaseLink.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="SnmpGetter.cs" />
|
<Compile Include="SnmpGetter.cs" />
|
||||||
|
@ -88,7 +88,7 @@ namespace Snmp2Mysql
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SnmpGetter
|
class SnmpGetter : IDisposable
|
||||||
{
|
{
|
||||||
private OctetString mCommunity;
|
private OctetString mCommunity;
|
||||||
private IpAddress mHost;
|
private IpAddress mHost;
|
||||||
@ -118,6 +118,11 @@ namespace Snmp2Mysql
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
~SnmpGetter()
|
~SnmpGetter()
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -3,3 +3,5 @@ c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bi
|
|||||||
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bin\Debug\Snmp2Mysql.pdb
|
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.exe
|
||||||
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\obj\Debug\Snmp2Mysql.pdb
|
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\obj\Debug\Snmp2Mysql.pdb
|
||||||
|
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bin\Debug\MySql.Data.dll
|
||||||
|
c:\users\dehottgw\documents\visual studio 2013\Projects\Snmp2Mysql\Snmp2Mysql\bin\Debug\SnmpSharpNet.dll
|
||||||
|
Binary file not shown.
Reference in New Issue
Block a user