From 0d2860dd8eb8ff53d29635b15750b5377c6316ed Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Wed, 27 May 2020 21:05:38 +0800 Subject: [PATCH] Plugin collect support stdin and env --- control | 2 +- sql/n9e_mon.sql | 2 ++ sql/upgrade_2.3.0.sql | 3 +++ src/model/collect.go | 2 ++ src/modules/collector/log/worker/worker.go | 5 +---- src/modules/collector/sys/plugins/plugin.go | 2 ++ src/modules/collector/sys/plugins/reader.go | 5 +++-- src/modules/collector/sys/plugins/scheduler.go | 18 ++++++++++++++++++ 8 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 sql/upgrade_2.3.0.sql diff --git a/control b/control index 76d6b6c9..fbb6e3ab 100755 --- a/control +++ b/control @@ -1,7 +1,7 @@ #!/bin/bash # release version -version=2.2.2 +version=2.3.0 CWD=$(cd $(dirname $0)/; pwd) cd $CWD diff --git a/sql/n9e_mon.sql b/sql/n9e_mon.sql index f66b71ab..9fa4033b 100644 --- a/sql/n9e_mon.sql +++ b/sql/n9e_mon.sql @@ -263,6 +263,8 @@ CREATE TABLE `plugin_collect` ( `step` int(11) NOT NULL DEFAULT '0' COMMENT '采集周期', `file_path` varchar(255) NOT NULL COMMENT 'file_path', `params` varchar(255) NOT NULL COMMENT 'params', + `stdin` text NOT NULL COMMENT 'stdin', + `env` text NOT NULL COMMENT 'env', `comment` varchar(512) NOT NULL DEFAULT '' COMMENT 'comment', `creator` varchar(255) NOT NULL DEFAULT '' COMMENT 'creator', `created` datetime NOT NULL COMMENT 'created', diff --git a/sql/upgrade_2.3.0.sql b/sql/upgrade_2.3.0.sql new file mode 100644 index 00000000..f381e396 --- /dev/null +++ b/sql/upgrade_2.3.0.sql @@ -0,0 +1,3 @@ +use n9e_mon; +ALTER TABLE plugin_collect ADD stdin text AFTER params; +ALTER TABLE plugin_collect ADD env text AFTER params; \ No newline at end of file diff --git a/src/model/collect.go b/src/model/collect.go index 968fcc7b..61bb9f5c 100644 --- a/src/model/collect.go +++ b/src/model/collect.go @@ -141,6 +141,8 @@ type PluginCollect struct { Step int `json:"step"` FilePath string `json:"file_path"` Params string `json:"params"` + Stdin string `json:"stdin"` + Env string `json:"env"` Comment string `json:"comment"` Creator string `json:"creator"` Created time.Time `xorm:"updated" json:"created"` diff --git a/src/modules/collector/log/worker/worker.go b/src/modules/collector/log/worker/worker.go index 2f693796..8cea81a4 100644 --- a/src/modules/collector/log/worker/worker.go +++ b/src/modules/collector/log/worker/worker.go @@ -224,10 +224,7 @@ func (w *Worker) producer(line string, strategy *stra.Strategy) (*AnalysPoint, e t = reg.ReplaceAllString(t, rep) } - // [风险]统一使用东八区 - // loc, err := time.LoadLocation("Asia/Shanghai") - loc := time.FixedZone("CST", 8*3600) - tms, err := time.ParseInLocation(timeFormat, t, loc) + tms, err := time.Parse(timeFormat, t) if err != nil { return nil, err } diff --git a/src/modules/collector/sys/plugins/plugin.go b/src/modules/collector/sys/plugins/plugin.go index 27232091..bece52c1 100644 --- a/src/modules/collector/sys/plugins/plugin.go +++ b/src/modules/collector/sys/plugins/plugin.go @@ -3,6 +3,8 @@ package plugins type Plugin struct { FilePath string Params string + Env string + Stdin string MTime int64 Cycle int } diff --git a/src/modules/collector/sys/plugins/reader.go b/src/modules/collector/sys/plugins/reader.go index de7b09aa..8cbcffc9 100644 --- a/src/modules/collector/sys/plugins/reader.go +++ b/src/modules/collector/sys/plugins/reader.go @@ -30,12 +30,13 @@ func ListPluginsFromMonapi() map[string]*Plugin { plugins := stra.Collect.GetPlugin() for key, p := range plugins { - fpath := p.FilePath plugin := &Plugin{ - FilePath: fpath, + FilePath: p.FilePath, MTime: p.LastUpdated.Unix(), Cycle: p.Step, Params: p.Params, + Env: p.Env, + Stdin: p.Stdin, } ret[key] = plugin diff --git a/src/modules/collector/sys/plugins/scheduler.go b/src/modules/collector/sys/plugins/scheduler.go index 25478198..a912de5d 100644 --- a/src/modules/collector/sys/plugins/scheduler.go +++ b/src/modules/collector/sys/plugins/scheduler.go @@ -62,9 +62,27 @@ func PluginRun(plugin *Plugin) { cmd := exec.Command(fpath, params...) cmd.Dir = filepath.Dir(fpath) var stdout bytes.Buffer + cmd.Stdout = &stdout var stderr bytes.Buffer cmd.Stderr = &stderr + + if plugin.Stdin != "" { + cmd.Stdin = bytes.NewReader([]byte(plugin.Stdin)) + } + + if plugin.Env != "" { + envs := []string{} + err := json.Unmarshal([]byte(plugin.Env), &envs) + if err != nil { + logger.Errorf("plugin:%+v %v", plugin, err) + return + } + for _, env := range envs { + cmd.Env = append(cmd.Env, env) + } + } + err := cmd.Start() if err != nil { logger.Error(err)