forked from thecubed/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputexec.go
147 lines (123 loc) · 3.34 KB
/
inputexec.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package inputexec
import (
"bytes"
"context"
"encoding/json"
"os"
"os/exec"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/tsaikd/KDGoLib/errutil"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
)
// ModuleName is the name used in config file
const ModuleName = "exec"
// ErrorTag tag added to event when process module failed
const ErrorTag = "gogstash_input_exec_error"
// InputConfig holds the configuration json fields and internal objects
type InputConfig struct {
config.InputConfig
Command string `json:"command"` // Command to run. e.g. “uptime”
Args []string `json:"args,omitempty"` // Arguments of command
Interval int `json:"interval,omitempty"` // Second, default: 60
MsgTrim string `json:"message_trim,omitempty"` // default: " \t\r\n"
MsgPrefix string `json:"message_prefix,omitempty"` // only in text type, e.g. "%{@timestamp} [uptime] "
MsgType MsgType `json:"message_type,omitempty"` // default: "text"
hostname string
}
// DefaultInputConfig returns an InputConfig struct with default values
func DefaultInputConfig() InputConfig {
return InputConfig{
InputConfig: config.InputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
Interval: 60,
MsgTrim: " \t\r\n",
MsgType: MsgTypeText,
}
}
// errors
var (
ErrorExecCommandFailed1 = errutil.NewFactory("run exec failed: %q")
)
// InitHandler initialize the input plugin
func InitHandler(ctx context.Context, raw *config.ConfigRaw) (config.TypeInputConfig, error) {
conf := DefaultInputConfig()
err := config.ReflectConfig(raw, &conf)
if err != nil {
return nil, err
}
if conf.hostname, err = os.Hostname(); err != nil {
return nil, err
}
return &conf, nil
}
// Start wraps the actual function starting the plugin
func (t *InputConfig) Start(ctx context.Context, msgChan chan<- logevent.LogEvent) (err error) {
startChan := make(chan bool, 1) // startup tick
ticker := time.NewTicker(time.Duration(t.Interval) * time.Second)
defer ticker.Stop()
startChan <- true
for {
select {
case <-ctx.Done():
return nil
case <-startChan:
t.exec(msgChan, config.Logger)
case <-ticker.C:
t.exec(msgChan, config.Logger)
}
}
}
func (t *InputConfig) exec(msgChan chan<- logevent.LogEvent, logger *logrus.Logger) {
errs := []error{}
message, err := t.doExecCommand()
if err != nil {
errs = append(errs, err)
}
extra := map[string]interface{}{
"host": t.hostname,
}
switch t.MsgType {
case MsgTypeJson:
if err = json.Unmarshal([]byte(message), &extra); err != nil {
errs = append(errs, err)
} else {
message = ""
}
}
event := logevent.LogEvent{
Timestamp: time.Now(),
Message: message,
Extra: extra,
}
switch t.MsgType {
case MsgTypeText:
event.Message = event.Format(t.MsgPrefix) + event.Message
}
if len(errs) > 0 {
event.AddTag(ErrorTag)
event.Extra["error"] = errutil.NewErrors(errs...).Error()
}
msgChan <- event
return
}
func (t *InputConfig) doExecCommand() (data string, err error) {
buferr := &bytes.Buffer{}
cmd := exec.Command(t.Command, t.Args...)
cmd.Stderr = buferr
raw, err := cmd.Output()
if err != nil {
return
}
data = string(raw)
data = strings.Trim(data, t.MsgTrim)
if buferr.Len() > 0 {
err = ErrorExecCommandFailed1.New(nil, buferr.String())
}
return
}