Esempio n. 1
0
        public void PollCycle()
        {
            Metrics.Add("event_type", PerfmonPlugin.DefaultEvent);
            var metricNames = new Dictionary <string, int>();

            // Working backwards so we can safely delete queries that fail because of invalid classes.
            for (int i = PerfmonQueries.Count - 1; i >= 0; i--)
            {
                var thisQuery = PerfmonQueries[i];
                try
                {
                    if (thisQuery.counterOrQuery is PerformanceCounter)
                    {
                        try
                        {
                            Debug("Collecting Perf Counter: " + ((PerformanceCounter)thisQuery.counterOrQuery).ToString());

                            float  value      = ((PerformanceCounter)thisQuery.counterOrQuery).NextValue();
                            string metricName = thisQuery.metricName;
                            if (!float.IsNaN(value))
                            {
                                if (metricNames.ContainsKey(metricName))
                                {
                                    metricName = metricName + "#" + metricNames[metricName]++;
                                }
                                else
                                {
                                    metricNames.Add(metricName, 1);
                                }
                                Debug(string.Format("{0}/{1}: {2}", Name, metricName, value));
                                Metrics.Add(metricName, value);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("Exception occurred in processing next value. {0}\r\n{1}", e.Message, e.StackTrace);
                        }
                    }
                    else if (thisQuery.counterOrQuery is string)
                    {
                        try
                        {
                            string scopeString = "\\\\" + Name + "\\" + thisQuery.queryNamespace;
                            if (Scope == null)
                            {
                                Debug("Setting up scope: " + scopeString);
                                Scope = new ManagementScope(scopeString);
                            }
                            else if (Scope != null && !Scope.Path.ToString().Equals(scopeString))
                            {
                                Debug("Updating Scope Path from " + Scope.Path + " to " + scopeString);
                                Scope = new ManagementScope(scopeString);
                            }

                            if (!Scope.IsConnected)
                            {
                                Debug("Connecting to scope: " + scopeString);
                                Scope.Connect();
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("Unable to connect to \"{0}\". {1}", Name, e.Message);
                            continue;
                        }
                        if (thisQuery.queryType.Equals(WMIQuery))
                        {
                            Debug("Running Query: " + (string)thisQuery.counterOrQuery);
                            var queryResults = (new ManagementObjectSearcher(Scope,
                                                                             new ObjectQuery((string)thisQuery.counterOrQuery))).Get();
                            foreach (ManagementObject result in queryResults)
                            {
                                {
                                    RecordMetricMap(thisQuery, result);
                                    continue;
                                }
                            }
                        }
                        else if (thisQuery.queryType.Equals(WMIEvent))
                        {
                            Debug("Running Event Listener: " + thisQuery.counterOrQuery);
                            var watcher = new ManagementEventWatcher(Scope,
                                                                     new EventQuery((string)thisQuery.counterOrQuery)).WaitForNextEvent();
                            RecordMetricMap(thisQuery, watcher);
                        }
                    }
                }
                catch (ManagementException e)
                {
                    Console.Error.WriteLine("Exception occurred in polling. {0}: {1}", e.Message, (string)thisQuery.counterOrQuery);
                    if (e.Message.ToLower().Contains("invalid class") || e.Message.ToLower().Contains("not supported"))
                    {
                        Console.Error.WriteLine("Query Removed: {0}", thisQuery.counterOrQuery);
                        PerfmonQueries.RemoveAt(i);
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Exception occurred in processing results. {0}\r\n{1}", e.Message, e.StackTrace);
                }
            }
            ReportAll();
        }
Esempio n. 2
0
        public void AddCounter(Counterlist aCounter, int whichCounter)
        {
            if (!String.IsNullOrEmpty(aCounter.query))
            {
                if (aCounter.counters != null)
                {
                    foreach (var testCounter in aCounter.counters)
                    {
                        if (String.IsNullOrEmpty(testCounter.counter))
                        {
                            Console.Error.WriteLine("plugin.json contains malformed counter: counterlist[{0}] missing 'counter' in 'counters'. Please review and compare to template.", whichCounter);
                            return;
                        }
                    }
                    PerfmonQueries.Add(new PerfmonQuery(aCounter.query, aCounter.eventname, aCounter.querytype, aCounter.querynamespace, aCounter.counters));
                }
                else
                {
                    PerfmonQueries.Add(new PerfmonQuery(aCounter.query, aCounter.eventname, aCounter.querytype, aCounter.querynamespace, null));
                }
                return;
            }

            if (String.IsNullOrEmpty(aCounter.provider) || String.IsNullOrEmpty(aCounter.category))
            {
                Console.Error.WriteLine("plugin.json contains malformed counter: counterlist[{0}] missing 'provider' or 'category'. Please review and compare to template.", whichCounter);
                return;
            }

            string instanceName = string.Empty;

            if (!String.IsNullOrEmpty(aCounter.instance) && !String.Equals(aCounter.instance, "*"))
            {
                instanceName = aCounter.instance.ToString();
            }

            if (aCounter.counters != null)
            {
                int    whichSubCounter = -1;
                string countersStr     = "";
                foreach (var aSubCounter in aCounter.counters)
                {
                    whichSubCounter++;
                    if (String.IsNullOrEmpty(aSubCounter.counter))
                    {
                        Console.Error.WriteLine("plugin.json contains malformed counter: 'counters' in counterlist[{0}] missing 'counter' in element {1}. Please review and compare to template.", whichCounter, whichSubCounter);
                        continue;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(countersStr))
                        {
                            countersStr = aSubCounter.counter;
                        }
                        else
                        {
                            countersStr += (", " + aSubCounter.counter);
                        }
                    }
                }
                if (!String.IsNullOrEmpty(countersStr))
                {
                    PerfmonQueries.Add(new PerfmonQuery(aCounter.provider, aCounter.category, countersStr, instanceName));
                }
            }
            else
            {
                Console.Error.WriteLine("plugin.json contains malformed counter: counterlist[{0}] missing 'counters'. Please review and compare to template.", whichCounter);
            }
        }