1
1
using System . Collections . Generic ;
2
2
using System . Diagnostics ;
3
+ using System . Linq ;
3
4
using Tether . Plugins ;
4
5
5
6
namespace Tether . Metrics
6
7
{
7
8
public class CPUUtilisationMetricProvider : IMetricProvider
8
9
{
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
+
11
24
public CPUUtilisationMetricProvider ( )
12
25
{
13
26
14
27
}
15
28
16
29
public List < Metric > GetMetrics ( )
17
30
{
18
- if ( counter == null )
31
+ if ( counters == null )
19
32
{
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
+ }
21
46
}
22
-
23
- return new List < Metric >
24
- {
25
- new Metric ( "serverdensity.cpu.util.pct" , counter . NextValue ( ) ) ,
26
- new Metric ( "system.load.1" , counter . NextValue ( ) ) ,
27
47
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 ) ,
28
55
} ;
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 ;
29
78
}
30
79
}
31
80
}
0 commit comments