Beispiel #1
0
        public MonitorStates RefreshStates()
        {
            AbortPolling = false;
            BusyPolling  = true;
            MonitorStates globalState = MonitorStates.Good;
            Stopwatch     sw          = new Stopwatch();

            sw.Start();
            //First get collectors with no dependancies
            List <CollectorEntry> noDependantCollectors = (from c in Collectors
                                                           where c.ParentCollectorId.Length == 0
                                                           select c).ToList();

            //Using .Net 4 Parralel processing extensions
            int threads = concurrencyLevel;

#if DEBUG
            //threads = 5;
#endif
            if (concurrencyLevel > 1)
            {
                ParallelOptions po = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = threads
                };
                ParallelLoopResult parResult = Parallel.ForEach(noDependantCollectors, po, collector => RefreshCollectorState(collector));
                if (!parResult.IsCompleted)
                {
                    SendNotifierAlert(AlertLevel.Error, DetailLevel.Summary, "N/A", "GlobalState", MonitorStates.NotAvailable, MonitorStates.Error, new CollectorMessage("Error querying collectors in parralel"));
                }
            }
            else             //use old single threaded way
            {
                //Refresh states
                foreach (CollectorEntry collector in noDependantCollectors)
                {
                    RefreshCollectorState(collector);
                }
            }
            sw.Stop();
            PCSetCollectorsQueryTime(sw.ElapsedMilliseconds);
#if DEBUG
            Trace.WriteLine(string.Format("RefreshStates - Global time: {0}ms", sw.ElapsedMilliseconds));
#endif

            //set global state
            //All disabled
            if (Collectors.Count == Collectors.Count(c => c.LastMonitorState == MonitorStates.Disabled || c.LastMonitorState == MonitorStates.Folder))
            {
                globalState = MonitorStates.Disabled;
            }
            //All NotAvailable
            else if (Collectors.Count == Collectors.Count(c => c.LastMonitorState == MonitorStates.NotAvailable || c.LastMonitorState == MonitorStates.Folder))
            {
                globalState = MonitorStates.NotAvailable;
            }
            //All good
            else if (Collectors.Count == Collectors.Count(c => c.LastMonitorState == MonitorStates.Good || c.LastMonitorState == MonitorStates.Disabled || c.LastMonitorState == MonitorStates.NotAvailable || c.LastMonitorState == MonitorStates.Folder))
            {
                globalState = MonitorStates.Good;
            }
            //Error state
            else if (Collectors.Count == Collectors.Count(c => c.LastMonitorState == MonitorStates.Error || c.LastMonitorState == MonitorStates.ConfigurationError || c.LastMonitorState == MonitorStates.Disabled || c.LastMonitorState == MonitorStates.Folder))
            {
                globalState = MonitorStates.Error;
            }
            else
            {
                globalState = MonitorStates.Warning;
            }

            AlertLevel globalAlertLevel = AlertLevel.Info;
            if (globalState == MonitorStates.Error || globalState == MonitorStates.ConfigurationError)
            {
                globalAlertLevel = AlertLevel.Error;
            }
            else if (globalState == MonitorStates.Warning)
            {
                globalAlertLevel = AlertLevel.Warning;
            }

            sw.Restart();
            SendNotifierAlert(globalAlertLevel, DetailLevel.Summary, "N/A", "GlobalState", MonitorStates.NotAvailable, MonitorStates.NotAvailable, new CollectorMessage());
            sw.Stop();
            PCSetNotifiersSendTime(sw.ElapsedMilliseconds);
#if DEBUG
            Trace.WriteLine(string.Format("RefreshStates - Global notification time: {0}ms", sw.ElapsedMilliseconds));
#endif

            BusyPolling = false;
            return(globalState);
        }