2019-09-05 01:37:13 +02:00
|
|
|
package watchdog
|
2019-09-07 03:59:50 +02:00
|
|
|
|
|
|
|
import (
|
2020-07-24 22:45:51 +02:00
|
|
|
"fmt"
|
2019-09-07 03:59:50 +02:00
|
|
|
"github.com/TwinProduction/gatus/config"
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
2019-11-16 21:47:28 +01:00
|
|
|
"github.com/TwinProduction/gatus/metric"
|
2019-09-09 03:07:08 +02:00
|
|
|
"log"
|
2019-09-07 03:59:50 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
serviceResults = make(map[string][]*core.Result)
|
|
|
|
rwLock sync.RWMutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetServiceResults() *map[string][]*core.Result {
|
|
|
|
return &serviceResults
|
|
|
|
}
|
|
|
|
|
2019-12-04 22:44:35 +01:00
|
|
|
func Monitor(cfg *config.Config) {
|
|
|
|
for _, service := range cfg.Services {
|
2020-04-07 00:58:13 +02:00
|
|
|
go monitor(service)
|
|
|
|
// To prevent multiple requests from running at the same time
|
2020-04-15 01:20:00 +02:00
|
|
|
time.Sleep(1111 * time.Millisecond)
|
2020-04-07 00:58:13 +02: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()
|
|
|
|
log.Printf("[watchdog][Monitor] Monitoring serviceName=%s", service.Name)
|
|
|
|
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 22:45:51 +02:00
|
|
|
var extra string
|
|
|
|
if !result.Success {
|
|
|
|
extra = fmt.Sprintf("responseBody=%s", result.Body)
|
|
|
|
}
|
2020-04-07 00:58:13 +02:00
|
|
|
log.Printf(
|
2020-07-24 22:45:51 +02:00
|
|
|
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s; %s",
|
2020-04-07 00:58:13 +02:00
|
|
|
service.Name,
|
|
|
|
len(result.Errors),
|
|
|
|
result.Duration.Round(time.Millisecond),
|
2020-07-24 22:45:51 +02:00
|
|
|
extra,
|
2020-04-07 00:58:13 +02:00
|
|
|
)
|
|
|
|
log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
|
|
|
|
time.Sleep(service.Interval)
|
2019-09-07 03:59:50 +02:00
|
|
|
}
|
|
|
|
}
|