From 653291ea1a826490c1969afcd8775bed5dd1e1e9 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 13 Oct 2015 23:12:50 +0200 Subject: [PATCH 1/2] -merge --- Snmp2Mysql.userprefs | 4 ++ Snmp2Mysql/App.config | 5 +- Snmp2Mysql/Program.cs | 125 ++++++++++++++++++++--------------- Snmp2Mysql/Snmp2Mysql.csproj | 1 + 4 files changed, 79 insertions(+), 56 deletions(-) diff --git a/Snmp2Mysql.userprefs b/Snmp2Mysql.userprefs index 54d3486..d435b3c 100644 --- a/Snmp2Mysql.userprefs +++ b/Snmp2Mysql.userprefs @@ -1,11 +1,15 @@  +<<<<<<< local + +======= +>>>>>>> other diff --git a/Snmp2Mysql/App.config b/Snmp2Mysql/App.config index fad249e..3cdd758 100644 --- a/Snmp2Mysql/App.config +++ b/Snmp2Mysql/App.config @@ -3,4 +3,7 @@ - \ No newline at end of file + + + + diff --git a/Snmp2Mysql/Program.cs b/Snmp2Mysql/Program.cs index 3aa356f..ad7a5f0 100644 --- a/Snmp2Mysql/Program.cs +++ b/Snmp2Mysql/Program.cs @@ -1,55 +1,70 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Snmp2Mysql -{ - - - class Program - { - static void Main(string[] args) - { - string dbConnStr = "SERVER=localhost;" + - "DATABASE=statsdb;" + - "UID=statsuser;" + - "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); - } - } - - try - { - SnmpGetterResultProvider res = snmpGetter.Exec(); - foreach (SnmpGetterResult r in res) - { - // Console.WriteLine("{0} {1} {2} {3}: {4}", dt.Description, r.Index, r.Oid, r.Type, r.Value); - dc.add((int)r.Index, r.Value); - } - } - catch (SnmpGetterException) { - // Console.WriteLine ("{0}, no result: {1}", dt.Description, e.Message); - } - } - } - } - } - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Configuration; + +namespace Snmp2Mysql +{ + class Snmp2MysqlException : Exception + { + public Snmp2MysqlException(string msg) : base(msg) {} + public Snmp2MysqlException(string msg, Exception rootCause) : base(msg, rootCause) { } + } + + + class Program + { + static void Main(string[] args) + { + //string loc = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; + //Console.WriteLine ("loc: {0}", loc); + + string dbConnStr = ConfigurationManager.AppSettings["DbConnStr"]; + if (dbConnStr == null || "".Equals (dbConnStr)) { + throw new Snmp2MysqlException ("dbConnStr not found in configuration"); + } + string verboseStr = ConfigurationManager.AppSettings ["Verbose"]; + if (verboseStr == null || "".Equals (verboseStr)) { + throw new Snmp2MysqlException ("Verbose not found in configuration"); + } + bool verbose = verboseStr.ToUpper().Equals ("TRUE"); + + DatabaseLink dbLink = new DatabaseLink(dbConnStr); + using (DataCollector dc = dbLink.DataCollector) + using (DeviceProvider dp = dbLink.DeviceProvider) + { + foreach (DeviceTuple dt in dp) + { + if (verbose) 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) + { + 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) + { + Console.WriteLine("{0} {1} {2}: {3}", r.Index, r.Oid, r.Type, r.Value); + dc.add((int)r.Index, r.Value); + } + } + catch (SnmpGetterException e) { + Console.WriteLine ("no result: {0}", e.Message); + } + } + } + } + } + } +} diff --git a/Snmp2Mysql/Snmp2Mysql.csproj b/Snmp2Mysql/Snmp2Mysql.csproj index f9c5394..ef4c479 100644 --- a/Snmp2Mysql/Snmp2Mysql.csproj +++ b/Snmp2Mysql/Snmp2Mysql.csproj @@ -45,6 +45,7 @@ dependencies\SnmpSharpNet.dll + From f847f4abf67218211f4d293f286e137f1fe40e92 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 13 Oct 2015 23:16:10 +0200 Subject: [PATCH 2/2] configuration file introduced --- Snmp2Mysql/App.config | 1 + Snmp2Mysql/Program.cs | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Snmp2Mysql/App.config b/Snmp2Mysql/App.config index 3cdd758..d291303 100644 --- a/Snmp2Mysql/App.config +++ b/Snmp2Mysql/App.config @@ -5,5 +5,6 @@ + diff --git a/Snmp2Mysql/Program.cs b/Snmp2Mysql/Program.cs index ad7a5f0..d6a46fc 100644 --- a/Snmp2Mysql/Program.cs +++ b/Snmp2Mysql/Program.cs @@ -31,6 +31,8 @@ namespace Snmp2Mysql } bool verbose = verboseStr.ToUpper().Equals ("TRUE"); + if (verbose) + Console.WriteLine ("Database connection to {0}", dbConnStr); DatabaseLink dbLink = new DatabaseLink(dbConnStr); using (DataCollector dc = dbLink.DataCollector) using (DeviceProvider dp = dbLink.DeviceProvider) @@ -55,12 +57,12 @@ namespace Snmp2Mysql SnmpGetterResultProvider res = snmpGetter.Exec(); foreach (SnmpGetterResult r in res) { - Console.WriteLine("{0} {1} {2}: {3}", r.Index, r.Oid, r.Type, r.Value); + 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) { - Console.WriteLine ("no result: {0}", e.Message); + if (verbose) Console.WriteLine ("no result: {0}", e.Message); } } }