added file size in metadata

This commit is contained in:
2025-11-04 12:11:54 +01:00
parent d018b9bf1f
commit 321422d1d7
7 changed files with 118 additions and 73 deletions
+6 -7
View File
@@ -7,12 +7,12 @@ import (
)
type StatsByIp struct {
IpAddress string `json:"ipAddress"`
TotalLogs int `json:"totalLogs"`
TotalFound int `json:"totalFound"`
TotalBanned int `json:"totalBanned"`
TotalUnbanned int `json:"totalUnbanned"`
Country string `json:"county"`
IpAddress string `json:"ipAddress"`
TotalLogs int `json:"totalLogs"`
TotalFound int `json:"totalFound"`
TotalBanned int `json:"totalBanned"`
TotalUnbanned int `json:"totalUnbanned"`
Country string `json:"county"`
}
func analyseLogs() {
@@ -73,7 +73,6 @@ func analyseLogs() {
}
func analyseExtractedData() {
totalCounter := make(map[string]float64)
-5
View File
@@ -8,11 +8,6 @@ import (
type function func()
func main() {
//timedRun(parseLogsInJson)
//timedRun(analyseLogs)
//timedRun(analyseExtractedData)
//timedRun(starter)
starter()
}
+9 -6
View File
@@ -9,7 +9,8 @@ import (
)
type State struct {
Offset int64 `json:"offset"`
Offset int64 `json:"offset"`
ParsedFileSize int64 `json:"size"`
}
func starter() {
@@ -20,7 +21,7 @@ func starter() {
flag.StringVar(source, "s", "", "Source Log File (shorthand)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s --destDir <dir> --source <file>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage: %s --destDir=<dir> --source=<file>\n", os.Args[0])
flag.PrintDefaults()
}
@@ -31,15 +32,16 @@ func starter() {
os.Exit(1)
}
checkParameters(*destinationDirectory, *source)
stateFile := checkParameters(*destinationDirectory, *source)
parseFile(*stateFile, *source, *destinationDirectory)
}
/*
* TODO: This function does not check read/write permissions yet
*/
func checkParameters(destinationDirectory string, source string) {
func checkParameters(destinationDirectory string, source string) (stateFilePath *string) {
if _, err := os.Stat(source); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: source file does not exist: %s\n", source)
@@ -66,7 +68,7 @@ func checkParameters(destinationDirectory string, source string) {
os.Exit(1)
}
parseFile(stateFile, source, destinationDirectory)
return &stateFile
}
func initState(destinationDirectory string) (stateFilePath string, err error) {
@@ -81,6 +83,7 @@ func initState(destinationDirectory string) (stateFilePath string, err error) {
state := State{
Offset: 0,
ParsedFileSize: 0,
}
data, err := json.MarshalIndent(state, "", " ")
if err != nil {
+12 -54
View File
@@ -32,7 +32,8 @@ func parseFile(stateFilePath string, logFilePath string, destinationDirectory st
destinationFilePath := filepath.Join(destinationDirectory, "parsed.json")
// Load metadata
offset := checkState(stateFilePath).Offset
metadata := checkState(stateFilePath)
offset := metadata.Offset
// Check if the log file has rolled over
file, err := os.Open(logFilePath)
@@ -105,63 +106,20 @@ func parseFile(stateFilePath string, logFilePath string, destinationDirectory st
jsonData, err := json.MarshalIndent(logs, "", " ")
_ = os.WriteFile(destinationFilePath, jsonData, 0644)
newOffset, _ := file.Seek(0, io.SeekCurrent)
newState := State{
Offset: newOffset,
}
updateState(stateFilePath, newState)
}
func parseLogsInJson() {
data, err := os.Open(LogFile)
// Get parsed log file size
logFile, err := os.Open(destinationFilePath)
if err != nil {
fmt.Printf("Error opening file: $%v", err)
return
}
defer data.Close()
defer logFile.Close()
stat, _ = logFile.Stat()
parsedLogfileSize := stat.Size()
logs := []Logs{}
const lenTimestamp = 23
dateRegex, _ := regexp.Compile(`\d{4}-\d{2}-\d{2}`)
handlerRegex, _ := regexp.Compile(`fail2ban\.\w+`)
ipRegex, _ := regexp.Compile(`(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
levelRegex, _ := regexp.Compile(`\s*(?:[A-Z]+)\s+`)
serviceRegex, _ := regexp.Compile(`\s*(?:\[[a-z]+\])\s+`)
actionRegex, _ := regexp.Compile(`(Found|already banned|Ban|Unban)`)
scanner := bufio.NewScanner(data)
logEntry := Logs{}
for scanner.Scan() {
line := scanner.Text()
if len(line) < lenTimestamp {
continue
} else if !dateRegex.MatchString(line[:lenTimestamp]) {
continue
}
timestamp := line[:lenTimestamp]; timestamp = strings.TrimSpace(timestamp)
logString := line[lenTimestamp:]
ipAddress := strings.TrimSpace(ipRegex.FindString(logString))
handler := strings.TrimSpace(handlerRegex.FindString(logString))
level := strings.TrimSpace(levelRegex.FindString(logString))
service := strings.TrimSpace(serviceRegex.FindString(logString))
action := strings.TrimSpace(actionRegex.FindString(logString))
logEntry.IpAddress = ipAddress
logEntry.Timestamp = timestamp
logEntry.Handler = handler
logEntry.Level = level
logEntry.Source = service
logEntry.Message = action
logs = append(logs, logEntry)
newOffset, _ := file.Seek(0, io.SeekCurrent)
newState := State{
Offset: newOffset,
ParsedFileSize: parsedLogfileSize,
}
jsonData, err := json.MarshalIndent(logs, "", " ")
err = os.WriteFile(ParsedJson, jsonData, 0644)
updateState(stateFilePath, newState)
}