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
# release version
version=2.2.2
version=2.3.0
CWD=$(cd $(dirname $0)/; pwd)
cd $CWD

View File

@ -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',

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"`
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"`

View File

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

View File

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

View File

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

View File

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