merge
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
@@ -44,4 +45,9 @@
|
||||
<Name>NOCQ</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="settings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -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<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();
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
37
src/NOCQ.Application/settings.json
Normal file
37
src/NOCQ.Application/settings.json
Normal 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",
|
||||
|
||||
}]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -59,6 +59,7 @@
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -72,6 +73,10 @@
|
||||
</Compile>
|
||||
<Compile Include="Model\Alert.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="Extensability\CatalogRepository.cs" />
|
||||
<Compile Include="Imports\IDataImportMetadata.cs" />
|
||||
@@ -87,5 +92,6 @@
|
||||
<Folder Include="Plugins\" />
|
||||
<Folder Include="Plugins\Email\" />
|
||||
<Folder Include="Model\" />
|
||||
<Folder Include="Settings\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -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<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);
|
||||
}
|
||||
|
||||
//var rules = settings ["ParseRules"] as List<ParseRule>;
|
||||
|
||||
parseRules = (IEnumerable<ParseRule>)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<Alert> ();
|
||||
|
||||
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");
|
||||
|
||||
12
src/NOCQ/Settings/PluginSettings.cs
Normal file
12
src/NOCQ/Settings/PluginSettings.cs
Normal 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;}
|
||||
}
|
||||
}
|
||||
|
||||
14
src/NOCQ/Settings/RedisSettings.cs
Normal file
14
src/NOCQ/Settings/RedisSettings.cs
Normal 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;}
|
||||
}
|
||||
}
|
||||
|
||||
13
src/NOCQ/Settings/SettingsFIle.cs
Normal file
13
src/NOCQ/Settings/SettingsFIle.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
|
||||
12
src/NOCQ/Settings/SettingsParser.cs
Normal file
12
src/NOCQ/Settings/SettingsParser.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user