Skip to content

Commit

Permalink
When the witness file is absent, create it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Honig committed Jan 20, 2021
1 parent 1971450 commit a52e1e7
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions pendingtime/pendingtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/sensu-community/sensu-plugin-sdk/sensu"
)

// Given a path, the time in seconds since the file was last updated is returned
// GetLastAccess Given a path, the time in seconds since the file was last updated is returned
func GetLastAccess(path string) (int64, error){
fileinfo, err := os.Stat(path)
if err != nil {
Expand All @@ -21,30 +21,49 @@ func GetLastAccess(path string) (int64, error){
return atime, nil
}

// TouchLastAccess touch the witness file to track the time
func TouchLastAccess(path string) (error){
af, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("Can not open file for writing %s: %s", path, err)
return fmt.Errorf("can not open file for writing %s: %s", path, err)
}
defer af.Close()

// Truncate the file to write the new date
err = af.Truncate(0)
if err != nil {
return fmt.Errorf("Truncate failed on %s: %s\n", path, err)
return fmt.Errorf("truncate failed on %s: %s", path, err)
}

_, err = fmt.Fprintf(af, "%s", time.Now().UTC().Format(time.UnixDate))
if err != nil {
return fmt.Errorf("Writing time failed: %s\n", err)
return fmt.Errorf("writing time failed: %s", err)
}

return nil
}

// This function returns the status, and the days since the patch counter was last 0
// CreateIfNotExist if the file is not present create it
func CreateIfNotExist(path string) (error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
err2 := TouchLastAccess(path)
if err2 != nil {
return err2
}
}
return nil
}

// PendingTime This function returns the status, and the days since the patch counter was last 0
// status, days since no patches, error
func PendingTime(path string, pending int, daysWarn int, daysCrit int) (int, int, error){
// Create the empty file if it doesn't exist yet
err := CreateIfNotExist(path)
if err != nil {
return 0, 0, err
}

// Only when no patches are outstanding touch the file
if pending == 0 {
err := TouchLastAccess(path)
Expand Down

0 comments on commit a52e1e7

Please sign in to comment.