Skip to content
Merged
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
* creation of `state-directory` with a provided recursive path is made possible.

## [0.6.0] - 2022-05-05

Expand Down
27 changes: 16 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,19 @@ func setStatus(currentStatus int, numMatches int) int {
func executeCheck(event *corev2.Event) (int, error) {
var status int
status = 0

//create state directory if not existing already
if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) {
//creating recursive directories incase
err := os.MkdirAll(plugin.StateDir, os.ModePerm)
if err != 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.Error())
}

logs, e := buildLogArray()
if e != nil {
return sensu.CheckStateCritical, e
Expand Down