@@ -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 {
594586func 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