basic and pretty bad settings parsing from settings.json

This commit is contained in:
Norm MacLennan
2014-05-07 16:01:55 -07:00
parent 7a16f8eb42
commit bf7d2ed68d
5 changed files with 89 additions and 52 deletions

View File

@@ -1,20 +1,23 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="src/NOCQ/Plugins/Email/ImapInput.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="src/NOCQ.Application/Program.cs">
<Files>
<File FileName="src/NOCQ/Plugins/Email/EmailSettings.cs" Line="15" Column="22" />
<File FileName="src/NOCQ.Application/Program.cs" Line="3" Column="21" />
<File FileName="src/NOCQ/Settings/SettingsFIle.cs" Line="15" Column="3" />
<File FileName="src/NOCQ.Application/settings.json" Line="10" Column="2" />
<File FileName="src/NOCQ/Plugins/Email/ImapInput.cs" Line="98" Column="5" />
<File FileName="src/NOCQ.Application/Program.cs" Line="14" Column="2" />
<File FileName="src/NOCQ/Plugins/Email/ParseRule.cs" Line="5" Column="24" />
<File FileName="src/NOCQ/Plugins/Email/IEmailSetting.cs" Line="14" Column="15" />
<File FileName="src/NOCQ/Model/Alert.cs" Line="1" Column="1" />
<File FileName="src/NOCQ/DB/RedisDataase.cs" Line="1" Column="1" />
<File FileName="src/NOCQ/Settings/RedisSettings.cs" Line="7" Column="3" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="NOCQ" expanded="True" selected="True" />
<Node name="NOCQ.Application" expanded="True">
<Node name="NOCQ" expanded="True">
<Node name="Plugins" expanded="True">
<Node name="Email" expanded="True" />
</Node>
<Node name="Settings" expanded="True" />
</Node>
<Node name="NOCQ.Application" expanded="True" selected="True">
<Node name="References" expanded="True" />
</Node>
</State>

View File

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

View File

@@ -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<ParseRule> { rule };
var email = new ImapInput (settings);
email.Execute (null,null);
Console.ReadKey ();
}
}
}

View File

@@ -58,8 +58,8 @@
<Reference Include="System.Configuration" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Package>monodevelop</Package>
</Reference>
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -73,6 +73,10 @@
<Compile Include="Plugins\Email\IEmailSetting.cs" />
<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" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -85,5 +89,6 @@
<Folder Include="Plugins\" />
<Folder Include="Plugins\Email\" />
<Folder Include="Model\" />
<Folder Include="Settings\" />
</ItemGroup>
</Project>

View File

@@ -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<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;
}
@@ -49,23 +74,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);
@@ -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");