forked from dynport/metrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.go
204 lines (189 loc) · 3.67 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
203
204
package main
import (
"bufio"
"bytes"
"io/ioutil"
"os"
"strconv"
"strings"
)
var statMapping = map[string]string{
"ctxt": "Ctxt",
"btime": "Btime",
"processes": "Processes",
"procs_running": "ProcsRunning",
"procs_blocked": "ProcsBlocked",
}
const (
cpuFieldUser = iota + 1
cpuFieldNice
cpuFieldSystem
cpuFieldIdle
cpuFieldIOWait
cpuFieldIrq
cpuFieldSoftIrq
cpuFieldSteal
cpuFieldGuest
cpuFieldGuestNice
)
var cpuLineMapping = map[int]string{
1: "User",
2: "Nice",
3: "System",
4: "Idle",
5: "IOWait",
6: "IRC",
7: "SoftIRQ",
}
const CPU = "cpu"
func init() {
parser.Add(CPU, "true", "Collect cpu metrics")
}
type Cpu struct {
Cpus []*CpuStat
Ctxt int64
Btime int64
Processes int64
ProcsRunning int64
ProcsBlocked int64
}
func (c *Cpu) Load(b []byte) error {
scanner := bufio.NewScanner(bytes.NewReader(b))
c.Cpus = []*CpuStat{}
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) < 2 {
// continue
}
key := parts[0]
if strings.HasPrefix(key, "cpu") {
s := &CpuStat{Id: strings.TrimPrefix(key, "cpu")}
for i, v := range parts[1:] {
value, e := strconv.ParseInt(v, 10, 64)
if e != nil {
return e
}
switch i + 1 {
case cpuFieldUser:
s.User = value
case cpuFieldNice:
s.Nice = value
case cpuFieldSystem:
s.System = value
case cpuFieldIdle:
s.Idle = value
case cpuFieldIOWait:
s.IOWait = value
case cpuFieldIrq:
s.IRQ = value
case cpuFieldSoftIrq:
s.SoftIRQ = value
case cpuFieldSteal:
s.Steal = value
case cpuFieldGuest:
s.Guest = value
case cpuFieldGuestNice:
s.GuestNice = value
}
}
c.Cpus = append(c.Cpus, s)
} else {
var e error
switch key {
case "ctxt":
c.Ctxt, e = parseIntE(parts[1])
if e != nil {
return e
}
case "btime":
c.Btime, e = parseIntE(parts[1])
if e != nil {
return e
}
case "processes":
c.Processes, e = parseIntE(parts[1])
if e != nil {
return e
}
case "procs_running":
c.ProcsRunning, e = parseIntE(parts[1])
if e != nil {
return e
}
case "procs_blocked":
c.ProcsBlocked, e = parseIntE(parts[1])
if e != nil {
return e
}
}
}
}
return scanner.Err()
}
type CpuStat struct {
Id string
User int64
Nice int64
System int64
Idle int64
IOWait int64
IRQ int64
SoftIRQ int64
Steal int64
Guest int64
GuestNice int64
}
func (*Cpu) Prefix() string {
return "cpu"
}
func ProcRoot() string {
return os.Getenv("PROC_ROOT")
}
func ReadProcFile(path string) (r string) {
if b, e := ioutil.ReadFile(ProcRoot() + "/proc/" + path); e == nil {
r = string(b)
}
return
}
func (cpu *Cpu) Collect(c *MetricsCollection) (e error) {
str := ReadProcFile("stat")
e = cpu.Load([]byte(str))
if e != nil {
return e
}
values := map[string]int64{
"ctxt": cpu.Ctxt,
"btime": cpu.Btime,
"processes": cpu.Processes,
"procs_running": cpu.ProcsRunning,
"procs_blocked": cpu.ProcsBlocked,
}
for k, v := range values {
c.Add(k, v)
}
for _, cpu := range cpu.Cpus {
tags := map[string]string{}
if cpu.Id != "" {
tags["cpu_id"] = cpu.Id
} else {
tags["total"] = "true"
}
values := map[string]int64{
"user": cpu.User,
"nice": cpu.Nice,
"system": cpu.System,
"idle": cpu.Idle,
"io_wait": cpu.IOWait,
"irq": cpu.IRQ,
"soft_irq": cpu.SoftIRQ,
"steal": cpu.Steal,
"guest": cpu.Guest,
"guest_nice": cpu.GuestNice,
}
for k, v := range values {
c.AddWithTags(k, v, tags)
}
}
return nil
}