2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2016-06-13 17:42:05 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using System.Configuration;
|
2016-06-13 17:42:05 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Web.Http;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using Umbraco.Core.Configuration;
|
|
|
|
|
|
using Umbraco.Core.Configuration.HealthChecks;
|
2016-06-13 17:42:05 +02:00
|
|
|
|
using Umbraco.Web.Editors;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.HealthCheck
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The API controller used to display the health check info and execute any actions
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class HealthCheckController : UmbracoAuthorizedJsonController
|
|
|
|
|
|
{
|
2016-09-01 19:06:08 +02:00
|
|
|
|
private readonly HealthCheckCollection _checks;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
private readonly IList<Guid> _disabledCheckIds;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
private readonly ILogger _logger;
|
2016-06-13 17:42:05 +02:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
public HealthCheckController(HealthCheckCollection checks, ILogger logger)
|
2016-06-13 17:42:05 +02:00
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
_checks = checks ?? throw new ArgumentNullException(nameof(checks));
|
|
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
var healthCheckConfig = UmbracoConfig.For.HealthCheck();
|
|
|
|
|
|
_disabledCheckIds = healthCheckConfig.DisabledChecks
|
|
|
|
|
|
.Select(x => x.Id)
|
|
|
|
|
|
.ToList();
|
2016-06-13 17:42:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a grouped list of health checks, but doesn't actively check the status of each health check.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>Returns a collection of anonymous objects representing each group.</returns>
|
|
|
|
|
|
public object GetAllHealthChecks()
|
|
|
|
|
|
{
|
2016-09-01 19:06:08 +02:00
|
|
|
|
var groups = _checks
|
2017-09-08 19:39:13 +02:00
|
|
|
|
.Where(x => _disabledCheckIds.Contains(x.Id) == false)
|
2016-06-13 17:42:05 +02:00
|
|
|
|
.GroupBy(x => x.Group)
|
|
|
|
|
|
.OrderBy(x => x.Key);
|
|
|
|
|
|
var healthCheckGroups = new List<HealthCheckGroup>();
|
|
|
|
|
|
foreach (var healthCheckGroup in groups)
|
|
|
|
|
|
{
|
|
|
|
|
|
var hcGroup = new HealthCheckGroup
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = healthCheckGroup.Key,
|
|
|
|
|
|
Checks = healthCheckGroup
|
|
|
|
|
|
.OrderBy(x => x.Name)
|
|
|
|
|
|
.ToList()
|
|
|
|
|
|
};
|
|
|
|
|
|
healthCheckGroups.Add(hcGroup);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return healthCheckGroups;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
[HttpGet]
|
2016-06-13 17:42:05 +02:00
|
|
|
|
public object GetStatus(Guid id)
|
|
|
|
|
|
{
|
2017-09-08 19:39:13 +02:00
|
|
|
|
var check = GetCheckById(id);
|
2017-09-23 10:08:18 +02:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
//Core.Logging.LogHelper.Debug<HealthCheckController>("Running health check: " + check.Name);
|
|
|
|
|
|
return check.GetStatus();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Error<HealthCheckController>("Exception in health check: " + check.Name, e);
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2016-06-13 17:42:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public HealthCheckStatus ExecuteAction(HealthCheckAction action)
|
|
|
|
|
|
{
|
2017-09-08 19:39:13 +02:00
|
|
|
|
var check = GetCheckById(action.HealthCheckId);
|
2016-06-13 17:42:05 +02:00
|
|
|
|
return check.ExecuteAction(action);
|
|
|
|
|
|
}
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
private HealthCheck GetCheckById(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var check = _checks
|
2017-09-14 11:41:46 +02:00
|
|
|
|
.Where(x => _disabledCheckIds.Contains(x.Id) == false)
|
|
|
|
|
|
.FirstOrDefault(x => x.Id == id);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
2017-09-14 11:41:46 +02:00
|
|
|
|
if (check == null) throw new InvalidOperationException($"No health check found with id {id}");
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
return check;
|
|
|
|
|
|
}
|
2016-06-13 17:42:05 +02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|