public MonitorState RefreshCurrentState(bool forceRefreshNow = false) { MonitorState resultMonitorState = new MonitorState() { State = CollectorState.NotAvailable, RepeatCount = 0 }; if (AlertHeaderText != null && AlertHeaderText.Trim().Length > 0) { resultMonitorState.AlertHeader = AlertHeaderText; } if (AlertFooterText != null && AlertFooterText.Trim().Length > 0) { resultMonitorState.AlertFooter = AlertFooterText; } RefreshCount++; //CurrentPollAborted = false; PollingOverrideActive = false; if (CurrentState.State != CollectorState.ConfigurationError) { if (InServiceWindow) //currently in Service window { if (ServiceWindows.IsInTimeWindow()) //Service window expired { InServiceWindow = false; RaiseEntereringServiceWindow(); } } else if (!ServiceWindows.IsInTimeWindow()) { InServiceWindow = true; RaiseExitingServiceWindow(); } if (!Enabled) { resultMonitorState.State = CollectorState.Disabled; StagnantStateFirstRepeat = false; StagnantStateSecondRepeat = false; StagnantStateThirdRepeat = false; resultMonitorState.RawDetails = "Collector host is disabled."; } else if (InServiceWindow) { resultMonitorState.State = CollectorState.NotInServiceWindow; StagnantStateFirstRepeat = false; StagnantStateSecondRepeat = false; StagnantStateThirdRepeat = false; resultMonitorState.RawDetails = "Disabled due to 'out of service window' event."; } else if (CollectorAgents == null || CollectorAgents.Count == 0) //like old folder type collector { resultMonitorState.State = CollectorState.None; StagnantStateFirstRepeat = false; StagnantStateSecondRepeat = false; StagnantStateThirdRepeat = false; resultMonitorState.RawDetails = "There are no agents."; } else if (CollectorAgents != null && CollectorAgents.Count == CollectorAgents.Count(ca => !ca.Enabled)) { resultMonitorState.State = CollectorState.Disabled; StagnantStateFirstRepeat = false; StagnantStateSecondRepeat = false; StagnantStateThirdRepeat = false; resultMonitorState.RawDetails = "Disabled because all agents are disabled."; } else if (CurrentState.State != CollectorState.NotAvailable && !forceRefreshNow && EnabledPollingOverride && !EnablePollFrequencySliding && (LastStateUpdate.AddSeconds(OnlyAllowUpdateOncePerXSec) > DateTime.Now)) { //Not time yet for update PollingOverrideActive = true; //CurrentPollAborted = true; //repeat same State resultMonitorState = null; resultMonitorState = CurrentState; //resultMonitorState.Timestamp = DateTime.Now; //if (resultMonitorState.RawDetails == null) // resultMonitorState.RawDetails = ""; //if (resultMonitorState.RawDetails.Length > 0) // resultMonitorState.RawDetails += "\r\n"; //resultMonitorState.RawDetails += "Due to polling override (OnlyAllowUpdateOncePerXSec) the previous state is repeated."; //resultMonitorState.State = CurrentState.State; //resultMonitorState.CurrentValue = CurrentState.ReadValues(); //resultMonitorState.RawDetails = "Due to polling override (OnlyAllowUpdateOncePerXSec) the previous state is repeated."; RaiseLoggingPollingOverridesTriggeredEvent(string.Format("Polling override of {0} seconds not reached yet", OnlyAllowUpdateOncePerXSec)); } else if (CurrentState.State != CollectorState.NotAvailable && !forceRefreshNow && EnabledPollingOverride && EnablePollFrequencySliding && !StagnantStateFirstRepeat && !StagnantStateThirdRepeat && !StagnantStateSecondRepeat && LastStateUpdate.AddSeconds(OnlyAllowUpdateOncePerXSec - 1) > DateTime.Now) { PollingOverrideActive = true; resultMonitorState = null; resultMonitorState = CurrentState; StagnantStateFirstRepeat = true; LastStateUpdate = DateTime.Now; } else if (CurrentState.State != CollectorState.NotAvailable && !forceRefreshNow && EnabledPollingOverride && EnablePollFrequencySliding && StagnantStateFirstRepeat && !StagnantStateThirdRepeat && !StagnantStateSecondRepeat && LastStateUpdate.AddSeconds(PollSlideFrequencyAfterFirstRepeatSec - 1) > DateTime.Now) { PollingOverrideActive = true; resultMonitorState = null; resultMonitorState = CurrentState; StagnantStateFirstRepeat = true; StagnantStateSecondRepeat = true; LastStateUpdate = DateTime.Now; } else if (CurrentState.State != CollectorState.NotAvailable && !forceRefreshNow && EnabledPollingOverride && EnablePollFrequencySliding && StagnantStateFirstRepeat && StagnantStateThirdRepeat && !StagnantStateSecondRepeat && LastStateUpdate.AddSeconds(PollSlideFrequencyAfterSecondRepeatSec - 1) > DateTime.Now) { PollingOverrideActive = true; resultMonitorState = null; resultMonitorState = CurrentState; StagnantStateFirstRepeat = true; StagnantStateSecondRepeat = true; StagnantStateThirdRepeat = true; LastStateUpdate = DateTime.Now; } else if (CurrentState.State != CollectorState.NotAvailable && !forceRefreshNow && EnabledPollingOverride && EnablePollFrequencySliding && StagnantStateFirstRepeat && StagnantStateThirdRepeat && StagnantStateSecondRepeat && LastStateUpdate.AddSeconds(PollSlideFrequencyAfterThirdRepeatSec - 1) > DateTime.Now) { PollingOverrideActive = true; resultMonitorState = null; resultMonitorState = CurrentState; StagnantStateFirstRepeat = true; StagnantStateSecondRepeat = true; StagnantStateThirdRepeat = true; if (!stagnantStateMaxReached) { LastStateUpdate = DateTime.Now; stagnantStateMaxReached = true; } } //else if (CurrentState.State != CollectorState.NotAvailable && !forceRefreshNow && EnabledPollingOverride && EnablePollFrequencySliding && // ( // (StagnantStateThirdRepeat && (LastStateUpdate.AddSeconds(PollSlideFrequencyAfterThirdRepeatSec) > DateTime.Now)) || // (!StagnantStateThirdRepeat && StagnantStateSecondRepeat && (LastStateUpdate.AddSeconds(PollSlideFrequencyAfterSecondRepeatSec) > DateTime.Now)) || // (!StagnantStateThirdRepeat && !StagnantStateSecondRepeat && StagnantStateFirstRepeat && (LastStateUpdate.AddSeconds(PollSlideFrequencyAfterFirstRepeatSec) > DateTime.Now)) || // (!StagnantStateFirstRepeat && !StagnantStateThirdRepeat && !StagnantStateSecondRepeat && (LastStateUpdate.AddSeconds(OnlyAllowUpdateOncePerXSec) > DateTime.Now)) // ) // ) //{ // //Not time yet for update // //CurrentPollAborted = true; // //repeat same State // resultMonitorState = null; // resultMonitorState = CurrentState.Clone(); // resultMonitorState.Timestamp = DateTime.Now; // //if (resultMonitorState.RawDetails == null) // // resultMonitorState.RawDetails = ""; // //if (resultMonitorState.RawDetails.Length > 0) // // resultMonitorState.RawDetails += "\r\n"; // //resultMonitorState.State = CurrentState.State; // //resultMonitorState.CurrentValue = CurrentState.ReadValues(); // //if (StagnantStateThirdRepeat) // // resultMonitorState.RawDetails += "Due to polling override (StagnantStateThirdRepeat) the previous state is repeated."; // //else if (StagnantStateSecondRepeat) // // resultMonitorState.RawDetails += "Due to polling override (StagnantStateSecondRepeat) the previous state is repeated."; // //else if (StagnantStateFirstRepeat) // // resultMonitorState.RawDetails += "Due to polling override (StagnantStateFirstRepeat) the previous state is repeated."; // //else // // resultMonitorState.RawDetails += "Due to polling override (EnablePollFrequencySliding) the previous state is repeated."; //} else { stagnantStateMaxReached = false; //*********** Call actual collector GetState ********** LastStateCheckAttemptBegin = DateTime.Now; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //Applies config vars to Initial configs of agents (on the run) ApplyConfigVarsNow(); sw.Start(); //first check if remote host exection is required if (EnableRemoteExecute || (OverrideRemoteAgentHost && !BlockParentOverrideRemoteAgentHostSettings)) { resultMonitorState = GetRemoteState(); } else if (!RunAsEnabled || RunAs == null || RunAs.Length == 0 || RunTimeUserNameCacheFile == null || RunTimeUserNameCacheFile.Length == 0 || RunTimeMasterKey == null || RunTimeMasterKey.Length == 0) { resultMonitorState = GetStateFromLocalCurrentUser(); } else { resultMonitorState = GetStateFromLocal(); } sw.Stop(); resultMonitorState.CallDurationMS = (int)sw.ElapsedMilliseconds; RaiseAllAgentsExecutionTime(sw.ElapsedMilliseconds); #region Calculate summarized state if (resultMonitorState.ChildStates != null && resultMonitorState.ChildStates.Count > 0) { int allCount = resultMonitorState.ChildStates.Count; int disabledCount = (from cs in resultMonitorState.ChildStates where cs.State == CollectorState.Disabled select cs).Count(); int goodCount = (from cs in resultMonitorState.ChildStates where cs.State == CollectorState.Good select cs).Count(); int warningCount = (from cs in resultMonitorState.ChildStates where cs.State == CollectorState.Warning select cs).Count(); int errorCount = (from cs in resultMonitorState.ChildStates where cs.State == CollectorState.Error select cs).Count(); if (allCount == disabledCount) { resultMonitorState.State = CollectorState.Disabled; } else if (AgentCheckSequence == QuickMon.AgentCheckSequence.All) { if (allCount - disabledCount == goodCount) { resultMonitorState.State = CollectorState.Good; } else if (allCount - disabledCount == errorCount) { resultMonitorState.State = CollectorState.Error; } else { resultMonitorState.State = CollectorState.Warning; } } else if (AgentCheckSequence == QuickMon.AgentCheckSequence.FirstSuccess) { if (goodCount > 0) { resultMonitorState.State = CollectorState.Good; } else if (warningCount > 0) { resultMonitorState.State = CollectorState.Warning; } else { resultMonitorState.State = CollectorState.Error; } } else { if (errorCount > 0) { resultMonitorState.State = CollectorState.Error; } else if (warningCount > 0) { resultMonitorState.State = CollectorState.Warning; } else { resultMonitorState.State = CollectorState.Good; } } } #endregion } } else { resultMonitorState.State = CollectorState.ConfigurationError; } //Set current CH state plus raise any alerts if required // if (!PollingOverrideActive) SetCurrentState(resultMonitorState); return(resultMonitorState); }
public void SetCurrentState(MonitorState newState) { bool raiseAlertNow = false; if (!PollingOverrideActive) { if (currentState != null) { LastStateUpdate = DateTime.Now; if (FirstStateUpdate < (new DateTime(2000, 1, 1))) { FirstStateUpdate = DateTime.Now; } bool stateChanged = currentState.State != newState.State; #region Polling overide stuff if (stateChanged) { LastStateChange = DateTime.Now; StagnantStateFirstRepeat = false; StagnantStateSecondRepeat = false; StagnantStateThirdRepeat = false; if (EnabledPollingOverride && EnablePollFrequencySliding && CurrentState.State != CollectorState.NotAvailable) { RaiseLoggingPollingOverridesTriggeredEvent("Frequency sliding cancelled due to collector state value changed"); } } else if (EnabledPollingOverride && EnablePollFrequencySliding && CurrentState.State != CollectorState.NotAvailable) { if (!StagnantStateFirstRepeat) { newState.RepeatCount = 1; StagnantStateFirstRepeat = true; StagnantStateSecondRepeat = false; StagnantStateThirdRepeat = false; RaiseLoggingPollingOverridesTriggeredEvent("Frequency sliding reached 1st stagnant stage"); } else if (!StagnantStateSecondRepeat) { newState.RepeatCount = 2; StagnantStateSecondRepeat = true; StagnantStateThirdRepeat = false; RaiseLoggingPollingOverridesTriggeredEvent("Frequency sliding reached 2nd stagnant stage"); } else if (!StagnantStateThirdRepeat) { newState.RepeatCount = 3; StagnantStateThirdRepeat = true; RaiseLoggingPollingOverridesTriggeredEvent("Frequency sliding reached 3rd stagnant stage"); } } #endregion #region Check if alert should be raised now if (stateChanged) { if (newState.State == CollectorState.Good) { numberOfPollingsInErrWarn = 0; } if (DelayErrWarnAlertForXSec > 0 || DelayErrWarnAlertForXPolls > 0) // alert should be delayed { delayErrWarnAlertTime = DateTime.Now.AddSeconds(DelayErrWarnAlertForXSec); numberOfPollingsInErrWarn = 0; waitAlertTimeErrWarnInMinFlagged = true; } else { raiseAlertNow = true; } } else { if (waitAlertTimeErrWarnInMinFlagged) //waiting for delayed alert { if (DelayErrWarnAlertForXSec > 0 && DateTime.Now > delayErrWarnAlertTime) { raiseAlertNow = true; waitAlertTimeErrWarnInMinFlagged = false; numberOfPollingsInErrWarn = 0; //handle further alerts as if it changed now again LastStateChange = DateTime.Now; } else if (DelayErrWarnAlertForXPolls > 0 && DelayErrWarnAlertForXPolls <= numberOfPollingsInErrWarn) { raiseAlertNow = true; waitAlertTimeErrWarnInMinFlagged = false; numberOfPollingsInErrWarn = 0; //handle further alerts as if it changed now again LastStateChange = DateTime.Now; } else { raiseAlertNow = false; } } else { if ( (RepeatAlertInXMin > 0 && (LastStateChange.AddMinutes(RepeatAlertInXMin) < DateTime.Now)) || (RepeatAlertInXPolls > 0 && RepeatAlertInXPolls <= numberOfPollingsInErrWarn) ) { raiseAlertNow = true; numberOfPollingsInErrWarn = 0; //handle further alerts as if it changed now again LastStateChange = DateTime.Now; } else { raiseAlertNow = false; } } } if (raiseAlertNow) { //only allow repeat alert after specified minutes if (AlertOnceInXMin > 0 && LastAlertTime.AddMinutes(AlertOnceInXMin) > DateTime.Now) { raiseAlertNow = false; //cancel alert } } else { if (newState.State == CollectorState.Warning || newState.State == CollectorState.Error) { numberOfPollingsInErrWarn++; } else { numberOfPollingsInErrWarn = 0; } } #endregion AddStateToHistory(currentState); #region Corrective scripts try { if (!CorrectiveScriptDisabled && (ParentMonitorPack == null || ParentMonitorPack.CorrectiveScriptsEnabled)) { if (newState.State == CollectorState.Good && (currentState.State == CollectorState.Error || currentState.State == CollectorState.Warning)) { foreach (string scriptName in RunRestorationScripts(stateChanged)) { newState.ScriptsRan.Add("Restoration script: " + scriptName); } } else // if (stateChanged || !CorrectiveScriptsOnlyOnStateChange) { if (newState.State == CollectorState.Error) { foreach (string scriptName in RunErrorCorrectiveScripts(stateChanged)) { newState.ScriptsRan.Add("Error corrective script: " + scriptName); } } else if (newState.State == CollectorState.Warning) { foreach (string scriptName in RunWarningCorrectiveScripts(stateChanged)) { newState.ScriptsRan.Add("Warning corrective script: " + scriptName); } } } } } catch (Exception corrEx) { CorrectiveScriptExecutionFailed?.Invoke(this, "Error executing corrective scripts", corrEx.Message); } #endregion } currentState = newState; #region Set Alert texts if (AlertHeaderText != null && AlertHeaderText.Trim().Length > 0) { currentState.AlertHeader = AlertHeaderText; } if (AlertFooterText != null && AlertFooterText.Trim().Length > 0) { currentState.AlertFooter = AlertFooterText; } if (currentState.State == CollectorState.Good && GoodAlertText != null && GoodAlertText.Trim().Length > 0) { currentState.AdditionalAlertText = GoodAlertText; } else if (currentState.State == CollectorState.Warning && WarningAlertText != null && WarningAlertText.Trim().Length > 0) { currentState.AdditionalAlertText = WarningAlertText; } else if (currentState.State == CollectorState.Error && ErrorAlertText != null && ErrorAlertText.Trim().Length > 0) { currentState.AdditionalAlertText = ErrorAlertText; } #endregion } RaiseStateUpdated(); #region Raise event for Alert to be handled by Monitorpack if (raiseAlertNow) { LastAlertTime = DateTime.Now; //reset alert time switch (newState.State) { case CollectorState.Good: LastGoodState = newState; GoodStateCount++; RaiseAlertGoodState(); break; case CollectorState.Warning: LastWarningState = newState; WarningStateCount++; RaiseAlertWarningState(); break; case CollectorState.Error: case CollectorState.ConfigurationError: LastErrorState = newState; ErrorStateCount++; RaiseAlertErrorState(); break; default: RaiseNoStateChanged(); break; } } else { RaiseNoStateChanged(); } #endregion }