-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
155 lines (143 loc) · 4.47 KB
/
plugin.go
File metadata and controls
155 lines (143 loc) · 4.47 KB
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
148
149
150
151
152
153
154
155
package qfilter_syslog
import (
"fmt"
"reflect"
"github.com/zpatrick/go-config"
"github.com/qframe/types/messages"
"github.com/qframe/types/plugin"
"github.com/qframe/types/qchannel"
"github.com/qframe/types/constants"
"github.com/qframe/types/syslog"
"github.com/deckarep/golang-set"
"sync"
)
const (
version = "0.1.0"
pluginTyp = qtypes_constants.FILTER
pluginPkg = "syslog"
)
type Plugin struct {
*qtypes_plugin.Plugin
mu sync.Mutex
}
func New(qChan qtypes_qchannel.QChan, cfg *config.Config, name string) (p Plugin, err error) {
p = Plugin{
Plugin: qtypes_plugin.NewNamedPlugin(qChan, cfg, pluginTyp, pluginPkg, name, version),
}
return
}
// Lock locks the plugins' mutex.
func (p *Plugin) Lock() {
p.mu.Lock()
}
// Unlock unlocks the plugins' mutex.
func (p *Plugin) Unlock() {
p.mu.Unlock()
}
// Run fetches everything from the Data channel and flushes it to stdout
func (p *Plugin) Run() {
p.Log("notice", fmt.Sprintf("Start plugin v%s", p.Version))
setCeeJsonKey := p.CfgStringOr("cee-json-key", "")
annotateDockerMeta := p.CfgBoolOr("annotate-docker-meta", false)
setEngineNameToHost := p.CfgBoolOr("engine-name-to-host", false)
ignoreContainerEvents := p.CfgBoolOr("ignore-container-events", true)
dc := p.QChan.Data.Join()
for {
select {
case val := <-dc.Read:
switch val.(type) {
case qtypes_messages.Message:
qm := val.(qtypes_messages.Message)
p.Log("trace", "received qtypes_messages.Message")
if qm.StopProcessing(p.Plugin, false) {
continue
}
isCee := false
if setCeeJsonKey != "" {
if _, ok := qm.Tags[setCeeJsonKey]; !ok {
p.Log("error", fmt.Sprintf("could not find 'setSyslogMsgKey' '%s' in kv", setCeeJsonKey))
} else {
// marshall JSON
isCee = true
qm.Tags[qtypes_syslog.KEY_MSG] = qm.Tags[setCeeJsonKey]
p.Log("debug", fmt.Sprintf("Overwrite KY_MSG '%s' with '%s'", qtypes_syslog.KEY_MSG, setCeeJsonKey))
}
}
sl, err := qtypes_syslog.NewSyslogFromKV(qm.Tags)
if err != nil {
p.Log("error", fmt.Sprintf("error parsing Tags to syslog5424 struct: %s", err.Error()))
}
if isCee {
p.Log("debug", "Enable CEE prefix to message")
sl.EnableCEE()
}
sm := qtypes_messages.NewSyslogMessage(qm.Base, sl)
p.QChan.SendData(sm)
case qtypes_messages.ContainerMessage:
qm := val.(qtypes_messages.ContainerMessage)
p.Log("trace", "received qtypes_messages.ContainerMessage")
if qm.StopProcessing(p.Plugin, false) {
continue
}
isCee := false
if setCeeJsonKey != "" {
if _, ok := qm.Tags[setCeeJsonKey]; !ok {
p.Log("error", fmt.Sprintf("could not find 'setSyslogMsgKey' '%s' in kv", setCeeJsonKey))
} else {
isCee = true
ms := mapset.NewSet(setCeeJsonKey)
newKv := qm.Message.ParseJsonMap(p.Plugin, ms, qm.Tags)
p.Lock()
for k,v := range newKv {
if oldV, ok := qm.Tags[k]; !ok {
qm.Tags[k] = v
} else {
p.Log("debug", fmt.Sprintf("Won't overwrite tag '%s=%s' with val '%s'", k, oldV, v))
}
}
msg := qm.Tags[setCeeJsonKey]
// Annotate container/engine information to the JSON string
if annotateDockerMeta {
annoKv := map[string]string{
"engine_name": qm.Engine.Name,
"container_id": qm.Container.ID,
"container_name": qm.Container.Name,
}
msg, _ = AnnotateMsg(msg, annoKv)
p.Log("debug", fmt.Sprintf("Rewrote JSON string to '%s', marshaled from '%v'", msg, newKv))
}
p.Unlock()
qm.Tags[qtypes_syslog.KEY_MSG] = msg
p.Log("debug", fmt.Sprintf("Overwrite KY_MSG '%s' with '%s'", qtypes_syslog.KEY_MSG, setCeeJsonKey))
}
}
sl, err := qtypes_syslog.NewSyslogFromKV(qm.Tags)
if setEngineNameToHost {
sl.Host = qm.Engine.Name
}
if err != nil {
p.Log("error", fmt.Sprintf("error parsing Tags to syslog5424 struct: %s", err.Error()))
}
if isCee {
p.Log("debug", "Enable CEE prefix to message")
sl.EnableCEE()
}
sm := qtypes_messages.NewSyslogMessage(qm.Base, sl)
p.QChan.SendData(sm)
default:
if ignoreContainerEvents {
continue
}
p.Log("trace", fmt.Sprintf("Dunno how to handle type: %s", reflect.TypeOf(val)))
}
}
}
}
// AnnotateMsg assumes the msg is a JSON string and annotates key/value pairs to the string.
func AnnotateMsg(msg string, kv map[string]string) (res string, err error) {
res = msg[:len(msg)-1]
for k,v := range kv {
res = fmt.Sprintf(`%s,"%s":"%s"`, res, k,v)
}
return res+"}",err
}