Skip to content

Commit

Permalink
recursive state di creation allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
ManishaKumari295 committed Apr 2, 2024
1 parent 94ad9cc commit f625dbb
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"encoding/json"
"errors"
"fmt"
corev2 "github.com/sensu/core/v2"
"github.com/sensu/sensu-plugin-sdk/sensu"
"io"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

corev2 "github.com/sensu/core/v2"
"github.com/sensu/sensu-plugin-sdk/sensu"
)

// Config represents the check plugin config.
Expand Down Expand Up @@ -310,16 +309,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) {
//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)
}
if plugin.DryRun {
plugin.Verbose = true
fmt.Printf("LogFileExpr: %s StateDir: %s\n", plugin.LogFileExpr, plugin.StateDir)
Expand Down Expand Up @@ -598,6 +587,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)
}

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

0 comments on commit f625dbb

Please sign in to comment.