-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinux.go
446 lines (376 loc) · 9.33 KB
/
linux.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
import (
"bufio"
"flag"
"fmt"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
"github.com/mackerelio/mackerel-agent/logging"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
)
var logger = logging.GetLogger("metrics.plugin.linux-proc-stats")
// /proc/PID/stat fileds
const (
Comm = 1
State = 2
UTime = 13
STime = 14
CUTime = 15
CSTime = 16
NumThreads = 19
StartTime = 21
VSize = 22
Rss = 23
)
// Process Status
type Process struct {
State string
UTime float64
STime float64
CUTime float64
CSTime float64
StartTime float64
NumThreads float64
VSize float64
RSS float64
}
// Processes slice of Process
type Processes []Process
var uptime float64
var cpuTick float64
var metricKey string
// CPUUsage retrieve cpu usage
func (p *Process) CPUUsage(includeDeadChildren bool) float64 {
total := p.UTime + p.STime
if includeDeadChildren {
total += p.CUTime + p.CSTime
}
sec := uptime - (p.StartTime / cpuTick)
return 100 * ((total / cpuTick) / sec)
}
// LinuxProcStatsPlugin mackerel plugin for Linux processes
type LinuxProcStatsPlugin struct {
Tempfile string
Pid string
Pids []string
FollowChildProcesses bool
IncludeDeadChildren bool
}
// FetchMetrics interface for mackerelplugin
func (lp LinuxProcStatsPlugin) FetchMetrics() (stats map[string]interface{}, err error) {
ps := make(Processes, 0)
if len(lp.Pids) > 0 {
for _, pid := range lp.Pids {
p, err := readProcPIDStat(statFile(pid))
if err != nil {
return stats, err
}
ps = append(ps, p)
}
} else {
p, err := readProcPIDStat(statFile(lp.Pid))
if err != nil {
return stats, err
}
ps = append(ps, p)
if lp.FollowChildProcesses {
options := []string{"--ppid", lp.Pid, "-o", "pid", "--no-headers"}
ps, err = childStats(ps, options)
if err != nil {
return stats, err
}
}
}
running, numThreads, vSize, rss, cpuUsage := sumStats(ps, lp.IncludeDeadChildren)
stats = make(map[string]interface{})
stats["running"] = running
stats["processes"] = float64(len(ps))
stats["threads"] = numThreads
stats["vsize"] = vSize
stats["rss"] = rss
stats["usage"] = cpuUsage
return stats, err
}
// GraphDefinition interface for mackerelplugin
func (lp LinuxProcStatsPlugin) GraphDefinition() map[string](mp.Graphs) {
comm, err := readProcPIDComm(lp.Pid)
if err != nil {
logger.Errorf("Failed to read /proc/PID/stat comm. %s", err)
return map[string](mp.Graphs){}
}
return map[string](mp.Graphs){
fmt.Sprintf("%s_process.num", comm): mp.Graphs{
Label: fmt.Sprintf("%v Running", comm),
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "running", Label: "Current Running Processes", Diff: false},
mp.Metrics{Name: "processes", Label: "All Processes", Diff: false},
mp.Metrics{Name: "threads", Label: "Num Threads", Diff: false},
},
},
fmt.Sprintf("%s_process.cpu", comm): mp.Graphs{
Label: fmt.Sprintf("%v CPU", comm),
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "usage", Label: "CPU Usage", Stacked: true, Diff: false},
},
},
fmt.Sprintf("%s_process.memory", comm): mp.Graphs{
Label: fmt.Sprintf("%v Memory", comm),
Unit: "bytes",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "vsize", Label: "Vsize", Stacked: false, Diff: false},
mp.Metrics{Name: "rss", Label: "RSS", Stacked: false, Diff: false},
},
},
}
}
func readPIDFile(f string) (pid string, err error) {
fp, err := os.Open(f)
if err != nil {
return pid, err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
scanner.Scan()
pid = scanner.Text()
return pid, err
}
func readStatFile(f string) ([]string, error) {
b, err := ioutil.ReadFile(f)
if err != nil {
return []string{""}, err
}
s := strings.Split(string(b), " ")
return s, err
}
func readProcPIDComm(pid string) (comm string, err error) {
if metricKey == "" {
s, err := readStatFile(statFile(pid))
if err != nil {
return comm, err
}
comm = strings.Trim(s[Comm], "()")
} else {
comm = metricKey
}
return comm, err
}
func readProcPIDStat(f string) (p Process, err error) {
s, err := readStatFile(f)
if err != nil {
return p, err
}
numThreads, err := strconv.ParseFloat(s[NumThreads], 64)
if err != nil {
return p, err
}
vSize, err := strconv.ParseFloat(s[VSize], 64)
if err != nil {
return p, err
}
rss, err := strconv.ParseFloat(s[Rss], 64)
if err != nil {
return p, err
}
utime, err := strconv.ParseFloat(s[UTime], 64)
if err != nil {
return p, err
}
stime, err := strconv.ParseFloat(s[STime], 64)
if err != nil {
return p, err
}
cutime, err := strconv.ParseFloat(s[CUTime], 64)
if err != nil {
return p, err
}
cstime, err := strconv.ParseFloat(s[CSTime], 64)
if err != nil {
return p, err
}
startTime, err := strconv.ParseFloat(s[StartTime], 64)
if err != nil {
return p, err
}
p = Process{
State: s[State],
NumThreads: numThreads,
VSize: vSize,
RSS: rss * float64(os.Getpagesize()),
UTime: utime,
STime: stime,
CUTime: cutime,
CSTime: cstime,
StartTime: startTime,
}
return p, err
}
func statFile(pid string) string {
return fmt.Sprintf("/proc/%v/stat", strings.TrimSpace(pid))
}
func childStats(ps Processes, options []string) (Processes, error) {
cmd := exec.Command("ps", options...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return ps, err
}
cmd.Start()
scanner := bufio.NewScanner(stdout)
var p Process
for scanner.Scan() {
childPid := scanner.Text()
p, err = readProcPIDStat(statFile(childPid))
if err == nil {
ps = append(ps, p)
}
}
cmd.Wait()
return ps, err
}
func sumStats(
ps Processes,
includeDeadChildren bool,
) (running, numThreads, vSize, rss, cpuUsage float64) {
for _, p := range ps {
if p.State == `R` {
running++
}
numThreads += p.NumThreads
vSize += p.VSize
rss += p.RSS
cpuUsage += p.CPUUsage(includeDeadChildren)
}
return running, numThreads, vSize, rss, cpuUsage
}
func getCPUTick() (tick float64, err error) {
var out []byte
out, err = exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
if err != nil {
return tick, err
}
tick, err = strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
return tick, err
}
func getUptime() (float64, error) {
sysinfo := syscall.Sysinfo_t{}
err := syscall.Sysinfo(&sysinfo)
return float64(sysinfo.Uptime), err
}
func getPIDsByProcessPattern(pat string) (pids []string, err error) {
r, err := regexp.Compile(pat)
if err != nil {
return pids, fmt.Errorf("Failed to compile %s. %s", pat, err)
}
res, err := getPSResult()
if err != nil {
return pids, fmt.Errorf("Failed to getPSResult. %s", err)
}
pids = make([]string, 0)
p, pp := strconv.Itoa(os.Getpid()), strconv.Itoa(os.Getppid())
for _, ps := range res {
if !r.MatchString(ps.cmd) {
continue
}
if ps.pid == p {
continue
}
if ps.pid == pp {
continue
}
pids = append(pids, ps.pid)
}
return pids, nil
}
type psResult struct {
pid string
cmd string
}
func getPSResult() (res []psResult, err error) {
psformat := "pid,command"
output, err := exec.Command("ps", "axwwo", psformat).Output()
if err != nil {
return res, err
}
for _, line := range strings.Split(string(output), "\n")[1:] {
if line == "" {
continue
}
r, err := parsePSResult(line)
if err != nil {
return res, err
}
res = append(res, r)
}
return res, nil
}
func parsePSResult(line string) (r psResult, err error) {
fields := strings.Fields(line)
fieldsMinLen := 2
if len(fields) < fieldsMinLen {
return r, fmt.Errorf(
"parsePSResult: insufficient words. line=%s, fields=%v",
line, fields,
)
}
return psResult{fields[0], strings.Join(fields[1:], " ")}, nil
}
func main() {
optPID := flag.String("pid", "", "PID")
optPIDFile := flag.String("pidfile", "", "PID file")
optProcPat := flag.String("process-pattern", "", "Match a command against this pattern")
optTempfile := flag.String("tempfile", "", "Temp file name")
optFollowChildProcesses := flag.Bool("follow-child-processes", false, "Follow child processes")
optMetricKey := flag.String("metric-key-prefix", "", "Metric key prefix")
optVersion := flag.Bool("version", false, "Version")
optIncludeDeadChildren := flag.Bool("include-dead-children", true, "Include dead kids in cpu sum")
flag.Parse()
if *optVersion {
fmt.Println("0.3")
os.Exit(0)
}
var pid string
var pids []string
var err error
if *optProcPat != "" {
pids, err = getPIDsByProcessPattern(*optProcPat)
if err != nil {
logger.Errorf("Failed to match %s. %s", *optProcPat, err)
}
} else {
if *optPID != "" {
pid = *optPID
} else {
pid, err = readPIDFile(*optPIDFile)
if err != nil {
logger.Errorf("Failed to read /proc/%s/stat. %s", pid, err)
}
}
}
metricKey = *optMetricKey
var procStats LinuxProcStatsPlugin
procStats.Pid = pid
procStats.Pids = pids
procStats.FollowChildProcesses = *optFollowChildProcesses
procStats.IncludeDeadChildren = *optIncludeDeadChildren
uptime, err = getUptime()
if err != nil {
logger.Errorf("Failed to fetch uptime. %s", err)
}
cpuTick, err = getCPUTick()
if err != nil {
logger.Errorf("Failed to fetch cputick. %s", err)
}
helper := mp.NewMackerelPlugin(procStats)
if *optTempfile != "" {
helper.Tempfile = *optTempfile
} else {
helper.Tempfile = "/tmp/mackerel-plugin-linux-proc-stats"
}
helper.Run()
}