diff --git a/NOCQ.userprefs b/NOCQ.userprefs index 02029e3..8b8d573 100644 --- a/NOCQ.userprefs +++ b/NOCQ.userprefs @@ -1,20 +1,23 @@  - + - + + + - - - - - + - - + + + + + + + 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 2b53dba..6b8dbdd 100644 --- a/src/NOCQ.Application/Program.cs +++ b/src/NOCQ.Application/Program.cs @@ -1,7 +1,9 @@ using System; using System.Dynamic; +using NOCQ.Settings; using NOCQ.Plugins.Email; using System.Collections.Generic; +using System.Linq; namespace NOCQ.Application { @@ -9,33 +11,19 @@ namespace NOCQ.Application { public static void Main (string[] args) { - var settings = new EmailSettings (); - settings.Host = "imap.gmail.com"; - settings.IsSsl = true; - settings.Frequency = 20; - settings.Username = "gwyrox@gmail.com"; + // 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; - Console.WriteLine ("Password: "); - settings.Password = Console.ReadLine (); - settings.Port = 993; - settings.Folder = "INBOX"; + //.Create and start an email plugin instance + var emailPlugin = new ImapInput(emailSettings); + emailPlugin.Execute(null,null); - var rule = new ParseRule (); - rule.Name = "Nagios"; - rule.Enabled = true; - rule.From = "gwyrox@gmail.com"; - rule.Source = "Nagios"; - rule.System = "(?<=Host: ).*"; - rule.Service = "(?<=Service: ).*"; - rule.Data = "(?<=Additional Info:[\\n]*).*"; - rule.Severity = "P3"; - rule.Runbook = "http://google.com"; - - settings.ParseRules = new List { rule }; - - var email = new ImapInput (settings); - email.Execute (null,null); + Console.ReadKey (); } } } diff --git a/src/NOCQ/NOCQ.csproj b/src/NOCQ/NOCQ.csproj index 9663b03..75b1b6a 100644 --- a/src/NOCQ/NOCQ.csproj +++ b/src/NOCQ/NOCQ.csproj @@ -58,8 +58,8 @@ ..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll - monodevelop + @@ -73,6 +73,10 @@ + + + + @@ -85,5 +89,6 @@ + diff --git a/src/NOCQ/Plugins/Email/ImapInput.cs b/src/NOCQ/Plugins/Email/ImapInput.cs index 70f5344..bc4a43d 100644 --- a/src/NOCQ/Plugins/Email/ImapInput.cs +++ b/src/NOCQ/Plugins/Email/ImapInput.cs @@ -4,6 +4,7 @@ using AE.Net.Mail; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using Newtonsoft.Json.Linq; namespace NOCQ.Plugins.Email { @@ -22,25 +23,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; } @@ -49,23 +74,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); @@ -90,17 +117,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) { + RedisDataase.SaveAlert (alert, "output"); + } } + // Start the timer public void Run() { Console.WriteLine ("Start"); + timer.Enabled = true; timer.Start (); } + // Stop the timer public void Stop() { Console.WriteLine ("Stop");