From 2c4b190b21ed8639518f32a5fefce8fa5e5c686d Mon Sep 17 00:00:00 2001 From: tbr-sparklane Date: Mon, 30 Nov 2020 18:07:55 +0100 Subject: [PATCH] ignore times and duration optins to ignore down check result during recurring maintenance with service disruption for example - fix timezone --- httpchecker.go | 11 +++++++++-- tlschecker.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/httpchecker.go b/httpchecker.go index 248e716..f5530dd 100644 --- a/httpchecker.go +++ b/httpchecker.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net" "net/http" + "os" "strings" "time" @@ -200,10 +201,16 @@ func (c HTTPChecker) conclude(result Result) Result { result.ThresholdRTT = c.ThresholdRTT if len(c.IgnoreTimes) > 0 && c.IgnoreDuration > 0 { - now := time.Now().UTC() + timeZone := os.Getenv("TZ") + if timeZone == "" { + timeZone = "Europe/Paris" + } + location, _ := time.LoadLocation(timeZone) + now := time.Now().UTC().In(location) for i := range c.IgnoreTimes { - start, _ := time.Parse("15:04:05", c.IgnoreTimes[i]) + start, _ := time.ParseInLocation("15:04:05", c.IgnoreTimes[i], location) start = start.AddDate(now.Year(), int(now.Month())-1, now.Day()-1) + start = start.In(location) end := start.Add(c.IgnoreDuration) if now.After(start) && now.Before(end) { result.Healthy = true diff --git a/tlschecker.go b/tlschecker.go index b5d7a23..ac6005f 100644 --- a/tlschecker.go +++ b/tlschecker.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "net" + "os" "time" ) @@ -143,10 +144,16 @@ func (c TLSChecker) conclude(conns []*tls.Conn, result Result) Result { }() if len(c.IgnoreTimes) > 0 && c.IgnoreDuration > 0 { - now := time.Now().UTC() + timeZone := os.Getenv("TZ") + if timeZone == "" { + timeZone = "Europe/Paris" + } + location, _ := time.LoadLocation(timeZone) + now := time.Now().UTC().In(location) for i := range c.IgnoreTimes { - start, _ := time.Parse("15:04:05", c.IgnoreTimes[i]) + start, _ := time.ParseInLocation("15:04:05", c.IgnoreTimes[i], location) start = start.AddDate(now.Year(), int(now.Month())-1, now.Day()-1) + start = start.In(location) end := start.Add(c.IgnoreDuration) if now.After(start) && now.Before(end) { result.Healthy = true