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

recurcive state directory creation #46

Open
wants to merge 6 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
* creation of `state-directory` with a provided recursive path is made possible.

## [0.6.0] - 2022-05-05

Expand Down
34 changes: 20 additions & 14 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,15 +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) {
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 +334,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()
}

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