/
logconsumer.go
162 lines (129 loc) · 4.49 KB
/
logconsumer.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
package cloudflare
import (
"bufio"
"compress/gzip"
"os"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/pquerna/ffjson/ffjson"
)
type LogConsumer struct {
TotalLogFileSegments int
cloudflareClient *CloudflareClient
LogFilesReady chan string
EventsReady chan common.MapStr
CompletedNotifier chan bool
ProcessorTerminateSig chan bool
WaitGroup sync.WaitGroup
}
// NewLogConsumer reutrns a instance of the LogConsumer struct
func NewLogConsumer(cfEmail string, cfAPIKey string, numSegments int, eventBufferSize int, processors int) *LogConsumer {
lc := &LogConsumer{
TotalLogFileSegments: numSegments,
LogFilesReady: make(chan string, numSegments*10),
EventsReady: make(chan common.MapStr, eventBufferSize),
CompletedNotifier: make(chan bool, 1),
ProcessorTerminateSig: make(chan bool, processors),
WaitGroup: sync.WaitGroup{},
}
lc.cloudflareClient = NewClient(map[string]interface{}{
"api_key": cfAPIKey,
"email": cfEmail,
"debug": false,
})
return lc
}
// DownloadCurrentLogFiles downloads the log file segments from the Cloudflare ELS API
func (lc *LogConsumer) DownloadCurrentLogFiles(zoneTag string, timeStart int, timeEnd int) {
segmentSize := (timeEnd - timeStart) / lc.TotalLogFileSegments
currTimeStart := timeStart
currTimeEnd := currTimeStart + segmentSize
var timeNow int
lc.WaitGroup.Add(lc.TotalLogFileSegments)
for i := 0; i < lc.TotalLogFileSegments; i++ {
go func(lc *LogConsumer, segmentNum int, currTimeStart int, currTimeEnd int) {
timeNow = int(time.Now().UTC().Unix())
logp.Info("Downloading log segment #%d from %d to %d", segmentNum, currTimeStart, currTimeEnd)
//logp.Info("Downloading log segment #%d from %d to %d", segmentNum, currTimeStart, currTimeEnd)
filename, err := lc.cloudflareClient.GetLogRangeFromTimestamp(map[string]interface{}{
"zone_tag": zoneTag,
"time_start": currTimeStart,
"time_end": currTimeEnd,
})
if err != nil {
logp.Err("Could not download logs from CF: %v", err)
return
}
lc.LogFilesReady <- filename
logp.Info("Total download time for log file: %d seconds", (int(time.Now().UTC().Unix()) - timeNow))
//logp.Info("Total download time for log file: %d seconds", (int(time.Now().UTC().Unix()) - timeNow))
}(lc, i, currTimeStart, currTimeEnd)
currTimeStart = currTimeEnd + 1
currTimeEnd = currTimeStart + segmentSize
runtime.Gosched()
}
}
func (lc *LogConsumer) PrepareEvents() {
var l map[string]interface{}
var evt common.MapStr
var logItem []byte
completedProcessingNotifer := make(chan bool, 1)
// goroutine that will send notification to the goroutine publishing the events to say it's done all the files
go func() {
lc.WaitGroup.Wait()
lc.CompletedNotifier <- true
completedProcessingNotifer <- true
close(completedProcessingNotifer)
}()
for {
select {
case <-completedProcessingNotifer:
logp.Info("Done preparing events for publishing. Returning from goroutine.")
return
case logFileName := <-lc.LogFilesReady:
logp.Info("Log file %s ready for processing.", logFileName)
fh, err := os.Open(logFileName)
if err != nil {
logp.Err("Could not open gziped file for reading: %v", err)
DeleteLogLife(logFileName)
lc.WaitGroup.Done()
continue
}
/* Now we need to read the content form the file, split line by line and itterate over them */
logp.Info("Opening gziped file %s for reading...", logFileName)
gz, err := gzip.NewReader(fh)
if err != nil {
logp.Err("Could not open file for reading: %v", err)
DeleteLogLife(logFileName)
lc.WaitGroup.Done()
continue
}
timePreIndex := int(time.Now().UTC().Unix())
scanner := bufio.NewScanner(gz)
for scanner.Scan() {
logItem = scanner.Bytes()
err := ffjson.Unmarshal(logItem, &l)
if err == nil {
evt = BuildMapStr(l)
evt["@timestamp"] = common.Time(time.Unix(0, int64(l["timestamp"].(float64))))
evt["type"] = "cloudflare"
evt["cfbeat_log_file"] = filepath.Base(logFileName)
lc.EventsReady <- evt
} else {
logp.Err("Could not load JSON: %s", err)
}
}
logp.Info("Total processing time: %d seconds", (int(time.Now().UTC().Unix()) - timePreIndex))
// Now close the related handles and delete the log file
gz.Close()
fh.Close()
DeleteLogLife(logFileName)
lc.WaitGroup.Done()
runtime.Gosched()
} // END select
} // End for loop
}