From b121999c1f620e37b069030cab15a41237eb9e8a Mon Sep 17 00:00:00 2001 From: Lutfa Ibtihaji Ilham Date: Sun, 12 Feb 2023 13:44:16 +0700 Subject: [PATCH 1/4] Add timedate util --- util/timedate.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 util/timedate.go diff --git a/util/timedate.go b/util/timedate.go new file mode 100644 index 0000000..d8e00a3 --- /dev/null +++ b/util/timedate.go @@ -0,0 +1,29 @@ +package util + +import ( + "fmt" + "time" +) + +// Define constant YYMMDD as the date format used in the function +const ( + YYMMDD = "2006/01/02 15:04:05" +) + +// TimeDifference calculates the difference in minutes between two timestamps +// timeFrom and timeNow, passed as strings in the format defined in YYMMDD +// The function returns a string with the difference in minutes +func TimeDifference(timeFrom string, timeNow string) string { + parsedTimeFrom, err := time.Parse(YYMMDD, timeFrom) + if err != nil { + fmt.Printf("error: parsing time from: %v\n", err) + } + + parsedTimeNow, err := time.Parse(YYMMDD, timeNow) + if err != nil { + fmt.Printf("error: parsing time now: %v\n", err) + } + + diff := parsedTimeNow.Sub(parsedTimeFrom).Minutes() + return fmt.Sprintf("%d minutes", uint(diff)) +} From 6b8fb1fcdf970beaacdaf698c4bd71eadbc05ae3 Mon Sep 17 00:00:00 2001 From: Lutfa Ibtihaji Ilham Date: Sun, 12 Feb 2023 13:47:15 +0700 Subject: [PATCH 2/4] Add down time interface --- service.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service.go b/service.go index b7f9635..eac8ae5 100644 --- a/service.go +++ b/service.go @@ -47,6 +47,12 @@ type Service interface { // IsRecover will return recovered status IsRecover() bool + // LastDownTime will set last down time of service to current time + SetLastDownTimeNow() + + // GetDownTimeDiff will return down time service difference in minutes + GetDownTimeDiff() string + // SetCheckInterval will set check interval to service SetCheckInterval(interval int) From 3035fd787afef3b9a735ca9bc00c47c9bd89b527 Mon Sep 17 00:00:00 2001 From: Lutfa Ibtihaji Ilham Date: Sun, 12 Feb 2023 13:48:30 +0700 Subject: [PATCH 3/4] Implement down time --- services/dummy/dummy.go | 16 ++++++++++++++++ services/mongodb/mongo.go | 19 +++++++++++++++++-- services/mysqldb/mysql.go | 18 +++++++++++++++++- services/postgres/postgres.go | 18 +++++++++++++++++- services/redisdb/redis.go | 18 +++++++++++++++++- services/web/web.go | 19 ++++++++++++++++++- 6 files changed, 102 insertions(+), 6 deletions(-) diff --git a/services/dummy/dummy.go b/services/dummy/dummy.go index 16832b6..402124a 100644 --- a/services/dummy/dummy.go +++ b/services/dummy/dummy.go @@ -3,12 +3,16 @@ package dummy import ( "log" "math/rand" + "time" + + "github.com/telkomdev/tob/util" ) // Dummy service type Dummy struct { url string recovered bool + lastDownTime string enabled bool verbose bool logger *log.Logger @@ -78,6 +82,18 @@ func (d *Dummy) IsRecover() bool { return d.recovered } +// LastDownTime will set last down time of service to current time +func (d *Dummy) SetLastDownTimeNow() { + if d.recovered { + d.lastDownTime = time.Now().Format(util.YYMMDD) + } +} + +// GetDownTimeDiff will return down time service difference in minutes +func (d *Dummy) GetDownTimeDiff() string { + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) +} + // SetCheckInterval will set check interval to service func (d *Dummy) SetCheckInterval(interval int) { d.checkInterval = interval diff --git a/services/mongodb/mongo.go b/services/mongodb/mongo.go index 756faf8..1f1b598 100644 --- a/services/mongodb/mongo.go +++ b/services/mongodb/mongo.go @@ -2,16 +2,19 @@ package mongodb import ( "context" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" "log" "time" + + "github.com/telkomdev/tob/util" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) // Mongo service type Mongo struct { url string recovered bool + lastDownTime string enabled bool verbose bool logger *log.Logger @@ -119,6 +122,18 @@ func (d *Mongo) IsRecover() bool { return d.recovered } +// LastDownTime will set last down time of service to current time +func (d *Mongo) SetLastDownTimeNow() { + if d.recovered { + d.lastDownTime = time.Now().Format(util.YYMMDD) + } +} + +// GetDownTimeDiff will return down time service difference in minutes +func (d *Mongo) GetDownTimeDiff() string { + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) +} + // SetCheckInterval will set check interval to service func (d *Mongo) SetCheckInterval(interval int) { d.checkInterval = interval diff --git a/services/mysqldb/mysql.go b/services/mysqldb/mysql.go index 01ab972..ed5f7dd 100644 --- a/services/mysqldb/mysql.go +++ b/services/mysqldb/mysql.go @@ -2,14 +2,18 @@ package mysqldb import ( "database/sql" - _ "github.com/go-sql-driver/mysql" "log" + "time" + + _ "github.com/go-sql-driver/mysql" + "github.com/telkomdev/tob/util" ) // MySQL service type MySQL struct { url string recovered bool + lastDownTime string enabled bool verbose bool logger *log.Logger @@ -106,6 +110,18 @@ func (d *MySQL) IsRecover() bool { return d.recovered } +// LastDownTime will set last down time of service to current time +func (d *MySQL) SetLastDownTimeNow() { + if d.recovered { + d.lastDownTime = time.Now().Format(util.YYMMDD) + } +} + +// GetDownTimeDiff will return down time service difference in minutes +func (d *MySQL) GetDownTimeDiff() string { + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) +} + // SetCheckInterval will set check interval to service func (d *MySQL) SetCheckInterval(interval int) { d.checkInterval = interval diff --git a/services/postgres/postgres.go b/services/postgres/postgres.go index 211e797..8938175 100644 --- a/services/postgres/postgres.go +++ b/services/postgres/postgres.go @@ -2,14 +2,18 @@ package postgres import ( "database/sql" - _ "github.com/lib/pq" "log" + "time" + + _ "github.com/lib/pq" + "github.com/telkomdev/tob/util" ) // Postgres service type Postgres struct { url string recovered bool + lastDownTime string enabled bool verbose bool logger *log.Logger @@ -106,6 +110,18 @@ func (d *Postgres) IsRecover() bool { return d.recovered } +// LastDownTime will set last down time of service to current time +func (d *Postgres) SetLastDownTimeNow() { + if d.recovered { + d.lastDownTime = time.Now().Format(util.YYMMDD) + } +} + +// GetDownTimeDiff will return down time service difference in minutes +func (d *Postgres) GetDownTimeDiff() string { + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) +} + // SetCheckInterval will set check interval to service func (d *Postgres) SetCheckInterval(interval int) { d.checkInterval = interval diff --git a/services/redisdb/redis.go b/services/redisdb/redis.go index b5f40a3..b89719d 100644 --- a/services/redisdb/redis.go +++ b/services/redisdb/redis.go @@ -2,15 +2,19 @@ package redisdb import ( "context" - "github.com/redis/go-redis/v9" "log" "net/url" + "time" + + "github.com/redis/go-redis/v9" + "github.com/telkomdev/tob/util" ) // Redis service type Redis struct { url string recovered bool + lastDownTime string enabled bool verbose bool logger *log.Logger @@ -128,6 +132,18 @@ func (d *Redis) IsRecover() bool { return d.recovered } +// LastDownTime will set last down time of service to current time +func (d *Redis) SetLastDownTimeNow() { + if d.recovered { + d.lastDownTime = time.Now().Format(util.YYMMDD) + } +} + +// GetDownTimeDiff will return down time service difference in minutes +func (d *Redis) GetDownTimeDiff() string { + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) +} + // SetCheckInterval will set check interval to service func (d *Redis) SetCheckInterval(interval int) { d.checkInterval = interval diff --git a/services/web/web.go b/services/web/web.go index 311bc9b..64309ae 100644 --- a/services/web/web.go +++ b/services/web/web.go @@ -2,14 +2,19 @@ package web import ( "fmt" - "github.com/telkomdev/tob/httpx" "log" + "time" + + "github.com/telkomdev/tob/httpx" + "github.com/telkomdev/tob/util" ) // Web service type Web struct { url string recovered bool + serviceName string + lastDownTime string enabled bool verbose bool logger *log.Logger @@ -92,6 +97,18 @@ func (d *Web) IsRecover() bool { return d.recovered } +// LastDownTime will set last down time of service to current time +func (d *Web) SetLastDownTimeNow() { + if d.recovered { + d.lastDownTime = time.Now().Format(util.YYMMDD) + } +} + +// GetDownTimeDiff will return down time service difference in minutes +func (d *Web) GetDownTimeDiff() string { + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) +} + // SetCheckInterval will set check interval to service func (d *Web) SetCheckInterval(interval int) { d.checkInterval = interval From 3fa38f351bad55b88185422f41b785c91e4bbff0 Mon Sep 17 00:00:00 2001 From: Lutfa Ibtihaji Ilham Date: Sun, 12 Feb 2023 13:49:04 +0700 Subject: [PATCH 4/4] Add down time message --- runner.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/runner.go b/runner.go index 2f389d0..c43fc1b 100644 --- a/runner.go +++ b/runner.go @@ -4,6 +4,9 @@ import ( "context" "errors" "fmt" + "net/url" + "time" + "github.com/telkomdev/tob/config" "github.com/telkomdev/tob/services/dummy" "github.com/telkomdev/tob/services/mongodb" @@ -11,8 +14,6 @@ import ( "github.com/telkomdev/tob/services/postgres" "github.com/telkomdev/tob/services/redisdb" "github.com/telkomdev/tob/services/web" - "net/url" - "time" ) // Runner the tob runner @@ -167,6 +168,8 @@ func healthCheck(n string, s Service, t *time.Ticker, waiter Waiter, notificator resp := s.Ping() respStr := string(resp) if respStr == NotOk && s.IsRecover() { + // set last downtime + s.SetLastDownTimeNow() // set recover to false s.SetRecover(false) @@ -186,7 +189,7 @@ func healthCheck(n string, s Service, t *time.Ticker, waiter Waiter, notificator for _, notificator := range notificators { if notificator.IsEnabled() { - err := notificator.Send(fmt.Sprintf("%s is UP", n)) + err := notificator.Send(fmt.Sprintf("%s is UP. It was down for %s", n, s.GetDownTimeDiff())) if err != nil { Logger.Println(err) }