/
reader.go
56 lines (46 loc) · 1011 Bytes
/
reader.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
package plugins
import (
"github.com/open-falcon/falcon-plus/modules/agent/g"
"github.com/toolkits/file"
"io/ioutil"
"log"
"path/filepath"
"strconv"
"strings"
)
// key: sys/ntp/60_ntp.py
func ListPlugins(relativePath string) map[string]*Plugin {
ret := make(map[string]*Plugin)
if relativePath == "" {
return ret
}
dir := filepath.Join(g.Config().Plugin.Dir, relativePath)
if !file.IsExist(dir) || file.IsFile(dir) {
return ret
}
fs, err := ioutil.ReadDir(dir)
if err != nil {
log.Println("can not list files under", dir)
return ret
}
for _, f := range fs {
if f.IsDir() {
continue
}
filename := f.Name()
arr := strings.Split(filename, "_")
if len(arr) < 2 {
continue
}
// filename should be: $cycle_$xx
var cycle int
cycle, err = strconv.Atoi(arr[0])
if err != nil {
continue
}
fpath := filepath.Join(relativePath, filename)
plugin := &Plugin{FilePath: fpath, MTime: f.ModTime().Unix(), Cycle: cycle}
ret[fpath] = plugin
}
return ret
}