forked from kyallanum/athena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (111 loc) · 3.48 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
package main
import (
"flag"
"fmt"
"log"
"os"
config "github.com/kyallanum/athena/v1.0.0/models/config"
library "github.com/kyallanum/athena/v1.0.0/models/library"
logs "github.com/kyallanum/athena/v1.0.0/models/logs"
)
func errCheck(err error) {
if err != nil {
panic(err)
}
}
func parseFlags(configFile *string, logFile *string) error {
var configFlag string
var logFlag string
flag.StringVar(&configFlag, "c", "", "")
flag.StringVar(&configFlag, "config", "", "")
flag.StringVar(&logFlag, "l", "", "")
flag.StringVar(&logFlag, "log-file", "", "")
flag.Parse()
if *configFile == "" {
*configFile = configFlag
}
if *logFile == "" {
*logFile = logFlag
}
if *configFile == "" {
return fmt.Errorf("configuration file was not specified")
}
if *logFile == "" {
return fmt.Errorf("log file was not specified")
}
return nil
}
func resolveLogFile(contents *logs.LogFile, configuration *config.Configuration) (*library.Library, error) {
wrapError := func(err error) error {
return fmt.Errorf("unable to resolve log file: \n\t%w", err)
}
if contents == nil || contents.Len() == 0 {
return nil, fmt.Errorf("log file contains no contents")
} else if configuration == nil || (configuration.Name == "" && configuration.Rules == nil) {
return nil, fmt.Errorf("configuration file has no contents")
} else if configuration.Name == "" {
return nil, fmt.Errorf("configuration file contains no log name")
} else if configuration.Rules == nil || len(configuration.Rules) == 0 {
return nil, fmt.Errorf("configuration does not have any rules")
}
ret_library := library.Library.New(library.Library{}, configuration.Name)
fmt.Println("Resolving Log File")
for i := 0; i < len(configuration.Rules); i++ {
currentRuleData, err := config.ResolveRule(contents, &configuration.Rules[i])
if err != nil {
return nil, wrapError(err)
}
ret_library.AddRuleData(configuration.Rules[i].Name, currentRuleData)
}
fmt.Println("Log File Resolved")
return ret_library, nil
}
func printSummary(library *library.Library) error {
wrapError := func(err error) error {
return fmt.Errorf("unable to print summary: \n\t%w", err)
}
libraryName, err := library.Name()
if err != nil {
return wrapError(err)
}
fmt.Printf("\n--------------- %s Log File Summary ---------------\n", libraryName)
libraryKeys := library.LibraryKeys()
for _, rule := range libraryKeys {
fmt.Printf("Rule: %s\n", rule)
ruleData, _ := library.RuleData(rule)
summaryDataLen := ruleData.SummaryDataLen()
if summaryDataLen == 0 {
fmt.Println("No summary lines provided.")
} else {
for i := 0; i < summaryDataLen; i++ {
fmt.Println("\t", ruleData.SummaryData(i))
}
}
fmt.Println()
}
return nil
}
func main() {
logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime)
defer func() {
if err := recover(); err != nil {
logger.Fatal("\nAn issue occurred:", err)
}
}()
CONFIG_FILE := os.Getenv("ATHENA_CONFIG_FILE")
LOG_FILE := os.Getenv("ATHENA_LOG_FILE")
parseFlags(&CONFIG_FILE, &LOG_FILE)
logger.Print("Athena v1.0.0 Starting")
fmt.Println("Getting Configuration File: ", CONFIG_FILE, "...")
configuration, err := config.CreateConfiguration(CONFIG_FILE)
errCheck(err)
fmt.Println("Configuration Loaded")
fmt.Println("Loading Log File: ", LOG_FILE, "... ")
logFileContents, err := logs.LoadLogFile(LOG_FILE)
errCheck(err)
fmt.Println("Log File Loaded")
library, err := resolveLogFile(logFileContents, configuration)
errCheck(err)
err = printSummary(library)
errCheck(err)
}