This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
cpu.go
202 lines (186 loc) · 4.76 KB
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package psutil
import (
"fmt"
"runtime"
"time"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
"github.com/shirou/gopsutil/cpu"
)
var cpuLabels = map[string]label{
"user": label{
description: "",
unit: "",
},
"system": label{
description: "",
unit: "",
},
"idle": label{
description: "",
unit: "",
},
"nice": label{
description: "",
unit: "",
},
"iowait": label{
description: "",
unit: "",
},
"irq": label{
description: "",
unit: "",
},
"softirq": label{
description: "",
unit: "",
},
"steal": label{
description: "",
unit: "",
},
"guest": label{
description: "",
unit: "",
},
"guest_nice": label{
description: "",
unit: "",
},
"stolen": label{
description: "",
unit: "",
},
}
func cpuTimes(nss []plugin.Namespace) ([]plugin.Metric, error) {
// gather metrics per each cpu
defer timeSpent(time.Now(), "cpuTimes")
timesCPUs, err := cpu.Times(true)
if err != nil {
return nil, err
}
// gather accumulated metrics for all cpus
timesAll, err := cpu.Times(false)
if err != nil {
return nil, err
}
results := []plugin.Metric{}
for _, ns := range nss {
// set requested metric name from last namespace element
metricName := ns.Element(len(ns) - 1).Value
// check if requested metric is dynamic (requesting metrics for all cpu ids)
if ns[3].Value == "*" {
for _, timesCPU := range timesCPUs {
// prepare namespace copy to update value
// this will allow to keep namespace as dynamic (name != "")
dyn := make([]plugin.NamespaceElement, len(ns))
copy(dyn, ns)
dyn[3].Value = timesCPU.CPU
// get requested metric value
val, err := getCPUTimeValue(×CPU, metricName)
if err != nil {
return nil, err
}
metric := plugin.Metric{
Namespace: dyn,
Data: val,
Timestamp: time.Now(),
Unit: cpuLabels[metricName].unit,
}
results = append(results, metric)
}
} else {
timeStats := append(timesAll, timesCPUs...)
// find stats for interface name or all cpus
timeStat := findCPUTimeStat(timeStats, ns[3].Value)
if timeStat == nil {
return nil, fmt.Errorf("Requested cpu id %s not found", ns[3].Value)
}
// get requested metric value from struct
val, err := getCPUTimeValue(timeStat, metricName)
if err != nil {
return nil, err
}
metric := plugin.Metric{
Namespace: ns,
Data: val,
Timestamp: time.Now(),
Unit: cpuLabels[metricName].unit,
}
results = append(results, metric)
}
}
return results, nil
}
func findCPUTimeStat(timeStats []cpu.TimesStat, name string) *cpu.TimesStat {
for _, timeStat := range timeStats {
if timeStat.CPU == name {
return &timeStat
}
}
return nil
}
func getCPUTimeValue(stat *cpu.TimesStat, name string) (float64, error) {
switch name {
case "user":
return stat.User, nil
case "system":
return stat.System, nil
case "idle":
return stat.Idle, nil
case "nice":
return stat.Nice, nil
case "iowait":
return stat.Iowait, nil
case "irq":
return stat.Irq, nil
case "softirq":
return stat.Softirq, nil
case "steal":
return stat.Steal, nil
case "guest":
return stat.Guest, nil
case "guest_nice":
return stat.GuestNice, nil
case "stolen":
return stat.Stolen, nil
default:
return 0, fmt.Errorf("Requested CPUTime statistic %s is not available", name)
}
}
func getCPUTimesMetricTypes() ([]plugin.Metric, error) {
defer timeSpent(time.Now(), "getCPUTimesMetricTypes")
//passing true to CPUTimes indicates per CPU
mts := []plugin.Metric{}
switch runtime.GOOS {
case "linux", "darwin":
for k, label := range cpuLabels {
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "cpu").AddDynamicElement("cpu_id", "physical cpu id").AddStaticElement(k),
Description: label.description,
Unit: label.unit,
})
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "cpu", "cpu-total").AddStaticElement(k),
Description: label.description,
Unit: label.unit,
})
}
default:
return nil, fmt.Errorf("%s not supported by plugin", runtime.GOOS)
}
return mts, nil
}