-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1086 lines (952 loc) · 40 KB
/
main.go
File metadata and controls
1086 lines (952 loc) · 40 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2025 SBOSOFT, Serkan Özkan
This file is part of, SBOLogProcessor, https://github.com/SBOsoft/SBOLogProcessor/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/SBOsoft/SBOLogProcessor/db"
"github.com/SBOsoft/SBOLogProcessor/handlers"
"github.com/SBOsoft/SBOLogProcessor/logparsers"
"github.com/SBOsoft/SBOLogProcessor/metrics"
)
const (
SBO_GLOBAL_PROFILE_METRICS string = "metrics"
SBO_GLOBAL_PROFILE_COUNT string = "count"
SBO_GLOBAL_PROFILE_SECURITY string = "security"
COUNTER_TOPN_SIZE_DEFAULT int = 10
COUNTER_OUTPUT_INTERVAL_DEFAULT int = 30
SBO_LOGP_LOG_FILE string = "./sbologp-logs.log"
)
// default settings that apply to all files unless there is a file specific config entry
const DEFAULT_CONFIG_KEY string = "--default--"
// there may be an entry with this name in the config file
const OSMETRICS_CONFIG_KEY string = "--OS-metrics--"
var globalConfig map[string]*ConfigForAMonitoredFile = make(map[string]*ConfigForAMonitoredFile)
var globalActiveProfile string = SBO_GLOBAL_PROFILE_METRICS
var globalActiveLogLevel slog.Level = slog.LevelInfo
func main() {
if len(os.Args) < 2 {
log.Fatal("Please provide a file path as argument")
}
parseCommandArgs()
var logFile *os.File = configureLogging(globalActiveLogLevel)
if logFile != nil {
defer logFile.Close()
}
slog.Info("Starting app with configuration:")
for fp, cfg := range globalConfig {
slog.Info("File", "filePath", fp, "configuration", cfg)
}
var wg sync.WaitGroup
for filePath, _ := range globalConfig {
if filePath == DEFAULT_CONFIG_KEY {
//do nothing. this is not a real file, just a default config entry
continue
}
if filePath == OSMETRICS_CONFIG_KEY {
//not a real file. setup OS metrics collection and continue
wg.Add(1)
go setupOSMetricsCollection(&wg)
continue
}
wg.Add(1)
go processFile(filePath, &wg)
}
wg.Wait()
}
func setupOSMetricsCollection(wg *sync.WaitGroup) {
defer wg.Done()
now := time.Now()
currentMinute := now.Minute()
//Find the next target minute. We want to start OS metrics collection on the scheduled minute
// e.g if you want to collect metrics at 0, 10, 20, 30, 40, 50, 60 minutes and the process was started at 07:33
// we will wait for 2 minutes 27 seconds and start at 10:00
var metricsRunInterval int = 0
nextTargetMinute := 0
switch globalConfig[OSMETRICS_CONFIG_KEY].OSMetricsIntervalMinutes {
case 1:
nextTargetMinute = currentMinute + 1
metricsRunInterval = 1
case 5:
//must end with 0 or 5
if currentMinute%10 == 0 {
nextTargetMinute = currentMinute
} else if currentMinute%10 > 5 {
nextTargetMinute = currentMinute + (10 - (currentMinute % 10))
} else {
nextTargetMinute = currentMinute + (5 - (currentMinute % 10))
}
metricsRunInterval = 5
case 15:
//must end with one of 00 15 30 45
if currentMinute > 45 {
nextTargetMinute = 60
} else if currentMinute > 30 {
nextTargetMinute = 45
} else if currentMinute > 15 {
nextTargetMinute = 30
} else if currentMinute > 0 {
nextTargetMinute = 15
} else {
nextTargetMinute = 0
}
metricsRunInterval = 15
case 30:
//must end with 00 or 30
if currentMinute > 30 {
nextTargetMinute = 60
} else {
nextTargetMinute = 30
}
metricsRunInterval = 30
case 60:
//must end with 00
nextTargetMinute = 60
metricsRunInterval = 60
default: //default is 10. time window must end with one of 00, 10, 20, 30, 40, 50
//must end with 0 or 5
if currentMinute%10 == 0 {
nextTargetMinute = currentMinute
} else {
nextTargetMinute = currentMinute + (10 - (currentMinute % 10))
}
metricsRunInterval = 10
}
// Calculate the time to the next target minute
nextRunTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), nextTargetMinute, 0, 0, now.Location())
initialDelay := nextRunTime.Sub(now)
if initialDelay > 0 {
slog.Info("Will start OS metrics collection at", "time", nextRunTime, "initialDelay", initialDelay.Round(time.Second))
// Wait for the initial delay
time.Sleep(initialDelay)
}
//db conn
sbodb := db.NewSBOAnalyticsDB()
dbInitialized, err := sbodb.Init(globalConfig[OSMETRICS_CONFIG_KEY].DbUser, globalConfig[OSMETRICS_CONFIG_KEY].DbPassword, globalConfig[OSMETRICS_CONFIG_KEY].DbAddress, globalConfig[OSMETRICS_CONFIG_KEY].DbDatabase)
if !dbInitialized {
slog.Warn("Failed to initialize database connection for OS metrics. Check database settings for OS metrics entry with key "+OSMETRICS_CONFIG_KEY+" in the configuration file", "error", err)
return
}
defer sbodb.Close()
// Run the function immediately after the initial delay
slog.Debug("Start OS metrics initial run")
if !processOSMetrics(sbodb, true) {
//if it didn't work on the first attempt it's probably not necessary to try again, e.g it's an unsupported OS
slog.Warn("NOT starting OS metrics collection. Initial attempt failed and won't try again. See supported operating systems in documentation")
return
}
// Set up a ticker to run every metricsRunInterval minutes
ticker := time.NewTicker(time.Duration(metricsRunInterval) * time.Minute)
defer ticker.Stop() // Ensure the ticker is stopped when func exits
for range ticker.C {
slog.Debug("Collecting OS metrics in scheduled task")
processOSMetrics(sbodb, false)
}
}
func processOSMetrics(sbodb *db.SBOAnalyticsDB, isInitialCall bool) bool {
uptimeInfo, err := metrics.GetOSUptimeInfo()
if err != nil {
slog.Warn("Failed to collect OS uptime info. This feature is not available on all platforms. Check supported operating systems.", "error", err)
return false
}
memoryInfo, err := metrics.GetOSMemoryInfo()
if err != nil && isInitialCall { //don't repeat the same log message forever, log only the first time
slog.Warn("Failed to collect OS memory info. This feature is not available on all platforms. Check supported operating systems.", "error", err)
//not returning false as at least uptime worked
//return false
}
saveResult, _ := sbodb.SaveOSMetrics(uptimeInfo, memoryInfo, globalConfig[OSMETRICS_CONFIG_KEY].HostId)
return saveResult
}
func getConfigForFile(filePath string) *ConfigForAMonitoredFile {
foundConfig, ok := globalConfig[filePath]
if !ok {
foundConfig, ok = globalConfig[DEFAULT_CONFIG_KEY]
}
if !ok {
slog.Warn("Failed to find configuration for file", "filePath", filePath)
}
return foundConfig
}
/*
This function will run before logging is set up. Don't use slog here yet
*/
func parseCommandArgs() {
logLevelPtr := flag.String("l", "info", "Log level. Defaults to info. Supported values are: debug, info, warn.")
profilePtr := flag.String("p", "metrics", "Active profile. Defaults to metrics which will create metrics. Available options are: metrics, count, security. Where count will output total stats from the given file and security will output malicious IPs and security stats.")
confFilePtr := flag.String("c", "", "Configuration file in json format. There is no default value but you will want to pass a config file when -m=metrics. ")
followPtr := flag.Bool("f", false, "Follow changes to the file, as in tail -f")
windowSizePtr := flag.Int("w", 1, "Statistics window size in minutes, e.g to report request statistics for every 5 minute window.")
startFromPtr := flag.Int("s", START_FROM_BEGINNING, "When 0, file will be processed starting from the beginning. When -1, file will be processed starting from the end (i.e only lines appended after the program starts will be processed). Defaults to 0")
domainPtr := flag.String("d", "", "Domain name to report, needed when domain names are not available in logs")
handlerPtr := flag.String("a", "", "Enabled handler name, defaults to METRICS. Note: It's NOT possible to pass multiple handlers using command line parameters, you need to use a configuration file if you need to enable multiple handlers.")
writeToFileTargetPtr := flag.String("t", "", "Target file path, required when handler is WRITE_TO_FILE")
counterTopNPtr := flag.Int("n", COUNTER_TOPN_SIZE_DEFAULT, "Applies to count profile only: Number of items (such as IP addresses, referers, paths) to be displayed. Only the top n items will be displayed in the output.")
counterOutputIntervalPtr := flag.Int("i", COUNTER_OUTPUT_INTERVAL_DEFAULT, "Applies to count profile only: Number of seconds between successive count outputs")
helpPtr := flag.Bool("h", false, "Show command line parameters")
flag.Parse()
if *helpPtr {
fmt.Println()
fmt.Println("SBOLogProcessor command line tool for monitoring web server access logs and more. See https://github.com/SBOsoft/SBOLogProcessor for more details")
fmt.Println("Passing a configuration file using -c parameter is the recommended method for providing configuration options.")
fmt.Println("Command line arguments should suffice for the counter profile BUT a command line parameter for every possible configuration option may NOT be available.")
fmt.Println()
fmt.Println("Usage: 'sbologp [command line options, e.g -f -p=metrics] access-log-file-path' OR 'sbologp -c path-to-config-file.json'")
fmt.Println("For example: ./sbologp -f -p=count /var/log/apache/access.log OR ./sbologp -c sbologp-config.json")
flag.PrintDefaults()
os.Exit(0)
}
switch *logLevelPtr {
case "info":
globalActiveLogLevel = slog.LevelInfo
case "warn":
globalActiveLogLevel = slog.LevelWarn
case "debug":
globalActiveLogLevel = slog.LevelDebug
}
globalActiveProfile = *profilePtr
if globalActiveProfile != SBO_GLOBAL_PROFILE_COUNT &&
globalActiveProfile != SBO_GLOBAL_PROFILE_METRICS &&
globalActiveProfile != SBO_GLOBAL_PROFILE_SECURITY {
fmt.Printf("Invalid profile value (invalid -p parameter): '%s' ", globalActiveProfile)
fmt.Println()
fmt.Println("Use -h parameter to view command line options")
//flag.PrintDefaults()
os.Exit(1)
}
configFileName := *confFilePtr
loadedConfigFromFile := false
if len(configFileName) > 0 {
loadedConfigFromFile = loadConfigFromFile(configFileName)
}
if !loadedConfigFromFile {
if len(flag.Arg(0)) > 0 {
handlerName := *handlerPtr
//default handlers for profiles
if len(handlerName) < 1 {
if globalActiveProfile == SBO_GLOBAL_PROFILE_COUNT {
handlerName = handlers.COUNTER_HANDLER_NAME
} else if globalActiveProfile == SBO_GLOBAL_PROFILE_METRICS {
handlerName = handlers.METRIC_GENERATOR_HANDLER_NAME
}
}
//Creating config here is not the best way to invoke the program
var cfFromCmdLine = ConfigForAMonitoredFile{
Follow: *followPtr,
StartFrom: *startFromPtr,
TimeWindowSizeMinutes: *windowSizePtr,
DomainName: *domainPtr,
Handlers: []string{handlerName}, //<- only 1 handler is supported
FilePath: flag.Arg(0),
WriteToFileTargetFile: *writeToFileTargetPtr,
HandlerInstances: make(map[string]SBOLogHandlerInterface, 1),
WriteMetricsToDb: false,
DbAddress: "",
DbUser: "",
DbPassword: "",
DbDatabase: "",
ReplaceExistingMetrics: true,
MetricsWindowSize: 3,
CounterTopNForKeyedMetrics: *counterTopNPtr,
CounterOutputIntervalSeconds: *counterOutputIntervalPtr}
globalConfig[cfFromCmdLine.FilePath] = &cfFromCmdLine
} else {
fmt.Println("Invalid options, cannot continue, missing log file path. Either a configuration file or command line parameters are required. Use -h parameter to view command line options. See https://github.com/SBOsoft/SBOLogProcessor for more details")
os.Exit(1)
}
}
}
/*
TODO should be improved
First we load the config file into a map[string]map[string]interface{} because we want to know if
values for were provided for each field or not so that we can override with values defined under defaults
'interface{}' requires a lot of type conversions float64 => int, []interface{} => []string
*/
func loadConfigFromFile(configFileName string) bool {
var configLoadedFromFile map[string]map[string]interface{} = make(map[string]map[string]interface{})
fileInfo, err := os.Stat(configFileName)
if os.IsNotExist(err) {
//no config file provided
slog.Error("Configuration file path parameter, -c, points to non-existent file. It must point to a json file. Ignoring parameter", "file", configFileName)
return false
}
if fileInfo.IsDir() {
slog.Error("Configuration file path parameter, -c, points to a directory. It must point to a json file. Ignoring parameter")
return false
}
configFileBytes, err := os.ReadFile(configFileName)
if err != nil {
slog.Error("Failed to read configuration file", "file", configFileName, "error", err)
return false
}
err = json.Unmarshal(configFileBytes, &configLoadedFromFile)
if err != nil {
slog.Error("Failed to load configuration from file", "file", configFileName, "error", err)
return false
}
for fp, conf := range configLoadedFromFile {
//validation and defaults
mapCounterOutputIntervalSeconds, ok := conf["CounterOutputIntervalSeconds"].(float64)
if !ok || mapCounterOutputIntervalSeconds < 1 {
mapCounterOutputIntervalSeconds = 30
}
conf["CounterOutputIntervalSeconds_ok"] = ok
mapCounterTopNForKeyedMetrics, ok := conf["CounterTopNForKeyedMetrics"].(float64)
if !ok || (mapCounterTopNForKeyedMetrics < 1 || mapCounterTopNForKeyedMetrics > 100) {
mapCounterTopNForKeyedMetrics = 10
}
conf["CounterTopNForKeyedMetrics_ok"] = ok
intMetricsWindowSize, ok := conf["MetricsWindowSize"].(float64)
conf["MetricsWindowSize_ok"] = ok
windowSizeToUse := 3
if ok {
windowSizeToUse = int(intMetricsWindowSize)
}
if !ok || (intMetricsWindowSize < 2 || intMetricsWindowSize > 10) {
//allow sensible values only
windowSizeToUse = 3
}
mapEnabled, ok := conf["Enabled"].(bool)
conf["Enabled_ok"] = ok
mapFilePath, ok := conf["FilePath"].(string)
conf["FilePath_ok"] = ok
mapHandlers, ok := conf["Handlers"].([]interface{})
conf["Handlers_ok"] = ok
mapStartFrom, ok := conf["StartFrom"].(float64)
conf["StartFrom_ok"] = ok
mapSkipIfLineMatchesRegex, ok := conf["SkipIfLineMatchesRegex"].(string)
conf["SkipIfLineMatchesRegex_ok"] = ok
mapFollow, ok := conf["Follow"].(bool)
conf["Follow_ok"] = ok
mapDomainName, ok := conf["DomainName"].(string)
conf["DomainName_ok"] = ok
mapHostId, ok := conf["HostId"].(float64)
conf["HostId_ok"] = ok
mapTimeWindowSizeMinutes, ok := conf["TimeWindowSizeMinutes"].(float64)
conf["TimeWindowSizeMinutes_ok"] = ok
mapWriteToFileTargetFile, ok := conf["WriteToFileTargetFile"].(string)
conf["WriteToFileTargetFile_ok"] = ok
mapWriteMetricsToDb, ok := conf["WriteMetricsToDb"].(bool)
conf["WriteMetricsToDb_ok"] = ok
mapDbAddress, ok := conf["DbAddress"].(string)
conf["DbAddress_ok"] = ok
mapDbUser, ok := conf["DbUser"].(string)
conf["DbUser_ok"] = ok
mapDbPassword, ok := conf["DbPassword"].(string)
conf["DbPassword_ok"] = ok
mapDbDatabase, ok := conf["DbDatabase"].(string)
conf["DbDatabase_ok"] = ok
mapReplaceExistingMetrics, ok := conf["ReplaceExistingMetrics"].(bool)
conf["ReplaceExistingMetrics_ok"] = ok
mapSaveLogsToDb, ok := conf["SaveLogsToDb"].(bool)
conf["SaveLogsToDb_ok"] = ok
mapSaveLogsToDbMaskIPs, ok := conf["SaveLogsToDbMaskIPs"].(bool)
conf["SaveLogsToDbMaskIPs_ok"] = ok
mapSaveLogsToDbOnlyRelevant, ok := conf["SaveLogsToDbOnlyRelevant"].(float64)
conf["SaveLogsToDbOnlyRelevant_ok"] = ok
mapOSMetricsEnabled, ok := conf["OSMetricsEnabled"].(bool)
conf["OSMetricsEnabled_ok"] = ok
mapOSMetricsIntervalMinutes, ok := conf["OSMetricsIntervalMinutes"].(float64)
conf["OSMetricsIntervalMinutes_ok"] = ok
handlersArrayAsStrings := make([]string, len(mapHandlers))
for indexInHandlers, handlerNameValue := range mapHandlers {
handlersArrayAsStrings[indexInHandlers] = fmt.Sprint(handlerNameValue)
}
globalConfig[fp] = &ConfigForAMonitoredFile{
Enabled: mapEnabled,
FilePath: mapFilePath,
Handlers: handlersArrayAsStrings,
StartFrom: int(mapStartFrom),
SkipIfLineMatchesRegex: mapSkipIfLineMatchesRegex,
Follow: mapFollow,
DomainName: mapDomainName,
HostId: int(mapHostId),
TimeWindowSizeMinutes: int(mapTimeWindowSizeMinutes),
WriteToFileTargetFile: mapWriteToFileTargetFile,
HandlerInstances: make(map[string]SBOLogHandlerInterface),
WriteMetricsToDb: mapWriteMetricsToDb,
DbAddress: mapDbAddress,
DbUser: mapDbUser,
DbPassword: mapDbPassword,
DbDatabase: mapDbDatabase,
ReplaceExistingMetrics: mapReplaceExistingMetrics,
MetricsWindowSize: windowSizeToUse,
CounterTopNForKeyedMetrics: int(mapCounterTopNForKeyedMetrics),
CounterOutputIntervalSeconds: int(mapCounterOutputIntervalSeconds),
SaveLogsToDb: mapSaveLogsToDb,
SaveLogsToDbMaskIPs: mapSaveLogsToDbMaskIPs,
SaveLogsToDbOnlyRelevant: int(mapSaveLogsToDbOnlyRelevant),
OSMetricsEnabled: mapOSMetricsEnabled,
OSMetricsIntervalMinutes: int(mapOSMetricsIntervalMinutes)}
}
_, configContainsDefaultEntry := globalConfig[DEFAULT_CONFIG_KEY]
if configContainsDefaultEntry {
for filePath, _ := range globalConfig {
if filePath == DEFAULT_CONFIG_KEY || filePath == OSMETRICS_CONFIG_KEY {
continue
}
if !configLoadedFromFile[filePath]["Handlers_ok"].(bool) && len(globalConfig[filePath].Handlers) < 1 {
globalConfig[filePath].Handlers = globalConfig[DEFAULT_CONFIG_KEY].Handlers
}
if !configLoadedFromFile[filePath]["StartFrom_ok"].(bool) {
globalConfig[filePath].StartFrom = globalConfig[DEFAULT_CONFIG_KEY].StartFrom
}
if !configLoadedFromFile[filePath]["SkipIfLineMatchesRegex_ok"].(bool) {
globalConfig[filePath].SkipIfLineMatchesRegex = globalConfig[DEFAULT_CONFIG_KEY].SkipIfLineMatchesRegex
}
if !configLoadedFromFile[filePath]["Follow_ok"].(bool) {
globalConfig[filePath].Follow = globalConfig[DEFAULT_CONFIG_KEY].Follow
}
if !configLoadedFromFile[filePath]["DomainName_ok"].(bool) {
globalConfig[filePath].DomainName = globalConfig[DEFAULT_CONFIG_KEY].DomainName
}
if !configLoadedFromFile[filePath]["HostId_ok"].(bool) {
globalConfig[filePath].HostId = globalConfig[DEFAULT_CONFIG_KEY].HostId
}
if !configLoadedFromFile[filePath]["TimeWindowSizeMinutes_ok"].(bool) {
globalConfig[filePath].TimeWindowSizeMinutes = globalConfig[DEFAULT_CONFIG_KEY].TimeWindowSizeMinutes
}
if !configLoadedFromFile[filePath]["WriteToFileTargetFile_ok"].(bool) {
globalConfig[filePath].WriteToFileTargetFile = globalConfig[DEFAULT_CONFIG_KEY].WriteToFileTargetFile
}
if !configLoadedFromFile[filePath]["WriteMetricsToDb_ok"].(bool) {
globalConfig[filePath].WriteMetricsToDb = globalConfig[DEFAULT_CONFIG_KEY].WriteMetricsToDb
}
if !configLoadedFromFile[filePath]["DbAddress_ok"].(bool) {
globalConfig[filePath].DbAddress = globalConfig[DEFAULT_CONFIG_KEY].DbAddress
}
if !configLoadedFromFile[filePath]["DbUser_ok"].(bool) {
globalConfig[filePath].DbUser = globalConfig[DEFAULT_CONFIG_KEY].DbUser
}
if !configLoadedFromFile[filePath]["DbPassword_ok"].(bool) {
globalConfig[filePath].DbPassword = globalConfig[DEFAULT_CONFIG_KEY].DbPassword
}
if !configLoadedFromFile[filePath]["DbDatabase_ok"].(bool) {
globalConfig[filePath].DbDatabase = globalConfig[DEFAULT_CONFIG_KEY].DbDatabase
}
if !configLoadedFromFile[filePath]["ReplaceExistingMetrics_ok"].(bool) {
globalConfig[filePath].ReplaceExistingMetrics = globalConfig[DEFAULT_CONFIG_KEY].ReplaceExistingMetrics
}
if !configLoadedFromFile[filePath]["MetricsWindowSize_ok"].(bool) {
globalConfig[filePath].MetricsWindowSize = globalConfig[DEFAULT_CONFIG_KEY].MetricsWindowSize
}
if !configLoadedFromFile[filePath]["CounterTopNForKeyedMetrics_ok"].(bool) {
globalConfig[filePath].CounterTopNForKeyedMetrics = globalConfig[DEFAULT_CONFIG_KEY].CounterTopNForKeyedMetrics
}
if !configLoadedFromFile[filePath]["CounterOutputIntervalSeconds_ok"].(bool) {
globalConfig[filePath].CounterOutputIntervalSeconds = globalConfig[DEFAULT_CONFIG_KEY].CounterOutputIntervalSeconds
}
if !configLoadedFromFile[filePath]["SaveLogsToDb_ok"].(bool) {
globalConfig[filePath].SaveLogsToDb = globalConfig[DEFAULT_CONFIG_KEY].SaveLogsToDb
}
if !configLoadedFromFile[filePath]["SaveLogsToDbMaskIPs_ok"].(bool) {
globalConfig[filePath].SaveLogsToDbMaskIPs = globalConfig[DEFAULT_CONFIG_KEY].SaveLogsToDbMaskIPs
}
if !configLoadedFromFile[filePath]["SaveLogsToDbOnlyRelevant_ok"].(bool) {
globalConfig[filePath].SaveLogsToDbOnlyRelevant = globalConfig[DEFAULT_CONFIG_KEY].SaveLogsToDbOnlyRelevant
}
if !configLoadedFromFile[filePath]["OSMetricsEnabled_ok"].(bool) {
globalConfig[filePath].OSMetricsEnabled = globalConfig[DEFAULT_CONFIG_KEY].OSMetricsEnabled
}
if !configLoadedFromFile[filePath]["OSMetricsIntervalMinutes_ok"].(bool) {
globalConfig[filePath].OSMetricsIntervalMinutes = globalConfig[DEFAULT_CONFIG_KEY].OSMetricsIntervalMinutes
}
}
_, ok := globalConfig[OSMETRICS_CONFIG_KEY]
if ok {
if len(globalConfig[OSMETRICS_CONFIG_KEY].DbAddress) < 1 {
globalConfig[OSMETRICS_CONFIG_KEY].DbAddress = globalConfig[DEFAULT_CONFIG_KEY].DbAddress
}
if len(globalConfig[OSMETRICS_CONFIG_KEY].DbDatabase) < 1 {
globalConfig[OSMETRICS_CONFIG_KEY].DbDatabase = globalConfig[DEFAULT_CONFIG_KEY].DbDatabase
}
if len(globalConfig[OSMETRICS_CONFIG_KEY].DbPassword) < 1 {
globalConfig[OSMETRICS_CONFIG_KEY].DbPassword = globalConfig[DEFAULT_CONFIG_KEY].DbPassword
}
if len(globalConfig[OSMETRICS_CONFIG_KEY].DbUser) < 1 {
globalConfig[OSMETRICS_CONFIG_KEY].DbUser = globalConfig[DEFAULT_CONFIG_KEY].DbUser
}
if globalConfig[OSMETRICS_CONFIG_KEY].HostId < 1 {
globalConfig[OSMETRICS_CONFIG_KEY].HostId = globalConfig[DEFAULT_CONFIG_KEY].HostId
}
}
}
slog.Debug("Loaded config from file", "file", configFileName)
return true
}
func createHandler(filePath string, handlerName string, dataToSaveChan chan *metrics.SBOMetricWindowDataToBeSaved, metricsManager *metrics.SBOMetricsManager) SBOLogHandlerInterface {
config := getConfigForFile(filePath)
switch {
case handlerName == handlers.WRITE_TO_FILE_HANDLER_NAME:
writeToFile := handlers.NewWriteToFileHandler()
err := writeToFile.Begin(config.WriteToFileTargetFile)
slog.Info("Created WriteToFileHandler", "error", err)
return writeToFile
case handlerName == handlers.METRIC_GENERATOR_HANDLER_NAME:
metricsGenerator := handlers.NewMetricGeneratorHandler(filePath, metricsManager, config.TimeWindowSizeMinutes)
metricsGenerator.Begin(dataToSaveChan)
slog.Info("Created MetricGeneratorHandler")
return metricsGenerator
case handlerName == handlers.COUNTER_HANDLER_NAME:
counterHandler := handlers.NewCounterHandler(filePath)
counterHandler.Begin(dataToSaveChan,
config.Follow,
config.CounterOutputIntervalSeconds,
config.CounterTopNForKeyedMetrics)
slog.Info("Created CounterHandler")
return counterHandler
}
slog.Warn("createHandler failed no handler for handler name", "handlerName", handlerName)
return nil
}
func processFile(filePath string, parentWaitGroup *sync.WaitGroup) {
defer parentWaitGroup.Done()
slog.Info("Starting to process file", "file", filePath)
config := getConfigForFile(filePath)
lines := make(chan string, 10) // Buffered channel to prevent blocking
dataToBeSavedChannel := make(chan *metrics.SBOMetricWindowDataToBeSaved, 100)
sbodb := db.NewSBOAnalyticsDB()
if config.WriteMetricsToDb || config.SaveLogsToDb {
//if not writing to db then db stuff is unnecessary
defer sbodb.Close()
sbodb.Init(config.DbUser, config.DbPassword, config.DbAddress, config.DbDatabase)
}
var waitGroupForThisFile sync.WaitGroup
waitGroupForThisFile.Add(1)
// Start consumer (producer -> consumer -> save data) consumer generates metrics etc
go consumeLinesFromChannel(filePath, lines, &waitGroupForThisFile, dataToBeSavedChannel, sbodb)
waitGroupForThisFile.Add(1)
// Start goroutine for saving data
go processMetricDataToBeSaved(filePath, dataToBeSavedChannel, &waitGroupForThisFile, sbodb)
// Start producer
produceLinesFromFile(filePath, lines)
//WaitGroup specific to the file
waitGroupForThisFile.Wait()
slog.Info("Finished processing file", "file", filePath)
}
func configureLogging(logLevel slog.Level) *os.File {
////////log config
slog.SetLogLoggerLevel(logLevel)
log.SetFlags(log.Ldate | log.Lmicroseconds)
logFile, err := os.OpenFile(SBO_LOGP_LOG_FILE, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
// If we can't open the log file, log to stderr and exit.
slog.Error(fmt.Sprintf("Failed to open log file '%v'. Will log to stderr instead", SBO_LOGP_LOG_FILE), "error", err)
logHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(logHandler))
return nil
} else {
logHandler := slog.NewTextHandler(logFile, &slog.HandlerOptions{Level: logLevel})
slog.SetDefault(slog.New(logHandler))
return logFile
}
}
/*
Save metric data
*/
func processMetricDataToBeSaved(filePath string, dataToBeSavedChannel chan *metrics.SBOMetricWindowDataToBeSaved, wg *sync.WaitGroup, sbodb *db.SBOAnalyticsDB) {
defer wg.Done()
config := getConfigForFile(filePath)
for dataToSave := range dataToBeSavedChannel {
if !config.WriteMetricsToDb {
//nothing to do here, just move
continue
}
domainName := config.DomainName
if len(dataToSave.DomainName) > 0 {
domainName = dataToSave.DomainName
}
domainId, _ := sbodb.GetDomainId(domainName, config.TimeWindowSizeMinutes)
/*
if domainId < 1 {
slog.Warn("domainId < 1 for data", "data", dataToSave)
}
*/
sbodb.SaveMetricData(dataToSave, domainId, config.ReplaceExistingMetrics)
slog.Debug("processMetricDataToBeSaved save data:", "dataToSave", dataToSave)
}
slog.Debug("processMetricDataToBeSaved done")
}
func consumeLinesFromChannel(filePath string, linesChannel chan string, wg *sync.WaitGroup, dataToBeSavedChannel chan *metrics.SBOMetricWindowDataToBeSaved, sbodb *db.SBOAnalyticsDB) {
var processedLineCount, errorCount int
var parserFunction func(string) (*logparsers.SBOHttpRequestLog, error) = nil
var lineResult bool
config := getConfigForFile(filePath)
//*metrics.SBOMetricsManager
metricsManager := metrics.NewSBOMetricsManager(config.MetricsWindowSize)
for _, handlerName := range config.Handlers {
config.HandlerInstances[handlerName] = createHandler(filePath, handlerName, dataToBeSavedChannel, metricsManager)
}
defer wg.Done()
defer close(dataToBeSavedChannel)
slog.Debug("Start consumer in consumeLinesFromChannel", "filePath", filePath)
for line := range linesChannel {
lineResult, parserFunction = processSingleLogLine(filePath, line, parserFunction, dataToBeSavedChannel, sbodb)
if lineResult {
processedLineCount++
} else {
errorCount++
}
}
for _, h := range config.HandlerInstances {
h.End()
}
slog.Info("consumeLinesFromChannel finished", "processedLineCount", processedLineCount, "errorCount", errorCount)
}
func processSingleLogLine(filePath string, logLine string,
parserFunction func(string) (*logparsers.SBOHttpRequestLog, error),
dataToBeSavedChannel chan *metrics.SBOMetricWindowDataToBeSaved,
sbodb *db.SBOAnalyticsDB) (bool, func(string) (*logparsers.SBOHttpRequestLog, error)) {
if len(logLine) < 1 {
return false, parserFunction
}
var parseResult *logparsers.SBOHttpRequestLog
var parseErr error
config := getConfigForFile(filePath)
if parserFunction == nil {
// Try parsing with each format
formats := []struct {
name string
fn func(string) (*logparsers.SBOHttpRequestLog, error)
}{
//TODO add formats and parsers here
{"Apache Common Log Format", logparsers.ParseApacheCommonLogFormat},
{"Apache Combined Log Format", logparsers.ParseApacheCombinedLogFormat},
{"Apache VHost Combined Log Format", logparsers.ParseApacheVHostCombinedLogFormat},
{"Nginx Combined Log Format", logparsers.ParseNginxCombinedFormat},
{"Nginx Custom Log Format", logparsers.ParseNginxCustomFormat},
}
slog.Debug("parserFunction not set, trying to find a match")
for _, format := range formats {
parseResult, parseErr := format.fn(logLine)
if parseResult != nil && parseErr == nil {
parserFunction = format.fn
slog.Debug("***************************** Successfully parsed as format. Will use this format for this file going forward ********************", "format", format.name)
}
}
} else {
parseResult, parseErr = parserFunction(logLine)
}
if parseResult != nil {
if parseErr != nil {
//invalid line
return false, parserFunction
} else {
//now calculate stats or do whatever needs to be done
callHandlersForRequestLogEntry(filePath, parseResult, dataToBeSavedChannel)
//save log to db
if config.SaveLogsToDb && sbodb != nil && sbodb.IsInitialized {
var domainId int = 0
if len(parseResult.Domain) > 0 {
domainId, _ = sbodb.GetDomainId(parseResult.Domain, config.TimeWindowSizeMinutes)
} else {
domainId, _ = sbodb.GetDomainId(config.DomainName, config.TimeWindowSizeMinutes)
}
if config.SaveLogsToDbOnlyRelevant == 1 {
//save only if not irrelevant
if (parseResult.Malicious == logparsers.REQUEST_MALICIOUS_UNKNOWN) &&
(strings.HasPrefix(parseResult.Status, "2") || strings.HasPrefix(parseResult.Status, "5")) &&
parseResult.UserAgent.DeviceType != logparsers.DeviceType_Script &&
(parseResult.UserAgent.Family != logparsers.UAFamily_Scanner &&
parseResult.UserAgent.Family != logparsers.UAFamily_SEOBot &&
//parseResult.UserAgent.Family != logparsers.UAFamily_SocialBot &&
//parseResult.UserAgent.Family != logparsers.UAFamily_SearchBot &&
parseResult.UserAgent.Family != logparsers.UAFamily_Script) {
sbodb.SaveRawLog(parseResult, domainId, config.HostId, config.SaveLogsToDbMaskIPs)
}
} else {
sbodb.SaveRawLog(parseResult, domainId, config.HostId, config.SaveLogsToDbMaskIPs)
}
}
return true, parserFunction
}
}
return false, parserFunction
}
func callHandlersForRequestLogEntry(filePath string, parsedLogEntry *logparsers.SBOHttpRequestLog, dataToBeSavedChannel chan *metrics.SBOMetricWindowDataToBeSaved) {
//processMetricsForRequestLogEntry(filePath, parsedLogEntry)
config := getConfigForFile(filePath)
for _, h := range config.HandlerInstances {
h.HandleEntry(parsedLogEntry)
}
}
func produceLinesFromFile(filePath string, lines chan<- string) {
slog.Debug("Enter produceLinesFromFile for file", "filePath", filePath)
var watcher *fsnotify.Watcher
slog.Debug("produceLinesFromFile global config", "globalConfig", globalConfig)
defer close(lines)
config := getConfigForFile(filePath)
var watcherErr error
if config.Follow {
watcher, watcherErr = fsnotify.NewWatcher()
if watcherErr != nil {
slog.Error("Error setting up fsnotify.NewWatcher", "filePath", filePath, "error", watcherErr)
return
}
defer watcher.Close()
// Watch the directory containing the file
dir := filepath.Dir(filePath)
watcherErr = watcher.Add(dir)
if watcherErr != nil {
slog.Error("Error adding watcher", "filePath", filePath, "error", watcherErr)
return
}
slog.Debug("Set up watcher", "dir", dir, "watchlist", watcher.WatchList())
}
baseNameForFile := filepath.Base(filePath)
var file *os.File
var fileReader *bufio.Reader
var err error
// Initial file open
if file, fileReader, err = openFile(false, filePath); err != nil {
slog.Error("Error opening file", "filePath", filePath, "error", err)
return
}
var waitingForNewData bool = false
var isFileAtEnd bool = false
for {
if fileReader != nil {
if !waitingForNewData { //dont even try to read if just waiting
isFileAtEnd = readSingleLineFromFileReturnTrueIfEOF(filePath, lines, fileReader)
if isFileAtEnd && !config.Follow {
slog.Info("Finished reading the file and not following, so done...")
return
}
if isFileAtEnd {
waitingForNewData = true
file.Seek(0, 2)
fileReader.Reset(file)
slog.Debug("readSingleLineFromFile isFileAtEnd after waitingForNewData was false", "isFileAtEnd", isFileAtEnd)
}
} else {
//slog.Warn("waitingForNewData is true in produceLinesFromFile")
}
} else {
slog.Warn("fileReader is nil in produceLinesFromFile", "filePath", filePath)
break
}
if config.Follow {
//slog.Warn("follow in produceLinesFromFile before select")
select {
case event, ok := <-watcher.Events:
if !ok {
slog.Info("watcher.Events not ok", "event", event)
return
}
//slog.Debug("fsnotify event ", "event", event)
if filepath.Base(event.Name) != baseNameForFile {
//irrelevant event
continue
}
if event.Has(fsnotify.Write) {
// File was modified, continue reading, normal case
slog.Debug("File was modified after receiving EOF in the previous read. Continue reading", "file", filePath)
waitingForNewData = false
continue
}
if event.Has(fsnotify.Rename) || event.Has(fsnotify.Remove) {
// File was renamed/removed (log rotation)
slog.Info("File was renamed/removed (log rotation)", "file", filePath)
// read file to end before switching
readFileToEnd(filePath, lines, fileReader)
file.Close()
waitingForNewData = false
// Try to reopen the file
for i := 0; i < 5; i++ {
if file, fileReader, err = openFile(true, filePath); err == nil {
break
}
time.Sleep(1 * time.Second)
}
if fileReader == nil {
slog.Warn("File was renamed/removed (log rotation) but could not be reopened", "file", filePath)
return
} else {
slog.Info("Re-opened file after rotation", "file", filePath)
}
}
case err, ok := <-watcher.Errors:
if !ok {
slog.Error("Watcher error", "error", err)
return
}
default:
if waitingForNewData {
//slog.Warn("wait while waitingForNewData", "waitingForNewData", waitingForNewData)
time.Sleep(1000 * time.Millisecond)
}
continue
}
} //if follow
}
}
func openFile(reopeningAfterRotate bool, filePath string) (*os.File, *bufio.Reader, error) {
var err error
var file *os.File
var fileReader *bufio.Reader
config := getConfigForFile(filePath)
file, err = os.Open(filePath)
fileReader = nil
if err != nil {
slog.Error("Error opening file", "filePath", filePath, "error", err)
return file, fileReader, err
}
if reopeningAfterRotate || config.StartFrom == START_FROM_BEGINNING {
// Seek to beginning if file exists
_, err = file.Seek(0, 0)
if err != nil {
file.Close()
slog.Error("Error seeking to beginning of file", "filePath", filePath, "error", err)
return file, fileReader, err
}
} else if config.StartFrom == START_FROM_END || config.StartFrom < 0 {
// Seek to end if file exists
_, err = file.Seek(0, 2)
if err != nil {
file.Close()
slog.Error("Error seeking to end of file", "filePath", filePath, "error", err)
return file, fileReader, err
}
} else {
//skip until the line
slog.Info("Skipping lines after opening file", "skippedLines", config.StartFrom)
fileReader = bufio.NewReaderSize(file, 8192)
lineNo := 1
for {
_, err := fileReader.ReadString('\n')
lineNo++
if lineNo >= config.StartFrom {
break
}
if err != nil {
break
}
}
}
if fileReader == nil {
fileReader = bufio.NewReaderSize(file, 8192)
}
return file, fileReader, nil
}
func readFileToEnd(filePath string, lines chan<- string, fileReader *bufio.Reader) {
slog.Debug("Reading file to end ", "filePath", filePath)
for {
isEOF := readSingleLineFromFileReturnTrueIfEOF(filePath, lines, fileReader)
if isEOF {
slog.Debug("readFileToEnd done", "filePath", filePath)
return
}
}
}
func readSingleLineFromFileReturnTrueIfEOF(filePath string, lines chan<- string, fileReader *bufio.Reader) bool {
bytesRead, err := fileReader.ReadString('\n')
if len(bytesRead) > 0 {
theLine := strings.TrimSpace(string(bytesRead[:]))
//slog.Debug("Read line:", "filePath", filePath, "line", theLine)
lines <- theLine
}
if err != nil {
//slog.Debug("fileReader error", "filePath", filePath, "error", err)
return io.EOF == err