From 45fe14b4392584d52d6b08eabe5c15c45f2802c1 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Mon, 20 Apr 2020 19:07:38 +0800 Subject: [PATCH] get plugin collect from monapi --- src/model/collect.go | 46 ++++++++++++++++--- src/modules/collector/sys/config.go | 1 + src/modules/collector/sys/plugins/cron.go | 4 +- src/modules/collector/sys/plugins/plugin.go | 1 + src/modules/collector/sys/plugins/reader.go | 43 ++++++++++++++++- .../collector/sys/plugins/scheduler.go | 4 +- 6 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/model/collect.go b/src/model/collect.go index 63ce75a8..6fdee511 100644 --- a/src/model/collect.go +++ b/src/model/collect.go @@ -12,16 +12,18 @@ import ( type Collect struct { sync.RWMutex - Ports map[int]*PortCollect `json:"ports"` - Procs map[string]*ProcCollect `json:"procs"` - Logs map[string]*LogCollect `json:"logs"` + Ports map[int]*PortCollect `json:"ports"` + Procs map[string]*ProcCollect `json:"procs"` + Logs map[string]*LogCollect `json:"logs"` + Plugins map[string]*PluginCollect `json:"plugins"` } func NewCollect() *Collect { return &Collect{ - Ports: make(map[int]*PortCollect), - Procs: make(map[string]*ProcCollect), - Logs: make(map[string]*LogCollect), + Ports: make(map[int]*PortCollect), + Procs: make(map[string]*ProcCollect), + Logs: make(map[string]*LogCollect), + Plugins: make(map[string]*PluginCollect), } } @@ -45,6 +47,12 @@ func (c *Collect) Update(cc *Collect) { for k, v := range cc.Logs { c.Logs[k] = v } + + //更新plugin采集配置 + c.Plugins = make(map[string]*PluginCollect) + for k, v := range cc.Plugins { + c.Plugins[k] = v + } } func (c *Collect) GetPorts() map[int]*PortCollect { @@ -80,6 +88,17 @@ func (c *Collect) GetLogConfig() map[string]*LogCollect { return tmp } +func (c *Collect) GetPlugin() map[string]*PluginCollect { + c.RLock() + defer c.RUnlock() + + tmp := make(map[string]*PluginCollect) + for k, v := range c.Plugins { + tmp[k] = v + } + return tmp +} + type PortCollect struct { Id int64 `json:"id"` Nid int64 `json:"nid"` @@ -114,6 +133,21 @@ type ProcCollect struct { CollectMethod string `json:"collect_method"` } +type PluginCollect struct { + Id int64 `json:"id"` + Nid int64 `json:"nid"` + CollectType string `json:"collect_type"` + Name string `json:"name"` + Step int `json:"step"` + FilePath string `json:"file_path"` + Params string `json:"params"` + Comment string `json:"comment"` + Creator string `json:"creator"` + Created time.Time `xorm:"updated" json:"created"` + LastUpdator string `xorm:"last_updator" json:"last_updator"` + LastUpdated time.Time `xorm:"updated" json:"last_updated"` +} + type LogCollect struct { Id int64 `json:"id"` Nid int64 `json:"nid"` diff --git a/src/modules/collector/sys/config.go b/src/modules/collector/sys/config.go index ed2814b9..143890ab 100644 --- a/src/modules/collector/sys/config.go +++ b/src/modules/collector/sys/config.go @@ -8,6 +8,7 @@ type SysSection struct { IgnoreMetricsMap map[string]struct{} `yaml:"-"` NtpServers []string `yaml:"ntpServers"` Plugin string `yaml:"plugin"` + PluginRemote bool `yaml:"pluginRemote"` Interval int `yaml:"interval"` Timeout int `yaml:"timeout"` } diff --git a/src/modules/collector/sys/plugins/cron.go b/src/modules/collector/sys/plugins/cron.go index 152068e8..fe8b4c14 100644 --- a/src/modules/collector/sys/plugins/cron.go +++ b/src/modules/collector/sys/plugins/cron.go @@ -2,8 +2,6 @@ package plugins import ( "time" - - "github.com/didi/nightingale/src/modules/collector/sys" ) func Detect() { @@ -19,7 +17,7 @@ func loopDetect() { } func detect() { - ps := ListPlugins(sys.Config.Plugin) + ps := ListPlugins() DelNoUsePlugins(ps) AddNewPlugins(ps) } diff --git a/src/modules/collector/sys/plugins/plugin.go b/src/modules/collector/sys/plugins/plugin.go index a9986fd8..35b69e8a 100644 --- a/src/modules/collector/sys/plugins/plugin.go +++ b/src/modules/collector/sys/plugins/plugin.go @@ -2,6 +2,7 @@ package plugins type Plugin struct { FilePath string + Params string MTime int64 Cycle int } diff --git a/src/modules/collector/sys/plugins/reader.go b/src/modules/collector/sys/plugins/reader.go index 78eca0e5..f5f86ff6 100644 --- a/src/modules/collector/sys/plugins/reader.go +++ b/src/modules/collector/sys/plugins/reader.go @@ -2,16 +2,57 @@ package plugins import ( "io/ioutil" + "os" "path/filepath" "strconv" "strings" + "github.com/didi/nightingale/src/modules/collector/stra" + "github.com/didi/nightingale/src/modules/collector/sys" + "github.com/toolkits/pkg/file" "github.com/toolkits/pkg/logger" ) // key: 60_ntp.py -func ListPlugins(dir string) map[string]*Plugin { +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 _, p := range plugins { + fpath := p.FilePath + fileInfo, err := os.Stat(fpath) + if err != nil { + logger.Warningf("plugin:%s get info err:%v", p.FilePath, err) + continue + } + + plugin := &Plugin{ + FilePath: fpath, + MTime: fileInfo.ModTime().Unix(), + Cycle: p.Step, + Params: p.Params, + } + + ret[fpath] = 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) { diff --git a/src/modules/collector/sys/plugins/scheduler.go b/src/modules/collector/sys/plugins/scheduler.go index f9f28887..84777948 100644 --- a/src/modules/collector/sys/plugins/scheduler.go +++ b/src/modules/collector/sys/plugins/scheduler.go @@ -5,6 +5,7 @@ import ( "encoding/json" "os/exec" "path/filepath" + "strings" "time" "github.com/toolkits/pkg/file" @@ -57,7 +58,8 @@ func PluginRun(plugin *Plugin) { } logger.Debug(fpath, " running") - cmd := exec.Command(fpath) + params := strings.Split(plugin.Params, " ") + cmd := exec.Command(fpath, params...) cmd.Dir = filepath.Dir(fpath) var stdout bytes.Buffer cmd.Stdout = &stdout -- GitLab