diff --git a/src/Umbraco.Web/HealthCheck/HealthCheckController.cs b/src/Umbraco.Web/HealthCheck/HealthCheckController.cs index 14bfaaea9f..92f72cc5a6 100644 --- a/src/Umbraco.Web/HealthCheck/HealthCheckController.cs +++ b/src/Umbraco.Web/HealthCheck/HealthCheckController.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Linq; using System.Web.Http; +using Umbraco.Core.Configuration.HealthChecks; using Umbraco.Web.Editors; namespace Umbraco.Web.HealthCheck @@ -12,10 +14,16 @@ namespace Umbraco.Web.HealthCheck public class HealthCheckController : UmbracoAuthorizedJsonController { private readonly IHealthCheckResolver _healthCheckResolver; + private readonly IList _disabledCheckIds; public HealthCheckController() { _healthCheckResolver = HealthCheckResolver.Current; + + var healthCheckConfig = (HealthChecksSection)ConfigurationManager.GetSection("umbracoConfiguration/HealthChecks"); + _disabledCheckIds = healthCheckConfig.DisabledChecks + .Select(x => x.Id) + .ToList(); } public HealthCheckController(IHealthCheckResolver healthCheckResolver) @@ -30,6 +38,7 @@ namespace Umbraco.Web.HealthCheck public object GetAllHealthChecks() { var groups = _healthCheckResolver.HealthChecks + .Where(x => _disabledCheckIds.Contains(x.Id) == false) .GroupBy(x => x.Group) .OrderBy(x => x.Key); var healthCheckGroups = new List(); @@ -51,9 +60,8 @@ namespace Umbraco.Web.HealthCheck [HttpGet] public object GetStatus(Guid id) { - var check = _healthCheckResolver.HealthChecks.FirstOrDefault(x => x.Id == id); - if (check == null) throw new InvalidOperationException("No health check found with ID " + id); - + var check = GetCheckById(id); + try { //Core.Logging.LogHelper.Debug("Running health check: " + check.Name); @@ -69,10 +77,19 @@ namespace Umbraco.Web.HealthCheck [HttpPost] public HealthCheckStatus ExecuteAction(HealthCheckAction action) { - var check = _healthCheckResolver.HealthChecks.FirstOrDefault(x => x.Id == action.HealthCheckId); - if (check == null) throw new InvalidOperationException("No health check found with id " + action.HealthCheckId); - + var check = GetCheckById(action.HealthCheckId); return check.ExecuteAction(action); } + + private HealthCheck GetCheckById(Guid id) + { + var check = _healthCheckResolver.HealthChecks + .Where(x => _disabledCheckIds.Contains(x.Id) == false) + .FirstOrDefault(x => x.Id == id); + + if (check == null) throw new InvalidOperationException(string.Format("No health check found with id {0}", id)); + + return check; + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs index f4687c7751..f998e76287 100644 --- a/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs +++ b/src/Umbraco.Web/Scheduling/HealthCheckNotifier.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Configuration; using System.Linq; +using System.Net.Mail; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -9,6 +10,7 @@ using Slack.Webhooks; using Umbraco.Core; using Umbraco.Core.Configuration.HealthChecks; using Umbraco.Core.Logging; +using Umbraco.Core.Security; using Umbraco.Web.HealthCheck; namespace Umbraco.Web.Scheduling @@ -56,10 +58,19 @@ namespace Umbraco.Web.Scheduling var results = new HealthCheckResults(checks); results.LogResults(); - // TODO: get email address and send + // Send to email address if configured if (!string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.RecipientEmail)) { + using (var client = new SmtpClient()) + using (var mailMessage = new MailMessage()) + { + mailMessage.Body = "Results"; // TODO - get from results + mailMessage.To.Add(healthCheckConfig.NotificationSettings.RecipientEmail); + mailMessage.Subject = "Umbraco Scheduled HeathChecks Results"; + mailMessage.IsBodyHtml = true; + await client.SendMailAsync(mailMessage); + } } // TODO: get web hook and post