forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
output_file.go
206 lines (169 loc) · 4.77 KB
/
output_file.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
package main
import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
)
const RotatorMaxFiles = 1000
type FileOutputType struct {
OutputInterface
rotator FileRotator
}
type FileRotator struct {
Path string
Name string
RotateEveryBytes uint64
KeepFiles int
current *os.File
current_size uint64
}
var FileOutput FileOutputType
func (out *FileOutputType) Init(config tomlMothership) error {
out.rotator.Path = config.Path
out.rotator.Name = config.Filename
out.rotator.RotateEveryBytes = uint64(config.Rotate_every_kb) * 1024
if out.rotator.RotateEveryBytes == 0 {
out.rotator.RotateEveryBytes = 10 * 1024
}
out.rotator.KeepFiles = config.Number_of_files
if out.rotator.KeepFiles == 0 {
out.rotator.KeepFiles = 7
}
err := out.rotator.CreateDirectory()
if err != nil {
return err
}
err = out.rotator.CheckIfConfigSane()
if err != nil {
return err
}
return nil
}
func (out *FileOutputType) PublishIPs(name string, localAddrs []string) error {
// not supported by this output type
return nil
}
func (out *FileOutputType) UpdateLocalTopologyMap() {
// not supported by this output type
}
func (out *FileOutputType) PublishEvent(event *Event) error {
json_event, err := json.Marshal(event)
if err != nil {
ERR("Fail to convert the event to JSON: %s", err)
return err
}
err = out.rotator.WriteLine(json_event)
if err != nil {
return err
}
return nil
}
func (rotator *FileRotator) CreateDirectory() error {
fileinfo, err := os.Stat(rotator.Path)
if err == nil {
if !fileinfo.IsDir() {
return MsgError("%s exists but it's not a directory", rotator.Path)
}
}
if os.IsNotExist(err) {
err = os.MkdirAll(rotator.Path, 0755)
if err != nil {
return err
}
}
return nil
}
func (rotator *FileRotator) CheckIfConfigSane() error {
if rotator.KeepFiles < 2 || rotator.KeepFiles >= RotatorMaxFiles {
return MsgError("The number of files to keep should be between 2 and %d", RotatorMaxFiles-1)
}
return nil
}
func (rotator *FileRotator) WriteLine(line []byte) error {
if rotator.shouldRotate() {
err := rotator.Rotate()
if err != nil {
return err
}
}
_, err := rotator.current.Write(line)
if err != nil {
return err
}
_, err = rotator.current.Write([]byte("\n"))
if err != nil {
return err
}
rotator.current_size += uint64(len(line) + 1)
return nil
}
func (rotator *FileRotator) shouldRotate() bool {
if rotator.current == nil {
return true
}
if rotator.current_size >= rotator.RotateEveryBytes {
return true
}
return false
}
func (rotator *FileRotator) FilePath(file_no int) string {
if file_no == 0 {
return filepath.Join(rotator.Path, rotator.Name)
}
filename := strings.Join([]string{rotator.Name, strconv.Itoa(file_no)}, ".")
return filepath.Join(rotator.Path, filename)
}
func (rotator *FileRotator) FileExists(file_no int) bool {
file_path := rotator.FilePath(file_no)
_, err := os.Stat(file_path)
if os.IsNotExist(err) {
return false
}
return true
}
func (rotator *FileRotator) Rotate() error {
if rotator.current != nil {
if err := rotator.current.Close(); err != nil {
return err
}
}
// delete any extra files, normally we shouldn't have any
for file_no := rotator.KeepFiles; file_no < RotatorMaxFiles; file_no++ {
if rotator.FileExists(file_no) {
perr := os.Remove(rotator.FilePath(file_no))
if perr != nil {
return perr
}
}
}
// shift all files from last to first
for file_no := rotator.KeepFiles - 1; file_no >= 0; file_no-- {
if !rotator.FileExists(file_no) {
// file doesn't exist, don't rotate
continue
}
file_path := rotator.FilePath(file_no)
if rotator.FileExists(file_no + 1) {
// next file exists, something is strange
return MsgError("File %s exists, when rotating would overwrite it", rotator.FilePath(file_no+1))
}
err := os.Rename(file_path, rotator.FilePath(file_no+1))
if err != nil {
return err
}
}
// create the new file
file_path := rotator.FilePath(0)
current, err := os.Create(file_path)
if err != nil {
return err
}
rotator.current = current
rotator.current_size = 0
// delete the extra file, ignore errors here
file_path = rotator.FilePath(rotator.KeepFiles)
os.Remove(file_path)
return nil
}