This commit is contained in:
Tommy Parnell
2014-05-07 16:30:13 -07:00
parent 5157dbac46
commit e0a3ac5f50
9 changed files with 191 additions and 19 deletions

View File

@@ -32,6 +32,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
@@ -44,4 +45,9 @@
<Name>NOCQ</Name> <Name>NOCQ</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@@ -1,9 +1,10 @@
using System; using System;
using System.Dynamic; using System.Dynamic;
using System.Linq; using NOCQ.Settings;
using NOCQ.Plugins.Email; using NOCQ.Plugins.Email;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NOCQ.Extensability; using NOCQ.Extensability;
namespace NOCQ.Application namespace NOCQ.Application
@@ -24,9 +25,42 @@ namespace NOCQ.Application
}); });
//RedisDatabase.SaveAlert(, "127.0.0.1", RedisQueues.Output, 6379, 3000); //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<Alert>();
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(); Console.ReadLine();
*/
} }
} }
} }

View File

@@ -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",
}]
}
}
]
}

View File

@@ -59,6 +59,7 @@
<Reference Include="Newtonsoft.Json"> <Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.CSharp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@@ -72,6 +73,10 @@
</Compile> </Compile>
<Compile Include="Model\Alert.cs" /> <Compile Include="Model\Alert.cs" />
<Compile Include="DB\RedisQueues.cs" /> <Compile Include="DB\RedisQueues.cs" />
<Compile Include="Settings\SettingsFIle.cs" />
<Compile Include="Settings\RedisSettings.cs" />
<Compile Include="Settings\SettingsParser.cs" />
<Compile Include="Settings\PluginSettings.cs" />
<Compile Include="DB\RedisDatabase.cs" /> <Compile Include="DB\RedisDatabase.cs" />
<Compile Include="Extensability\CatalogRepository.cs" /> <Compile Include="Extensability\CatalogRepository.cs" />
<Compile Include="Imports\IDataImportMetadata.cs" /> <Compile Include="Imports\IDataImportMetadata.cs" />
@@ -87,5 +92,6 @@
<Folder Include="Plugins\" /> <Folder Include="Plugins\" />
<Folder Include="Plugins\Email\" /> <Folder Include="Plugins\Email\" />
<Folder Include="Model\" /> <Folder Include="Model\" />
<Folder Include="Settings\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -4,7 +4,11 @@ using AE.Net.Mail;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
<<<<<<< HEAD
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
=======
using Newtonsoft.Json.Linq;
>>>>>>> de89a87aa5549aaa31cb301c1a98fff6336c7aeb
namespace NOCQ.Plugins.Email namespace NOCQ.Plugins.Email
{ {
@@ -33,25 +37,49 @@ namespace NOCQ.Plugins.Email
public ImapInput (dynamic settings) public ImapInput (dynamic settings)
{ {
var sets = settings as EmailSettings; // Load settings from the dynamic object
// TODO get it to be a EmailSettings now
if (sets.GetType().GetProperty("Username") == null if (settings["Username"] == null
|| sets.GetType().GetProperty("Password") == null || settings["Password"] == null
|| sets.GetType().GetProperty("Host") == null || settings["Host"] == null
|| sets.GetType().GetProperty("Folder") == null) || settings["Folder"] == null)
throw new ArgumentException ("You are missing a required setting."); throw new ArgumentException ("You are missing a required setting.");
if (sets.ParseRules != null) { if (settings["ParseRules"] != null) {
parseRules = sets.ParseRules.Where (x => x.Enabled).ToList (); var rules = new List<ParseRule> ();
foreach (JObject rule in settings["ParseRules"]) {
//var r = rule.Children().Value<ParseRule>();
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);
} }
loginName = sets.Username; //var rules = settings ["ParseRules"] as List<ParseRule>;
password = sets.Password;
server = sets.Host;
folderPath = sets.Folder;
port = sets.Port;
timer = new Timer (sets.Frequency); parseRules = (IEnumerable<ParseRule>)rules.Where (x => x.Enabled).ToList ();
}
loginName = settings["Username"];
password = settings["Password"];
server = settings["Host"];
folderPath = settings["Folder"];
port = settings["Port"];
var period = (string) settings ["Frequency"];
// Set up the timer
timer = new Timer (Double.Parse(period));
timer.Elapsed += Execute; timer.Elapsed += Execute;
} }
@@ -60,6 +88,8 @@ namespace NOCQ.Plugins.Email
var alerts = new List<Alert> (); var alerts = new List<Alert> ();
using(var imap = new ImapClient(server, loginName, password, ImapClient.AuthMethods.Login, 993, true)) { 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( var msgs = imap.SearchMessages(
SearchCondition.Undeleted().And( SearchCondition.Undeleted().And(
SearchCondition.SentSince(new DateTime(2014, 5, 7)) SearchCondition.SentSince(new DateTime(2014, 5, 7))
@@ -69,14 +99,14 @@ namespace NOCQ.Plugins.Email
{ {
var realMsg = msg.Value; 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)); var rule = parseRules.Where (x => x.From.Equals (realMsg.From.Address, StringComparison.CurrentCultureIgnoreCase));
//"fuc".Com
if (rule.Any ()) if (rule.Any ())
{ {
var thisRule = rule.First (); var thisRule = rule.First ();
// Email + ParseRule = Alert
var source = thisRule.Source; var source = thisRule.Source;
var sysRegex = new Regex(thisRule.System); var sysRegex = new Regex(thisRule.System);
var servRegex = new Regex(thisRule.Service); var servRegex = new Regex(thisRule.Service);
@@ -101,17 +131,25 @@ namespace NOCQ.Plugins.Email
return alerts; return alerts;
} }
// Gather alerts from recent emails and throw them at redis
public void Execute(object sender, ElapsedEventArgs args) public void Execute(object sender, ElapsedEventArgs args)
{ {
var alerts = getAlerts (); var alerts = getAlerts ();
foreach (var alert in alerts) {
//RedisDatabase.SaveAlert (alert);
}
} }
// Start the timer
public void Run() public void Run()
{ {
Console.WriteLine ("Start"); Console.WriteLine ("Start");
timer.Enabled = true;
timer.Start (); timer.Start ();
} }
// Stop the timer
public void Stop() public void Stop()
{ {
Console.WriteLine ("Stop"); Console.WriteLine ("Stop");

View File

@@ -0,0 +1,12 @@
using System;
using System.Dynamic;
namespace NOCQ
{
public class PluginSettings
{
public string Name {get;set;}
public dynamic Settings {get;set;}
}
}

View File

@@ -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;}
}
}

View File

@@ -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<PluginSettings> InputPlugins {get; set; }
}
}

View File

@@ -0,0 +1,12 @@
using System;
namespace NOCQ.Settings
{
public class SettingsParser
{
public static SettingsFile Parse(string json){
return Newtonsoft.Json.JsonConvert.DeserializeObject<SettingsFile>(json);
}
}
}