Plugin collect support stdin and env

This commit is contained in:
710leo 2020-05-27 21:05:38 +08:00
parent ea25842f9d
commit 0d2860dd8e
8 changed files with 32 additions and 7 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# release version # release version
version=2.2.2 version=2.3.0
CWD=$(cd $(dirname $0)/; pwd) CWD=$(cd $(dirname $0)/; pwd)
cd $CWD cd $CWD

View File

@ -263,6 +263,8 @@ CREATE TABLE `plugin_collect` (
`step` int(11) NOT NULL DEFAULT '0' COMMENT '采集周期', `step` int(11) NOT NULL DEFAULT '0' COMMENT '采集周期',
`file_path` varchar(255) NOT NULL COMMENT 'file_path', `file_path` varchar(255) NOT NULL COMMENT 'file_path',
`params` varchar(255) NOT NULL COMMENT 'params', `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', `comment` varchar(512) NOT NULL DEFAULT '' COMMENT 'comment',
`creator` varchar(255) NOT NULL DEFAULT '' COMMENT 'creator', `creator` varchar(255) NOT NULL DEFAULT '' COMMENT 'creator',
`created` datetime NOT NULL COMMENT 'created', `created` datetime NOT NULL COMMENT 'created',

3
sql/upgrade_2.3.0.sql Normal file
View File

@ -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;

View File

@ -141,6 +141,8 @@ type PluginCollect struct {
Step int `json:"step"` Step int `json:"step"`
FilePath string `json:"file_path"` FilePath string `json:"file_path"`
Params string `json:"params"` Params string `json:"params"`
Stdin string `json:"stdin"`
Env string `json:"env"`
Comment string `json:"comment"` Comment string `json:"comment"`
Creator string `json:"creator"` Creator string `json:"creator"`
Created time.Time `xorm:"updated" json:"created"` Created time.Time `xorm:"updated" json:"created"`

View File

@ -224,10 +224,7 @@ func (w *Worker) producer(line string, strategy *stra.Strategy) (*AnalysPoint, e
t = reg.ReplaceAllString(t, rep) t = reg.ReplaceAllString(t, rep)
} }
// [风险]统一使用东八区 tms, err := time.Parse(timeFormat, t)
// loc, err := time.LoadLocation("Asia/Shanghai")
loc := time.FixedZone("CST", 8*3600)
tms, err := time.ParseInLocation(timeFormat, t, loc)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,6 +3,8 @@ package plugins
type Plugin struct { type Plugin struct {
FilePath string FilePath string
Params string Params string
Env string
Stdin string
MTime int64 MTime int64
Cycle int Cycle int
} }

View File

@ -30,12 +30,13 @@ func ListPluginsFromMonapi() map[string]*Plugin {
plugins := stra.Collect.GetPlugin() plugins := stra.Collect.GetPlugin()
for key, p := range plugins { for key, p := range plugins {
fpath := p.FilePath
plugin := &Plugin{ plugin := &Plugin{
FilePath: fpath, FilePath: p.FilePath,
MTime: p.LastUpdated.Unix(), MTime: p.LastUpdated.Unix(),
Cycle: p.Step, Cycle: p.Step,
Params: p.Params, Params: p.Params,
Env: p.Env,
Stdin: p.Stdin,
} }
ret[key] = plugin ret[key] = plugin

View File

@ -62,9 +62,27 @@ func PluginRun(plugin *Plugin) {
cmd := exec.Command(fpath, params...) cmd := exec.Command(fpath, params...)
cmd.Dir = filepath.Dir(fpath) cmd.Dir = filepath.Dir(fpath)
var stdout bytes.Buffer var stdout bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
var stderr bytes.Buffer var stderr bytes.Buffer
cmd.Stderr = &stderr 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() err := cmd.Start()
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)