Skip to content

Commit ab56207

Browse files
Merge pull request #46 from sensu/manisha_recurciveDirectory
recurcive state directory creation
2 parents 9b301c4 + e2db541 commit ab56207

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
* Use summary output by default in generated events
1010
* Include files with zero matching lines in summary output
1111
* typo in long argument for invert-thresholds
12+
* creation of `state-directory` with a provided recursive path is made possible.
1213

1314
## [0.6.0] - 2022-05-05
1415

main.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,6 @@ func checkArgs(event *corev2.Event) (int, error) {
310310
if plugin.MatchExpr == "" {
311311
return sensu.CheckStateCritical, fmt.Errorf("--match-expr not specified")
312312
}
313-
if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) {
314-
err := os.Mkdir(plugin.StateDir, os.ModePerm)
315-
if err != nil {
316-
return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created", plugin.StateDir)
317-
}
318-
}
319-
if _, err := os.Stat(plugin.StateDir); err != nil {
320-
return sensu.CheckStateCritical, fmt.Errorf("unexpected error accessing --state-directory %s: %s", plugin.StateDir, err)
321-
}
322313
if plugin.DryRun {
323314
plugin.Verbose = true
324315
fmt.Printf("LogFileExpr: %s StateDir: %s\n", plugin.LogFileExpr, plugin.StateDir)
@@ -344,7 +335,7 @@ func main() {
344335
if err != nil {
345336
panic(err)
346337
}
347-
check := sensu.NewGoCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin)
338+
check := sensu.NewCheck(&plugin.PluginConfig, options, checkArgs, executeCheck, useStdin)
348339
check.Execute()
349340
}
350341

@@ -540,7 +531,8 @@ func processLogFile(file string, enc *json.Encoder) (int, error) {
540531
state.Offset = int64(offset + bytesRead)
541532
state.MatchExpr = plugin.MatchExpr
542533
if plugin.Verbose {
543-
fmt.Printf("File %s Match Status %v BytesRead: %v New Offset: %v\n", file, status, bytesRead, state.Offset)
534+
fmt.Printf("File %s Match Status %v BytesRead: %v"+
535+
" New Offset: %v\n", file, status, bytesRead, state.Offset)
544536
}
545537

546538
if err := setState(state, stateFile); err != nil {
@@ -594,6 +586,19 @@ func setStatus(currentStatus int, numMatches int) int {
594586
func executeCheck(event *corev2.Event) (int, error) {
595587
var status int
596588
status = 0
589+
590+
//create state directory if not existing already
591+
if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) {
592+
//creating recursive directories incase
593+
err := os.MkdirAll(plugin.StateDir, os.ModePerm)
594+
if err != nil {
595+
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)
596+
}
597+
}
598+
if _, err := os.Stat(plugin.StateDir); err != nil {
599+
return sensu.CheckStateCritical, fmt.Errorf("unexpected error accessing --state-directory %s: %s", plugin.StateDir, err.Error())
600+
}
601+
597602
logs, e := buildLogArray()
598603
if e != nil {
599604
return sensu.CheckStateCritical, e

0 commit comments

Comments
 (0)