gatus/core/service.go

230 lines
6.3 KiB
Go
Raw Normal View History

package core
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/TwinProduction/gatus/client"
)
const (
// HostHeader is the name of the header used to specify the host
HostHeader = "Host"
)
var (
2020-10-22 04:56:35 +02:00
// ErrServiceWithNoCondition is the error with which gatus will panic if a service is configured with no conditions
ErrServiceWithNoCondition = errors.New("you must specify at least one condition per service")
2020-10-23 22:29:20 +02:00
// ErrServiceWithNoURL is the error with which gatus will panic if a service is configured with no url
ErrServiceWithNoURL = errors.New("you must specify an url for each service")
2020-10-22 04:56:35 +02:00
// ErrServiceWithNoName is the error with which gatus will panic if a service is configured with no name
ErrServiceWithNoName = errors.New("you must specify a name for each service")
)
2020-09-01 06:29:17 +02:00
// Service is the configuration of a monitored endpoint
type Service struct {
2020-09-01 06:29:17 +02:00
// Name of the service. Can be anything.
Name string `yaml:"name"`
2020-11-27 00:09:01 +01:00
// Group the service is a part of. Used for grouping multiple services together on the front end.
Group string `yaml:"group,omitempty"`
2020-09-01 06:29:17 +02:00
// URL to send the request to
2020-10-23 22:29:20 +02:00
URL string `yaml:"url"`
2020-09-01 06:29:17 +02:00
2020-11-18 00:55:31 +01:00
// DNS is the configuration of DNS monitoring
DNS *DNS `yaml:"dns,omitempty"`
2020-09-01 06:29:17 +02:00
// Method of the request made to the url of the service
Method string `yaml:"method,omitempty"`
// Body of the request
Body string `yaml:"body,omitempty"`
// GraphQL is whether to wrap the body in a query param ({"query":"$body"})
GraphQL bool `yaml:"graphql,omitempty"`
// Headers of the request
Headers map[string]string `yaml:"headers,omitempty"`
// Interval is the duration to wait between every status check
Interval time.Duration `yaml:"interval,omitempty"`
// Conditions used to determine the health of the service
Conditions []*Condition `yaml:"conditions"`
// Alerts is the alerting configuration for the service in case of failure
Alerts []*Alert `yaml:"alerts"`
2020-08-21 03:11:22 +02:00
// Insecure is whether to skip verifying the server's certificate chain and host name
Insecure bool `yaml:"insecure,omitempty"`
2020-10-22 04:56:35 +02:00
// NumberOfFailuresInARow is the number of unsuccessful evaluations in a row
NumberOfFailuresInARow int
// NumberOfFailuresInARow is the number of successful evaluations in a row
2020-09-17 01:26:19 +02:00
NumberOfSuccessesInARow int
}
// ValidateAndSetDefaults validates the service's configuration and sets the default value of fields that have one
func (service *Service) ValidateAndSetDefaults() {
// Set default values
if service.Interval == 0 {
2020-09-01 06:25:57 +02:00
service.Interval = 1 * time.Minute
}
if len(service.Method) == 0 {
service.Method = http.MethodGet
}
if len(service.Headers) == 0 {
service.Headers = make(map[string]string)
}
2020-08-22 20:15:08 +02:00
for _, alert := range service.Alerts {
if alert.FailureThreshold <= 0 {
alert.FailureThreshold = 3
2020-08-22 20:15:08 +02:00
}
if alert.SuccessThreshold <= 0 {
alert.SuccessThreshold = 2
2020-09-17 01:26:19 +02:00
}
2020-08-22 20:15:08 +02:00
}
2020-10-22 04:56:35 +02:00
if len(service.Name) == 0 {
panic(ErrServiceWithNoName)
}
2020-10-23 22:29:20 +02:00
if len(service.URL) == 0 {
panic(ErrServiceWithNoURL)
}
if len(service.Conditions) == 0 {
2020-10-22 04:56:35 +02:00
panic(ErrServiceWithNoCondition)
}
2020-11-18 00:55:31 +01:00
if service.DNS != nil {
service.DNS.validateAndSetDefault()
return
}
// Make sure that the request can be created
2020-10-23 22:29:20 +02:00
_, err := http.NewRequest(service.Method, service.URL, bytes.NewBuffer([]byte(service.Body)))
if err != nil {
panic(err)
}
}
// EvaluateHealth sends a request to the service's URL and evaluates the conditions of the service.
func (service *Service) EvaluateHealth() *Result {
result := &Result{Success: true, Errors: []string{}}
2020-11-20 04:41:30 +01:00
service.getIP(result)
if len(result.Errors) == 0 {
service.call(result)
} else {
result.Success = false
}
for _, condition := range service.Conditions {
success := condition.evaluate(result)
if !success {
result.Success = false
}
}
result.Timestamp = time.Now()
return result
}
2020-10-22 04:56:35 +02:00
// GetAlertsTriggered returns a slice of alerts that have been triggered
2020-08-21 03:11:22 +02:00
func (service *Service) GetAlertsTriggered() []Alert {
var alerts []Alert
if service.NumberOfFailuresInARow == 0 {
2020-08-21 03:11:22 +02:00
return alerts
}
for _, alert := range service.Alerts {
if alert.Enabled && alert.FailureThreshold == service.NumberOfFailuresInARow {
2020-08-21 03:11:22 +02:00
alerts = append(alerts, *alert)
continue
}
}
return alerts
}
2020-10-23 22:29:20 +02:00
func (service *Service) getIP(result *Result) {
2020-11-20 04:41:30 +01:00
if service.DNS != nil {
result.Hostname = strings.TrimSuffix(service.URL, ":53")
} else {
urlObject, err := url.Parse(service.URL)
if err != nil {
result.Errors = append(result.Errors, err.Error())
return
}
result.Hostname = urlObject.Hostname()
}
2020-11-20 04:41:30 +01:00
ips, err := net.LookupIP(result.Hostname)
if err != nil {
result.Errors = append(result.Errors, err.Error())
return
}
2020-10-23 22:29:20 +02:00
result.IP = ips[0].String()
}
func (service *Service) call(result *Result) {
var request *http.Request
var response *http.Response
var err error
2020-11-20 04:41:30 +01:00
isServiceTCP := strings.HasPrefix(service.URL, "tcp://")
isServiceDNS := service.DNS != nil
isServiceHTTP := !isServiceTCP && !isServiceDNS
if isServiceHTTP {
request = service.buildHTTPRequest()
}
startTime := time.Now()
2020-11-20 04:41:30 +01:00
if isServiceDNS {
service.DNS.query(service.URL, result)
result.Duration = time.Since(startTime)
} else if isServiceTCP {
2020-10-23 22:29:20 +02:00
result.Connected = client.CanCreateConnectionToTCPService(strings.TrimPrefix(service.URL, "tcp://"))
result.Duration = time.Since(startTime)
} else {
2020-10-23 22:29:20 +02:00
response, err = client.GetHTTPClient(service.Insecure).Do(request)
result.Duration = time.Since(startTime)
if err != nil {
result.Errors = append(result.Errors, err.Error())
return
}
2020-11-17 18:35:21 +01:00
if response.TLS != nil && len(response.TLS.PeerCertificates) > 0 {
certificate := response.TLS.PeerCertificates[0]
result.CertificateExpiration = certificate.NotAfter.Sub(time.Now())
}
2020-10-23 22:29:20 +02:00
result.HTTPStatus = response.StatusCode
result.Connected = response.StatusCode > 0
result.Body, err = ioutil.ReadAll(response.Body)
if err != nil {
result.Errors = append(result.Errors, err.Error())
}
}
}
2020-11-20 04:41:30 +01:00
func (service *Service) buildHTTPRequest() *http.Request {
var bodyBuffer *bytes.Buffer
if service.GraphQL {
graphQlBody := map[string]string{
"query": service.Body,
}
body, _ := json.Marshal(graphQlBody)
bodyBuffer = bytes.NewBuffer(body)
} else {
bodyBuffer = bytes.NewBuffer([]byte(service.Body))
}
2020-10-23 22:29:20 +02:00
request, _ := http.NewRequest(service.Method, service.URL, bodyBuffer)
for k, v := range service.Headers {
request.Header.Set(k, v)
if k == HostHeader {
request.Host = v
}
}
return request
}