Exemple #1
0
        public void PollWMIQueries()
        {
            // Working backwards so we can safely delete queries that fail because of invalid classes.
            for (int i = WMIQueries.Count - 1; i >= 0; i--)
            {
                var thisQuery = WMIQueries[i];
                if (String.IsNullOrEmpty(thisQuery.queryString))
                {
                    Log.WriteLog(String.Format("Null query removed"), Log.LogLevel.WARN);
                    WMIQueries.RemoveAt(i);
                    continue;
                }
                try
                {
                    string scopeString = "\\\\" + MachineName + "\\" + thisQuery.queryNamespace;
                    if (Scope == null || !Scope.Path.ToString().Equals(scopeString))
                    {
                        Log.WriteLog("Setting up scope: " + scopeString, Log.LogLevel.VERBOSE);
                        Scope = new ManagementScope(scopeString, RUser.GetConnectionOptions());
                    }

                    if (!Scope.IsConnected)
                    {
                        Log.WriteLog("Connecting to scope: " + scopeString, Log.LogLevel.VERBOSE);
                        Scope.Connect();
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLog(String.Format("Unable to connect to \"{0}\". {1}", MachineName, e.Message), Log.LogLevel.ERROR);
                    continue;
                }
                try
                {
                    if (thisQuery.queryType.Equals(WMIQuery))
                    {
                        Log.WriteLog("Running Query: " + (string)thisQuery.queryString, Log.LogLevel.INFO);
                        var queryResults = (new ManagementObjectSearcher(Scope, new ObjectQuery((string)thisQuery.queryString))).Get();
                        if (queryResults.Count == 0)
                        {
                            Log.WriteLog(String.Format("Query \"{0}\" returned no results.", thisQuery.queryString), Log.LogLevel.VERBOSE);
                        }
                        foreach (ManagementObject result in queryResults)
                        {
                            {
                                RecordMetricMap(thisQuery, result);
                                continue;
                            }
                        }
                    }
                    else if (thisQuery.queryType.Equals(WMIEvent))
                    {
                        Log.WriteLog("Running Event Listener: " + thisQuery.queryString, Log.LogLevel.VERBOSE);
                        var watcher = new ManagementEventWatcher(Scope,
                                                                 new EventQuery((string)thisQuery.queryString)).WaitForNextEvent();
                        RecordMetricMap(thisQuery, watcher);
                    }
                }
                catch (ManagementException e)
                {
                    Log.WriteLog(String.Format("Exception occurred in polling. {0}: {1}", e.Message, (string)thisQuery.queryString), Log.LogLevel.ERROR);
                    if (e.Message.ToLower().Contains("invalid class") || e.Message.ToLower().Contains("not supported"))
                    {
                        Log.WriteLog(String.Format("Query Removed: {0}", thisQuery.queryString), Log.LogLevel.WARN);
                        WMIQueries.RemoveAt(i);
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLog(String.Format("Exception occurred in processing results. {0}\r\n{1}", e.Message, e.StackTrace), Log.LogLevel.ERROR);
                }
            }
        }
Exemple #2
0
        public void AddCounter(Counterlist aCounter, int whichCounter)
        {
            if (!String.IsNullOrEmpty(aCounter.query))
            {
                if (aCounter.counters != null)
                {
                    int whichOfTheseCounters = -1;
                    foreach (var testCounter in aCounter.counters)
                    {
                        whichOfTheseCounters++;
                        if (String.IsNullOrEmpty(testCounter.counter))
                        {
                            Log.WriteLog(String.Format("{0} contains malformed counter: 'counters' in counterlist[{1}] missing 'counter' in element {2}. Please review and compare to template.",
                                                       fileName, whichCounter, whichOfTheseCounters),
                                         Log.LogLevel.ERROR);
                            continue;
                        }
                    }
                }
                WMIQueries.Add(new WMIQuery(aCounter.query, aCounter.eventname, aCounter.querytype, aCounter.querynamespace, aCounter.counters));
            }
            else
            {
                if (String.IsNullOrEmpty(aCounter.provider) || String.IsNullOrEmpty(aCounter.category))
                {
                    Log.WriteLog(String.Format("{0} contains malformed counter: counterlist[{1}] missing 'provider' or 'category'. Please review and compare to template.",
                                               fileName, whichCounter), Log.LogLevel.ERROR);
                }
                if (aCounter.counters == null)
                {
                    Log.WriteLog(String.Format("{0} contains malformed counter: counterlist[{1}] missing 'counters'. Please review and compare to template.",
                                               fileName, whichCounter), Log.LogLevel.ERROR);
                }

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

                int whichOfTheseCounters = -1;
                foreach (var testCounter in aCounter.counters)
                {
                    whichOfTheseCounters++;
                    if (String.IsNullOrEmpty(testCounter.counter))
                    {
                        Log.WriteLog(String.Format("{0} contains malformed counter: 'counters' in counterlist[{1}] missing 'counter' in element {2}. Please review and compare to template.",
                                                   fileName, whichCounter, whichOfTheseCounters), Log.LogLevel.ERROR);
                        continue;
                    }
                }

                if (String.Equals(aCounter.provider, PerfCounterType))
                {
                    List <string> pcounters = new List <string>();
                    foreach (var pCounter in aCounter.counters)
                    {
                        pcounters.Add(pCounter.counter);
                    }
                    PerfCounter AddPC = RUser.RunAsRemoteUser <PerfCounter>(() => new PerfCounter(aCounter.category, pcounters, instanceName, MachineName));
                    PerfCounters.Add(AddPC);
                }
                else
                {
                    string countersStr = "";
                    foreach (var wCounter in aCounter.counters)
                    {
                        if (String.IsNullOrEmpty(countersStr))
                        {
                            countersStr = wCounter.counter;
                        }
                        else
                        {
                            countersStr += (", " + wCounter.counter);
                        }
                    }
                    if (!String.IsNullOrEmpty(countersStr))
                    {
                        WMIQueries.Add(new WMIQuery(aCounter.provider, aCounter.category, countersStr, instanceName));
                    }
                }
            }
        }