-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
256 lines (210 loc) · 5.74 KB
/
Copy pathmain.go
File metadata and controls
256 lines (210 loc) · 5.74 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
package main
import (
_ "embed"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/jasonlvhit/gocron"
"github.com/patrickmn/go-cache"
"github.com/rakutentech/go-watch-logs/pkg"
)
var f pkg.Flags
var version = "dev"
var filePaths []string
var filePathsMutex sync.Mutex
var cacheMutex sync.Mutex
var caches = make(map[string]*cache.Cache)
//go:embed geoip.csv
var geoipCSV string
var geoIPDB *pkg.GeoIPDatabase
var httpClient *http.Client
// setHTTPClient initializes the singleton HTTP client with timeout and proxy configuration
func setHTTPClient() error {
timeout := time.Duration(3 * time.Second)
if f.Proxy == "" {
httpClient = &http.Client{
Timeout: timeout,
}
return nil
}
proxy, err := url.Parse(f.Proxy)
if err != nil {
return err
}
transport := &http.Transport{Proxy: http.ProxyURL(proxy)}
httpClient = &http.Client{
Transport: transport,
Timeout: timeout,
}
return nil
}
func main() {
pkg.Parseflags(&f)
// Initialize proxy and HTTP client before logging setup
parseProxy()
if err := setHTTPClient(); err != nil {
slog.Error("Failed to set HTTP client", "error", err.Error())
return
}
pkg.SetupLoggingStdout(f, httpClient) // nolint: errcheck
// Initialize GeoIP database
var err error
geoIPDB, err = pkg.ParseGeoIPCSV(geoipCSV)
if err != nil {
slog.Error("Failed to parse GeoIP database", "error", err.Error())
}
wantsVersion()
validate()
if f.Test {
pkg.TestIt(f.FilePath, f.Match)
return
}
syncFilePaths()
for _, filePath := range filePaths {
watch(filePath)
}
if f.Every > 0 {
startCron()
}
}
func syncCaches() {
cacheMutex.Lock()
defer cacheMutex.Unlock()
for filePath := range caches {
found := false
for _, f := range filePaths {
if f == filePath {
found = true
break
}
}
if !found {
slog.Info("Deleting cache obj", "filePath", filePath)
delete(caches, filePath)
}
}
for _, filePath := range filePaths {
if _, ok := caches[filePath]; ok {
continue
}
slog.Info("Creating cache obj", "filePath", filePath)
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
}
}
func startCron() {
if err := gocron.Every(1).Second().Do(pkg.PrintMemUsage, &f); err != nil {
slog.Error("Error scheduling memory usage", "error", err.Error())
return
}
if err := gocron.Every(f.Every).Second().Do(cronWatch); err != nil {
slog.Error("Error scheduling cron", "error", err.Error())
return
}
<-gocron.Start()
}
func cronWatch() {
syncFilePaths()
filePathsMutex.Lock()
defer filePathsMutex.Unlock()
cacheMutex.Lock()
defer cacheMutex.Unlock()
for _, filePath := range filePaths {
watch(filePath)
}
}
func syncFilePaths() {
slog.Info("Syncing files")
fpCrawled, err := pkg.FilesByPattern(f.FilePath, f.FileRecentSecs)
if err != nil {
slog.Error("Error finding files", "error", err.Error())
return
}
if len(fpCrawled) == 0 {
slog.Warn("No files found", "filePath", f.FilePath)
slog.Warn("Keep watching for new files")
return
}
// Filter and cap file paths
filePathsMutex.Lock()
defer filePathsMutex.Unlock()
filePaths = filterTextFiles(pkg.Capped(f.FilePathsCap, fpCrawled))
syncCaches()
slog.Info("Files synced", "fileCount", len(filePaths), "cacheCount", len(caches))
}
// filterTextFiles filters file paths to include only text files.
func filterTextFiles(paths []string) []string {
filtered := make([]string, 0, len(paths))
for _, path := range paths {
if isText, err := pkg.IsTextFile(path); err == nil && isText {
filtered = append(filtered, path)
}
}
return filtered
}
func validate() {
if f.Test {
return
}
if f.FilePath == "" {
slog.Error("file-path is required")
return
}
}
func watch(filePath string) {
watcher, err := pkg.NewWatcher(filePath, f, caches[filePath], geoIPDB)
if err != nil {
slog.Error("Error creating watcher", "error", err.Error(), "filePath", filePath)
return
}
defer watcher.Close()
slog.Info("Scanning file", "filePath", filePath)
result, err := watcher.Scan()
if err != nil {
slog.Warn("Error scanning file", "error", err.Error(), "filePath", filePath)
return
}
reportResult(result)
if _, err := pkg.ExecShell(f.PostCommand); err != nil {
slog.Error("Error running post command", "error", err.Error())
}
}
func reportResult(result *pkg.ScanResult) {
slog.Info("File info", "filePath", result.FilePath, "size", result.FileInfo.Size(), "modTime", result.FileInfo.ModTime())
slog.Info("Lines read", "count", result.LinesRead)
slog.Info("Scanning complete", "filePath", result.FilePath)
slog.Info("1st line", "date", result.FirstDate, "line", pkg.Truncate(result.FirstLine, pkg.TruncateMax))
slog.Info("Preview line", "line", pkg.Truncate(result.PreviewLine, pkg.TruncateMax))
slog.Info("Last line", "date", result.LastDate, "line", pkg.Truncate(result.LastLine, pkg.TruncateMax))
slog.Info("Error count", "percent", fmt.Sprintf("%d (%.2f)", result.ErrorCount, result.ErrorPercent)+"%")
slog.Info("History", "max streak", f.Streak, "current streaks", result.Streak, "symbols", pkg.StreakSymbols(result.Streak, f.Streak, f.Min))
slog.Info("Countries", "counts", fmt.Sprintf("%d, %v", len(result.CountryCounts), result.CountryCounts))
slog.Info("Scan", "count", result.ScanCount)
if result.IsFirstScan() {
slog.Info("First scan, skipping notification")
return
}
if !pkg.NonStreakZero(result.Streak, f.Streak, f.Min) {
slog.Info("Streak not met", "streak", f.Streak, "streaks", result.Streak)
return
}
if pkg.IsRecentlyModified(result.FileInfo, f.Every) {
pkg.Notify(result, f, version, httpClient)
}
}
func parseProxy() string {
systemProxy := pkg.SystemProxy()
if systemProxy != "" && f.Proxy == "" {
f.Proxy = systemProxy
}
return f.Proxy
}
func wantsVersion() {
if f.Version {
slog.Info("Version", "version", version)
os.Exit(0)
}
}