Skip to content

Commit

Permalink
check to make sure state directory exists or abort with error
Browse files Browse the repository at this point in the history
  • Loading branch information
jspaleta committed Jul 21, 2021
1 parent 1d23c9b commit b862b40
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -34,7 +35,6 @@ type Config struct {
DisableEvent bool
DryRun bool
Verbose bool
GenerateNewEvents bool
EnableStateReset bool
CheckNameTemplate string
}
Expand Down Expand Up @@ -100,7 +100,7 @@ var (
&sensu.PluginConfigOption{
Path: "match-expr",
Env: "CHECK_LOG_MATCH_EXPR",
Argument: "match-string",
Argument: "match-expr",
Shorthand: "m",
Default: "",
Usage: "RE2 regexp matcher expression. (required)",
Expand All @@ -112,7 +112,7 @@ var (
Argument: "match-event-status",
Shorthand: "s",
Default: 1,
Usage: "RE2 regexp matcher expression.",
Usage: "Event status to return on match in generated event.",
Value: &plugin.MatchStatus,
},
&sensu.PluginConfigOption{
Expand Down Expand Up @@ -142,15 +142,6 @@ var (
Usage: "Suppresses alerts for any matches found on the first run of the plugin.",
Value: &plugin.IgnoreInitialRun,
},
&sensu.PluginConfigOption{
Path: "generate-new-events",
Env: "CHECK_LOG_GENERATE_NEW_EVENTS",
Argument: "generate-new-events",
Shorthand: "g",
Default: false,
Usage: "Generate new events on match, requires check to be configured with stdin: True",
Value: &plugin.GenerateNewEvents,
},
&sensu.PluginConfigOption{
Path: "check-name-tamplate",
Env: "CHECK_LOG_CHECK_NAME_TEMPLATE",
Expand All @@ -160,14 +151,6 @@ var (
Usage: "Check name to use in generated events",
Value: &plugin.CheckNameTemplate,
},
&sensu.PluginConfigOption{
Path: "dry-run",
Argument: "dry-run",
Shorthand: "n",
Default: false,
Usage: "Suppress generation of events and report intended actions instead. (implies verbose)",
Value: &plugin.DryRun,
},
&sensu.PluginConfigOption{
Path: "disable-event-generation",
Env: "CHECK_LOG_CHECK_DISABLE_EVENT_GENERATION",
Expand All @@ -194,6 +177,14 @@ var (
Usage: "Verbose output, useful for testing.",
Value: &plugin.Verbose,
},
&sensu.PluginConfigOption{
Path: "dry-run",
Argument: "dry-run",
Shorthand: "n",
Default: false,
Usage: "Suppress generation of events and report intended actions instead. (implies verbose)",
Value: &plugin.DryRun,
},
}
)

Expand Down Expand Up @@ -254,12 +245,16 @@ func checkArgs(event *types.Event) (int, error) {
if plugin.StateDir == "" {
return sensu.CheckStateCritical, fmt.Errorf("--state-directory not specified")
}
_, err := os.Stat(plugin.StateDir)
if errors.Is(err, os.ErrNotExist) {
return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist", plugin.StateDir)
}
if plugin.MatchExpr == "" {
return sensu.CheckStateCritical, fmt.Errorf("--match-expr not specified")
}
if plugin.DryRun {
plugin.Verbose = true
log.Printf("LogFileExpr: %s StatDir: %s\n", plugin.LogFileExpr, plugin.StateDir)
log.Printf("LogFileExpr: %s StateDir: %s\n", plugin.LogFileExpr, plugin.StateDir)
}
return sensu.CheckStateOK, nil
}
Expand Down Expand Up @@ -378,6 +373,11 @@ func executeCheck(event *types.Event) (int, error) {
log.Println("stateFile", stateFile)
}
state, err := getState(stateFile)
if err != nil {
file_errors = append(file_errors, file)
log.Printf("error couldn't get state for log file %s: %s", file, err)
continue
}
// Do we need to reset the state because the requested MatchExpr is different?
if state.MatchExpr != "" && state.MatchExpr != plugin.MatchExpr {
if plugin.EnableStateReset {
Expand All @@ -391,11 +391,6 @@ func executeCheck(event *types.Event) (int, error) {
continue
}
}
if err != nil {
file_errors = append(file_errors, file)
log.Printf("error couldn't get state for log file %s: %s", file, err)
continue
}
info, err := f.Stat()
if err != nil {
file_errors = append(file_errors, file)
Expand Down

0 comments on commit b862b40

Please sign in to comment.