From 0f5e9228519ff54d21bc9ad1314ac47fa4260fce Mon Sep 17 00:00:00 2001 From: Shivam Sandbhor Date: Mon, 28 Feb 2022 19:27:59 +0530 Subject: [PATCH] Warn when log file in explain command is large. (#1293) * Warn when log file in explain command is large. Signed-off-by: Shivam Sandbhor --- cmd/crowdsec-cli/explain.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/crowdsec-cli/explain.go b/cmd/crowdsec-cli/explain.go index 92a8cc7d9..14c2faed7 100644 --- a/cmd/crowdsec-cli/explain.go +++ b/cmd/crowdsec-cli/explain.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" "os" "os/exec" @@ -51,7 +52,6 @@ cscli explain --dsn "file://myfile.log" --type nginx defer f.Close() _, err = f.WriteString(logLine) - if err != nil { log.Fatal(err) } @@ -63,6 +63,10 @@ cscli explain --dsn "file://myfile.log" --type nginx log.Fatalf("unable to get absolue path of '%s', exiting", logFile) } dsn = fmt.Sprintf("file://%s", absolutePath) + lineCount := getLineCountForFile(absolutePath) + if lineCount > 100 { + log.Warnf("log file contains %d lines. This may take lot of resources.", lineCount) + } } if dsn == "" { @@ -108,3 +112,17 @@ cscli explain --dsn "file://myfile.log" --type nginx return cmdExplain } + +func getLineCountForFile(filepath string) int { + f, err := os.Open(filepath) + if err != nil { + log.Fatalf("unable to open log file %s", filepath) + } + defer f.Close() + lc := 0 + fs := bufio.NewScanner(f) + for fs.Scan() { + lc++ + } + return lc +}