Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling with incorrect argument #45

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 14 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +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 {
return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created", 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)
Expand All @@ -344,7 +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)
check.Execute()
}

Expand Down Expand Up @@ -540,7 +531,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 {
Expand Down Expand Up @@ -594,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
Expand Down
Loading