From b0dbe01a8adc2bea6a5cacf917b1d9347250f5de Mon Sep 17 00:00:00 2001 From: manisha kumari Date: Tue, 26 Mar 2024 12:50:09 +0530 Subject: [PATCH 1/5] Error handling with incorrect argument --- main.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 13799d9..b277497 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/sirupsen/logrus" "io" "os" "path/filepath" @@ -313,7 +314,11 @@ func checkArgs(event *corev2.Event) (int, error) { if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) { err := os.Mkdir(plugin.StateDir, os.ModePerm) if err != nil { - return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created", plugin.StateDir) + //err = fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory.", plugin.StateDir) + //fmt.Println(err.Error()) + //return sensu.CheckStateCritical, nil + logrus.Exit(sensu.CheckStateCritical) + return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory ", plugin.StateDir) } } if _, err := os.Stat(plugin.StateDir); err != nil { @@ -344,7 +349,9 @@ func main() { if err != nil { panic(err) } - check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin) + //check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin) + check := sensu.NewCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin) + //fmt.Println("Check==", check.) check.Execute() } @@ -540,7 +547,8 @@ func processLogFile(file string, enc *json.Encoder) (int, error) { state.Offset = int64(offset + bytesRead) state.MatchExpr = plugin.MatchExpr if plugin.Verbose { - fmt.Printf("File %s Match Status %v BytesRead: %v New Offset: %v\n", file, status, bytesRead, state.Offset) + fmt.Printf("File %s Match Status %v BytesRead: %v"+ + " New Offset: %v\n", file, status, bytesRead, state.Offset) } if err := setState(state, stateFile); err != nil { From 5312f7d97c65828f79fcf2c96e4da8b59fdd0bd7 Mon Sep 17 00:00:00 2001 From: manisha kumari Date: Tue, 26 Mar 2024 13:09:58 +0530 Subject: [PATCH 2/5] Error handling with incorrect argument --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index b277497..e6b0b5b 100644 --- a/main.go +++ b/main.go @@ -314,8 +314,8 @@ func checkArgs(event *corev2.Event) (int, error) { if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) { err := os.Mkdir(plugin.StateDir, os.ModePerm) if err != nil { - //err = fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory.", plugin.StateDir) - //fmt.Println(err.Error()) + err = fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory.", plugin.StateDir) + fmt.Println(err.Error()) //return sensu.CheckStateCritical, nil logrus.Exit(sensu.CheckStateCritical) return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory ", plugin.StateDir) From ec1d37c117c89daccb81a3fe7353976d48eeff87 Mon Sep 17 00:00:00 2001 From: manisha kumari Date: Tue, 26 Mar 2024 13:13:52 +0530 Subject: [PATCH 3/5] Error handling with incorrect argument in state-dir --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index e6b0b5b..a2eda7c 100644 --- a/main.go +++ b/main.go @@ -314,11 +314,11 @@ func checkArgs(event *corev2.Event) (int, error) { if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) { err := os.Mkdir(plugin.StateDir, os.ModePerm) if err != nil { - err = fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory.", plugin.StateDir) + err = fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory", plugin.StateDir) fmt.Println(err.Error()) - //return sensu.CheckStateCritical, nil logrus.Exit(sensu.CheckStateCritical) - return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory ", plugin.StateDir) + //return sensu.CheckStateCritical, nil + //return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory", plugin.StateDir) } } if _, err := os.Stat(plugin.StateDir); err != nil { From 29962e034ead97e9a8cc59b08f5637cb4e7df76d Mon Sep 17 00:00:00 2001 From: manisha kumari Date: Tue, 2 Apr 2024 20:23:23 +0530 Subject: [PATCH 4/5] changing the mkdir code to execute --- main.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index a2eda7c..f452b27 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/sirupsen/logrus" "io" "os" "path/filepath" @@ -311,19 +310,6 @@ func checkArgs(event *corev2.Event) (int, error) { if plugin.MatchExpr == "" { return sensu.CheckStateCritical, fmt.Errorf("--match-expr not specified") } - if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) { - err := os.Mkdir(plugin.StateDir, os.ModePerm) - if err != nil { - err = fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory", plugin.StateDir) - fmt.Println(err.Error()) - logrus.Exit(sensu.CheckStateCritical) - //return sensu.CheckStateCritical, nil - //return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory", plugin.StateDir) - } - } - if _, err := os.Stat(plugin.StateDir); err != nil { - return sensu.CheckStateCritical, fmt.Errorf("unexpected error accessing --state-directory %s: %s", plugin.StateDir, err) - } if plugin.DryRun { plugin.Verbose = true fmt.Printf("LogFileExpr: %s StateDir: %s\n", plugin.LogFileExpr, plugin.StateDir) @@ -349,9 +335,7 @@ func main() { if err != nil { panic(err) } - //check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin) check := sensu.NewCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin) - //fmt.Println("Check==", check.) check.Execute() } @@ -602,6 +586,17 @@ func setStatus(currentStatus int, numMatches int) int { func executeCheck(event *corev2.Event) (int, error) { var status int status = 0 + //create the state dir if not present + if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) { + err2 := os.Mkdir(plugin.StateDir, os.ModePerm) + if err2 != nil { + return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.Expected a correct Path to create/reach the directory", plugin.StateDir) + } + } + if _, err := os.Stat(plugin.StateDir); err != nil { + return sensu.CheckStateCritical, fmt.Errorf("unexpected error accessing --state-directory %s: %s", plugin.StateDir, err) + } + logs, e := buildLogArray() if e != nil { return sensu.CheckStateCritical, e From 636248b7866de194ff9c5c7e1ef2dee1f2c63ad2 Mon Sep 17 00:00:00 2001 From: manisha kumari Date: Tue, 2 Apr 2024 20:46:28 +0530 Subject: [PATCH 5/5] changelog.md modified --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 896cbc2..51722d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Use summary output by default in generated events * Include files with zero matching lines in summary output * typo in long argument for invert-thresholds +* In case wrong argument is passed to `state-directory` only the error will be shown and not the help arguments along ## [0.6.0] - 2022-05-05