Refactoring the HealthChecks Configuration to make it cleaner & easier to add futher notification providers

This commit is contained in:
Jeavon Leopold
2017-06-05 11:57:03 +02:00
parent 6b419f4260
commit d30b70ea12
7 changed files with 118 additions and 33 deletions

View File

@@ -0,0 +1,28 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.HealthChecks
{
public class EmailSettingsElement : ConfigurationElement
{
private const string RECIPIENT_EMAIL_KEY = "recipientEmail";
private const string SUBJECT_KEY = "subject";
[ConfigurationProperty(RECIPIENT_EMAIL_KEY, IsRequired = true)]
public string RecipientEmail
{
get
{
return ((string)(base[RECIPIENT_EMAIL_KEY]));
}
}
[ConfigurationProperty(SUBJECT_KEY, IsRequired = true)]
public string Subject
{
get
{
return ((string)(base[SUBJECT_KEY]));
}
}
}
}

View File

@@ -11,9 +11,9 @@ namespace Umbraco.Core.Configuration.HealthChecks
private const string ENABLED_KEY = "enabled";
private const string FIRST_RUN_TIME_KEY = "firstRunTime";
private const string PERIOD_KEY = "periodInHours";
private const string RECIPIENT_EMAIL_KEY = "recipientEmail";
private const string WEBHOOK_URL_KEY = "webHookUrl";
private const string DISABLED_CHECKS_KEY = "disabledChecks";
private const string EMAIL_SETTINGS_KEY = "emailSettings";
private const string SLACK_SETTINGS_KEY = "slackSettings";
[ConfigurationProperty(ENABLED_KEY, IsRequired = true)]
public bool Enabled
@@ -42,24 +42,6 @@ namespace Umbraco.Core.Configuration.HealthChecks
}
}
[ConfigurationProperty(RECIPIENT_EMAIL_KEY, IsRequired = true)]
public string RecipientEmail
{
get
{
return ((string)(base[RECIPIENT_EMAIL_KEY]));
}
}
[ConfigurationProperty(WEBHOOK_URL_KEY, IsRequired = true)]
public string WebhookUrl
{
get
{
return ((string)(base[WEBHOOK_URL_KEY]));
}
}
[ConfigurationProperty(DISABLED_CHECKS_KEY, IsDefaultCollection = true, IsRequired = false)]
public DisabledHealthChecksElementCollection DisabledChecks
{
@@ -68,5 +50,23 @@ namespace Umbraco.Core.Configuration.HealthChecks
return ((DisabledHealthChecksElementCollection)(base[DISABLED_CHECKS_KEY]));
}
}
[ConfigurationProperty(EMAIL_SETTINGS_KEY, IsDefaultCollection = true, IsRequired = false)]
public EmailSettingsElement EmailSettings
{
get
{
return ((EmailSettingsElement)(base[EMAIL_SETTINGS_KEY]));
}
}
[ConfigurationProperty(SLACK_SETTINGS_KEY, IsDefaultCollection = true, IsRequired = false)]
public SlackSettingsElement SlackSettings
{
get
{
return ((SlackSettingsElement)(base[SLACK_SETTINGS_KEY]));
}
}
}
}

View File

@@ -0,0 +1,38 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.HealthChecks
{
public class SlackSettingsElement : ConfigurationElement
{
private const string WEBHOOKURL_KEY = "webHookUrl";
private const string CHANNEL_KEY = "channel";
private const string USERNAME_KEY = "username";
[ConfigurationProperty(WEBHOOKURL_KEY, IsRequired = true)]
public string WebHookUrl
{
get
{
return ((string)(base[WEBHOOKURL_KEY]));
}
}
[ConfigurationProperty(CHANNEL_KEY, IsRequired = true)]
public string Channel
{
get
{
return ((string)(base[CHANNEL_KEY]));
}
}
[ConfigurationProperty(USERNAME_KEY, IsRequired = true)]
public string UserName
{
get
{
return ((string)(base[USERNAME_KEY]));
}
}
}
}

View File

@@ -218,7 +218,9 @@
<Compile Include="Configuration\Dashboard\SectionElement.cs" />
<Compile Include="Configuration\Dashboard\TabCollection.cs" />
<Compile Include="Configuration\Dashboard\TabElement.cs" />
<Compile Include="Configuration\HealthChecks\SlackSettingsElement.cs" />
<Compile Include="Configuration\HealthChecks\HealthCheckNotificationSettingsElement.cs" />
<Compile Include="Configuration\HealthChecks\EmailSettingsElement.cs" />
<Compile Include="Configuration\HealthChecks\DisabledHealthCheckElement.cs" />
<Compile Include="Configuration\FileSystemProviderElement.cs" />
<Compile Include="Configuration\HealthChecks\DisabledHealthChecksElementCollection.cs" />

