public void Configure()
{
var config = ConfigurationManager.GetSection("ReadWindowsPerfCounters") as ReadWindowsPerfCountersPluginConfig;
if (config == null)
{
throw new Exception("Cannot get configuration section : ReadWindowsPerfCounters");
}
_hostName = Util.GetHostName();
// Set reload time
_reloadInterval = config.ReloadInterval;
Logger.Info("Loading metric configuration. Reload interval: {0} sec", _reloadInterval);
_lastUpdated = Util.GetNow();
// Load the metrics - this checks for existence
_metrics.Clear();
int metricCounter = 0;
foreach (CounterConfig counter in config.Counters)
{
if (counter.Instance == "")
{
// Instance not specified
if (AddPerformanceCounter(counter.Category, counter.Name,
counter.Instance, counter.Multiplier,
counter.DecimalPlaces, counter.CollectdPlugin,
counter.CollectdPluginInstance, counter.CollectdType,
counter.CollectdTypeInstance))
{
metricCounter++;
}
}
else
{
// Match instance with regex
string[] instances = new string[0];
try
{
Regex regex = new Regex(counter.Instance, RegexOptions.None);
var cat = new PerformanceCounterCategory(counter.Category);
instances = cat.GetInstanceNames();
List<string> instanceList = new List<string>();
foreach (string instance in instances)
{
if (regex.IsMatch(instance))
{
instanceList.Add(instance);
}
}
instances = instanceList.ToArray();
}
catch (ArgumentException ex)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Failed to initialise performance counter");
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
Logger.Warn(string.Format("Failed to parse instance regular expression: category={0}, instance={1}, counter={2}", counter.Category, counter.Instance, counter.Name), ex);
}
catch (InvalidOperationException ex)
{
if (ex.Message.ToLower().Contains("category does not exist")) {
Logger.Warn(string.Format("Performance Counter not added: Category does not exist: {0}", counter.Category));
}
else
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Failed to initialise performance counter");
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
Logger.Warn(string.Format("Could not initialise performance counter category: {0}, instance: {1}, counter: {2}", counter.Category, counter.Instance, counter.Name), ex);
}
}
catch (Exception ex)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Error, Logger.Name, "Failed to initialise performance counter");
logEvent.Properties.Add("EventID", ErrorCodes.ERROR_CONFIGURATION_EXCEPTION);
Logger.Log(logEvent);
Logger.Warn(string.Format("Could not initialise performance counter category: {0}", counter.Category), ex);
}
if (instances.Length == 0)
{
Logger.Warn("No instances matching category: {0}, instance: {1}", counter.Category, counter.Instance);
}
foreach (string instance in instances)
{
string instanceAlias = instance;
if (instances.Length == 1)
{
// There is just one instance
// If this is because the regex was hardcoded then replace the instance name with the CollectdInstanceName - i.e., alias the instance
// But if the regex contains wildcards then it is a fluke that there was a single match
if (counter.Instance.IndexOf("?") < 0 && counter.Instance.IndexOf("*") < 0)
{
// No wildcards => this was hardcoded value.
instanceAlias = counter.CollectdPluginInstance;
}
}
// Replace collectd_plugin_instance with the Instance got from counter
if (AddPerformanceCounter(counter.Category, counter.Name,
instance, counter.Multiplier,
counter.DecimalPlaces, counter.CollectdPlugin,
instanceAlias, counter.CollectdType,
counter.CollectdTypeInstance))
{
metricCounter++;
}
}
}
}
// Wait 1 second for the two-valued counters to be ready for next incremental read - see https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue(v=vs.110).aspx
Thread.Sleep(1000);
Logger.Info("ReadWindowsPerfeCounters plugin configured {0} metrics", metricCounter);
}