1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00
twin-gatus/watchdog/watchdog.go

93 lines
2.7 KiB
Go
Raw Normal View History

2019-09-04 23:37:13 +00:00
package watchdog
import (
"encoding/json"
"fmt"
"log"
"sync"
"time"
"github.com/TwinProduction/gatus/config"
"github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/metric"
2020-12-31 11:49:41 +00:00
"github.com/TwinProduction/gatus/storage"
)
var (
2020-12-31 11:49:41 +00:00
store = storage.NewInMemoryStore()
// monitoringMutex is used to prevent multiple services from being evaluated at the same time.
// Without this, conditions using response time may become inaccurate.
monitoringMutex sync.Mutex
)
// GetServiceStatusesAsJSON returns a list of core.ServiceStatus for each services encoded using json.Marshal.
func GetServiceStatusesAsJSON() ([]byte, error) {
2020-12-31 11:49:41 +00:00
serviceStatuses := store.GetAll()
2020-11-26 23:09:01 +00:00
data, err := json.Marshal(serviceStatuses)
return data, err
}
// GetUptimeByServiceGroupAndName returns the uptime of a service based on its group and name
func GetUptimeByServiceGroupAndName(group, name string) *core.Uptime {
key := fmt.Sprintf("%s_%s", group, name)
2020-12-31 11:49:41 +00:00
serviceStatuses := store.GetAll()
serviceStatus, exists := serviceStatuses[key]
if !exists {
return nil
}
return serviceStatus.Uptime
}
2020-08-18 00:25:29 +00:00
// Monitor loops over each services and starts a goroutine to monitor each services separately
func Monitor(cfg *config.Config) {
for _, service := range cfg.Services {
go monitor(service)
// To prevent multiple requests from running at the same time
time.Sleep(1111 * time.Millisecond)
}
}
2020-08-18 00:25:29 +00:00
// monitor monitors a single service in a loop
func monitor(service *core.Service) {
cfg := config.Get()
for {
if !cfg.DisableMonitoringLock {
// 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
monitoringMutex.Lock()
}
if cfg.Debug {
log.Printf("[watchdog][monitor] Monitoring serviceName=%s", service.Name)
}
result := service.EvaluateHealth()
metric.PublishMetricsForService(service, result)
2020-11-26 23:09:01 +00:00
UpdateServiceStatuses(service, result)
var extra string
if !result.Success {
extra = fmt.Sprintf("responseBody=%s", result.Body)
}
log.Printf(
"[watchdog][monitor] Monitored serviceName=%s; success=%v; errors=%d; requestDuration=%s; %s",
service.Name,
result.Success,
len(result.Errors),
result.Duration.Round(time.Millisecond),
extra,
)
HandleAlerting(service, result)
if cfg.Debug {
log.Printf("[watchdog][monitor] Waiting for interval=%s before monitoring serviceName=%s again", service.Interval, service.Name)
}
if !cfg.DisableMonitoringLock {
monitoringMutex.Unlock()
}
time.Sleep(service.Interval)
}
}
2020-11-26 23:09:01 +00:00
// UpdateServiceStatuses updates the slice of service statuses
func UpdateServiceStatuses(service *core.Service, result *core.Result) {
2020-12-31 11:49:41 +00:00
store.Insert(service, result)
2020-11-26 23:09:01 +00:00
}