forked from packetzoom/logzoom
/
s3.go
290 lines (228 loc) · 6.52 KB
/
s3.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
package s3
import (
"compress/gzip"
"crypto/rand"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/packetzoom/logzoom/buffer"
"github.com/packetzoom/logzoom/output"
"github.com/jehiah/go-strftime"
"github.com/paulbellamy/ratecounter"
"gopkg.in/yaml.v2"
)
const (
s3FlushInterval = 10
recvBuffer = 100
maxSimultaneousUploads = 8
)
func uuid() string {
b := make([]byte, 16)
rand.Read(b)
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
type Config struct {
AwsKeyId string `yaml:"aws_key_id"`
AwsSecKey string `yaml:"aws_sec_key"`
AwsS3Bucket string `yaml:"aws_s3_bucket"`
AwsS3Region string `yaml:"aws_s3_region"`
LocalPath string `yaml:"local_path"`
Path string `yaml:"s3_path"`
TimeSliceFormat string `yaml:"time_slice_format"`
AwsS3OutputKey string `yaml:"aws_s3_output_key"`
}
type OutputFileInfo struct {
Filename string
Count int
}
type FileSaver struct {
Config Config
Writer *gzip.Writer
FileInfo OutputFileInfo
RateCounter *ratecounter.RateCounter
}
func (fileSaver *FileSaver) WriteToFile(event *buffer.Event) error {
if fileSaver.Writer == nil {
log.Println("Creating new S3 gzip writer")
file, err := ioutil.TempFile(fileSaver.Config.LocalPath, "s3_output_")
if err != nil {
log.Printf("Error creating temporary file:", err)
}
fileSaver.Writer = gzip.NewWriter(file)
fileSaver.FileInfo.Filename = file.Name()
fileSaver.FileInfo.Count = 0
}
text := *event.Text
_, err := fileSaver.Writer.Write([]byte(text))
if err != nil {
log.Println("Error writing:", err)
return err
}
_, err = fileSaver.Writer.Write([]byte("\n"))
if err != nil {
log.Println("Error writing:", err)
return err
}
fileSaver.FileInfo.Count += 1
fileSaver.RateCounter.Incr(1)
return nil
}
func (s3Writer *S3Writer) doUpload(fileInfo OutputFileInfo) error {
log.Printf("Opening file %s\n", fileInfo.Filename)
reader, err := os.Open(fileInfo.Filename)
if err != nil {
log.Printf("Failed to open file:", err)
return err
}
curTime := time.Now()
hostname, _ := os.Hostname()
timeKey := strftime.Format(s3Writer.Config.TimeSliceFormat, curTime)
valuesForKey := map[string]string{
"path": s3Writer.Config.Path,
"timeSlice": timeKey,
"hostname": hostname,
"uuid": uuid(),
}
destFile := s3Writer.Config.AwsS3OutputKey
for key, value := range valuesForKey {
expr := "%{" + key + "}"
destFile = strings.Replace(destFile, expr, value, -1)
}
result, s3Error := s3Writer.S3Uploader.Upload(&s3manager.UploadInput{
Body: reader,
Bucket: aws.String(s3Writer.Config.AwsS3Bucket),
Key: aws.String(destFile),
ContentEncoding: aws.String("gzip"),
})
log.Printf("%d events written to S3 %s", fileInfo.Count, result.Location)
if s3Error == nil {
os.Remove(fileInfo.Filename)
} else {
log.Printf("Error uploading to S3", s3Error)
}
return s3Error
}
func (s3Writer *S3Writer) WaitForUpload() {
for {
select {
case fileInfo := <-s3Writer.uploadChannel:
s3Writer.doUpload(fileInfo)
}
}
}
func (s3Writer *S3Writer) InitiateUploadToS3(fileSaver *FileSaver) {
if fileSaver.Writer == nil {
return
}
log.Printf("Upload to S3, current event rate: %d/s\n", fileSaver.RateCounter.Rate())
writer := fileSaver.Writer
fileInfo := fileSaver.FileInfo
fileSaver.Writer = nil
writer.Close()
s3Writer.uploadChannel <- fileInfo
}
type S3Writer struct {
Config Config
Sender buffer.Sender
S3Uploader *s3manager.Uploader
uploadChannel chan OutputFileInfo
term chan bool
}
func init() {
output.Register("s3", &S3Writer{
term: make(chan bool, 1),
})
}
func (s3Writer *S3Writer) ValidateConfig(config *Config) error {
if len(config.LocalPath) == 0 {
return errors.New("missing local path")
}
// Create the local path if necessary
if err := os.MkdirAll(config.LocalPath, 0700); err != nil {
return errors.New("could not mkdir " + config.LocalPath)
}
// Try writing to local path
if _, err := ioutil.TempFile(config.LocalPath, "logzoom"); err != nil {
return errors.New("unable to write to " + config.LocalPath)
}
if len(config.AwsS3Bucket) == 0 {
return errors.New("missing AWS S3 bucket")
}
if len(config.AwsS3Region) == 0 {
return errors.New("missing AWS S3 region")
}
if len(config.AwsS3OutputKey) == 0 {
return errors.New("missing AWS S3 output key")
}
return nil
}
func (s3Writer *S3Writer) Init(config yaml.MapSlice, sender buffer.Sender) error {
var s3Config *Config
// go-yaml doesn't have a great way to partially unmarshal YAML data
// See https://github.com/go-yaml/yaml/issues/13
yamlConfig, _ := yaml.Marshal(config)
if err := yaml.Unmarshal(yamlConfig, &s3Config); err != nil {
return fmt.Errorf("Error parsing S3 config: %v", err)
}
if err := s3Writer.ValidateConfig(s3Config); err != nil {
return fmt.Errorf("Error in config: %v", err)
}
s3Writer.uploadChannel = make(chan OutputFileInfo, maxSimultaneousUploads)
s3Writer.Config = *s3Config
s3Writer.Sender = sender
aws_access_key_id := s3Writer.Config.AwsKeyId
aws_secret_access_key := s3Writer.Config.AwsSecKey
token := ""
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
_, err := creds.Get()
if err != nil {
return err
}
session := session.New(&aws.Config{
Region: &s3Writer.Config.AwsS3Region,
Credentials: creds,
})
s3Writer.S3Uploader = s3manager.NewUploader(session)
log.Println("Done instantiating S3 uploader")
return nil
}
func (s3Writer *S3Writer) Start() error {
// Create file saver
fileSaver := new(FileSaver)
fileSaver.Config = s3Writer.Config
fileSaver.RateCounter = ratecounter.NewRateCounter(1 * time.Second)
id := "s3_output"
// Add the client as a subscriber
receiveChan := make(chan *buffer.Event, recvBuffer)
s3Writer.Sender.AddSubscriber(id, receiveChan)
defer s3Writer.Sender.DelSubscriber(id)
// Loop events and publish to S3
tick := time.NewTicker(time.Duration(s3FlushInterval) * time.Second)
go s3Writer.WaitForUpload()
for {
select {
case ev := <-receiveChan:
fileSaver.WriteToFile(ev)
case <-tick.C:
s3Writer.InitiateUploadToS3(fileSaver)
case <-s3Writer.term:
log.Println("S3Writer received term signal")
return nil
}
}
return nil
}
func (s *S3Writer) Stop() error {
s.term <- true
return nil
}