View File

@@ -1,5 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version ="1.0" encoding="utf-8" ?>
<!--
Please note: to use healthcheck notifications you must ensure to set the umbracoApplicationUrl value
in umbracoSettings.config. For more information on this setting, please see:
https://our.umbraco.org/documentation/reference/config/umbracosettings/#web-routing
-->
<HealthChecks>
</HealthChecks>
<disabledChecks>
<!--<check id="1B5D221B-CE99-4193-97CB-5F3261EC73DF" disabledOn="" disabledBy="0" />-->
</disabledChecks>
<notificationSettings enabled="false" firstRunTime="" periodInHours="24" >
<!--<emailSettings recipientEmail="" subject="" />-->
<!--<slackSettings webHookUrl="" channel="" username="" />-->
<disabledChecks>
<!--<check id="EB66BB3B-1BCD-4314-9531-9DA2C1D6D9A7" disabledOn="" disabledBy="0" />-->
</disabledChecks>
</notificationSettings>
</HealthChecks>

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version ="1.0" encoding="utf-8" ?>
<!--
Please note: to use healthcheck notifications you must ensure to set the umbracoApplicationUrl value
@@ -9,12 +9,15 @@ https://our.umbraco.org/documentation/reference/config/umbracosettings/#web-rout
<disabledChecks>
<check id="1B5D221B-CE99-4193-97CB-5F3261EC73DF" disabledOn="" disabledBy="0" /> <!-- Smtp-->
</disabledChecks>
<notificationSettings enabled="true" firstRunTime="2300" periodInHours="24" recipientEmail="" webHookUrl="">
<notificationSettings enabled="true" firstRunTime="" periodInHours="24" >
<emailSettings recipientEmail="" subject="Umbraco Health Check Notifier" />
<slackSettings webHookUrl="" channel="#test" username="Umbraco Health Check Notifier" />
<disabledChecks>
<check id="EB66BB3B-1BCD-4314-9531-9DA2C1D6D9A7" disabledOn="" disabledBy="0" /> <!-- Https -->
<check id="ED0D7E40-971E-4BE8-AB6D-8CC5D0A6A5B0" disabledOn="" disabledBy="0" /> <!-- Click jack -->
<check id="92ABBAA2-0586-4089-8AE2-9A843439D577" disabledOn="" disabledBy="0" /> <!-- Excessive headers -->
</disabledChecks>
</notificationSettings>
</HealthChecks>

View File

@@ -70,13 +70,13 @@ namespace Umbraco.Web.Scheduling
results.LogResults();
// Send to email address if configured
if (!string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.RecipientEmail))
if (healthCheckConfig.NotificationSettings.EmailSettings != null && string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.EmailSettings.RecipientEmail) == false)
{
using (var client = new SmtpClient())
using (var mailMessage = new MailMessage())
{
mailMessage.Body = "Results"; // TODO - get from results
mailMessage.To.Add(healthCheckConfig.NotificationSettings.RecipientEmail);
mailMessage.To.Add(healthCheckConfig.NotificationSettings.EmailSettings.RecipientEmail);
mailMessage.Subject = "Umbraco Scheduled HeathChecks Results";
mailMessage.IsBodyHtml = true;
@@ -84,16 +84,16 @@ namespace Umbraco.Web.Scheduling
}
}
// TODO: get web hook and post
if (!string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.WebhookUrl))
// send Slack Incoming Webhook if configured
if (healthCheckConfig.NotificationSettings.SlackSettings != null && string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.SlackSettings.WebHookUrl) == false)
{
var slackClient = new SlackClient(healthCheckConfig.NotificationSettings.WebhookUrl);
var slackClient = new SlackClient(healthCheckConfig.NotificationSettings.SlackSettings.WebHookUrl);
var slackMessage = new SlackMessage
{
Channel = "#test",
Channel = healthCheckConfig.NotificationSettings.SlackSettings.Channel,
Text = results.ResultsAsMarkDown(true),
IconEmoji = Emoji.Ghost,
Username = "Umbraco Health Check Notifier"
Username = healthCheckConfig.NotificationSettings.SlackSettings.UserName
};
slackClient.Post(slackMessage);
}