public Dictionary <string, float> Refresh(float minValue) { InstanceDataCollectionCollection allData = TryGetPerformanceData(() => this.category.ReadCategory(), out Exception ex); Dictionary <string, float> result = new Dictionary <string, float>(StringComparer.CurrentCultureIgnoreCase); if (allData != null) { InstanceDataCollection processData = allData["% Processor Time"]; if (processData != null) { Dictionary <string, CounterSample> currentSamples = new Dictionary <string, CounterSample>(); foreach (InstanceData data in processData.Values) { string instanceName = data.InstanceName; CounterSample currentSample = data.Sample; currentSamples[instanceName] = currentSample; if (this.previousSamples != null && this.previousSamples.TryGetValue(instanceName, out CounterSample previousSample)) { // We have to average the usage across all the logical processors. // On an 8 processor machine, the counter can return up to 800! float value = CounterSampleCalculator.ComputeCounterValue(previousSample, currentSample) / ProcessorCount; result[data.InstanceName] = value >= minValue ? value : float.NaN; } } this.previousSamples = currentSamples; } } return(result); }
private static void CalculateResults(ArrayList samplesList) { for (int i = 0; i < samplesList.Count; i++) { // Output the sample. OutputSample((CounterSample)samplesList[i]); // Use .NET to calculate the counter value. Console.WriteLine(".NET computed counter value = " + CounterSampleCalculator.ComputeCounterValue((CounterSample)samplesList[i])); // Calculate the counter value manually. Console.WriteLine("My computed counter value = " + MyComputeCounterValue((CounterSample)samplesList[i])); } }
public override void Execute() { var sample = _counter.NextSample(); Thread.Sleep(1000); var sample2 = _counter.NextSample(); var value = Math.Round(CounterSampleCalculator.ComputeCounterValue(sample, sample2)); Publish(NotificationRequestBuilder.For(_config.NotificationMode, HealthCheckData.For(Identity, "Cpu utilisation is {0}%", value) .Succeeded() .ResultCountIs(value) .DisplayUnitIs("%")) .Build()); Logger.Debug("CpuCheck reports {0}%", value); }
/// <summary> /// When a breach of the threshold is detected we want to send a list /// the top 5 cpu hogging processes in the alert /// </summary> /// <param name="message"></param> protected override void AdjustMessageForBreach(INotificationEventCore message) { base.AdjustMessageForBreach(message); var counters = new List<ProcessCounterSample>(); var ps = Process.GetProcesses(); ps.ForEach(process => { try { var counter = (string.IsNullOrEmpty(_config.MachineName) ? new PerformanceCounter("Process", "% Processor Time", process.ProcessName) : new PerformanceCounter("Process", "% Processor Time", process.ProcessName, _config.MachineName)); counter.ReadOnly = true; counters.Add(new ProcessCounterSample { Counter = counter, Process = process, Sample1 = counter.NextSample() }); } catch { Logger.Debug("*** Process '{0}' has no Performance Counter ***", process.ProcessName); } }); Logger.Info("Getting CPU% for {0} processes...", counters.Count); Thread.Sleep(1000); counters.ForEach(pcs => { var sample2 = pcs.Counter.NextSample(); pcs.Value = Math.Round(CounterSampleCalculator.ComputeCounterValue(pcs.Sample1, sample2)); }); counters.Where(pcs => pcs.Value > 0) .OrderByDescending(pcs => pcs.Value) .Take(5) .ForEach(pcs => { var name = string.Format("{0}[{1}]", pcs.Process.ProcessName, pcs.Process.Id); // TODO: add property to core notification properties? //message.AddProperty(name, Convert.ToString(pcs.Value)); }); }
void CalculateResults(ArrayList samplesList) { for (int i = 0; i < (samplesList.Count - 1); i++) { // Output the sample. OutputSample((CounterSample)samplesList[i]); OutputSample((CounterSample)samplesList[i + 1]); // Use .NET to calculate the counter value. if (verbose) { Console.WriteLine(".NET computed counter value = " + CounterSampleCalculator.ComputeCounterValue((CounterSample)samplesList[i], (CounterSample)samplesList[i + 1])); // Calculate the counter value manually. Console.WriteLine("My computed counter value = " + MyComputeCounterValue((CounterSample)samplesList[i], (CounterSample)samplesList[i + 1])); } } }
/// <summary> /// Compute the counter value for this sample compared with the provided baseline sample /// </summary> /// <remarks> /// The baseline sample must be for a date and time prior to this sample for correct results. /// </remarks> /// <param name="baselineSample">The previous baseline sample to calculate a difference for</param> /// <returns>The calculated counter value</returns> public override double ComputeValue(SampledMetricSample baselineSample) { double computedValue; //we have to verify a few things depending on whether we use a baseline if (RequiresMultipleSamples) { if (baselineSample == null) { throw new ArgumentNullException(nameof(baselineSample), "A baseline metric sample is required and none was provided."); } if (baselineSample.Timestamp > Timestamp) { throw new ArgumentOutOfRangeException(nameof(baselineSample), baselineSample.Timestamp, "The baseline sample must be for a date & time before this sample to be valid for comparison."); } //gateway to the counter sample calculator PerfCounterMetricSample baselinePerfCounterSample = (PerfCounterMetricSample)baselineSample; computedValue = CounterSampleCalculator.ComputeCounterValue(baselinePerfCounterSample, Packet.CounterSample); } else { computedValue = CounterSampleCalculator.ComputeCounterValue(Packet.CounterSample); } //is this a percentage value? Perf counters "upscale" at the calculation stage which we want to undo. if (Metric.Definition.IsPercentage) { computedValue = computedValue / 100; } return(computedValue); }
/// <summary> /// Static functions to calculate the performance value off the samples /// </summary> public static float Calculate(CounterSample counterSample, CounterSample nextCounterSample) { return(CounterSampleCalculator.ComputeCounterValue(counterSample, nextCounterSample)); }
/// <summary> /// The initialize. /// </summary> internal void Initialize() { newSample = counter.NextSample(); oldSample = newSample; averageValue = CounterSampleCalculator.ComputeCounterValue(newSample); }
/// <summary> /// The get value. /// </summary> /// <returns> /// The <see cref="float"/>. /// </returns> internal float GetValue() { return(CounterSampleCalculator.ComputeCounterValue(this.oldSample, this.newSample)); }