From e0a3ac5f50e8ffdc308d6b5f3e2c4bcc27c75043 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Wed, 7 May 2014 16:30:13 -0700 Subject: [PATCH] merge --- src/NOCQ.Application/NOCQ.Application.csproj | 6 ++ src/NOCQ.Application/Program.cs | 36 +++++++++- src/NOCQ.Application/settings.json | 37 ++++++++++ src/NOCQ/NOCQ.csproj | 6 ++ src/NOCQ/Plugins/Email/ImapInput.cs | 74 +++++++++++++++----- src/NOCQ/Settings/PluginSettings.cs | 12 ++++ src/NOCQ/Settings/RedisSettings.cs | 14 ++++ src/NOCQ/Settings/SettingsFIle.cs | 13 ++++ src/NOCQ/Settings/SettingsParser.cs | 12 ++++ 9 files changed, 191 insertions(+), 19 deletions(-) create mode 100644 src/NOCQ.Application/settings.json create mode 100644 src/NOCQ/Settings/PluginSettings.cs create mode 100644 src/NOCQ/Settings/RedisSettings.cs create mode 100644 src/NOCQ/Settings/SettingsFIle.cs create mode 100644 src/NOCQ/Settings/SettingsParser.cs diff --git a/src/NOCQ.Application/NOCQ.Application.csproj b/src/NOCQ.Application/NOCQ.Application.csproj index f48de79..ee2f7fe 100644 --- a/src/NOCQ.Application/NOCQ.Application.csproj +++ b/src/NOCQ.Application/NOCQ.Application.csproj @@ -32,6 +32,7 @@ + @@ -44,4 +45,9 @@ NOCQ + + + PreserveNewest + + \ No newline at end of file diff --git a/src/NOCQ.Application/Program.cs b/src/NOCQ.Application/Program.cs index e516ef7..c443a52 100644 --- a/src/NOCQ.Application/Program.cs +++ b/src/NOCQ.Application/Program.cs @@ -1,9 +1,10 @@ using System; using System.Dynamic; -using System.Linq; +using NOCQ.Settings; using NOCQ.Plugins.Email; using System.Collections; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using NOCQ.Extensability; namespace NOCQ.Application @@ -24,9 +25,42 @@ namespace NOCQ.Application }); //RedisDatabase.SaveAlert(, "127.0.0.1", RedisQueues.Output, 6379, 3000); + // Parse the settings file + var json = System.IO.File.ReadAllText ("settings.json"); + var settings = SettingsParser.Parse (json); + + // Load the settings for the email plugin + var email = settings.InputPlugins.Single (x => x.Name == "Email"); + var emailSettings = email.Settings; + + //.Create and start an email plugin instance + var emailPlugin = new ImapInput(emailSettings); + emailPlugin.Execute(null,null); + + Console.ReadKey (); +/* + var list = new List(); + for (var i = 0; i < 3000; i++) + { + var al = new Alert() + {Data = "data" + Guid.NewGuid(), Runbook = "runbook", Service = "service", + Severity = "sev", + Source = "Source", + System = "System", + TimeStamp = new DateTime(2011,1,1) + }; + list.Add(al); + } + list.ForEach(al => RedisDatabase.SaveAlert(al, "127.0.0.1", RedisQueues.Input, 6379, 3000)); + for (var i = 0; i < 3000; i++) + { + var s = RedisDatabase.GetNextAlert("127.0.0.1", RedisQueues.Input, 6379, 3000); + Console.WriteLine(s.Data); + } Console.ReadLine(); +*/ } } } diff --git a/src/NOCQ.Application/settings.json b/src/NOCQ.Application/settings.json new file mode 100644 index 0000000..50f9068 --- /dev/null +++ b/src/NOCQ.Application/settings.json @@ -0,0 +1,37 @@ +{ + Redis: { + hostname: "127.0.0.1", + port: "6379", + timeout: "1000", + inputQueue: "input", + outputQueue: "output" + }, + InputPlugins: [ + { + Name: "Email", + Settings: { + Host: "imap.gmail.com", + Username: "something that's not a real email", + Password: "something that's not a real password", + Port: "993", + Folder: "INBOX", + IsSsl: "true", + Frequency: "10000", + + ParseRules:[{ + + Name: "Nagios", + Enabled: "true", + From: "gwyrox@gmail.com", + Source: "Nagios", + System: "(?<=Host: ).*", + Service: "(?<=Service: ).*", + Data: "(?<=Additional Info:[\\n]*).*", + Runbook: "http://google.com", + Severity: "P3", + + }] + } + } + ] +} diff --git a/src/NOCQ/NOCQ.csproj b/src/NOCQ/NOCQ.csproj index a7e12e0..8bfc818 100644 --- a/src/NOCQ/NOCQ.csproj +++ b/src/NOCQ/NOCQ.csproj @@ -59,6 +59,7 @@ ..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll + @@ -72,6 +73,10 @@ + + + + @@ -87,5 +92,6 @@ + diff --git a/src/NOCQ/Plugins/Email/ImapInput.cs b/src/NOCQ/Plugins/Email/ImapInput.cs index 5dbe838..5e1660a 100644 --- a/src/NOCQ/Plugins/Email/ImapInput.cs +++ b/src/NOCQ/Plugins/Email/ImapInput.cs @@ -4,7 +4,11 @@ using AE.Net.Mail; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +<<<<<<< HEAD using System.ComponentModel.Composition; +======= +using Newtonsoft.Json.Linq; +>>>>>>> de89a87aa5549aaa31cb301c1a98fff6336c7aeb namespace NOCQ.Plugins.Email { @@ -33,25 +37,49 @@ namespace NOCQ.Plugins.Email public ImapInput (dynamic settings) { - var sets = settings as EmailSettings; - - if (sets.GetType().GetProperty("Username") == null - || sets.GetType().GetProperty("Password") == null - || sets.GetType().GetProperty("Host") == null - || sets.GetType().GetProperty("Folder") == null) + // Load settings from the dynamic object + // TODO get it to be a EmailSettings now + if (settings["Username"] == null + || settings["Password"] == null + || settings["Host"] == null + || settings["Folder"] == null) throw new ArgumentException ("You are missing a required setting."); - if (sets.ParseRules != null) { - parseRules = sets.ParseRules.Where (x => x.Enabled).ToList (); + if (settings["ParseRules"] != null) { + var rules = new List (); + foreach (JObject rule in settings["ParseRules"]) { + + //var r = rule.Children().Value(); + + var r = new ParseRule () { + Name = rule["Name"].ToString(), + From = rule["From"].ToString(), + Enabled = rule["Enabled"].ToString().Equals("true") ? true:false, + Source = rule["Source"].ToString(), + System = rule["System"].ToString(), + Service = rule["Service"].ToString(), + Severity = rule["Severity"].ToString(), + Data = rule["Data"].ToString() + }; + + if (r.Enabled) + rules.Add (r); + } + + //var rules = settings ["ParseRules"] as List; + + parseRules = (IEnumerable)rules.Where (x => x.Enabled).ToList (); } - loginName = sets.Username; - password = sets.Password; - server = sets.Host; - folderPath = sets.Folder; - port = sets.Port; + loginName = settings["Username"]; + password = settings["Password"]; + server = settings["Host"]; + folderPath = settings["Folder"]; + port = settings["Port"]; + var period = (string) settings ["Frequency"]; - timer = new Timer (sets.Frequency); + // Set up the timer + timer = new Timer (Double.Parse(period)); timer.Elapsed += Execute; } @@ -60,23 +88,25 @@ namespace NOCQ.Plugins.Email var alerts = new List (); using(var imap = new ImapClient(server, loginName, password, ImapClient.AuthMethods.Login, 993, true)) { + // Find all undeleted messages from today + // TODO We probably want to check either all and delete or since last run var msgs = imap.SearchMessages( SearchCondition.Undeleted().And( - SearchCondition.SentSince(new DateTime(2014, 5, 7)) - )); + SearchCondition.SentSince(new DateTime(2014, 5, 7)) + )); foreach (var msg in msgs) { var realMsg = msg.Value; + // Figure out if any enabled parse rules apply var rule = parseRules.Where (x => x.From.Equals (realMsg.From.Address, StringComparison.CurrentCultureIgnoreCase)); - //"fuc".Com if (rule.Any ()) { var thisRule = rule.First (); + // Email + ParseRule = Alert var source = thisRule.Source; - var sysRegex = new Regex(thisRule.System); var servRegex = new Regex(thisRule.Service); @@ -101,17 +131,25 @@ namespace NOCQ.Plugins.Email return alerts; } + // Gather alerts from recent emails and throw them at redis public void Execute(object sender, ElapsedEventArgs args) { var alerts = getAlerts (); + + foreach (var alert in alerts) { + //RedisDatabase.SaveAlert (alert); + } } + // Start the timer public void Run() { Console.WriteLine ("Start"); + timer.Enabled = true; timer.Start (); } + // Stop the timer public void Stop() { Console.WriteLine ("Stop"); diff --git a/src/NOCQ/Settings/PluginSettings.cs b/src/NOCQ/Settings/PluginSettings.cs new file mode 100644 index 0000000..958db17 --- /dev/null +++ b/src/NOCQ/Settings/PluginSettings.cs @@ -0,0 +1,12 @@ +using System; +using System.Dynamic; + +namespace NOCQ +{ + public class PluginSettings + { + public string Name {get;set;} + public dynamic Settings {get;set;} + } +} + diff --git a/src/NOCQ/Settings/RedisSettings.cs b/src/NOCQ/Settings/RedisSettings.cs new file mode 100644 index 0000000..884db5f --- /dev/null +++ b/src/NOCQ/Settings/RedisSettings.cs @@ -0,0 +1,14 @@ +using System; + +namespace NOCQ.Settings +{ + public class RedisSettings + { + public string hostname {get;set;} + public string port {get;set;} + public string timeout{get;set;} + public string inputQueue{get;set;} + public string outputQueue{get;set;} + } +} + diff --git a/src/NOCQ/Settings/SettingsFIle.cs b/src/NOCQ/Settings/SettingsFIle.cs new file mode 100644 index 0000000..bd5297c --- /dev/null +++ b/src/NOCQ/Settings/SettingsFIle.cs @@ -0,0 +1,13 @@ +using System; +using System.Dynamic; +using System.Collections.Generic; + +namespace NOCQ.Settings +{ + public class SettingsFile + { + public RedisSettings Redis { get; set; } + public IEnumerable InputPlugins {get; set; } + } +} + diff --git a/src/NOCQ/Settings/SettingsParser.cs b/src/NOCQ/Settings/SettingsParser.cs new file mode 100644 index 0000000..4951761 --- /dev/null +++ b/src/NOCQ/Settings/SettingsParser.cs @@ -0,0 +1,12 @@ +using System; + +namespace NOCQ.Settings +{ + public class SettingsParser + { + public static SettingsFile Parse(string json){ + return Newtonsoft.Json.JsonConvert.DeserializeObject(json); + } + } +} +