This repository was archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
209 lines (183 loc) · 5.09 KB
/
main.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
package main
import (
"bufio"
"bytes"
"compress/gzip"
"flag"
"io"
"os"
"path/filepath"
"regexp"
"sync"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
ignoreRegex *regexp.Regexp
emailRegex *regexp.Regexp
emails = make(map[string]map[string]bool)
writeLock = sync.Mutex{}
sem chan bool
lineMatch = regexp.MustCompile(`.+ <= (?P<from>\S+) .+ for (?P<to>\S+)`)
lineCount = 0
matchCount = 0
ignoreCount = 0
fromCount = 0
remainingFiles = 0
startTime = time.Now()
logLineCount = 1
logFrequency = 1
)
func main() {
email := flag.String("email", ".*", "A regex that determines is an email should be selected to group against")
ignore := flag.String("ignore", "^$", "A regex that determines if a to email should be ignored")
glob := flag.String("files", "*main.log*", "A glob pattern for matching exim logfiles to eat")
logFreq := flag.Int("log", 1000000, "The number of lines to read per log message")
outFileName := flag.String("out", "emails", "The resulting email file")
level := flag.String("level", "info", "Log level is one of debug, info, warn, error, fatal, panic")
pretty := flag.Bool("pretty", true, "Use pretty logging (slower)")
threads := flag.Int("threads", 500, "The number of lines to read per log message")
flag.Parse()
if *pretty {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
loglevel, err := zerolog.ParseLevel(*level)
if err != nil {
log.Fatal().Str("loglevel", *level).Err(err).Msg("Loglevel must be one of debug, info, warn, error, fatal, panic")
}
zerolog.SetGlobalLevel(loglevel)
zerolog.TimeFieldFormat = ""
log.Info().
Str("email", *email).
Str("files", *glob).
Int("frequency", *logFreq).
Str("outfile", *outFileName).
Str("level", *level).
Str("ignore", *ignore).
Bool("pretty", *pretty).
Msg("Starting exim4 logfile cruncher")
ignoreRegex, err = regexp.Compile(*ignore)
if err != nil {
log.Fatal().Err(err).Msg("Ignore regex did not compile")
}
emailRegex, err = regexp.Compile(*email)
if err != nil {
log.Fatal().Err(err).Msg("Email regex did not compile")
}
fileNames, err := filepath.Glob(*glob)
if err != nil {
log.Fatal().Str("pattern", *glob).Err(err).Msg("Failed to get files by glob")
}
remainingFiles = len(fileNames)
outFile, err := os.Create(*outFileName)
defer outFile.Close()
if err != nil {
log.Fatal().Str("name", *outFileName).Err(err).Msg("Failed to open output file")
}
logFrequency = *logFreq
logLineCount = logFrequency
sem = make(chan bool, *threads)
for _, fileName := range fileNames {
sem <- true
go processFile(fileName)
}
for i := 0; i < cap(sem); i++ {
sem <- true
}
log.Info().Int("count", matchCount).Msg("Writing emails to file")
writer := bufio.NewWriter(outFile)
for us, theirEmails := range emails {
writer.WriteString(us)
for them := range theirEmails {
writer.WriteByte(',')
writer.WriteString(them)
}
writer.WriteByte('\n')
writer.Flush()
log.Debug().Str("for", us).Msg("Finished emails")
}
log.Info().
Int("lines", lineCount).
Int("matched", matchCount).
Int("ignored", ignoreCount).
Int("from", fromCount).
Dur("elapsed", time.Since(startTime)).
Msg("Finished crunching logfiles")
}
const letterDiff = 'A' - 'a'
func toLower(r rune) rune {
if 'A' <= r && r <= 'Z' {
return r - letterDiff
}
return r
}
func processFile(fileName string) {
defer func() { <-sem }()
inFile, err := os.Open(fileName)
defer inFile.Close()
if err != nil {
log.Error().Str("name", fileName).Err(err).Msg("Could not open file")
}
var reader *bufio.Reader
if filepath.Ext(fileName) == ".gz" {
gzReader, err := gzip.NewReader(inFile)
defer gzReader.Close()
if err != nil {
log.Error().Str("name", fileName).Err(err).Msg("Could not read gzipped file")
}
reader = bufio.NewReader(gzReader)
} else {
reader = bufio.NewReader(inFile)
}
log.Info().Str("name", fileName).Int("remaining", remainingFiles).Msg("Reading file")
for {
if logLineCount <= 0 {
logLineCount = logFrequency
log.Info().
Int("lines", lineCount).
Int("matched", matchCount).
Int("ignored", ignoreCount).
Int("from", fromCount).
Msg("Crunching progress")
}
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
} else {
log.Error().Str("name", fileName).Err(err).Msg("Could not read file")
return
}
}
matches := lineMatch.FindSubmatch(line)
if matches != nil {
from := matches[1]
if !emailRegex.Match(from) {
ignoreCount++
continue
}
to := matches[2]
if ignore := ignoreRegex.Match(to); ignore {
ignoreCount++
continue
}
fromAsString := string(bytes.Map(toLower, from))
toAsString := string(bytes.Map(toLower, to))
writeLock.Lock()
val, ok := emails[fromAsString]
if ok {
val[toAsString] = true
} else {
fromCount++
emails[fromAsString] = map[string]bool{toAsString: true}
}
writeLock.Unlock()
matchCount++
}
lineCount++
logLineCount--
}
remainingFiles--
log.Debug().Str("file", fileName).Dur("elapsed", time.Since(startTime)).Msg("Finished reading file")
}