/// <summary> /// Calculates average performance counters. /// </summary> /// <param name="metricName"> /// Metric name it will be passed to Sentinel /// </param> /// <param name="table"> /// metric table /// </param> /// <param name="moduleName"> /// The key part 1. Module name + the name of metric that is collected /// </param> /// <param name="counterName"> /// The key part 2. Module name + the name of metric that is collected /// </param> /// <param name="collectedSample"> /// performance counter sample /// </param> /// <param name="logger"> /// Logger object /// </param> private void UpdateCounterSamples(string metricName, Dictionary <string, string> table, string moduleName, string counterName, PerfCounterSampleHolder collectedSample, ILog logger) { // As a key for CounterSamples dictionary combination of module name and metric name will be used // (for cases different modules - the same metric name) if (!this.counterSamples.ContainsKey(moduleName + "_" + metricName)) { // checking whether it is first sample logger.DebugFormat("Adding first sample of {0}", moduleName + "_" + counterName); this.counterSamples.Add(moduleName + "_" + metricName, collectedSample); // holds last sample of relevant perf counter } else { logger.DebugFormat("We already have sample for {0}", moduleName + "_" + metricName); PerfCounterSampleHolder currentSample = this.counterSamples[moduleName + "_" + metricName]; // the smaple was collected and previous polling DateTime checkTime = currentSample.CollectingTime.AddSeconds(currentSample.Interval); // adding collecting period if (checkTime < collectedSample.CollectingTime) { // checking if it is time to culculate the value of average perf counter logger.Debug("It is time to calculate the new perf. counter value."); if (currentSample.Sample.TimeStamp100nSec == collectedSample.Sample.TimeStamp100nSec) { // checking if not the same sample logger.Debug("The sample is the same"); currentSample.TryToWaitDelta(); // waiting delta this.UpdateMetricTableWithExistingValueOrNA(metricName, table, logger); return; } if (!table.ContainsKey(metricName)) { if (!this.averageValues.ContainsKey(metricName)) { this.averageValues.Add(metricName, CounterSample.Calculate(currentSample.Sample, collectedSample.Sample).ToString(CultureInfo.InvariantCulture)); // add value } else { this.averageValues[metricName] = CounterSample.Calculate(currentSample.Sample, collectedSample.Sample).ToString(CultureInfo.InvariantCulture); // update value } logger.DebugFormat("Updating value of counter {0} with new one: {1}", metricName, this.averageValues[metricName]); table.Add(metricName, this.averageValues[metricName]); // add value to resulted table collectedSample.CollectingTime = checkTime; // update time logger.Debug("Updating sample"); this.counterSamples[moduleName + "_" + metricName] = collectedSample; // update sample } } } this.UpdateMetricTableWithExistingValueOrNA(metricName, table, logger); }
/// <summary> /// The get the performance counter value. /// </summary> /// <param name="moduleName"> /// Module Name /// </param> /// <param name="metricName"> /// The metric name. /// </param> /// <param name="metric"> /// The metric. /// </param> /// <param name="instanceName"> /// The instance Name. /// </param> /// <param name="category"> /// Category name /// </param> /// <param name="metricTable"> /// The metric table. /// </param> /// <param name="logger"> /// The logger. /// </param> private void GetThePerfCounterValue(string moduleName, string metricName, MetricInfo metric, string instanceName, PerformanceCounterCategory category, Dictionary <string, string> metricTable, ILog logger) { try { if (metric.CounterType.Equals("Average")) { // checking if it is perfcounter of average type logger.DebugFormat("Performace counter {0} - {1} is average", category.CategoryName, metric.CounterName); var val = this.performanceCounterWrapper.GetPerfCounterNextSample(category, metric.CounterName, instanceName, logger); if (val != null) { // creating sample holder for perf counter var holder = new PerfCounterSampleHolder((CounterSample)val, metric.CollectingInterval, metric.Delta); // calculating the value of avarage perf counter this.UpdateCounterSamples(metricName, metricTable, moduleName, metric.CounterName, holder, logger); } } else { // it is simple perfcounter logger.DebugFormat("Performace counter {0} - {1} is simple. Getting it's value", category.CategoryName, metric.CounterName); var val = this.performanceCounterWrapper.GetPerfCounterValue(category, metric.CounterName, instanceName, logger); if (val != null) { if (!metricTable.ContainsKey(metricName)) { metricTable.Add(metricName, ((float)val).ToString(CultureInfo.InvariantCulture)); // adding the value to resulted table } } } } catch (Exception exception) { logger.ErrorFormat("Error occured while trying to performance counter {0} status", metricName); logger.Error(exception); } if (!metricTable.ContainsKey(metricName)) { logger.ErrorFormat("Error occured while trying to collect metric {0} state", metricName); //metricTable.Add(metricName, SNMPAgentConstants.FAILED_TO_COLLECT_STATE); // adding the value to resulted table } }