2019-09-04 23:37:13 +00:00
|
|
|
package watchdog
|
2019-09-07 01:59:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/TwinProduction/gatus/config"
|
|
|
|
"github.com/TwinProduction/gatus/core"
|
2019-11-16 20:47:28 +00:00
|
|
|
"github.com/TwinProduction/gatus/metric"
|
2019-09-09 01:07:08 +00:00
|
|
|
"log"
|
2019-09-07 01:59:50 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
serviceResults = make(map[string][]*core.Result)
|
|
|
|
rwLock sync.RWMutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetServiceResults() *map[string][]*core.Result {
|
|
|
|
return &serviceResults
|
|
|
|
}
|
|
|
|
|
|
|
|
func Monitor() {
|
2019-09-09 01:07:08 +00:00
|
|
|
for _, service := range config.Get().Services {
|
|
|
|
go func(service *core.Service) {
|
2019-11-16 20:47:28 +00:00
|
|
|
|
2019-09-09 01:07:08 +00:00
|
|
|
for {
|
|
|
|
log.Printf("[watchdog][Monitor] Monitoring serviceName=%s", service.Name)
|
2019-09-07 01:59:50 +00:00
|
|
|
result := service.EvaluateConditions()
|
2019-11-16 20:47:28 +00:00
|
|
|
metric.PublishMetricsForService(service, result)
|
2019-09-07 01:59:50 +00:00
|
|
|
rwLock.Lock()
|
|
|
|
serviceResults[service.Name] = append(serviceResults[service.Name], result)
|
2019-09-09 01:07:08 +00:00
|
|
|
if len(serviceResults[service.Name]) > 10 {
|
|
|
|
serviceResults[service.Name] = serviceResults[service.Name][1:]
|
2019-09-07 01:59:50 +00:00
|
|
|
}
|
2019-09-09 01:07:08 +00:00
|
|
|
rwLock.Unlock()
|
|
|
|
log.Printf(
|
|
|
|
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s",
|
|
|
|
service.Name,
|
|
|
|
len(result.Errors),
|
|
|
|
result.Duration.Round(time.Millisecond),
|
|
|
|
)
|
2019-09-24 01:12:15 +00:00
|
|
|
log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
|
|
|
|
time.Sleep(service.Interval)
|
2019-09-09 01:07:08 +00:00
|
|
|
}
|
|
|
|
}(service)
|
2019-09-07 01:59:50 +00:00
|
|
|
}
|
|
|
|
}
|