/// <summary> /// Parses & loads a set of MBean counters for the given host using the XML tree. /// </summary> /// <param name="root">The root MBean counters config node.</param> /// <param name="host">The host to load counters for.</param> /// <param name="lifeCycleTypeToLoad">A filter that indicates whether ephemeral or persistent counters should be loaded.</param> /// <returns>Collection of MBean counters specified in the root node's config.</returns> public ICollection <ICounter> LoadCounters(XmlNode root, Host host, CounterLifecycleType lifeCycleTypeToLoad) { var counters = new List <ICounter>(); // We currently don't support ephemeral counters for MBeans, so just short circuit out in this case. if (lifeCycleTypeToLoad == CounterLifecycleType.Ephemeral) { return(counters); } var sourceNodes = root.SelectNodes("./Source"); if (sourceNodes != null) { foreach (XmlNode sourceNode in sourceNodes) { var startPort = Convert.ToInt32(sourceNode.Attributes["startport"].Value); var endPort = Convert.ToInt32(sourceNode.Attributes["endport"].Value); // Retrieve a collection of all available clients within the specified port range, then new up counters using those. // This way, multiple counters can share a single client & connection. var mbeanClientPool = MBeanClientFactory.CreateClients(host.Address, startPort, endPort); foreach (var mbeanClient in mbeanClientPool) { var countersForSource = BuildCountersForSourceNode(sourceNode, host, mbeanClient, startPort); counters.AddRange(countersForSource); } } } return(counters); }
/// <summary> /// Parses & loads a set of MBean counters for the given host using the XML tree. /// </summary> /// <param name="root">The root MBean counters config node.</param> /// <param name="host">The host to load counters for.</param> /// <param name="lifeCycleTypeToLoad">A filter that indicates whether ephemeral or persistent counters should be loaded.</param> /// <returns>Collection of MBean counters specified in the root node's config.</returns> public ICollection<ICounter> LoadCounters(XmlNode root, Host host, CounterLifecycleType lifeCycleTypeToLoad) { var counters = new List<ICounter>(); // We currently don't support ephemeral counters for MBeans, so just short circuit out in this case. if (lifeCycleTypeToLoad == CounterLifecycleType.Ephemeral) { return counters; } var sourceNodes = root.SelectNodes("./Source"); if (sourceNodes != null) { foreach (XmlNode sourceNode in sourceNodes) { var startPort = Convert.ToInt32(sourceNode.Attributes["startport"].Value); var endPort = Convert.ToInt32(sourceNode.Attributes["endport"].Value); // Retrieve a collection of all available clients within the specified port range, then new up counters using those. // This way, multiple counters can share a single client & connection. var mbeanClientPool = MBeanClientFactory.CreateClients(host.Name, startPort, endPort); foreach (var mbeanClient in mbeanClientPool) { var countersForSource = BuildCountersForSourceNode(sourceNode, host, mbeanClient, startPort); counters.AddRange(countersForSource); } } } return counters; }
/// <summary> /// Loads the Counters.config file, validates it against the XSD schema, and news up the appropriate CounterConfigReader object for each root counter type node. /// </summary> /// <param name="hosts">The target hosts to load counters for.</param> /// <param name="counterLifecycleType">The type of counter to load; e.g. ephemeral or persistent.</param> /// <returns>Collection of all valid counters in Counters.config across the set of given hosts.</returns> public static ICollection<ICounter> Load(IEnumerable<Host> hosts, CounterLifecycleType counterLifecycleType) { Log.DebugFormat(@"Loading {0} performance counters from {1}..", counterLifecycleType.ToString().ToLowerInvariant(), Path.Combine(Directory.GetCurrentDirectory(), PathToCountersConfig)); var counters = new Collection<ICounter>(); if (loadedConfigDocument == null) { loadedConfigDocument = LoadConfig(); } // Set the root element & begin loading counters. var documentRoot = loadedConfigDocument.DocumentElement.SelectSingleNode("/Counters"); var counterRootNodes = documentRoot.SelectNodes("child::*"); foreach (XmlNode counterRootNode in counterRootNodes) { var counterType = counterRootNode.Name; Log.DebugFormat("Loading {0} counters..", counterType); var configReader = CounterConfigReaderFactory.CreateConfigReader(counterType); foreach (var host in hosts) { var countersInNode = configReader.LoadCounters(counterRootNode, host, counterLifecycleType); if (countersInNode.Count > 0) { Log.DebugFormat("Loaded {0} {1} {2} {3} on {4}.", countersInNode.Count, counterLifecycleType.ToString().ToLowerInvariant(), counterType, "counter".Pluralize(countersInNode.Count), host.Name); counters.AddRange(countersInNode); } } } Log.DebugFormat("Successfully loaded {0} {1} from configuration file.", counters.Count, "counter".Pluralize(counters.Count)); return counters; }
/// <summary> /// Parses & loads a set of MBean counters for the given host using the XML tree. /// </summary> /// <param name="root">The root MBean counters config node.</param> /// <param name="host">The host to load counters for.</param> /// <param name="lifeCycleTypeToLoad">A filter that indicates whether ephemeral or persistent counters should be loaded.</param> /// <returns>Collection of MBean counters specified in the root node's config.</returns> public ICollection <ICounter> LoadCounters(XmlNode root, Host host, CounterLifecycleType lifeCycleTypeToLoad) { var counters = new List <ICounter>(); // We currently don't support ephemeral counters for MBeans, so just short circuit out in this case. if (lifeCycleTypeToLoad == CounterLifecycleType.Ephemeral) { return(counters); } var sourceNodes = root.SelectNodes("./Source"); if (sourceNodes != null) { foreach (XmlNode sourceNode in sourceNodes) { ICollection <IMBeanClient> mbeanClientPool = null; if (!host.SpecifyPorts) { var startPort = Convert.ToInt32(sourceNode.Attributes["startport"].Value); var endPort = Convert.ToInt32(sourceNode.Attributes["endport"].Value); // Retrieve a collection of all available clients within the specified port range, then new up counters using those. // This way, multiple counters can share a single client & connection. mbeanClientPool = MBeanClientFactory.CreateClients(host.Address, startPort, endPort); } else { var processName = sourceNode.Attributes["name"].Value; if (host.Processes.ContainsKey(processName)) { var ports = host.Processes[processName]; // Retrieve a collection of all available clients within the specified port range, then new up counters using those. // This way, multiple counters can share a single client & connection. mbeanClientPool = MBeanClientFactory.CreateClients(host.Address, ports); } else { Log.DebugFormat("Process \"{0}\" is not contained in host section. Continuing..", processName); } } if (mbeanClientPool != null) { foreach (var mbeanClient in mbeanClientPool) { var countersForSource = BuildCountersForSourceNode(sourceNode, host, mbeanClient); counters.AddRange(countersForSource); } } } } return(counters); }
public PerfmonCounter(Host host, CounterLifecycleType lifecycleType, string counterCategory, string counterName, string instance, string unit) { Host = host; LifecycleType = lifecycleType; CounterType = PerfmonCounterType; Source = PerfmonSource; Category = counterCategory; Counter = counterName; Instance = instance; Unit = unit; perfmonCounter = new PerformanceCounter(Category, Counter, Instance, Host.Name); }
/// <summary> /// Parses & loads all Perfmon counters for the given host using the XML tree. /// </summary> /// <param name="root">The root Perfmon counters config node.</param> /// <param name="host">The host to load counters for.</param> /// <param name="lifeCycleTypeToLoad">A filter that indicates whether ephemeral or persistent counters should be loaded.</param> /// <returns>Collection of Perfmon counters specified in the root node's config.</returns> public ICollection<ICounter> LoadCounters(XmlNode root, Host host, CounterLifecycleType lifeCycleTypeToLoad) { var counters = new Collection<ICounter>(); var perfmonCounterNodes = root.SelectNodes("./*/Counter"); foreach (XmlNode counterNode in perfmonCounterNodes) { // Set what we know. var counterName = counterNode.Attributes["name"].Value; var categoryName = counterNode.ParentNode.Attributes["name"].Value; string unitOfMeasurement = null; if (counterNode.Attributes.GetNamedItem("unit") != null) { unitOfMeasurement = counterNode.Attributes["unit"].Value; } // If any instance names are called out, shove them into a list of filters. var instanceFilters = new HashSet<string>(); if (counterNode.HasChildNodes) { var instanceNodes = counterNode.SelectNodes("./Instance"); if (instanceNodes == null) { continue; } foreach (var instanceNode in instanceNodes.Cast<XmlNode>().Where(instanceNode => instanceNode.Attributes.GetNamedItem("name") != null)) { string instanceName = instanceNode.Attributes["name"].Value; CounterLifecycleType configuredCounterLifecycleType = GetConfiguredInstanceLifecycleType(instanceNode); if (configuredCounterLifecycleType == lifeCycleTypeToLoad) { instanceFilters.Add(instanceName); } } } // Load an instance of this perfmon counter for each matching instance name that exists. // If no instance name is specified, just load them all. if (lifeCycleTypeToLoad == CounterLifecycleType.Persistent || instanceFilters.Count > 0) { var counterInstances = PerfmonCounterLoader.LoadInstancesForCounter(host, lifeCycleTypeToLoad, categoryName, counterName, unitOfMeasurement, instanceFilters); foreach (var counterInstance in counterInstances) { counters.Add(counterInstance); } } } return counters; }
/// <summary> /// Parses & loads all Perfmon counters for the given host using the XML tree. /// </summary> /// <param name="root">The root Perfmon counters config node.</param> /// <param name="host">The host to load counters for.</param> /// <param name="lifeCycleTypeToLoad">A filter that indicates whether ephemeral or persistent counters should be loaded.</param> /// <returns>Collection of Perfmon counters specified in the root node's config.</returns> public ICollection <ICounter> LoadCounters(XmlNode root, Host host, CounterLifecycleType lifeCycleTypeToLoad) { var counters = new Collection <ICounter>(); var perfmonCounterNodes = root.SelectNodes("./*/Counter"); foreach (XmlNode counterNode in perfmonCounterNodes) { // Set what we know. var counterName = counterNode.Attributes["name"].Value; var categoryName = counterNode.ParentNode.Attributes["name"].Value; string unitOfMeasurement = null; if (counterNode.Attributes.GetNamedItem("unit") != null) { unitOfMeasurement = counterNode.Attributes["unit"].Value; } // If any instance names are called out, shove them into a list of filters. var instanceFilters = new HashSet <string>(); if (counterNode.HasChildNodes) { var instanceNodes = counterNode.SelectNodes("./Instance"); if (instanceNodes == null) { continue; } foreach (var instanceNode in instanceNodes.Cast <XmlNode>().Where(instanceNode => instanceNode.Attributes.GetNamedItem("name") != null)) { string instanceName = instanceNode.Attributes["name"].Value; CounterLifecycleType configuredCounterLifecycleType = GetConfiguredInstanceLifecycleType(instanceNode); if (configuredCounterLifecycleType == lifeCycleTypeToLoad) { instanceFilters.Add(instanceName); } } } // Load an instance of this perfmon counter for each matching instance name that exists. // If no instance name is specified, just load them all. if (lifeCycleTypeToLoad == CounterLifecycleType.Persistent || instanceFilters.Count > 0) { var counterInstances = PerfmonCounterLoader.LoadInstancesForCounter(host, lifeCycleTypeToLoad, categoryName, counterName, unitOfMeasurement, instanceFilters); foreach (var counterInstance in counterInstances) { counters.Add(counterInstance); } } } return(counters); }
protected AbstractMBeanCounter(IMBeanClient mbeanClient, CounterLifecycleType lifecycleType, string counterType, string jmxDomain, Host host, string source, string filter, string category, string counter, string instance, string unit) { Host = host; LifecycleType = lifecycleType; CounterType = counterType; Source = source; Category = category; Counter = counter; Instance = instance; Unit = unit; MBeanClient = mbeanClient; JmxDomain = jmxDomain; Path = filter; }
/// <summary> /// Loads all Perfmon counters on the target host matching the given parameters. /// </summary> /// <param name="host">The host to check counters on.</param> /// <param name="lifecycleType">Indicates whether the counters should be loaded as persistent or ephemeral counters.</param> /// <param name="categoryName">The counter category.</param> /// <param name="counterName">The name of the counter.</param> /// <param name="unitOfMeasurement">The unit of measurement that this counter reports in. This is a piece of metadata we add in.</param> /// <param name="instanceFilters">A collection of search terms to use to filter out instances that do not match.</param> /// <returns>List of all matching Perfmon counters.</returns> public static IList <PerfmonCounter> LoadInstancesForCounter(Host host, CounterLifecycleType lifecycleType, string categoryName, string counterName, string unitOfMeasurement, ISet <string> instanceFilters) { IList <PerfmonCounter> counters = new List <PerfmonCounter>(); // If the requested category does not exist, log it and bail out. if (!ExistsCategory(categoryName, host.Name)) { Log.WarnFormat("PerfMon counter category '{0}' on host '{1}' does not exist.", categoryName, host.Name); return(counters); } // If the requested counter does not exist, log it and bail out. if (!ExistsCounter(counterName, categoryName, host.Name)) { Log.DebugFormat("PerfMon counter '{0}' in category '{1}' on host '{2}' does not exist.", counterName, categoryName, host.Name); return(counters); } var category = new PerformanceCounterCategory(categoryName, host.Name); // Perfmon has both "single-instance" and "multi-instance" counter types -- we need to handle both appropriately. switch (category.CategoryType) { case PerformanceCounterCategoryType.SingleInstance: counters.Add(new PerfmonCounter(host, lifecycleType, categoryName, counterName, instance: null, unit: unitOfMeasurement)); break; case PerformanceCounterCategoryType.MultiInstance: foreach (var instanceName in category.GetInstanceNames()) { if (IsInstanceRequested(instanceName, instanceFilters)) { counters.Add(new PerfmonCounter(host, lifecycleType, categoryName, counterName, instanceName, unitOfMeasurement)); } } break; default: Log.ErrorFormat("Unable to determine category type of PerfMon counter '{0}' in category '{1}' on host '{2}' is unknown; skipping loading it.", counterName, categoryName, host.Name); break; } return(counters); }
/// <summary> /// Loads all Perfmon counters on the target host matching the given parameters. /// </summary> /// <param name="host">The host to check counters on.</param> /// <param name="lifecycleType">Indicates whether the counters should be loaded as persistent or ephemeral counters.</param> /// <param name="categoryName">The counter category.</param> /// <param name="counterName">The name of the counter.</param> /// <param name="unitOfMeasurement">The unit of measurement that this counter reports in. This is a piece of metadata we add in.</param> /// <param name="instanceFilters">A collection of search terms to use to filter out instances that do not match.</param> /// <returns>List of all matching Perfmon counters.</returns> public static IList<PerfmonCounter> LoadInstancesForCounter(Host host, CounterLifecycleType lifecycleType, string categoryName, string counterName, string unitOfMeasurement, ISet<string> instanceFilters) { IList<PerfmonCounter> counters = new List<PerfmonCounter>(); // If the requested category does not exist, log it and bail out. if (!ExistsCategory(categoryName, host.Name)) { Log.WarnFormat("PerfMon counter category '{0}' on host '{1}' does not exist.", categoryName, host.Name); return counters; } // If the requested counter does not exist, log it and bail out. if (!ExistsCounter(counterName, categoryName, host.Name)) { Log.DebugFormat("PerfMon counter '{0}' in category '{1}' on host '{2}' does not exist.", counterName, categoryName, host.Name); return counters; } var category = new PerformanceCounterCategory(categoryName, host.Name); // Perfmon has both "single-instance" and "multi-instance" counter types -- we need to handle both appropriately. switch (category.CategoryType) { case PerformanceCounterCategoryType.SingleInstance: counters.Add(new PerfmonCounter(host, lifecycleType, categoryName, counterName, instance: null, unit: unitOfMeasurement)); break; case PerformanceCounterCategoryType.MultiInstance: foreach (var instanceName in category.GetInstanceNames()) { if (IsInstanceRequested(instanceName, instanceFilters)) { counters.Add(new PerfmonCounter(host, lifecycleType, categoryName, counterName, instanceName, unitOfMeasurement)); } } break; default: Log.ErrorFormat("Unable to determine category type of PerfMon counter '{0}' in category '{1}' on host '{2}' is unknown; skipping loading it.", counterName, categoryName, host.Name); break; } return counters; }
/// <summary> /// Parses & loads all Perfmon counters for the given host using the XML tree. /// </summary> /// <param name="root">The root Perfmon counters config node.</param> /// <param name="host">The host to load counters for.</param> /// <param name="lifeCycleTypeToLoad">A filter that indicates whether ephemeral or persistent counters should be loaded.</param> /// <returns>Collection of Perfmon counters specified in the root node's config.</returns> public ICollection <ICounter> LoadCounters(XmlNode root, Host host, CounterLifecycleType lifeCycleTypeToLoad) { var counters = new Collection <ICounter>(); var perfmonCounterNodes = root.SelectNodes("./*/Counter"); try { foreach (XmlNode counterNode in perfmonCounterNodes) { counters.AddRange(LoadCountersForNode(counterNode, host, lifeCycleTypeToLoad)); } } catch (System.IO.IOException ex) { Log.ErrorFormat("Unable to load PerfMon counters for host {0}: {1}Please verify that the computer name is configured correctly.", host, ex.Message); } return(counters); }
/// <summary> /// Loads the Counters.config file, validates it against the XSD schema, and news up the appropriate CounterConfigReader object for each root counter type node. /// </summary> /// <param name="hosts">The target hosts to load counters for.</param> /// <param name="counterLifecycleType">The type of counter to load; e.g. ephemeral or persistent.</param> /// <returns>Collection of all valid counters in Counters.config across the set of given hosts.</returns> public static ICollection <ICounter> Load(IEnumerable <Host> hosts, CounterLifecycleType counterLifecycleType) { Log.DebugFormat(@"Loading {0} performance counters from {1}..", counterLifecycleType.ToString().ToLowerInvariant(), Path.Combine(Directory.GetCurrentDirectory(), PathToCountersConfig)); var counters = new Collection <ICounter>(); if (loadedConfigDocument == null) { loadedConfigDocument = LoadConfig(); } // Set the root element & begin loading counters. var documentRoot = loadedConfigDocument.DocumentElement.SelectSingleNode("/Counters"); var counterRootNodes = documentRoot.SelectNodes("child::*"); foreach (XmlNode counterRootNode in counterRootNodes) { var counterType = counterRootNode.Name; Log.DebugFormat("Loading {0} counters..", counterType); var configReader = CounterConfigReaderFactory.CreateConfigReader(counterType); foreach (var host in hosts) { var countersInNode = configReader.LoadCounters(counterRootNode, host, counterLifecycleType); if (countersInNode.Count > 0) { Log.DebugFormat("Loaded {0} {1} {2} {3} on {4}.", countersInNode.Count, counterLifecycleType.ToString().ToLowerInvariant(), counterType, "counter".Pluralize(countersInNode.Count), host.Address); counters.AddRange(countersInNode); } } } Log.DebugFormat("Successfully loaded {0} {1} from configuration file.", counters.Count, "counter".Pluralize(counters.Count)); return(counters); }