Beispiel #1
0
        /// <summary>
        /// Loops until this instance is stopped, gathering performance data at the specified interval.
        /// </summary>
        private void monitorRunner()
        {
            try
            {
                int checkInstanceNameCounter = 0;
                int intervalMs = spec.GetIntervalMs();
                while (true)
                {
                    try
                    {
                        string instanceName = spec.GetInstanceName();
                        if (instanceName == null && spec.processFinder != null)
                        {
                            Logger.Info("Failed to find instance name for monitor " + spec.GetLabel());
                            Thread.Sleep(Math.Max(intervalMs, 10000));
                            continue;
                        }
                        using (PerformanceCounter counter = new PerformanceCounter(spec.categoryName, spec.counterName, instanceName, true))
                        {
                            counter.NextValue();
                            Thread.Sleep(intervalMs);
                            while (true)
                            {
                                if (spec.processFinder != null && ++checkInstanceNameCounter % 15 == 0 && spec.GetInstanceName() != counter.InstanceName)
                                {
                                    break;                                     // Breaking here will cause the PerformanceCounter to be reinitialized.  This will catch process restarts.
                                }
                                PerfMonValue pmv = new PerfMonValue();
                                pmv.Time  = (TimeUtil.GetTimeInMsSinceEpoch() / 100) * 100;                                // Round off to in 100ms interals so the graph tooltip works a little better.
                                pmv.Value = counter.NextValue();

                                lock (this)
                                {
                                    values.AddFirst(pmv);
                                    while (values.Last.Value.Time < pmv.Time - maxAge)
                                    {
                                        values.RemoveLast();
                                    }
                                }

                                Thread.Sleep(intervalMs);
                            }
                        }
                    }
                    catch (ThreadAbortException) { }
                    catch (Exception ex)
                    {
                        Logger.Debug(ex);
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Logger.Debug(ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Loops until this instance is stopped, gathering performance data at the specified interval.
        /// </summary>
        private void monitorRunner()
        {
            try
            {
                int intervalMs = spec.GetIntervalMs();
                //lock (this)
                //{
                //	DateTime now = DateTime.Now;
                //	DateTime date = now.AddMilliseconds(-1 * maxAge);
                //	while (date < now)
                //	{
                //		PerfMonValue pmv = MakeFakeValue(date);
                //		values.AddFirst(pmv);
                //		while (values.Last.Value.Time < pmv.Time - maxAge)
                //			values.RemoveLast();
                //		date = date.AddMilliseconds(intervalMs);
                //	}
                //}
                while (true)
                {
                    try
                    {
                        string instanceName = spec.GetInstanceName();
                        if (instanceName == null && spec.processFinder != null)
                        {
                            Error = "Failed to find Process instance name for monitor \"" + spec.GetLabel() + "\"";
                            Thread.Sleep(Math.Max(intervalMs, 10000));
                            continue;
                        }
                        using (PerformanceCounter counter = new PerformanceCounter(spec.categoryName, spec.counterName, instanceName, true))
                        {
                            counter.NextValue();
                            Thread.Sleep(intervalMs);
                            while (true)
                            {
                                PerfMonValue pmv = new PerfMonValue();
                                pmv.Value = counter.NextValue();
                                pmv.Time  = TimeUtil.GetTimeInMsSinceEpoch();

                                lock (this)
                                {
                                    values.AddFirst(pmv);
                                    while (values.Last.Value.Time < pmv.Time - maxAge)
                                    {
                                        values.RemoveLast();
                                    }
                                }

                                Error = "";

                                Thread.Sleep(intervalMs);
                            }
                        }
                    }
                    catch (ThreadAbortException) { }
                    catch (Exception ex)
                    {
                        Error = "Data collection error for monitor \"" + spec.GetLabel() + "\": " + ex.ToString();
                        Thread.Sleep(Math.Max(intervalMs, 10000));
                    }
                }
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                Error = "Data collection for monitor \"" + spec.GetLabel() + "\" has ended: " + ex.ToString();
                Logger.Debug(ex);
            }
            Error = "Data collection has ended.";
        }