Skip to content

Commit bf759b1

Browse files
committed
CPU shows up on snapshot!
1 parent 8c3a52f commit bf759b1

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed
Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,80 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
3+
using System.Linq;
34
using Tether.Plugins;
45

56
namespace Tether.Metrics
67
{
78
public class CPUUtilisationMetricProvider : IMetricProvider
89
{
9-
PerformanceCounter counter;
10-
// serverdensity.cpu.util.pct
10+
//List<PerformanceCounter> counters;
11+
Dictionary<string, List<PerformanceCounter>> counters;
12+
PerformanceCounterCategory performanceCounterCategory;
13+
string[] instanceNames;
14+
15+
List<string> counterNames = new List<string>
16+
{
17+
"% Interrupt Time",
18+
"% Privileged Time",
19+
"% Processor Time",
20+
"% User Time",
21+
"% Idle Time"
22+
};
23+
1124
public CPUUtilisationMetricProvider()
1225
{
1326

1427
}
1528

1629
public List<Metric> GetMetrics()
1730
{
18-
if (counter == null)
31+
if (counters == null)
1932
{
20-
counter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
33+
counters = new Dictionary<string, List<PerformanceCounter>>();
34+
35+
performanceCounterCategory = new PerformanceCounterCategory("Processor");
36+
instanceNames = performanceCounterCategory.GetInstanceNames();
37+
38+
foreach (var instanceName in instanceNames)
39+
{
40+
var performanceCounters = performanceCounterCategory.GetCounters(instanceName);
41+
42+
var list = performanceCounters.Where(f=> counterNames.Contains(f.CounterName) ).ToList();
43+
44+
counters.Add(instanceName, list);
45+
}
2146
}
22-
23-
return new List<Metric>
24-
{
25-
new Metric("serverdensity.cpu.util.pct", counter.NextValue()),
26-
new Metric("system.load.1", counter.NextValue()),
2747

48+
var totalProcessorTimeCounter = counters["_Total"].FirstOrDefault(r=>r.CounterName == "% Processor Time");
49+
var totalProcessorTimeValue = totalProcessorTimeCounter.NextValue();
50+
51+
var values = new List<Metric>
52+
{
53+
new Metric("serverdensity.cpu.util.pct", totalProcessorTimeValue),
54+
new Metric("system.load.1", totalProcessorTimeValue),
2855
};
56+
57+
foreach (var instance in counters)
58+
{
59+
values.Add(new Metric("serverdensity.cpu.gnice", 0, tags:new Dictionary<string, string>{{"device_name", instance.Key}}));
60+
values.Add(new Metric("serverdensity.cpu.guest", 0, tags:new Dictionary<string, string>{{"device_name", instance.Key}}));
61+
62+
values.Add(new Metric("serverdensity.cpu.sys", instance.Value.FirstOrDefault(f=>f.CounterName == "% Privileged Time").NextValue(), tags:new Dictionary<string, string>{{"device_name", instance.Key}}));
63+
values.Add(new Metric("serverdensity.cpu.usr", instance.Value.FirstOrDefault(f=>f.CounterName == "% User Time").NextValue(), tags:new Dictionary<string, string>{{"device_name", instance.Key}}));
64+
values.Add(new Metric("serverdensity.cpu.idle", instance.Value.FirstOrDefault(f=>f.CounterName == "% Idle Time").NextValue(), tags:new Dictionary<string, string>{{"device_name", instance.Key}}));
65+
66+
67+
68+
}
69+
70+
/*
71+
* 'serverdensity.cpu.gnice': 'cpuStats',
72+
'serverdensity.cpu.guest': 'cpuStats',
73+
'serverdensity.cpu.sys': 'cpuStats',
74+
'serverdensity.cpu.usr': 'cpuStats',
75+
'serverdensity.cpu.idle': 'cpuStats',
76+
*/
77+
return values;
2978
}
3079
}
3180
}

0 commit comments

Comments
 (0)