2020-04-15 01:20:00 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-05 21:47:11 +02:00
|
|
|
"crypto/x509"
|
2020-07-24 22:45:51 +02:00
|
|
|
"encoding/json"
|
2020-04-15 02:13:06 +02:00
|
|
|
"errors"
|
2020-04-15 01:20:00 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-10-05 01:49:02 +02:00
|
|
|
"strings"
|
2020-04-15 01:20:00 +02:00
|
|
|
"time"
|
2020-10-30 16:30:03 +01:00
|
|
|
|
2021-05-19 04:29:15 +02:00
|
|
|
"github.com/TwinProduction/gatus/alerting/alert"
|
2020-10-30 16:30:03 +01:00
|
|
|
"github.com/TwinProduction/gatus/client"
|
2020-04-15 01:20:00 +02:00
|
|
|
)
|
|
|
|
|
2020-12-18 23:37:03 +01:00
|
|
|
const (
|
|
|
|
// HostHeader is the name of the header used to specify the host
|
|
|
|
HostHeader = "Host"
|
2020-12-19 00:39:22 +01:00
|
|
|
|
|
|
|
// ContentTypeHeader is the name of the header used to specify the content type
|
|
|
|
ContentTypeHeader = "Content-Type"
|
2020-12-31 01:56:12 +01:00
|
|
|
|
|
|
|
// UserAgentHeader is the name of the header used to specify the request's user agent
|
|
|
|
UserAgentHeader = "User-Agent"
|
|
|
|
|
|
|
|
// GatusUserAgent is the default user agent that Gatus uses to send requests.
|
|
|
|
GatusUserAgent = "Gatus/1.0"
|
2020-12-18 23:37:03 +01:00
|
|
|
)
|
|
|
|
|
2020-04-15 02:13:06 +02:00
|
|
|
var (
|
2020-12-24 06:28:44 +01:00
|
|
|
// ErrServiceWithNoCondition is the error with which Gatus will panic if a service is configured with no conditions
|
2020-10-22 04:56:35 +02:00
|
|
|
ErrServiceWithNoCondition = errors.New("you must specify at least one condition per service")
|
|
|
|
|
2020-12-24 06:28:44 +01:00
|
|
|
// ErrServiceWithNoURL is the error with which Gatus will panic if a service is configured with no url
|
2020-10-23 22:29:20 +02:00
|
|
|
ErrServiceWithNoURL = errors.New("you must specify an url for each service")
|
2020-10-22 04:56:35 +02:00
|
|
|
|
2020-12-24 06:28:44 +01:00
|
|
|
// ErrServiceWithNoName is the error with which Gatus will panic if a service is configured with no name
|
2020-10-22 04:56:35 +02:00
|
|
|
ErrServiceWithNoName = errors.New("you must specify a name for each service")
|
2020-04-15 02:13:06 +02:00
|
|
|
)
|
|
|
|
|
2020-09-01 06:29:17 +02:00
|
|
|
// Service is the configuration of a monitored endpoint
|
2020-04-15 01:20:00 +02:00
|
|
|
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
|
2021-05-19 04:29:15 +02:00
|
|
|
Alerts []*alert.Alert `yaml:"alerts"`
|
2020-08-21 03:11:22 +02:00
|
|
|
|
2020-10-04 23:01:10 +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
|
|
|
|
|
2021-03-09 03:30:11 +01:00
|
|
|
// NumberOfSuccessesInARow is the number of successful evaluations in a row
|
2020-09-17 01:26:19 +02:00
|
|
|
NumberOfSuccessesInARow int
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 01:49:32 +02:00
|
|
|
// ValidateAndSetDefaults validates the service's configuration and sets the default value of fields that have one
|
2021-05-19 04:29:15 +02:00
|
|
|
func (service *Service) ValidateAndSetDefaults() error {
|
2020-04-15 01:20:00 +02:00
|
|
|
// Set default values
|
|
|
|
if service.Interval == 0 {
|
2020-09-01 06:25:57 +02:00
|
|
|
service.Interval = 1 * time.Minute
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
if len(service.Method) == 0 {
|
|
|
|
service.Method = http.MethodGet
|
|
|
|
}
|
2020-04-15 02:13:06 +02:00
|
|
|
if len(service.Headers) == 0 {
|
|
|
|
service.Headers = make(map[string]string)
|
|
|
|
}
|
2020-12-31 01:56:12 +01:00
|
|
|
// Automatically add user agent header if there isn't one specified in the service configuration
|
|
|
|
if _, userAgentHeaderExists := service.Headers[UserAgentHeader]; !userAgentHeaderExists {
|
|
|
|
service.Headers[UserAgentHeader] = GatusUserAgent
|
|
|
|
}
|
2020-12-19 00:39:22 +01:00
|
|
|
// Automatically add "Content-Type: application/json" header if there's no Content-Type set
|
|
|
|
// and service.GraphQL is set to true
|
|
|
|
if _, contentTypeHeaderExists := service.Headers[ContentTypeHeader]; !contentTypeHeaderExists && service.GraphQL {
|
|
|
|
service.Headers[ContentTypeHeader] = "application/json"
|
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
for _, serviceAlert := range service.Alerts {
|
|
|
|
if serviceAlert.FailureThreshold <= 0 {
|
|
|
|
serviceAlert.FailureThreshold = 3
|
2020-08-22 20:15:08 +02:00
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
if serviceAlert.SuccessThreshold <= 0 {
|
|
|
|
serviceAlert.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 {
|
2021-05-19 04:29:15 +02:00
|
|
|
return ErrServiceWithNoName
|
2020-10-22 04:56:35 +02:00
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
if len(service.URL) == 0 {
|
2021-05-19 04:29:15 +02:00
|
|
|
return ErrServiceWithNoURL
|
2020-04-15 02:13:06 +02:00
|
|
|
}
|
|
|
|
if len(service.Conditions) == 0 {
|
2021-05-19 04:29:15 +02:00
|
|
|
return ErrServiceWithNoCondition
|
2020-04-15 02:13:06 +02:00
|
|
|
}
|
2020-11-18 00:55:31 +01:00
|
|
|
if service.DNS != nil {
|
2021-05-19 04:29:15 +02:00
|
|
|
return service.DNS.validateAndSetDefault()
|
2020-11-18 00:55:31 +01:00
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
// 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)))
|
2020-04-15 01:20:00 +02:00
|
|
|
if err != nil {
|
2021-05-19 04:29:15 +02:00
|
|
|
return err
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
return nil
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 01:49:32 +02:00
|
|
|
// EvaluateHealth sends a request to the service's URL and evaluates the conditions of the service.
|
|
|
|
func (service *Service) EvaluateHealth() *Result {
|
2020-04-15 01:20:00 +02:00
|
|
|
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
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
for _, condition := range service.Conditions {
|
|
|
|
success := condition.evaluate(result)
|
|
|
|
if !success {
|
|
|
|
result.Success = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.Timestamp = time.Now()
|
2021-03-09 03:30:11 +01:00
|
|
|
// No need to keep the body after the service has been evaluated
|
|
|
|
result.body = nil
|
2020-04-15 01:20:00 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-06-06 00:50:24 +02:00
|
|
|
result.AddError(err.Error())
|
2020-11-20 04:41:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
result.Hostname = urlObject.Hostname()
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2020-11-20 04:41:30 +01:00
|
|
|
ips, err := net.LookupIP(result.Hostname)
|
2020-04-15 01:20:00 +02:00
|
|
|
if err != nil {
|
2021-06-06 00:50:24 +02:00
|
|
|
result.AddError(err.Error())
|
2020-04-15 01:20:00 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
result.IP = ips[0].String()
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) call(result *Result) {
|
2020-10-05 01:49:02 +02:00
|
|
|
var request *http.Request
|
|
|
|
var response *http.Response
|
|
|
|
var err error
|
2021-06-05 21:47:11 +02:00
|
|
|
var certificate *x509.Certificate
|
2020-11-20 04:41:30 +01:00
|
|
|
isServiceDNS := service.DNS != nil
|
2020-12-25 06:07:18 +01:00
|
|
|
isServiceTCP := strings.HasPrefix(service.URL, "tcp://")
|
|
|
|
isServiceICMP := strings.HasPrefix(service.URL, "icmp://")
|
2021-06-05 21:47:11 +02:00
|
|
|
isServiceStartTLS := strings.HasPrefix(service.URL, "starttls://")
|
|
|
|
isServiceHTTP := !isServiceDNS && !isServiceTCP && !isServiceICMP && !isServiceStartTLS
|
2020-11-20 04:41:30 +01:00
|
|
|
if isServiceHTTP {
|
|
|
|
request = service.buildHTTPRequest()
|
2020-10-05 01:49:02 +02:00
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
startTime := time.Now()
|
2020-11-20 04:41:30 +01:00
|
|
|
if isServiceDNS {
|
|
|
|
service.DNS.query(service.URL, result)
|
|
|
|
result.Duration = time.Since(startTime)
|
2021-06-05 21:47:11 +02:00
|
|
|
} else if isServiceStartTLS {
|
2021-06-06 00:50:24 +02:00
|
|
|
result.Connected, certificate, err = client.CanPerformStartTLS(strings.TrimPrefix(service.URL, "starttls://"), service.Insecure)
|
2021-06-05 21:47:11 +02:00
|
|
|
if err != nil {
|
2021-06-06 00:51:51 +02:00
|
|
|
result.AddError(err.Error())
|
2021-06-05 21:47:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
result.Duration = time.Since(startTime)
|
|
|
|
result.CertificateExpiration = time.Until(certificate.NotAfter)
|
2020-11-20 04:41:30 +01:00
|
|
|
} else if isServiceTCP {
|
2020-12-25 06:07:18 +01:00
|
|
|
result.Connected = client.CanCreateTCPConnection(strings.TrimPrefix(service.URL, "tcp://"))
|
|
|
|
result.Duration = time.Since(startTime)
|
|
|
|
} else if isServiceICMP {
|
2020-12-27 23:07:50 +01:00
|
|
|
result.Connected, result.Duration = client.Ping(strings.TrimPrefix(service.URL, "icmp://"))
|
2020-10-05 01:49:02 +02:00
|
|
|
} else {
|
2020-10-23 22:29:20 +02:00
|
|
|
response, err = client.GetHTTPClient(service.Insecure).Do(request)
|
2020-10-05 01:49:02 +02:00
|
|
|
result.Duration = time.Since(startTime)
|
|
|
|
if err != nil {
|
2021-06-06 00:50:24 +02:00
|
|
|
result.AddError(err.Error())
|
2020-10-05 01:49:02 +02:00
|
|
|
return
|
|
|
|
}
|
2021-02-20 02:34:35 +01:00
|
|
|
defer response.Body.Close()
|
2020-11-17 18:35:21 +01:00
|
|
|
if response.TLS != nil && len(response.TLS.PeerCertificates) > 0 {
|
2021-06-05 21:47:11 +02:00
|
|
|
certificate = response.TLS.PeerCertificates[0]
|
2021-02-20 02:34:35 +01:00
|
|
|
result.CertificateExpiration = time.Until(certificate.NotAfter)
|
2020-11-15 18:33:09 +01:00
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
result.HTTPStatus = response.StatusCode
|
2020-10-05 01:49:02 +02:00
|
|
|
result.Connected = response.StatusCode > 0
|
2021-03-09 03:30:11 +01:00
|
|
|
// Only read the body if there's a condition that uses the BodyPlaceholder
|
|
|
|
if service.needsToReadBody() {
|
|
|
|
result.body, err = ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
2021-06-06 00:50:24 +02:00
|
|
|
result.AddError(err.Error())
|
2021-03-09 03:30:11 +01:00
|
|
|
}
|
2020-10-05 01:49:02 +02:00
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:41:30 +01:00
|
|
|
func (service *Service) buildHTTPRequest() *http.Request {
|
2020-07-24 22:45:51 +02:00
|
|
|
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)
|
2020-04-15 01:20:00 +02:00
|
|
|
for k, v := range service.Headers {
|
|
|
|
request.Header.Set(k, v)
|
2020-12-18 23:37:03 +01:00
|
|
|
if k == HostHeader {
|
|
|
|
request.Host = v
|
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|
2021-03-09 03:30:11 +01:00
|
|
|
|
|
|
|
// needsToReadBody checks if there's any conditions that requires the response body to be read
|
|
|
|
func (service *Service) needsToReadBody() bool {
|
|
|
|
for _, condition := range service.Conditions {
|
|
|
|
if condition.hasBodyPlaceholder() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|