From 0ccbc80797bd2f86918214b0194cab6cac59843d Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 23 Jul 2018 16:20:50 +0200 Subject: [PATCH] Add missing log cleanup Signed-off-by: Knut Ahlers --- main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/main.go b/main.go index 7b9800e..45c1324 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "os" + "path/filepath" "regexp" "strings" "sync" @@ -149,6 +150,8 @@ func main() { lastResult := newCheckResult(statusUnknown, "Uninitialized", 0) + go cleanupLogFiles() + for range time.Tick(cfg.Interval) { var ( body *bytes.Buffer @@ -180,6 +183,25 @@ func main() { } } +func cleanupLogFiles() { + for { + if err := filepath.Walk(cfg.LogDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && time.Since(info.ModTime()) > cfg.LogRetention { + return os.Remove(path) + } + + return nil + }); err != nil { + fmt.Println() + log.WithError(err).Error("Could not clean up logs") + } + } +} + func doCheck(url string, match *regexp.Regexp, responseBody io.Writer) *checkResult { req, _ := http.NewRequest("GET", url, nil)