2019-09-04 19:37:13 -04:00
|
|
|
package watchdog
|
2019-09-06 21:59:50 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-07-24 16:45:51 -04:00
|
|
|
"fmt"
|
2019-09-08 21:07:08 -04:00
|
|
|
"log"
|
2019-09-06 21:59:50 -04:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
2020-10-30 21:00:03 +05:30
|
|
|
|
|
|
|
|
"github.com/TwinProduction/gatus/config"
|
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
|
|
|
|
"github.com/TwinProduction/gatus/metric"
|
2020-12-31 11:49:41 +00:00
|
|
|
"github.com/TwinProduction/gatus/storage"
|
2019-09-06 21:59:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2020-09-04 21:31:28 -04:00
|
|
|
// monitoringMutex is used to prevent multiple services from being evaluated at the same time.
|
|
|
|
|
// Without this, conditions using response time may become inaccurate.
|
|
|
|
|
monitoringMutex sync.Mutex
|
2019-09-06 21:59:50 -04:00
|
|
|
)
|
|
|
|
|
|
2020-08-17 20:25:29 -04:00
|
|
|
// Monitor loops over each services and starts a goroutine to monitor each services separately
|
2019-12-04 16:44:35 -05:00
|
|
|
func Monitor(cfg *config.Config) {
|
|
|
|
|
for _, service := range cfg.Services {
|
2021-02-24 22:41:36 -05:00
|
|
|
// To prevent multiple requests from running at the same time, we'll wait for a little bit before each iteration
|
2020-04-14 19:20:00 -04:00
|
|
|
time.Sleep(1111 * time.Millisecond)
|
2021-02-24 22:41:36 -05:00
|
|
|
go monitor(service)
|
2020-04-06 18:58:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 20:25:29 -04:00
|
|
|
// monitor monitors a single service in a loop
|
2020-04-06 18:58:13 -04:00
|
|
|
func monitor(service *core.Service) {
|
2020-09-04 21:31:28 -04:00
|
|
|
cfg := config.Get()
|
2020-04-06 18:58:13 -04:00
|
|
|
for {
|
2020-10-16 23:07:14 -04:00
|
|
|
if !cfg.DisableMonitoringLock {
|
|
|
|
|
// By placing the lock here, we prevent multiple services from being monitored at the exact same time, which
|
|
|
|
|
// could cause performance issues and return inaccurate results
|
|
|
|
|
monitoringMutex.Lock()
|
|
|
|
|
}
|
2020-09-04 21:31:28 -04:00
|
|
|
if cfg.Debug {
|
|
|
|
|
log.Printf("[watchdog][monitor] Monitoring serviceName=%s", service.Name)
|
|
|
|
|
}
|
2020-09-24 19:49:32 -04:00
|
|
|
result := service.EvaluateHealth()
|
2020-04-06 18:58:13 -04:00
|
|
|
metric.PublishMetricsForService(service, result)
|
2020-11-26 18:09:01 -05:00
|
|
|
UpdateServiceStatuses(service, result)
|
2020-07-24 16:45:51 -04:00
|
|
|
var extra string
|
|
|
|
|
if !result.Success {
|
|
|
|
|
extra = fmt.Sprintf("responseBody=%s", result.Body)
|
|
|
|
|
}
|
2020-04-06 18:58:13 -04:00
|
|
|
log.Printf(
|
2020-09-04 21:31:28 -04:00
|
|
|
"[watchdog][monitor] Monitored serviceName=%s; success=%v; errors=%d; requestDuration=%s; %s",
|
2020-04-06 18:58:13 -04:00
|
|
|
service.Name,
|
2020-09-04 21:31:28 -04:00
|
|
|
result.Success,
|
2020-04-06 18:58:13 -04:00
|
|
|
len(result.Errors),
|
|
|
|
|
result.Duration.Round(time.Millisecond),
|
2020-07-24 16:45:51 -04:00
|
|
|
extra,
|
2020-04-06 18:58:13 -04:00
|
|
|
)
|
2020-09-19 16:22:12 -04:00
|
|
|
HandleAlerting(service, result)
|
2020-09-04 21:31:28 -04:00
|
|
|
if cfg.Debug {
|
|
|
|
|
log.Printf("[watchdog][monitor] Waiting for interval=%s before monitoring serviceName=%s again", service.Interval, service.Name)
|
|
|
|
|
}
|
2020-10-16 23:07:14 -04:00
|
|
|
if !cfg.DisableMonitoringLock {
|
|
|
|
|
monitoringMutex.Unlock()
|
|
|
|
|
}
|
2020-09-04 18:23:56 -04:00
|
|
|
time.Sleep(service.Interval)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-26 18:09:01 -05:00
|
|
|
|
|
|
|
|
// UpdateServiceStatuses updates the slice of service statuses
|
|
|
|
|
func UpdateServiceStatuses(service *core.Service, result *core.Result) {
|
2021-02-02 23:06:34 -05:00
|
|
|
storage.Get().Insert(service, result)
|
2020-11-26 18:09:01 -05:00
|
|
|
}
|