/
log.go
366 lines (333 loc) · 10.2 KB
/
log.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package cmd
import (
"syscall"
"crypto/tls"
"strconv"
"path/filepath"
"io"
"strings"
"bytes"
"io/ioutil"
"net/http"
"time"
"regexp"
"os"
"log"
"fmt"
"github.com/nxadm/tail"
"github.com/json-iterator/go"
)
// type TailConfig tail.Config
//TailLogConfig -
type TailLogConfig struct {
LogFile
SeekOffset int64
TailConfig tail.Config
}
//TailOneGlob -
func TailOneGlob(cfg *TailLogConfig, globPtn string, c chan os.Signal) {
filesPath, e := filepath.Glob(globPtn)
if e != nil {
log.Printf("ERROR parsing glob pattern - %v\n", e)
return
}
if len(filesPath) == 0 {
log.Printf("INFO parsing glob pattern return no files\n")
return
}
for _, _logFile := range(filesPath) {
go TailOnePath(cfg, _logFile, c)
}
}
//TailOnePath -
func TailOnePath(cfg *TailLogConfig, logFile string, c chan os.Signal) {
log.Printf("Start tailing %s\n", logFile)
// offset − This is the position of the read/write pointer within the file.
// whence − This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.
// seek := &tail.SeekInfo{Offset: 0, Whence: 0}
// cfg.TailConfig.Location = seek
previousPos := LoadTailPosition(logFile, cfg)
seek := &tail.SeekInfo{Offset: previousPos, Whence: 0}
cfg.TailConfig.Location = seek
t, e := tail.TailFile(logFile, cfg.TailConfig)
if e != nil {
log.Fatalf("Can not tail file - %v\n", e)
}
if cfg.TailConfig.Follow {
go ProcessTailLines(cfg, t)
s := <-c
log.Printf("TailOnePath - Signal captured. Do cleaning up\n")
SaveTailPosition(t, cfg)
t.Stop()
t.Cleanup()
c<- s
} else {
ProcessTailLines(cfg, t)
c<- syscall.SIGQUIT
}
}
//TestTailLog -
func TestTailLog(cfg tail.Config, logFile string) {
log.Printf("Start test tailing %s\n", logFile)
t, e := tail.TailFile(logFile, cfg)
if e != nil {
log.Fatalf("Can not tail file - %v\n", e)
}
for line := range t.Lines {
fmt.Printf("%s\n", line.Text)
}
}
//TailLog -
func TailLog(cfg *TailLogConfig, c chan os.Signal){
for _, logFile := range(cfg.Paths) {
go TailOneGlob(cfg, logFile, c)
}
}
//SaveTailPosition -
func SaveTailPosition(t *tail.Tail, cfg *TailLogConfig) {
pos, e := t.Tell()
if e != nil {
log.Printf("WARN - Can not tell from tail where are we - %v\n", e)
} else {
filename := filepath.Join(os.Getenv("HOME"), "taillog-" + CreateHash(cfg.Name + t.Filename))
_pos := strconv.FormatInt(pos, 10)
if e = ioutil.WriteFile(filename, []byte(_pos), 0750); e != nil {
log.Printf("WARN - Can not save pos to %s - %v\n",filename ,e)
}
}
}
//LoadTailPosition -
func LoadTailPosition(tailFileName string, cfg *TailLogConfig) (int64) {
filename := filepath.Join(os.Getenv("HOME"), "taillog-" + CreateHash(cfg.Name + tailFileName))
data, e := ioutil.ReadFile(filename)
if e != nil {
log.Printf("WARN - Can not read previous pos. Will set seek to 0 - %s\n", e)
// os.Remove(filename)
return 0
}
out, e := strconv.ParseInt(string(data), 10, 64)
if e != nil {
log.Printf("WARN - Can not parse int previous pos. Will set seek to 0 - %s\n", e)
return 0
}
log.Printf("INFO - Loaded previous file pos %d from %s. To set from begining remove the file\n", out, filename)
return out
}
//IsEOF - NOt sure why tail does not provide this test.
func IsEOF(filename string, seek int64) (bool) {
fh, e := os.Open(filename)
defer fh.Close()
if e != nil {
fmt.Printf("ERROR can not open file - %v\n", e)
}
buff := make([]byte, 1)
fh.Seek(seek, 0)
_, e = fh.Read(buff)
if e == io.EOF {
// fmt.Printf("EOF reached\n")
return true
}
return false
}
//FilterPassword -
func FilterPassword(text string, passPtns []*regexp.Regexp) (string) {
out := text
for _, passPtn := range(passPtns) {
out = passPtn.ReplaceAllString(out, "$1 DATA_FILTERED ")
}
return out
}
//SendLine -
func SendLine(timeHarvest, timeParsed time.Time, hostStr, appNameStr, logFile, msgStr string) (bool) {
IsOK := true
// message := FilterPassword(msgStr, PasswordFilterPtns)
// message = DecodeJenkinsConsoleNote(message)
logData := LogData{
Timestamp: timeHarvest.UnixNano(),
Datelog: timeParsed.UnixNano(),
Host: hostStr,
Application: appNameStr,
Logfile: logFile,
Message: msgStr,
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
output, e := json.Marshal(&logData)
if e != nil {
log.Printf("ERROR - can not marshal json to output - %v\n", e)
IsOK = false
}
client := &http.Client{}
if strings.HasPrefix(Config.Serverurl, "https://") {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: Config.IgnoreCertificateCheck}
}
validToken, err := GenerateJWT()
if err != nil {
log.Printf("ERROR - Failed to generate token - %v\n", err)
IsOK = false
}
req, _ := http.NewRequest("POST", strings.Join([]string{Config.Serverurl, "log"}, "/"), bytes.NewBuffer(output))
req.Header.Set("Token", validToken)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Printf("ERROR - %v\n", err)
IsOK = false
}
if IsOK {defer res.Body.Close()}
if IsOK {
_, err = ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
IsOK = false
}
}
return IsOK
}
//ParseTimeAdjust - take a timeadjust and parse its meaning. Use the go time formatting eg. if it hits '2006' then it will replace it with current year. Similar.
// If string is not any of the go date layout then it is pass though as is.
// There are 5 tokens: Jan 02 15:04:05 2006 MST that we support
func ParseTimeAdjust(adjustStr string) string {
timeLayoutToken := []string{"Jan ", "02 ", "15:04:05 ", "2006 ", "MST "}
now := time.Now()
out := adjustStr
for _, tk := range(timeLayoutToken) {
if strings.Contains(adjustStr, tk) {
tkTrim := strings.TrimSpace(tk)
out = strings.ReplaceAll(out, tkTrim, now.Format(tkTrim))
}
}
return strings.TrimSpace(out)
}
//ProcessTailLines -
func ProcessTailLines(cfg *TailLogConfig, tail *tail.Tail) {
tailLines := tail.Lines
var timePtn, linePtn, multiLinePtn *regexp.Regexp
linePtnStr := strings.Join([]string{cfg.Timepattern, cfg.Pattern}, "" )
linePtn = regexp.MustCompile(linePtnStr)
multiLinePtn = regexp.MustCompile(cfg.Multilineptn)
var excludePtns, includePatterns []*regexp.Regexp
for _, ptn := range(cfg.Excludepatterns) {
if ptn != ""{
excludePtns = append(excludePtns, regexp.MustCompile(ptn))
}
}
for _, ptn := range(cfg.Includepatterns) {
if ptn != ""{
includePatterns = append(includePatterns, regexp.MustCompile(ptn))
}
}
if cfg.Timepattern != "" {
timePtn = regexp.MustCompile(cfg.Timepattern)
log.Printf("INFO - time ptn: '%s'\nline ptn: '%s'\n", cfg.Timepattern, linePtnStr)
}
timeLayout := cfg.Timelayout
var timeParsed time.Time
var e error
var hostStr, appNameStr string
if cfg.Appname != "" {
appNameStr = cfg.Appname
} else {
appNameStr = "-"
}
lineStack := []string{}
beginLineMatch := false
for line := range tailLines {
if line.Text == "" || line.Text == "\n" { continue }
mustInclude, excludeLine := false, false
for _, iPtn := range(includePatterns) {
mustInclude = iPtn.MatchString(line.Text)
if mustInclude { break }
}
if !mustInclude {
for _, iPtn := range(excludePtns) {
excludeLine = iPtn.MatchString(line.Text)
if excludeLine { break }
}
}
if excludeLine { continue }
// fmt.Printf("Processing LineText: '%s'\n", line.Text)
curSeek, _ := tail.Tell()
if IsEOF(tail.Filename, curSeek) {
msgStr := strings.Join(lineStack, "\n")
lineStack = lineStack[:0]
// log.Printf("EOF reached. Flush stack\n")
for {
if SendLine(line.Time, timeParsed, hostStr, appNameStr, tail.Filename, msgStr) { break }
time.Sleep(15 * time.Second)
}
}
match := []string{"notimeptn"}
if timePtn != nil {
match = timePtn.FindStringSubmatch(line.Text)
}
if len(match) > 0 {
beginLineMatch = true
if len(lineStack) > 0 {//Flush the multiline stack
msgStr := strings.Join(lineStack, "\n")
lineStack = lineStack[:0]
for {
if SendLine(line.Time, timeParsed, hostStr, appNameStr, tail.Filename, msgStr) {break}
time.Sleep(15 * time.Second)
}
}
if match[0] != "notimeptn" {
timeadjust := ParseTimeAdjust(cfg.Timeadjust)
timeStr := strings.Join([]string{match[1], timeadjust}, " ")
timeStr = strings.Replace(timeStr, " ", " 0", -1)
if len(cfg.Timestrreplace) == 2 {
timeStr = strings.Replace(timeStr, cfg.Timestrreplace[0], cfg.Timestrreplace[1], -1)
}
timeParsed, e = time.Parse(timeLayout, timeStr)
if e != nil {
log.Fatalf("ERROR Fail to parse time %v\n", e)
}
} else {
timeParsed = line.Time
}
match1 := linePtn.FindStringSubmatch(line.Text)
matchCount := len(match1)
if matchCount > 0 {
var msgStr string
switch matchCount {
case 2: //no timePtn
curHostname, _ := os.Hostname()
hostStr, msgStr = curHostname, match1[1]
case 3://For simple type we only support up to two matches, to parse the
if match[0] == "notimeptn" {
hostStr, msgStr = match1[1], match1[2]
} else {
curHostname, _ := os.Hostname()
hostStr, msgStr = curHostname, match1[2]
}
case 4:
hostStr, msgStr = match1[2], match1[3]
case 5:
hostStr, appNameStr, msgStr = match1[2], match1[3], match1[4]
}
if len(lineStack) == 0 {
lineStack = append(lineStack, msgStr)
}
} else {
log.Printf("WARN - The pattern does not parse correct components.\nLinePtn: '%s'.\nLine: '%s'\n", linePtnStr, line.Text)
beginLineMatch = false
}
} else {
if beginLineMatch {
mMatch := multiLinePtn.FindStringSubmatch(line.Text)
if len(mMatch) > 0 {
if len(lineStack) > 0 {
// fmt.Printf("Part of multiLine Text: '%s'\n", line.Text)
lineStack = append(lineStack, mMatch[1])
}
} else {
beginLineMatch = false
log.Printf("WARN - Can not parse multiline pattern. Previous time matched. Check your multiline pattern regex.\n")
fmt.Printf("WARN - Line Text: '%s'\nPattern: %s\n", line.Text, cfg.Timepattern)
}
} else {
log.Printf("WARN - Can not parse time pattern. No previous time matched. Is it raw log without time pattern? \n")
fmt.Printf("WARN - Line Text: '%s'\nPattern: %s\n", line.Text, cfg.Timepattern)
}
}
}
}