-
Notifications
You must be signed in to change notification settings - Fork 1
/
lustre_log.go
188 lines (160 loc) · 5.4 KB
/
lustre_log.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/patrickmn/go-cache"
"github.com/spf13/viper"
)
type LustreEvent struct {
Number int
Type string
Target string
Parent string
}
var c = cache.New(5*time.Minute, 10*time.Minute)
func listenLog() {
eventsChan := make(chan string, viper.GetInt("listen_queue"))
go func() {
for range time.NewTicker(5 * time.Second).C {
LLQueueLengthGauge.WithLabelValues(*fsListenParam).Set(float64(len(eventsChan)))
}
}()
for _, mdtstr := range *listenMdtParam {
mdt := strings.Split(mdtstr, ":")[0]
user := strings.Split(mdtstr, ":")[1]
go func(mdt string, user string) {
cmdName := "lfs"
cmdArgs := []string{"changelog", mdt, user}
for {
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Println(err.Error())
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Println(err.Error())
}
scanner := bufio.NewScanner(cmdReader)
err = cmd.Start()
if err != nil {
log.Println(err.Error())
}
var lastID string
for scanner.Scan() {
newEvt := scanner.Text()
eventsChan <- newEvt
lastID = strings.Split(newEvt, " ")[0]
}
slurp, _ := ioutil.ReadAll(stderr)
if err = cmd.Wait(); err != nil {
log.Printf("%s %s", err.Error(), slurp)
}
if lastID == "" {
time.Sleep(1 * time.Second)
} else {
if _, err = exec.Command("lfs", "changelog_clear", mdt, user, lastID).Output(); err != nil {
log.Println(err.Error())
}
}
}
}(mdt, user)
}
for j := 0; j < viper.GetInt("listen_workers"); j++ {
go func() {
for evtStr := range eventsChan {
evtTokens := strings.Split(evtStr, " ")
if len(evtTokens) < 2 {
continue
}
switch evtTokens[1] {
case "01CREAT":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of created file: %s", err.Error())
}
LLFilesCreatedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
case "02MKDIR":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of created folder: %s", err.Error())
}
LLFoldersCreatedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
case "07RMDIR":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of deleted folder: %s", err.Error())
}
LLFoldersRemovedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
case "06UNLNK":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of deleted file: %s", err.Error())
}
LLFilesRemovedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
case "10OPEN":
if len(evtTokens) < 5 || len(evtTokens[6]) < 5 {
logger.Errorf("Got unusual open event: |%s| %d %d %s", evtTokens, len(evtTokens), len(evtTokens[6]), evtTokens[6])
} else {
fstype, group, user, err := getOwner(evtTokens[6][3 : len(evtTokens[6])-1])
if err != nil {
logger.Errorf("Error getting fid of opened file: %s", err.Error())
}
LLFilesOpenedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
}
case "14SATTR":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of changed attr %s: %s", evtTokens[5][3:len(evtTokens[5])-1], err.Error())
}
LLAttrChangedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
case "15XATTR":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of changed attr %s: %s", evtTokens[5][3:len(evtTokens[5])-1], err.Error())
}
LLXAttrChangedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
case "17MTIME":
fstype, group, user, err := getOwner(evtTokens[5][3 : len(evtTokens[5])-1])
if err != nil {
logger.Errorf("Error getting fid of mtime %s: %s", evtTokens[5][3:len(evtTokens[5])-1], err.Error())
}
LLMtimeChangedCounter.WithLabelValues(fstype, group, user, *fsListenParam).Inc()
default:
logger.Errorf("Got unknown event: %s", evtTokens)
}
}
}()
}
}
func getOwner(fid string) (fstype string, project string, user string, err error) { // works for combined /projects and /scratch
var rel string
if relVal, found := c.Get(fid); found {
rel = relVal.(string)
LLCacheHitsCounter.WithLabelValues(*fsListenParam).Inc()
} else {
LLCacheMissesCounter.WithLabelValues(*fsListenParam).Inc()
fs := dataBackends[*fsListenParam]
pathB, err := exec.Command("lfs", "fid2path", fs.GetMountPath(), fid).Output()
if err != nil {
return "", "unknown", "unknown", err
}
path := string(pathB)
rel, err = filepath.Rel(fs.GetMountPath(), path)
if err != nil {
return "", "unknown", "unknown", err
}
c.Set(fid, rel, cache.DefaultExpiration)
}
if len(strings.Split(rel, "/")) < 3 {
return "", "", "", fmt.Errorf("Path %s does not contain group and user", rel)
}
splitPath := strings.Split(rel, "/")
return splitPath[0], splitPath[1], splitPath[2], nil
}