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