-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
348 lines (295 loc) · 8.47 KB
/
main.go
File metadata and controls
348 lines (295 loc) · 8.47 KB
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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"sync"
"sync/atomic"
"time"
//"log/syslog"
"github.com/BurntSushi/toml"
syslog "github.com/RackSec/srslog"
"github.com/asaskevich/govalidator"
"github.com/hpcloud/tail"
"github.com/juju/ratelimit"
)
//VERSION of log2syslog
const VERSION string = "0.9"
const file string = "file"
type eventIndex struct {
fileName string
length int
hash string
}
type config struct {
SyslogServer syslogServer `toml:"Syslog_server"`
Logsources logSources
General general
}
type syslogServer struct {
Host string `toml:"host"`
Port int `toml:"port"`
Protocol string `toml:"protocol"`
Format string `toml:"message_format"`
LogTag string `toml:"log_tag"`
}
type logSources struct {
LogFiles []string `toml:"log_files"`
inotity bool `toml:"inotify"`
}
type general struct {
Rate int `toml:"rate_limit"`
LogType string `toml:"log_type"`
LogFile string `toml:"log_file"`
MaxConcurrency int `toml:"max_concurrency"`
ReplaceControlChars bool `toml:"replace_control_characters"`
}
// Counters hold the stats structure used to increment counters
type Counters struct {
Read int64
Sent int64
}
// Global vars/Handlers
var conf config
var syslogHandler *syslog.Writer
var logFile *os.File
// Stats is channel used to receive stats from processing go routines
var Stats = make(chan Counters, 100)
var readLogCnt, sentLogCnt int64
// CounterStats read and publish stats from event insert
func CounterStats() {
for {
select {
case c := <-Stats:
if c.Read != 0 {
atomic.AddInt64(&readLogCnt, c.Read)
}
if c.Sent != 0 {
atomic.AddInt64(&sentLogCnt, c.Sent)
}
}
}
}
// PrintStats print the stats on defaul log writer each minute
func PrintStats() {
var glbRead, glbSent int64
var currentTime = time.Now()
var startTime = currentTime.Truncate(time.Minute).Add(time.Minute)
var duration = startTime.Sub(currentTime)
heartbeat := time.Tick(duration)
for {
select {
case <-heartbeat:
heartbeat = time.Tick(60 * time.Second)
// publish stats at a fixed interval
readPartial := atomic.LoadInt64(&readLogCnt)
atomic.StoreInt64(&readLogCnt, 0)
sentPartial := atomic.LoadInt64(&sentLogCnt)
atomic.StoreInt64(&sentLogCnt, 0)
glbRead = glbRead + readPartial
glbSent = glbSent + sentPartial
log.Printf("Readed: Total (from start): %d, Partial: %d/min (avr %d/sec)\n", glbRead, readPartial, readPartial/60)
log.Printf("Sent: Total (from start): %d, Partial: %d/min (avr %d/sec)\n", glbSent, sentPartial, sentPartial/60)
}
}
}
// stripCtlFromBytes convert
func replaceCtlandExtCharbySpace(str string) string {
b := make([]byte, len(str))
// fmt.Println("\nLen inicial: " + strconv.Itoa(len(str)))
var bl int
for i := 0; i < len(str); i++ {
c := str[i]
// fmt.Printf("Char: %d, ASCII: %s \n", c, string(c))
if c >= 32 && c <= 127 {
b[bl] = c
bl++
} else {
b[bl] = 32
bl++
}
}
// fmt.Println("\nLen Final: " + strconv.Itoa(len(b)))
return string(b[:bl])
}
func logRead(wg *sync.WaitGroup, logFile string, logLineChannel chan<- string, st chan<- Counters) {
defer wg.Done()
var seek tail.SeekInfo
// seek.Offset = fileInfo.Size()
followTail := true
t, err := tail.TailFile(logFile, tail.Config{
Follow: followTail,
ReOpen: followTail,
Location: &seek,
MustExist: true,
Poll: conf.Logsources.inotity,
Logger: tail.DiscardingLogger,
})
if err != nil {
log.Printf("Error on follow de index file: %s.\n", err)
}
var c Counters
for line := range t.Lines {
if conf.General.ReplaceControlChars {
logLineChannel <- replaceCtlandExtCharbySpace(line.Text)
} else {
logLineChannel <- line.Text
}
c.Read = 1
st <- c
}
}
func logPump(wg *sync.WaitGroup, logLineChannel <-chan string, st chan<- Counters, rate int) {
defer wg.Done()
var c Counters
mps := ratelimit.NewBucketWithRate(float64(rate), 1)
for v := range logLineChannel {
mps.Wait(1)
fmt.Fprint(syslogHandler, v)
c.Sent = 1
st <- c
}
}
func configInit(configFile string) error {
var errHappen bool
_, err := os.Stat(configFile)
if err != nil {
fmt.Printf(" Configuration file %s does not exist\n", configFile)
return err
}
_, err = toml.DecodeFile(configFile, &conf)
if err != nil {
fmt.Printf(" Check config file %s configuration and syntaxe, something wrong there!\n", configFile)
fmt.Printf(" Error: %s\n", err)
errHappen = true
}
// Validate config file
if !govalidator.StringLength(conf.SyslogServer.Host, "1", "120") || !govalidator.IsHost(conf.SyslogServer.Host) {
fmt.Printf(" Config Error: Syslog Host must be present, it must be an IP Address or hostname (%s).\n", conf.SyslogServer.Host)
errHappen = true
}
if !govalidator.InRangeInt(conf.SyslogServer.Port, 1, 65535) {
fmt.Printf(" Config Error: Syslog Port must be an integer from 1 to 65535 (%d).\n", conf.SyslogServer.Port)
errHappen = true
}
if !govalidator.InRangeInt(conf.General.Rate, 1, 1000000) {
fmt.Printf(" Config Error: Rate limit must be an integer from 1 to 1000000 (%d).\n", conf.General.Rate)
errHappen = true
}
if regexp.MustCompile(`^(udp|tcp)$`).MatchString(conf.SyslogServer.Protocol) == false {
fmt.Printf(" Config Error: Protocol must be udp or tcp (%s).\n", conf.SyslogServer.Protocol)
errHappen = true
}
for _, v := range conf.Logsources.LogFiles {
_, err = os.Stat(v)
if err != nil {
fmt.Printf(" Config Error: Log file %s does not exist, check it and try again.\n", v)
errHappen = true
}
}
if conf.General.LogType != file && conf.General.LogType != "syslog" && conf.General.LogType != "stdout" {
fmt.Printf(" Config Error: Log Type (log_type) must be either: 'file', 'syslog' or 'stdout'. \n The stdout is the default and will print the log on standard output - your screen,\n check config file (%s).\n", configFile)
errHappen = true
}
if conf.General.LogType == file {
_, err := os.Stat(conf.General.LogFile)
if err != nil {
err := ioutil.WriteFile(conf.General.LogFile, []byte("Start"), 0640)
if err != nil {
fmt.Printf(" Config Error: Impossible to write the log file %s, check file permission.\n", conf.General.LogFile)
errHappen = true
}
}
}
if errHappen {
return errors.New("Configuration errors, correct config file and try again")
}
return nil
}
func syslogInit() {
// init of syslog
var err error
tag := "log2syslog"
if len(conf.SyslogServer.LogTag) > 0 {
tag = conf.SyslogServer.LogTag
}
syslogHandler, err = syslog.Dial(conf.SyslogServer.Protocol, conf.SyslogServer.Host+":"+strconv.Itoa(conf.SyslogServer.Port), syslog.LOG_INFO|syslog.LOG_DAEMON, tag)
if err != nil {
log.Fatal(err)
}
switch conf.SyslogServer.Format {
case "none":
// no formater
log.SetOutput(syslogHandler)
case "RFC3164":
formater := syslog.Formatter(syslog.RFC3164Formatter)
syslogHandler.SetFormatter(formater)
log.SetOutput(syslogHandler)
case "RFC5424":
formater := syslog.Formatter(syslog.RFC5424Formatter)
syslogHandler.SetFormatter(formater)
log.SetOutput(syslogHandler)
default:
formater := syslog.Formatter(syslog.DefaultFormatter)
syslogHandler.SetFormatter(formater)
log.SetOutput(syslogHandler)
}
log.SetFlags(0)
//log.Println("Remote log initialized")
}
func localLogInit() {
if conf.General.LogType == file {
// init of log to file
var err error
logFile, err = os.OpenFile(conf.General.LogFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0660)
if err != nil {
fmt.Printf("Error opening file: %v", err)
}
log.SetOutput(logFile)
log.SetFlags(3)
} else if conf.General.LogType == "syslog" {
log.SetOutput(syslogHandler)
log.SetFlags(0)
} else {
log.SetOutput(os.Stdout)
}
log.Println("Log for log2syslog initialized")
}
func main() {
configFile := flag.String("f", "./log2syslog.conf", "Configuration file")
flag.Parse()
// config read and validation
err := configInit(*configFile)
if err != nil {
fmt.Println("\n", err)
os.Exit(1)
}
// initialize syslog Handler
syslogInit()
// initialize local log system
localLogInit()
go CounterStats()
go PrintStats()
// startTime := time.Now()
channelBufferSize := 5
logLineChannel := make(chan string, channelBufferSize)
var wg sync.WaitGroup
for _, v := range conf.Logsources.LogFiles {
wg.Add(1)
go logRead(&wg, v, logLineChannel, Stats)
}
// start log pump
wg.Add(1)
go logPump(&wg, logLineChannel, Stats, conf.General.Rate)
// wait goroutines to finish
wg.Wait()
// close log file
if conf.General.LogType == file {
logFile.Close()
}
}