-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathreader.go
More file actions
93 lines (75 loc) · 1.83 KB
/
reader.go
File metadata and controls
93 lines (75 loc) · 1.83 KB
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
package plugins
import (
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"
"github.com/didi/nightingale/src/modules/agent/stra"
"github.com/didi/nightingale/src/modules/agent/sys"
)
// key: 60_ntp.py
func ListPlugins() map[string]*Plugin {
plugins := make(map[string]*Plugin)
if sys.Config.PluginRemote {
plugins = ListPluginsFromMonapi()
} else {
plugins = ListPluginsFromLocal()
}
return plugins
}
func ListPluginsFromMonapi() map[string]*Plugin {
ret := make(map[string]*Plugin)
plugins := stra.Collect.GetPlugin()
for key, p := range plugins {
plugin := &Plugin{
FilePath: p.FilePath,
MTime: p.LastUpdated.Unix(),
Cycle: p.Step,
Params: p.Params,
Env: p.Env,
Stdin: p.Stdin,
}
ret[key] = plugin
}
return ret
}
func ListPluginsFromLocal() map[string]*Plugin {
dir := sys.Config.Plugin
ret := make(map[string]*Plugin)
if dir == "" || !file.IsExist(dir) || file.IsFile(dir) {
return ret
}
fs, err := ioutil.ReadDir(dir)
if err != nil {
logger.Error("[E] 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 {
logger.Debugf("plugin:%s name illegal, should be: $cycle_$xx", filename)
continue
}
// filename should be: $cycle_$xx
var cycle int
cycle, err = strconv.Atoi(arr[0])
if err != nil {
logger.Debugf("plugin:%s name illegal, should be: $cycle_$xx %v", filename, err)
continue
}
fpath, err := filepath.Abs(filepath.Join(dir, filename))
if err != nil {
logger.Debugf("plugin:%s absolute path get err:%v", filename, err)
continue
}
plugin := &Plugin{FilePath: fpath, MTime: f.ModTime().Unix(), Cycle: cycle}
ret[fpath] = plugin
}
return ret
}