-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.go
237 lines (184 loc) · 7.54 KB
/
preprocessor.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"fmt"
_ "log"
_ "os"
"regexp"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
type Preprocessor struct {
Orchestra Orchestrator
DbWorker SqliteWorker
Receiver SyslogListener
Sender SyslogSender
listenerQueue chan format.LogParts
inEngineQueue chan Mapping
outEngineQueue chan string
waitForTrigger bool
sendByInterval bool
}
var ComLogger *RotatingWriter
func (pp *Preprocessor) New(configFile string) {
// Initiates Orchestrator
pp.Orchestra = Orchestrator{}
pp.Orchestra.New(configFile)
// Initiates Logger
ComLogger = &RotatingWriter{}
ComLogger.New(pp.Orchestra.Config.AppLogger)
ComLogger.Write("D", fmt.Sprintf("Current Config: %#v", pp.Orchestra.Config) )
// Setting up channels & queues
pp.listenerQueue = make(chan format.LogParts, pp.Orchestra.Config.Receiver.QUEUESIZE)
pp.inEngineQueue = make(chan Mapping, pp.Orchestra.Config.Parser.QUEUESIZE * 2)
pp.outEngineQueue = make(chan string, pp.Orchestra.Config.Emitter.QUEUESIZE * 2)
pp.waitForTrigger = false
pp.sendByInterval = false
var dmpIval int = 0
if pp.Orchestra.Config.DbWorker.REGULARDUMPINGSEC > 0 {
pp.sendByInterval = true
dmpIval = pp.Orchestra.Config.DbWorker.REGULARDUMPINGSEC
} else if len(pp.Orchestra.Config.Parser.Mapping.MsgType.TRIGGER) > 0 {
pp.waitForTrigger = true
}
// SQLite Worker to unload intermediary queue (parsed items)
pp.DbWorker = SqliteWorker{}
pp.DbWorker.New(pp.Orchestra.Config.DbWorker.FILENAME, pp.Orchestra.Config.Parser.Mapping.MsgData, pp.Orchestra.Config.Parser.Mapping.MSGKEY, pp.Orchestra.Config.Parser.Mapping.MSGDATAENRICH, pp.sendByInterval, pp.waitForTrigger, pp.Orchestra.Config.DbWorker.GARBAGETIMEOUTMIN, dmpIval)
// Define listening thread for outgoing messages
pp.Sender = SyslogSender{}
pp.Sender.New( pp.Orchestra.Config.AppLogger.APPNAME, pp.Orchestra.Config.Emitter.BINDADDRESS, pp.Orchestra.Config.Emitter.PORT, pp.Orchestra.Config.Emitter.TYPE, pp.Orchestra.Config.Emitter.SYSLOGFORMAT )
// Define blocking thread for incoming messages
pp.Receiver = SyslogListener{}
pp.Receiver.New( pp.Orchestra.Config.Receiver.BINDADDRESS, pp.Orchestra.Config.Receiver.PORT, pp.Orchestra.Config.Receiver.TYPE, pp.Orchestra.Config.Receiver.SYSLOGFORMAT)
// Cleaning and monitor memory footprint, logging, etc
}
func (pp *Preprocessor) Run() {
// Deal with the queue now that it's filling up (parsing and extraction of valuable data)
go pp.Parser()
// Deal with the queue now that it's filling up (logs to emit)
go pp.Sender.Run(pp.outEngineQueue)
// SQLite Worker to unload intermediary queue (parsed items)
go pp.DbWorker.Run(pp.inEngineQueue, pp.outEngineQueue)
// Timeout or log completed: loads on output Queue for emission
// Testing
//testUdpClient := Udpclienttest{}
//testUdpClient.New( pp.Orchestra.Config.Receiver.BINDADDRESS, pp.Orchestra.Config.Receiver.PORT, pp.Orchestra.Config.Receiver.TYPE, pp.Orchestra.Config.Receiver.SYSLOGFORMAT)
//go testUdpClient.Run()
// Start blocking thread for incoming messages
pp.Receiver.Run( pp.listenerQueue )
// Cleaning and monitor memory footprint, logging, etc
pp.DbWorker.Quit()
}
func (pp *Preprocessor) Parser() {
for {
select {
case line := <-pp.listenerQueue:
msgMapped := Mapping{}
message := ""
if pp.Orchestra.Config.Receiver.SYSLOGFORMAT == "RFC3164" {
message = line["content"].(string)
} else if pp.Orchestra.Config.Receiver.SYSLOGFORMAT == "RFC5424" {
message = line["message"].(string)
} else {
break
}
if pp.CheckBlacklistForMsg( message ) == false {
msgType := pp.GetTypeFromMsg( message )
msgMapped.MSGMERGE = pp.GetIdFromMsg( message )
msgMapped.MsgMetadata.Hostname = pp.GetHostnameFromMsg(line, message)
msgMapped.MsgData = pp.GetDataFromMsg( message )
msgMapped.MSGRAW = message
if len(msgType) > 0 {
// Trigger add, push, pull or purge DB entry for MSG IDmsgMapped.MsgType
if msgType == "START" {
msgMapped.MsgType.START = msgType
} else if msgType == "TRIGGER" {
msgMapped.MsgType.TRIGGER = msgType
} else if msgType == "STOP" {
msgMapped.MsgType.STOP = msgType
}
}
pp.inEngineQueue <- msgMapped
} else {
continue
}
}
}
fmt.Println("Parser quit...")
}
func (pp *Preprocessor) GetTypeFromMsg(msg string) string {
retType := ""
re := regexp.MustCompile(pp.Orchestra.Config.Parser.Mapping.MsgType.START)
if len(re.Find([]byte(msg))) > 0 {
retType = "START"
}
if len(pp.Orchestra.Config.Parser.Mapping.MsgType.STOP) > 0 {
re = regexp.MustCompile(pp.Orchestra.Config.Parser.Mapping.MsgType.STOP)
if len(re.Find([]byte(msg))) > 0 {
retType = "STOP"
}
}
if len(pp.Orchestra.Config.Parser.Mapping.MsgType.TRIGGER) > 0 {
re = regexp.MustCompile(pp.Orchestra.Config.Parser.Mapping.MsgType.TRIGGER)
if len(re.Find([]byte(msg))) > 0 {
retType = "TRIGGER"
}
}
return retType
}
func (pp *Preprocessor) GetIdFromMsg(msg string) string {
retId := ""
re := regexp.MustCompile(pp.Orchestra.Config.Parser.Mapping.MSGMERGE)
if len(re.Find([]byte(msg))) > 0 {
find := re.FindAllStringSubmatch(msg, -1)
if len(find[0]) > 1 {
retId = find[0][1]
}
}
return retId
}
func (pp *Preprocessor) GetDataFromMsg(msg string) map[string]string {
refData := pp.Orchestra.Config.Parser.Mapping.MsgData
retData := MsgData{}
for key, value := range refData {
re := regexp.MustCompile(value)
if len(re.Find([]byte(msg))) > 0 {
find := re.FindAllStringSubmatch(msg, -1)
if len(find[0]) > 1 {
retData[key] = find[0][1]
} else {
retData[key] = ""
}
}
}
return retData
}
func (pp *Preprocessor) GetHostnameFromMsg(line format.LogParts, msg string) string {
retVal := ""
if len(pp.Orchestra.Config.Parser.Mapping.MsgMetadata.Hostname) > 0 {
re := regexp.MustCompile(pp.Orchestra.Config.Parser.Mapping.MsgMetadata.Hostname)
if len(re.Find([]byte(msg))) > 0 {
find := re.FindAllStringSubmatch(msg, -1)
if len(find[0]) > 1 {
retVal = find[0][1]
} else {
retVal = line["hostname"].(string)
}
} else {
retVal = line["hostname"].(string)
}
} else {
retVal = line["hostname"].(string)
}
return retVal
}
func (pp *Preprocessor) CheckBlacklistForMsg(msg string) bool {
refData := pp.Orchestra.Config.Parser.Mapping.MsgBlacklist
retData := false
for _, value := range refData {
re := regexp.MustCompile(value)
if len(re.Find([]byte(msg))) > 0 {
retData = true
break
}
}
return retData
}