public static async Task ReceiveMessage(Message message, CancellationToken token) { if (message.ContentType.Equals("application/json")) { HealthcheckResult result = null; var messageContract = JsonConvert.DeserializeObject <OutGoingMessage>(Encoding.UTF8.GetString(message.Body)); if (!SharedConfig.SubscriptionName.Equals(messageContract.TargetInstance, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(messageContract.TargetInstance)) { // Trigger only on the proper instance Sitecore.Diagnostics.Log.Info($"[Advanced.Healthcheck] - Skipping remote event: {messageContract.TargetInstance}, current instance/Subscription {SharedConfig.SubscriptionName}", message); return; } if (message.Label.Equals(Constants.TemplateNames.RemoteLogFileCheckTemplateName)) { result = LogFileCheck.RunHealthcheck(messageContract.Parameters["FileNameFormat"], Path.Combine(Sitecore.Configuration.Settings.DataFolder, "logs"), DateTime.ParseExact(messageContract.Parameters["LastCheckTime"], "yyyyMMddTHHmmss", CultureInfo.InvariantCulture), int.Parse(messageContract.Parameters["NumberOfDaysToCheck"]), true); } else if (message.Label.Equals(Constants.TemplateNames.RemoteCertificateCheckTemplateName)) { result = CertificateCheck.RunHealthcheck(messageContract.Parameters["StoreName"], messageContract.Parameters["Location"], messageContract.Parameters["Value"], messageContract.Parameters["FindByType"], int.Parse(messageContract.Parameters["Warn Before"])); } else if (message.Label.Equals(Constants.TemplateNames.RemoteXConnectApiCheckTemplateName)) { result = XConnectApiCheck.RunHealthcheck(messageContract.Parameters["XConnectApiCertificateConnectionStringKey"], messageContract.Parameters["XConnectApiConnectionStringKey"], int.Parse(messageContract.Parameters["Warn Before"])); } else if (message.Label.Equals(Constants.TemplateNames.RemoteApiHealthcheckTemplateName)) { result = ApiCheck.RunHealthcheck(messageContract); } else if (message.Label.Equals(Constants.TemplateNames.RemoteCustomHealthcheckTemplateName)) { result = CustomCheck.RunHealthcheck(messageContract.Parameters["Type"], Sitecore.StringUtil.GetNameValues(messageContract.Parameters["Parameters"])); } else if (message.Label.Equals(Constants.TemplateNames.RemoteDatabaseHealtcheckTemplateName)) { result = DatabaseCheck.RunHealthcheck(messageContract.Parameters["ConnectionStringKey"]); } else if (message.Label.Equals(Constants.TemplateNames.RemoteDiskSpaceCheckTemplateName)) { result = DiskSpaceCheck.RunHealthcheck(messageContract.Parameters["DriveName"], int.Parse(messageContract.Parameters["ErrorPercentageThreshold"]), int.Parse(messageContract.Parameters["WarningPercentageThreshold"])); } else if (message.Label.Equals(Constants.TemplateNames.RemoteLicenseHealthcheckTemplateName)) { result = LicenseCheck.RunHealthcheck(int.Parse(messageContract.Parameters["WarnBefore"]), int.Parse(messageContract.Parameters["ErrorBefore"])); } else if (message.Label.Equals(Constants.TemplateNames.RemoteWindowsServiceCheckTemplateName)) { result = WindowsServiceCheck.RunHealthcheck(messageContract.Parameters["ServiceName"], messageContract.Parameters["HealthyMessage"]); } if (result != null) { var sender = new MessageSender(SharedConfig.ConnectionStringOrKey, SharedConfig.IncomingQueueName); var incomingMessage = new HealthcheckResultMessage { Result = result, ComponentId = messageContract.ComponentId, LastCheckTime = messageContract.EventRaised }; await sender.SendAsync(new Message { Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(incomingMessage)), ContentType = "application/json", Label = "Result" }).ConfigureAwait(false); } } }
public void HealthcheckStarted(object sender, EventArgs args) { Assert.ArgumentNotNull(sender, "sender"); Assert.ArgumentNotNull(args, "args"); var data = args as RemoteEventArgs <HealthcheckStartedRemoteEvent>; // Receving the data var messageContract = data.Event.ComponentData; if (!SharedConfig.InstanceName.Equals(messageContract.TargetInstance) && !string.IsNullOrEmpty(messageContract.TargetInstance)) { // Trigger only on the proper instance Sitecore.Diagnostics.Log.Info($"[Advanced.Healthcheck] - Skipping remote event: {messageContract.TargetInstance}, current instance {Settings.InstanceName}", this); return; } HealthcheckResult result = null; if (data.Event.Type.Equals(Constants.TemplateNames.RemoteLogFileCheckTemplateName)) { result = LogFileCheck.RunHealthcheck(messageContract.Parameters["FileNameFormat"], Path.Combine(Sitecore.Configuration.Settings.DataFolder, "logs"), DateTime.ParseExact(messageContract.Parameters["LastCheckTime"], "yyyyMMddTHHmmss", CultureInfo.InvariantCulture), int.Parse(messageContract.Parameters["NumberOfDaysToCheck"]), true); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteCertificateCheckTemplateName)) { result = CertificateCheck.RunHealthcheck(messageContract.Parameters["StoreName"], messageContract.Parameters["Location"], messageContract.Parameters["Value"], messageContract.Parameters["FindByType"], int.Parse(messageContract.Parameters["Warn Before"])); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteXConnectApiCheckTemplateName)) { result = XConnectApiCheck.RunHealthcheck(messageContract.Parameters["XConnectApiCertificateConnectionStringKey"], messageContract.Parameters["XConnectApiConnectionStringKey"], int.Parse(messageContract.Parameters["Warn Before"])); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteApiHealthcheckTemplateName)) { result = ApiCheck.RunHealthcheck(messageContract); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteCustomHealthcheckTemplateName)) { result = CustomCheck.RunHealthcheck(messageContract.Parameters["Type"], Sitecore.StringUtil.GetNameValues(messageContract.Parameters["Parameters"])); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteDatabaseHealtcheckTemplateName)) { result = DatabaseCheck.RunHealthcheck(messageContract.Parameters["ConnectionStringKey"]); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteDiskSpaceCheckTemplateName)) { result = DiskSpaceCheck.RunHealthcheck(messageContract.Parameters["DriveName"], int.Parse(messageContract.Parameters["ErrorPercentageThreshold"]), int.Parse(messageContract.Parameters["WarningPercentageThreshold"])); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteLicenseHealthcheckTemplateName)) { result = LicenseCheck.RunHealthcheck(int.Parse(messageContract.Parameters["WarnBefore"]), int.Parse(messageContract.Parameters["ErrorBefore"])); } else if (data.Event.Type.Equals(Constants.TemplateNames.RemoteWindowsServiceCheckTemplateName)) { result = WindowsServiceCheck.RunHealthcheck(messageContract.Parameters["ServiceName"], messageContract.Parameters["HealthyMessage"]); } if (result != null) { if (result.ErrorList?.Entries != null && result.ErrorList.Entries.Any(t => t.Exception != null)) { foreach (var entry in result.ErrorList.Entries) { if (entry.Exception != null) { entry.SerializedException = JsonConvert.SerializeObject(entry.Exception).Replace("\"SafeSerializationManager\":", "\"_SafeSerializationManager\":"); entry.Exception = null; } } } var incomingMessage = new HealthcheckResultMessage { Result = result, ComponentId = messageContract.ComponentId, LastCheckTime = messageContract.EventRaised }; // Sending result back var database = Sitecore.Configuration.Factory.GetDatabase("web"); var eventQueue = database.RemoteEvents.EventQueue; var remoteEvent = new HealthcheckFinishedRemoteEvent("healthcheck:finished:remote", incomingMessage); eventQueue.QueueEvent <HealthcheckFinishedRemoteEvent>(remoteEvent, true, false); } }