Skip to content

Commit

Permalink
- Added ComputerName and Address to the config. (#138)
Browse files Browse the repository at this point in the history
- Updated the TabMon.config file to reflect the changes.
- Updated CounterSampler to use the address when databasing value.
- Updated host object to use both the address and computer name. This change is reflected in ToString().
- Updated perfmon code to refer to the computer name.
- Updated JMX code to refer to the address.
- Refactored the PerfmonCounterConfigReader class to make it more readable, skip the collection of PerfMon counters on incorrect computer names, and make the errors more readable.
- Removed the HostNameHelper since the computer name and address are now manually configured.
  • Loading branch information
danjrahm authored and jmangue committed Sep 21, 2017
1 parent fe394b5 commit 04778d1
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 131 deletions.
2 changes: 1 addition & 1 deletion TabMon/Config/TabMon.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!-- Target cluster & host configuration -->
<Clusters>
<Cluster name="Primary">
<Host name="localhost"/>
<Host computerName="YOURCOMPUTERNAME" address="localhost"/>
</Cluster>
</Clusters>
<!-- Results output database Configuration-->
Expand Down
4 changes: 2 additions & 2 deletions TabMon/CounterConfig/CounterConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class CounterConfigLoader
private const string PathToCountersConfig = @"Config\Counters.config";
private const string PathToSchema = @"Resources\CountersConfig.xsd";
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static XmlDocument loadedConfigDocument;
private static XmlDocument loadedConfigDocument;

/// <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.
Expand Down Expand Up @@ -54,7 +54,7 @@ public static ICollection<ICounter> Load(IEnumerable<Host> hosts, CounterLifecyc
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);
countersInNode.Count, counterLifecycleType.ToString().ToLowerInvariant(), counterType, "counter".Pluralize(countersInNode.Count), host.Address);
counters.AddRange(countersInNode);
}
}
Expand Down
4 changes: 2 additions & 2 deletions TabMon/CounterConfig/MBeanCounterConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ICollection<ICounter> LoadCounters(XmlNode root, Host host, CounterLifecy

// 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);
var mbeanClientPool = MBeanClientFactory.CreateClients(host.Address, startPort, endPort);
foreach (var mbeanClient in mbeanClientPool)
{
var countersForSource = BuildCountersForSourceNode(sourceNode, host, mbeanClient, startPort);
Expand Down Expand Up @@ -123,7 +123,7 @@ private static ICounter BuildCounterFromCounterNode(XmlNode counterNode, IMBeanC
catch (Exception ex)
{
Log.DebugFormat(@"Failed to register MBean counter {0}\{1}\{2}\{3}\{4}: {5}",
host.Name, sourceName, categoryName, counterName, instanceName, ex.Message);
host.Address, sourceName, categoryName, counterName, instanceName, ex.Message);
return null;
}
}
Expand Down
82 changes: 51 additions & 31 deletions TabMon/CounterConfig/PerfmonCounterConfigReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using log4net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Xml;
using TabMon.Counters;
using TabMon.Counters.Perfmon;
Expand All @@ -14,6 +16,8 @@ namespace TabMon.CounterConfig
/// </summary>
internal sealed class PerfmonCounterConfigReader : ICounterConfigReader
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

/// <summary>
/// Parses & loads all Perfmon counters for the given host using the XML tree.
/// </summary>
Expand All @@ -26,47 +30,63 @@ public ICollection<ICounter> LoadCounters(XmlNode root, Host host, CounterLifecy
var counters = new Collection<ICounter>();
var perfmonCounterNodes = root.SelectNodes("./*/Counter");

foreach (XmlNode counterNode in perfmonCounterNodes)
try
{
// 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)
foreach (XmlNode counterNode in perfmonCounterNodes)
{
unitOfMeasurement = counterNode.Attributes["unit"].Value;
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;
}

private ICollection<ICounter> LoadCountersForNode(XmlNode counterNode, Host host, CounterLifecycleType lifeCycleTypeToLoad)
{
var counters = new Collection<ICounter>();

// 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)
// 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)
{
var instanceNodes = counterNode.SelectNodes("./Instance");
if (instanceNodes == null)
{
continue;
}
return counters;
}

foreach (var instanceNode in instanceNodes.Cast<XmlNode>().Where(instanceNode => instanceNode.Attributes.GetNamedItem("name") != null))
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)
{
string instanceName = instanceNode.Attributes["name"].Value;
CounterLifecycleType configuredCounterLifecycleType = GetConfiguredInstanceLifecycleType(instanceNode);
if (configuredCounterLifecycleType == lifeCycleTypeToLoad)
{
instanceFilters.Add(instanceName);
}
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)
// 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)
{
var counterInstances = PerfmonCounterLoader.LoadInstancesForCounter(host, lifeCycleTypeToLoad, categoryName, counterName, unitOfMeasurement, instanceFilters);
foreach (var counterInstance in counterInstances)
{
counters.Add(counterInstance);
}
counters.Add(counterInstance);
}
}

Expand Down
2 changes: 1 addition & 1 deletion TabMon/Counters/Perfmon/PerfmonCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public PerfmonCounter(Host host, CounterLifecycleType lifecycleType, string coun
Counter = counterName;
Instance = instance;
Unit = unit;
perfmonCounter = new PerformanceCounter(Category, Counter, Instance, Host.Name);
perfmonCounter = new PerformanceCounter(Category, Counter, Instance, Host.ComputerName);
}

~PerfmonCounter()
Expand Down
12 changes: 6 additions & 6 deletions TabMon/Counters/Perfmon/PerfmonCounterLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public static IList<PerfmonCounter> LoadInstancesForCounter(Host host, CounterLi
IList<PerfmonCounter> counters = new List<PerfmonCounter>();

// If the requested category does not exist, log it and bail out.
if (!ExistsCategory(categoryName, host.Name))
if (!ExistsCategory(categoryName, host.ComputerName))
{
Log.WarnFormat("PerfMon counter category '{0}' on host '{1}' does not exist.",
categoryName, host.Name);
categoryName, host.ComputerName);
return counters;
}

// If the requested counter does not exist, log it and bail out.
if (!ExistsCounter(counterName, categoryName, host.Name))
if (!ExistsCounter(counterName, categoryName, host.ComputerName))
{
Log.DebugFormat("PerfMon counter '{0}' in category '{1}' on host '{2}' does not exist.",
counterName, categoryName, host.Name);
counterName, categoryName, host.ComputerName);
return counters;
}

var category = new PerformanceCounterCategory(categoryName, host.Name);
var category = new PerformanceCounterCategory(categoryName, host.ComputerName);

// Perfmon has both "single-instance" and "multi-instance" counter types -- we need to handle both appropriately.
switch (category.CategoryType)
Expand All @@ -65,7 +65,7 @@ public static IList<PerfmonCounter> LoadInstancesForCounter(Host host, CounterLi
}
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);
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.ComputerName);
break;
}

