public ActionResult Index() { OptionsHelper.ReloadOptions(); if (_resourcesStatusModel == null) { _resourcesStatusModel = new ResourcesStatusModel(); } else { _resourcesStatusModel.IsFirstIntervalChanging = true; } if (_timer == null) { _resourcesStatusModel.Initialize(); CheckStatus(_resourcesStatusModel); this._isFirstTime = false; _timer = new System.Threading.Timer(CheckStatus, _resourcesStatusModel, FIRST_TIME_DELAY_MS, Timeout.Infinite); } return(View()); }
public void SendToTelegramIfNesessary(ResourcesStatusModel resourcesStatusModel) { var messageToSendGeneral = string.Empty; var sitesErrorCount = 0; var diskErrorsCount = 0; var baseErrorsCount = 0; var serviceErrorsCount = 0; var commonErrorsCount = 0; // Errors with websites var sitesErrorMessage = string.Empty; foreach (var siteStatus in resourcesStatusModel.SitesStatus) { if (!siteStatus.IsAvailable) { sitesErrorCount++; sitesErrorMessage += $"{Environment.NewLine}{siteStatus.ErrorMessage}"; } } foreach (var serverStatus in resourcesStatusModel.ServersStatus) { var messageToSendByServer = string.Empty; var isCommonError = false; var isServiceError = false; var isBaseError = false; var isDiskError = false; // Errors with disks on current server var disksWithProblems = new List <string>(); foreach (var diskData in serverStatus.DisksData) { if (diskData.IsOverflow) { disksWithProblems.Add($"\t{diskData.Name} Used space: {diskData.UsedSpaceInPercent}% (free space:{diskData.FreeSpaceToDisplay})"); } } if (diskErrorsCount == 0) { diskErrorsCount = disksWithProblems.Count; } if (disksWithProblems.Count > 0) { messageToSendByServer += $"{Environment.NewLine}Disks with problems:{Environment.NewLine}{string.Join(Environment.NewLine, disksWithProblems) }"; isDiskError = true; Logger.Log($"{serverStatus.HostName} DISK ERROR!{Environment.NewLine}{messageToSendByServer}"); } // Check services' errors if (serverStatus.WindowsServicesStatus.Count > 0) { var servicesWithProblems = new List <string>(); foreach (var serviceStatus in serverStatus.WindowsServicesStatus) { if (serviceStatus.State != Constants.ServiceState.Running) { servicesWithProblems.Add($"{serviceStatus.Name}: {serviceStatus.State.ToString()}"); } } if (serviceErrorsCount == 0) { serviceErrorsCount = servicesWithProblems.Count; } if (servicesWithProblems.Count > 0) { messageToSendByServer += $"{Environment.NewLine}Services with problems:{Environment.NewLine}{string.Join(Environment.NewLine, servicesWithProblems) }"; isServiceError = true; } } // Checking for errors with DB if (serverStatus.IsNesseseryToCheckSqlBases) { var dataBaseProblems = new List <string>(); var baseCheckResult = serverStatus.CheckingDatabaseResult; if (!string.IsNullOrEmpty(baseCheckResult.ErrorMessage)) { dataBaseProblems.Add($"Base error: '{baseCheckResult.ErrorMessage}'"); } foreach (var basesStatus in baseCheckResult.DataBaseInfoList) { dataBaseProblems.Add($"{basesStatus.Name}: {basesStatus.StatusesOneLine}"); } if (baseErrorsCount == 0) { baseErrorsCount = dataBaseProblems.Count; } if (baseErrorsCount > 0) { messageToSendByServer += $"{Environment.NewLine}Database problems:{Environment.NewLine}{string.Join(Environment.NewLine, dataBaseProblems) }"; isBaseError = true; } } // Checking common errors if (serverStatus.ErrorMessages.Count > 0) { var importantMessages = new List <string>(); foreach (var errorMessage in serverStatus.ErrorMessages) { if (!errorMessage.Contains("The RPC server is unavailable") && !errorMessage.Contains("RPC_E_CALL_CANCELED") && !errorMessage.Contains("The remote procedure call failed") && !string.IsNullOrEmpty(errorMessage)) { importantMessages.Add(errorMessage); } } if (importantMessages.Count > 0) { messageToSendByServer += $"{Environment.NewLine}Common errors:{Environment.NewLine}{string.Join(Environment.NewLine, importantMessages) }"; isCommonError = true; } } if ((isCommonError || isServiceError || isBaseError || isDiskError) && !string.IsNullOrEmpty(messageToSendByServer)) { messageToSendByServer = $"{Environment.NewLine}{serverStatus.HostName} ({serverStatus.Ip}){Environment.NewLine}{messageToSendByServer}"; } messageToSendGeneral += messageToSendByServer; } if (sitesErrorCount > 0) { messageToSendGeneral += $"{Environment.NewLine}Site problems:{Environment.NewLine}{sitesErrorMessage}"; } if (InformationMessagesPreviousState.ServiceErrorsCount == 0 && serviceErrorsCount > 0) { InformationMessagesPreviousState.IterationWithErrorsByService = 1; } else if (InformationMessagesPreviousState.ServiceErrorsCount > 0 && serviceErrorsCount > 0) { InformationMessagesPreviousState.IterationWithErrorsByService += 1; } const int MAX_ITERATION_COUNT_WITH_SERVICE_ERROR = 3; if ( ( sitesErrorCount != InformationMessagesPreviousState.SiteErrorsCount || diskErrorsCount != InformationMessagesPreviousState.DiskErrorsCount || baseErrorsCount != InformationMessagesPreviousState.BaseErrorsCount || ( serviceErrorsCount == InformationMessagesPreviousState.ServiceErrorsCount && InformationMessagesPreviousState.IterationWithErrorsByService == MAX_ITERATION_COUNT_WITH_SERVICE_ERROR ) || commonErrorsCount != InformationMessagesPreviousState.CommonErrorsCount ) && !string.IsNullOrEmpty(messageToSendGeneral) && ( sitesErrorCount > 0 || diskErrorsCount > 0 || baseErrorsCount > 0 || serviceErrorsCount > 0 || commonErrorsCount > 0 ) ) { Logger.LogError($"⚠️ {messageToSendGeneral}", sendToTelegram: true); if (serviceErrorsCount > 0) { InformationMessagesPreviousState.IterationWithErrorsByService = 0; InformationMessagesPreviousState.ThereWasServiceErrorButItWasReset = true; } } if ( ( (InformationMessagesPreviousState.SiteErrorsCount > 0 && sitesErrorCount == 0) || (InformationMessagesPreviousState.DiskErrorsCount > 0 && diskErrorsCount == 0) || (InformationMessagesPreviousState.BaseErrorsCount > 0 && baseErrorsCount == 0) || (InformationMessagesPreviousState.ThereWasServiceErrorButItWasReset && serviceErrorsCount == 0) || (InformationMessagesPreviousState.CommonErrorsCount > 0 && commonErrorsCount == 0) ) && sitesErrorCount == 0 && diskErrorsCount == 0 && baseErrorsCount == 0 && serviceErrorsCount == 0 && commonErrorsCount == 0 ) { Logger.LogError($"InformationMessagesPreviousState.SiteErrorsCount: {InformationMessagesPreviousState.SiteErrorsCount}"); Logger.LogError($"sitesErrorCount: {sitesErrorCount}"); Logger.LogError($"InformationMessagesPreviousState.CommonErrorsCount: {InformationMessagesPreviousState.CommonErrorsCount}"); Logger.LogError($"commonErrorsCount: {commonErrorsCount}"); Logger.LogError($"InformationMessagesPreviousState.DiskErrorsCount: {InformationMessagesPreviousState.DiskErrorsCount}"); Logger.LogError($"diskErrorsCount: {diskErrorsCount}"); Logger.LogError($"InformationMessagesPreviousState.BaseErrorsCount: {InformationMessagesPreviousState.BaseErrorsCount}"); Logger.LogError($"baseErrorsCount: {baseErrorsCount}"); Logger.LogError($"InformationMessagesPreviousState.ThereWasServiceErrorButItWasReset: {InformationMessagesPreviousState.ThereWasServiceErrorButItWasReset}"); Logger.LogError($"serviceErrorsCount: {serviceErrorsCount}"); Logger.LogError("There are no more errors ✅", sendToTelegram: true); InformationMessagesPreviousState.ThereWasServiceErrorButItWasReset = false; } // Save info about previous state InformationMessagesPreviousState.SiteErrorsCount = sitesErrorCount; InformationMessagesPreviousState.DiskErrorsCount = diskErrorsCount; InformationMessagesPreviousState.BaseErrorsCount = baseErrorsCount; InformationMessagesPreviousState.ServiceErrorsCount = serviceErrorsCount; InformationMessagesPreviousState.CommonErrorsCount = commonErrorsCount; }