private void CollectorHostRefreshCurrentState(CollectorHost collectorHost, bool disablePollingOverrides = false, bool forceSingleCollectorUpdate = false) { if (!AbortPolling) { RaiseCollectorHostCalled(collectorHost); try { MonitorState chms = null; collectorHost.RunTimeMasterKey = ""; collectorHost.RunTimeUserNameCacheFile = ""; collectorHost.BlockedCollectorAgentTypes = new List <string>(); collectorHost.BlockedCollectorAgentTypes.AddRange(BlockedCollectorAgentTypes.ToArray()); if (collectorHost.RunAsEnabled && collectorHost.RunAs != null && collectorHost.RunAs.Length > 0) { if (UserNameCacheMasterKey.Length > 0 && System.IO.File.Exists(UserNameCacheFilePath) && QuickMon.Security.CredentialManager.IsAccountPersisted(UserNameCacheFilePath, UserNameCacheMasterKey, collectorHost.RunAs) && QuickMon.Security.CredentialManager.IsAccountDecryptable(UserNameCacheFilePath, UserNameCacheMasterKey, collectorHost.RunAs)) { collectorHost.RunTimeMasterKey = UserNameCacheMasterKey; collectorHost.RunTimeUserNameCacheFile = UserNameCacheFilePath; } else if (ApplicationUserNameCacheMasterKey != null && ApplicationUserNameCacheFilePath != null && ApplicationUserNameCacheMasterKey.Length > 0 && System.IO.File.Exists(ApplicationUserNameCacheFilePath) && QuickMon.Security.CredentialManager.IsAccountPersisted(ApplicationUserNameCacheFilePath, ApplicationUserNameCacheMasterKey, collectorHost.RunAs) && QuickMon.Security.CredentialManager.IsAccountDecryptable(ApplicationUserNameCacheFilePath, ApplicationUserNameCacheMasterKey, collectorHost.RunAs)) { collectorHost.RunTimeMasterKey = ApplicationUserNameCacheMasterKey; collectorHost.RunTimeUserNameCacheFile = ApplicationUserNameCacheFilePath; } } //collectorHost.ApplyConfigVarsNow(ConfigVariables); chms = collectorHost.RefreshCurrentState(disablePollingOverrides); #region Do/Check/Set dependant CollectorHosts if (chms.State == CollectorState.Error && collectorHost.ChildCheckBehaviour != ChildCheckBehaviour.ContinueOnWarningOrError) { SetDependantCollectorHostStates(collectorHost, CollectorState.NotAvailable); } else if (chms.State == CollectorState.Warning && collectorHost.ChildCheckBehaviour == ChildCheckBehaviour.OnlyRunOnSuccess) { SetDependantCollectorHostStates(collectorHost, CollectorState.NotAvailable); } else if (chms.State == CollectorState.Disabled || chms.State == CollectorState.ConfigurationError || !collectorHost.IsEnabledNow()) { SetDependantCollectorHostStates(collectorHost, CollectorState.Disabled); } else { //Remote execute and dependant Collector Hosts if (collectorHost.ForceRemoteExcuteOnChildCollectors) { collectorHost.OverrideForceRemoteExcuteOnChildCollectors = true; SetChildCollectorHostRemoteExecuteDetails(collectorHost, collectorHost.RemoteAgentHostAddress, collectorHost.RemoteAgentHostPort); } //Polling overrides and dependant Collector Hosts if (collectorHost.EnabledPollingOverride) { SetChildCollectorHostPollingOverrides(collectorHost); } #region Loop through dependant CollectorHosts if (ConcurrencyLevel > 1 & !forceSingleCollectorUpdate) { ParallelOptions po = new ParallelOptions() { MaxDegreeOfParallelism = ConcurrencyLevel }; ParallelLoopResult parResult = Parallel.ForEach((from c in CollectorHosts where c.ParentCollectorId == collectorHost.UniqueId select c), po, dependentCollectorHost => CollectorHostRefreshCurrentState(dependentCollectorHost, disablePollingOverrides)); if (!parResult.IsCompleted) { SendNotifierAlert(AlertLevel.Error, DetailLevel.All, "Error querying collectors in parralel"); } } else //use old single threaded way { //Refresh states foreach (CollectorHost dependentCollectorHost in (from c in CollectorHosts where c.ParentCollectorId == collectorHost.UniqueId select c)) { CollectorHostRefreshCurrentState(dependentCollectorHost, disablePollingOverrides); } } #endregion } #endregion LoggingCollectorEvent(string.Format("Collector '{0}' return state '{1}'", collectorHost.Name, collectorHost.CurrentState), collectorHost); } catch (Exception ex) { WriteLogging(string.Format("CollectorHostRefreshCurrentState error: {0}", ex.Message)); if (!ex.Message.Contains("Collection was modified; enumeration operation may not execute")) { RaiseMonitorPackError("Internal error. Collector config was modified while in use!"); } else { RaiseCollectorError(collectorHost, ex.Message); } } } }
private MonitorState GetStateFromLocalCurrentUser() { MonitorState resultMonitorState = new MonitorState() { State = CollectorState.UpdateInProgress, ExecutedOnHostComputer = System.Net.Dns.GetHostName(), RanAs = System.Security.Principal.WindowsIdentity.GetCurrent().Name }; try { //First set blank/NA state foreach (ICollector ca in CollectorAgents) { ca.CurrentState = new MonitorState() { ForAgent = ca.Name, State = CollectorState.UpdateInProgress, RawDetails = "N/A", HtmlDetails = "<p>N/A</p>" }; } int agentId = 0; foreach (ICollector ca in CollectorAgents) { MonitorState caMs; if (BlockedCollectorAgentTypes.Contains(ca.AgentClassName)) { caMs = new MonitorState() { State = CollectorState.Disabled, RawDetails = "This agent is disabled by host process", HtmlDetails = "<p>This agent is disabled by host process</p>" }; } else if (ca.Enabled) { caMs = ca.GetState(); } else { caMs = new MonitorState() { State = CollectorState.Disabled, RawDetails = "This agent is disabled", HtmlDetails = "<p>This agent is disabled</p>" }; } caMs.ForAgent = ca.Name; caMs.ForAgentId = agentId; caMs.PrimaryUIValue = ca.PrimaryUIValue; agentId++; resultMonitorState.ChildStates.Add(caMs); //If we only care for the first success and find it don't look further if (AgentCheckSequence == QuickMon.AgentCheckSequence.FirstSuccess && caMs.State == CollectorState.Good) { break; } //If we only care for the first error and find it don't look further else if (AgentCheckSequence == QuickMon.AgentCheckSequence.FirstError && caMs.State == CollectorState.Error) { break; } } } catch (Exception exLocal) { resultMonitorState.State = CollectorState.Error; resultMonitorState.RawDetails = exLocal.ToString(); } //resultMonitorState.ExecutedOnHostComputer = System.Net.Dns.GetHostName(); return(resultMonitorState); }
public MonitorState GetState(QuickMon.RemoteCollectorHost entry) { StringBuilder consoleOutPut = new StringBuilder(); MonitorState monitorState = new MonitorState(); /*** For Console debugging **/ consoleOutPut.AppendFormat("{0}: Running collector host: {1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), entry.Name); try { OperationContext context = OperationContext.Current; System.ServiceModel.Channels.MessageProperties messageProperties = context.IncomingMessageProperties; System.ServiceModel.Channels.RemoteEndpointMessageProperty endpointProperty = messageProperties[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty; consoleOutPut.AppendFormat(" Requested from {0}:{1}\r\n", endpointProperty.Address, endpointProperty.Port); } catch (Exception ex) { consoleOutPut.AppendFormat(" Error getting caller info: {0}\r\n", ex.Message); } consoleOutPut.AppendFormat("{0}\r\n", new string('*', 79)); Console.WriteLine(consoleOutPut.ToString()); consoleOutPut = new StringBuilder(); /*** For Console debugging **/ try { string collectorHostConfig = entry.ToCollectorHostXml(); string tempMonitorPack = "<monitorPack version=\"5.0.0.0\" name=\"\" typeName=\"\" enabled=\"True\" defaultNotifier=\"\" runCorrectiveScripts=\"False\" collectorStateHistorySize=\"1\" pollingFreqSecOverride=\"0\">" + "<collectorHosts>" + collectorHostConfig + "</collectorHosts>\r\n" + "<notifierHosts>\r\n" + "</notifierHosts>\r\n" + "</monitorPack>"; MonitorPack m = new MonitorPack(); m.LoadXml(tempMonitorPack); m.ApplicationUserNameCacheMasterKey = ApplicationUserNameCacheMasterKey; m.ApplicationUserNameCacheFilePath = ApplicationUserNameCacheFilePath; m.BlockedCollectorAgentTypes.AddRange(BlockedCollectorAgentTypes.ToArray()); m.ScriptsRepositoryDirectory = ScriptsRepositoryDirectory; if (m.CollectorHosts.Count == 1) { m.RefreshStates(); //Since there is only one CollectorHost CollectorHost ch = m.CollectorHosts[0]; monitorState = ch.CurrentState; monitorState.RanAs = ch.CurrentState.RanAs; } else { monitorState.CurrentValue = "There was a problem loading the Collector Host config on the remote host!"; monitorState.RawDetails = collectorHostConfig; monitorState.HtmlDetails = collectorHostConfig.EscapeXml(); monitorState.State = CollectorState.Error; } //If hosted in console test app consoleOutPut.AppendFormat("{0}: Results for collector host: {1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), entry.Name); consoleOutPut.AppendFormat(" State : {0}\r\n", monitorState.State); consoleOutPut.AppendFormat(" Ran as : {0}\r\n", monitorState.RanAs); consoleOutPut.AppendFormat(" Details : {0}\r\n", monitorState.ReadAllRawDetails()); } catch (Exception ex) { consoleOutPut.AppendFormat(" Error: {0}\r\n", ex); monitorState.CurrentValue = ex.Message; monitorState.State = CollectorState.Error; monitorState.RawDetails = ex.ToString(); monitorState.HtmlDetails = ex.ToString().EscapeXml(); } consoleOutPut.AppendLine(new string('*', 79)); Console.WriteLine(consoleOutPut.ToString()); monitorState.ExecutedOnHostComputer = System.Net.Dns.GetHostName(); return(monitorState); }