2019-09-04 23:37:13 +00:00
|
|
|
package watchdog
|
2019-09-07 01:59:50 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-19 02:29:15 +00:00
|
|
|
"context"
|
2019-09-09 01:07:08 +00:00
|
|
|
"log"
|
2019-09-07 01:59:50 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2020-10-30 15:30:03 +00:00
|
|
|
|
2022-12-06 06:41:09 +00:00
|
|
|
"github.com/TwiN/gatus/v5/alerting"
|
|
|
|
"github.com/TwiN/gatus/v5/config"
|
|
|
|
"github.com/TwiN/gatus/v5/config/maintenance"
|
|
|
|
"github.com/TwiN/gatus/v5/core"
|
|
|
|
"github.com/TwiN/gatus/v5/metrics"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store"
|
2019-09-07 01:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-10-23 20:47:12 +00:00
|
|
|
// monitoringMutex is used to prevent multiple endpoint from being evaluated at the same time.
|
2020-09-05 01:31:28 +00:00
|
|
|
// Without this, conditions using response time may become inaccurate.
|
|
|
|
monitoringMutex sync.Mutex
|
2021-05-19 02:29:15 +00:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancelFunc context.CancelFunc
|
2019-09-07 01:59:50 +00:00
|
|
|
)
|
|
|
|
|
2021-10-23 20:47:12 +00:00
|
|
|
// Monitor loops over each endpoint and starts a goroutine to monitor each endpoint separately
|
2019-12-04 21:44:35 +00:00
|
|
|
func Monitor(cfg *config.Config) {
|
2021-05-19 02:29:15 +00:00
|
|
|
ctx, cancelFunc = context.WithCancel(context.Background())
|
2021-10-23 20:47:12 +00:00
|
|
|
for _, endpoint := range cfg.Endpoints {
|
|
|
|
if endpoint.IsEnabled() {
|
2021-09-22 04:04:51 +00:00
|
|
|
// To prevent multiple requests from running at the same time, we'll wait for a little before each iteration
|
2021-10-28 23:35:46 +00:00
|
|
|
time.Sleep(777 * time.Millisecond)
|
2021-10-23 20:47:12 +00:00
|
|
|
go monitor(endpoint, cfg.Alerting, cfg.Maintenance, cfg.DisableMonitoringLock, cfg.Metrics, cfg.Debug, ctx)
|
2021-09-18 15:52:11 +00:00
|
|
|
}
|
2020-04-06 22:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 23:35:46 +00:00
|
|
|
// monitor a single endpoint in a loop
|
2021-10-23 20:47:12 +00:00
|
|
|
func monitor(endpoint *core.Endpoint, alertingConfig *alerting.Config, maintenanceConfig *maintenance.Config, disableMonitoringLock, enabledMetrics, debug bool, ctx context.Context) {
|
2021-05-19 02:29:15 +00:00
|
|
|
// Run it immediately on start
|
2021-10-23 20:47:12 +00:00
|
|
|
execute(endpoint, alertingConfig, maintenanceConfig, disableMonitoringLock, enabledMetrics, debug)
|
2021-05-19 02:29:15 +00:00
|
|
|
// Loop for the next executions
|
2020-04-06 22:58:13 +00:00
|
|
|
for {
|
2021-05-19 02:29:15 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-10-23 20:47:12 +00:00
|
|
|
log.Printf("[watchdog][monitor] Canceling current execution of group=%s; endpoint=%s", endpoint.Group, endpoint.Name)
|
2021-05-19 02:29:15 +00:00
|
|
|
return
|
2021-10-23 20:47:12 +00:00
|
|
|
case <-time.After(endpoint.Interval):
|
|
|
|
execute(endpoint, alertingConfig, maintenanceConfig, disableMonitoringLock, enabledMetrics, debug)
|
2020-09-05 01:31:28 +00:00
|
|
|
}
|
2021-05-19 02:29:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 20:47:12 +00:00
|
|
|
func execute(endpoint *core.Endpoint, alertingConfig *alerting.Config, maintenanceConfig *maintenance.Config, disableMonitoringLock, enabledMetrics, debug bool) {
|
2021-05-19 02:29:15 +00:00
|
|
|
if !disableMonitoringLock {
|
2021-10-23 20:47:12 +00:00
|
|
|
// By placing the lock here, we prevent multiple endpoints from being monitored at the exact same time, which
|
2021-05-19 02:29:15 +00:00
|
|
|
// could cause performance issues and return inaccurate results
|
|
|
|
monitoringMutex.Lock()
|
|
|
|
}
|
|
|
|
if debug {
|
2021-10-23 20:47:12 +00:00
|
|
|
log.Printf("[watchdog][execute] Monitoring group=%s; endpoint=%s", endpoint.Group, endpoint.Name)
|
2021-05-19 02:29:15 +00:00
|
|
|
}
|
2021-10-23 20:47:12 +00:00
|
|
|
result := endpoint.EvaluateHealth()
|
2021-05-19 02:29:15 +00:00
|
|
|
if enabledMetrics {
|
2022-06-16 22:55:51 +00:00
|
|
|
metrics.PublishMetricsForEndpoint(endpoint, result)
|
2021-05-19 02:29:15 +00:00
|
|
|
}
|
2021-10-23 20:47:12 +00:00
|
|
|
UpdateEndpointStatuses(endpoint, result)
|
2021-05-19 02:29:15 +00:00
|
|
|
log.Printf(
|
2021-10-23 20:47:12 +00:00
|
|
|
"[watchdog][execute] Monitored group=%s; endpoint=%s; success=%v; errors=%d; duration=%s",
|
|
|
|
endpoint.Group,
|
|
|
|
endpoint.Name,
|
2021-05-19 02:29:15 +00:00
|
|
|
result.Success,
|
|
|
|
len(result.Errors),
|
|
|
|
result.Duration.Round(time.Millisecond),
|
|
|
|
)
|
2021-09-22 04:04:51 +00:00
|
|
|
if !maintenanceConfig.IsUnderMaintenance() {
|
2021-12-03 02:05:17 +00:00
|
|
|
// TODO: Consider moving this after the monitoring lock is unlocked? I mean, how much noise can a single alerting provider cause...
|
2021-10-23 20:47:12 +00:00
|
|
|
HandleAlerting(endpoint, result, alertingConfig, debug)
|
2021-09-22 04:04:51 +00:00
|
|
|
} else if debug {
|
|
|
|
log.Println("[watchdog][execute] Not handling alerting because currently in the maintenance window")
|
|
|
|
}
|
2021-05-19 02:29:15 +00:00
|
|
|
if debug {
|
2021-10-23 20:47:12 +00:00
|
|
|
log.Printf("[watchdog][execute] Waiting for interval=%s before monitoring group=%s endpoint=%s again", endpoint.Interval, endpoint.Group, endpoint.Name)
|
2021-05-19 02:29:15 +00:00
|
|
|
}
|
|
|
|
if !disableMonitoringLock {
|
|
|
|
monitoringMutex.Unlock()
|
2020-09-04 22:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 23:09:01 +00:00
|
|
|
|
2021-10-23 20:47:12 +00:00
|
|
|
// UpdateEndpointStatuses updates the slice of endpoint statuses
|
|
|
|
func UpdateEndpointStatuses(endpoint *core.Endpoint, result *core.Result) {
|
2021-10-28 23:35:46 +00:00
|
|
|
if err := store.Get().Insert(endpoint, result); err != nil {
|
2021-10-23 20:47:12 +00:00
|
|
|
log.Println("[watchdog][UpdateEndpointStatuses] Failed to insert data in storage:", err.Error())
|
2021-09-22 04:04:51 +00:00
|
|
|
}
|
2020-11-26 23:09:01 +00:00
|
|
|
}
|
2021-05-19 02:29:15 +00:00
|
|
|
|
2021-10-23 20:47:12 +00:00
|
|
|
// Shutdown stops monitoring all endpoints
|
2021-05-19 02:29:15 +00:00
|
|
|
func Shutdown() {
|
|
|
|
cancelFunc()
|
|
|
|
}
|