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-06 21:59:50 -04:00
|
|
|
"github.com/TwinProduction/gatus/config"
|
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
2019-11-16 15:47:28 -05:00
|
|
|
"github.com/TwinProduction/gatus/metric"
|
2019-09-08 21:07:08 -04:00
|
|
|
"log"
|
2019-09-06 21:59:50 -04:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
serviceResults = make(map[string][]*core.Result)
|
|
|
|
|
rwLock sync.RWMutex
|
|
|
|
|
)
|
|
|
|
|
|
2020-08-17 20:25:29 -04:00
|
|
|
// GetServiceResults returns a list of the last 20 results for each services
|
2019-09-06 21:59:50 -04:00
|
|
|
func GetServiceResults() *map[string][]*core.Result {
|
|
|
|
|
return &serviceResults
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2020-04-06 18:58:13 -04:00
|
|
|
go monitor(service)
|
|
|
|
|
// To prevent multiple requests from running at the same time
|
2020-04-14 19:20:00 -04:00
|
|
|
time.Sleep(1111 * time.Millisecond)
|
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) {
|
|
|
|
|
for {
|
|
|
|
|
// 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
|
|
|
|
|
rwLock.Lock()
|
2020-08-17 20:25:29 -04:00
|
|
|
log.Printf("[watchdog][monitor] Monitoring serviceName=%s", service.Name)
|
2020-04-06 18:58:13 -04:00
|
|
|
result := service.EvaluateConditions()
|
|
|
|
|
metric.PublishMetricsForService(service, result)
|
|
|
|
|
serviceResults[service.Name] = append(serviceResults[service.Name], result)
|
|
|
|
|
if len(serviceResults[service.Name]) > 20 {
|
|
|
|
|
serviceResults[service.Name] = serviceResults[service.Name][1:]
|
|
|
|
|
}
|
|
|
|
|
rwLock.Unlock()
|
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-08-17 20:25:29 -04:00
|
|
|
"[watchdog][monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s; %s",
|
2020-04-06 18:58:13 -04:00
|
|
|
service.Name,
|
|
|
|
|
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-08-20 21:11:22 -04:00
|
|
|
|
|
|
|
|
cfg := config.Get()
|
|
|
|
|
if cfg.Alerting != nil {
|
|
|
|
|
for _, alertTriggered := range service.GetAlertsTriggered() {
|
2020-08-27 22:23:21 -04:00
|
|
|
var alertProvider *core.CustomAlertProvider
|
2020-08-20 21:11:22 -04:00
|
|
|
if alertTriggered.Type == core.SlackAlert {
|
|
|
|
|
if len(cfg.Alerting.Slack) > 0 {
|
|
|
|
|
log.Printf("[watchdog][monitor] Sending Slack alert because alert with description=%s has been triggered", alertTriggered.Description)
|
2020-08-27 22:23:21 -04:00
|
|
|
alertProvider = &core.CustomAlertProvider{
|
|
|
|
|
Url: cfg.Alerting.Slack,
|
|
|
|
|
Method: "POST",
|
|
|
|
|
Body: fmt.Sprintf(`{"text":"*[Gatus]*\n*service:* %s\n*description:* %s"}`, service.Name, alertTriggered.Description),
|
|
|
|
|
Headers: map[string]string{"Content-Type": "application/json"},
|
|
|
|
|
}
|
2020-08-20 21:11:22 -04:00
|
|
|
} else {
|
|
|
|
|
log.Printf("[watchdog][monitor] Not sending Slack alert despite being triggered, because there is no Slack webhook configured")
|
|
|
|
|
}
|
2020-08-27 22:23:21 -04:00
|
|
|
} else if alertTriggered.Type == core.CustomAlert {
|
|
|
|
|
if cfg.Alerting.Custom != nil && len(cfg.Alerting.Custom.Url) > 0 {
|
|
|
|
|
log.Printf("[watchdog][monitor] Sending custom alert because alert with description=%s has been triggered", alertTriggered.Description)
|
|
|
|
|
alertProvider = &core.CustomAlertProvider{
|
|
|
|
|
Url: cfg.Alerting.Custom.Url,
|
|
|
|
|
Method: cfg.Alerting.Custom.Method,
|
|
|
|
|
Body: cfg.Alerting.Custom.Body,
|
|
|
|
|
Headers: cfg.Alerting.Custom.Headers,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("[watchdog][monitor] Not sending custom alert despite being triggered, because there is no custom url configured")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if alertProvider != nil {
|
|
|
|
|
err := alertProvider.Send(service.Name, alertTriggered.Description)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("[watchdog][monitor] Ran into error sending an alert: %s", err.Error())
|
|
|
|
|
}
|
2020-08-20 21:11:22 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 20:25:29 -04:00
|
|
|
log.Printf("[watchdog][monitor] Waiting for interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
|
2020-04-06 18:58:13 -04:00
|
|
|
time.Sleep(service.Interval)
|
2019-09-06 21:59:50 -04:00
|
|
|
}
|
|
|
|
|
}
|