-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfiles.go
58 lines (50 loc) · 1.12 KB
/
files.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
package main
import (
"fmt"
"os/exec"
"strings"
)
const (
FILES = "files"
FILES_OPEN = "Open"
)
var LSOF_CMD = `lsof | tail -n +2 | tr -s " " "` + "\t" + `" | cut -f 1,2`
func init() {
parser.Add(FILES, "true", "Collect open files")
}
type Files struct {
RawStatus []byte
}
func (files *Files) fetch() (b []byte, e error) {
if len(files.RawStatus) == 0 {
args := []string{"-c"}
args = append(args, LSOF_CMD)
files.RawStatus, _ = exec.Command("bash", args...).Output()
if len(files.RawStatus) == 0 {
return b, fmt.Errorf("lsof returned empty result")
}
}
return files.RawStatus, nil
}
func (f *Files) Collect(col *MetricsCollection) error {
b, e := f.fetch()
if e != nil {
return e
}
stats := map[string]int64{}
for _, line := range strings.Split(string(b), "\n") {
stats[line]++
}
for k, v := range stats {
chunks := strings.SplitN(k, "\t", 2)
if len(chunks) == 2 {
name, pid := chunks[0], chunks[1]
tags := map[string]string{"name": NormalizeProcessName(name), "pid": pid}
col.AddWithTags(FILES_OPEN, v, tags)
}
}
return nil
}
func (f *Files) Prefix() string {
return FILES
}