Expand Down
10 changes: 6 additions & 4 deletions TabMon/Helpers/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
/// </summary>
public sealed class Host
{
public string Name { get; private set; }
public string Address { get; private set; }
public string ComputerName { get; private set; }
public string Cluster { get; private set; }

public Host(string name, string cluster)
public Host(string address, string computerName, string cluster)
{
Name = name;
Address = address;
ComputerName = computerName;
Cluster = cluster;
}

public override string ToString()
{
return Cluster + "\\" + Name;
return Cluster + "\\" + Address + "\\" + ComputerName;
}
}
}
45 changes: 0 additions & 45 deletions TabMon/Helpers/HostNameHelper.cs

This file was deleted.

7 changes: 6 additions & 1 deletion TabMon/Resources/TabMonConfig.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@
</xs:attribute>
</xs:complexType>
<xs:complexType name="TabMon.Config.HostCT">
<xs:attribute name="name" use="required" type="xs:string">
<xs:attribute name="computerName" use="required" type="xs:string">
<xs:annotation>
<xs:documentation>The computer name of the host to monitor.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="address" use="required" type="xs:string">
<xs:annotation>
<xs:documentation>The hostname or IP address of the host to monitor.</xs:documentation>
</xs:annotation>
Expand Down
2 changes: 1 addition & 1 deletion TabMon/Sampler/CounterSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static DataRow MapToSchema(ICounterSample sample, DataTable tableSchema,
var counter = sample.Counter;
row["timestamp"] = pollTimestamp;
row["cluster"] = counter.Host.Cluster;
row["machine"] = counter.Host.Name.ToLower();
row["machine"] = counter.Host.Address;
row["counter_type"] = counter.CounterType;
row["source"] = counter.Source;
row["category"] = counter.Category;
Expand Down
1 change: 0 additions & 1 deletion TabMon/TabMon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
<Compile Include="TabMonConfig\TabMonConfigReader.cs" />
<Compile Include="TabMonConfig\TabMonConfigSection.cs" />
<Compile Include="TabMonOptions.cs" />
<Compile Include="Helpers\HostnameHelper.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Config\TabMon.config">
Expand Down
3 changes: 1 addition & 2 deletions TabMon/TabMonConfig/TabMonConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public static void LoadOptions()
var clusterName = cluster.Name;
foreach (Host host in cluster)
{
var resolvedHostname = HostnameHelper.Resolve(host.Name);
options.Hosts.Add(new Helpers.Host(resolvedHostname, clusterName));
options.Hosts.Add(new Helpers.Host(host.Address, host.ComputerName, clusterName));
}
}
}
Expand Down
Loading

0 comments on commit 04778d1

Please sign in to comment.