This commit is contained in:
Wolfgang Hottgenroth
2015-10-17 17:07:20 +02:00
4 changed files with 82 additions and 56 deletions

View File

@ -1,5 +1,8 @@
<Properties StartupItem="Snmp2Mysql/Snmp2Mysql.csproj"> <Properties StartupItem="Snmp2Mysql/Snmp2Mysql.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Release" />
<<<<<<< local
<MonoDevelop.Ide.Workbench />
=======
<MonoDevelop.Ide.Workbench ActiveDocument="Snmp2Mysql/Program.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="Snmp2Mysql/Program.cs">
<Files> <Files>
<File FileName="Snmp2Mysql/DatabaseLink.cs" Line="1" Column="1" /> <File FileName="Snmp2Mysql/DatabaseLink.cs" Line="1" Column="1" />
@ -7,6 +10,7 @@
<File FileName="Snmp2Mysql/SnmpGetter.cs" Line="1" Column="1" /> <File FileName="Snmp2Mysql/SnmpGetter.cs" Line="1" Column="1" />
</Files> </Files>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>
>>>>>>> other
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore /> <BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints> </MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

@ -3,4 +3,8 @@
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup> </startup>
</configuration> <appSettings>
<add key="DbConnStr" value="SERVER=localhost; DATABASE=statsdb; UID=statsuser; PASSWORD=test123;"/>
<add key="Verbose" value="true"/>
</appSettings>
</configuration>

View File

@ -1,55 +1,72 @@
using System; using System;
using System.Collections.Generic; 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.Configuration;
namespace Snmp2Mysql
{ namespace Snmp2Mysql
{
class Snmp2MysqlException : Exception
class Program {
{ public Snmp2MysqlException(string msg) : base(msg) {}
static void Main(string[] args) public Snmp2MysqlException(string msg, Exception rootCause) : base(msg, rootCause) { }
{ }
string dbConnStr = "SERVER=localhost;" +
"DATABASE=statsdb;" +
"UID=statsuser;" + class Program
"PASSWORD=test123;"; {
DatabaseLink dbLink = new DatabaseLink(dbConnStr); static void Main(string[] args)
using (DataCollector dc = dbLink.DataCollector) {
using (DeviceProvider dp = dbLink.DeviceProvider) //string loc = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
{ //Console.WriteLine ("loc: {0}", loc);
foreach (DeviceTuple dt in dp)
{ string dbConnStr = ConfigurationManager.AppSettings["DbConnStr"];
// Console.WriteLine("dt: {0}, {1}", dt.DeviceAddress, dt.Community); if (dbConnStr == null || "".Equals (dbConnStr)) {
throw new Snmp2MysqlException ("dbConnStr not found in configuration");
using (SnmpGetter snmpGetter = new SnmpGetter(dt.Community, dt.DeviceAddress)) }
{ string verboseStr = ConfigurationManager.AppSettings ["Verbose"];
using (DeviceDataItemProvider ddip = dt.DeviceDataItemProvider) if (verboseStr == null || "".Equals (verboseStr)) {
{ throw new Snmp2MysqlException ("Verbose not found in configuration");
foreach (DeviceDataItemTuple ddit in dt.DeviceDataItemProvider) }
{ bool verbose = verboseStr.ToUpper().Equals ("TRUE");
// Console.WriteLine(" ddit: {0}, {1}", ddit.Id, ddit.Oid);
snmpGetter.AddOid(ddit.Id, ddit.Oid); if (verbose)
} Console.WriteLine ("Database connection to {0}", dbConnStr);
} DatabaseLink dbLink = new DatabaseLink(dbConnStr);
using (DataCollector dc = dbLink.DataCollector)
try using (DeviceProvider dp = dbLink.DeviceProvider)
{ {
SnmpGetterResultProvider res = snmpGetter.Exec(); foreach (DeviceTuple dt in dp)
foreach (SnmpGetterResult r in res) {
{ if (verbose) Console.WriteLine("dt: {0}, {1}", dt.DeviceAddress, dt.Community);
// Console.WriteLine("{0} {1} {2} {3}: {4}", dt.Description, r.Index, r.Oid, r.Type, r.Value);
dc.add((int)r.Index, r.Value); using (SnmpGetter snmpGetter = new SnmpGetter(dt.Community, dt.DeviceAddress))
} {
} using (DeviceDataItemProvider ddip = dt.DeviceDataItemProvider)
catch (SnmpGetterException) { {
// Console.WriteLine ("{0}, no result: {1}", dt.Description, e.Message); foreach (DeviceDataItemTuple ddit in dt.DeviceDataItemProvider)
} {
} if (verbose) Console.WriteLine(" ddit: {0}, {1}", ddit.Id, ddit.Oid);
} snmpGetter.AddOid(ddit.Id, ddit.Oid);
} }
} }
}
} try
{
SnmpGetterResultProvider res = snmpGetter.Exec();
foreach (SnmpGetterResult r in res)
{
if (verbose) Console.WriteLine("{0} {1} {2}: {3}", r.Index, r.Oid, r.Type, r.Value);
dc.add((int)r.Index, r.Value);
}
}
catch (SnmpGetterException e) {
if (verbose) Console.WriteLine ("no result: {0}", e.Message);
}
}
}
}
}
}
}

View File

@ -45,6 +45,7 @@
<Reference Include="SnmpSharpNet"> <Reference Include="SnmpSharpNet">
<HintPath>dependencies\SnmpSharpNet.dll</HintPath> <HintPath>dependencies\SnmpSharpNet.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Configuration" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DatabaseLink.cs" /> <Compile Include="DatabaseLink.cs" />