2020-04-15 01:20:00 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
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
|
|
|
"github.com/TwinProduction/gatus/client"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-15 02:13:06 +02:00
|
|
|
var (
|
|
|
|
ErrNoCondition = errors.New("you must specify at least one condition per service")
|
|
|
|
ErrNoUrl = errors.New("you must specify an url for each service")
|
|
|
|
)
|
|
|
|
|
2020-04-15 01:20:00 +02:00
|
|
|
type Service struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Url string `yaml:"url"`
|
|
|
|
Method string `yaml:"method,omitempty"`
|
|
|
|
Body string `yaml:"body,omitempty"`
|
2020-07-24 22:45:51 +02:00
|
|
|
GraphQL bool `yaml:"graphql,omitempty"`
|
2020-04-15 02:13:06 +02:00
|
|
|
Headers map[string]string `yaml:"headers,omitempty"`
|
|
|
|
Interval time.Duration `yaml:"interval,omitempty"`
|
2020-04-15 01:20:00 +02:00
|
|
|
Conditions []*Condition `yaml:"conditions"`
|
2020-08-21 03:11:22 +02:00
|
|
|
Alerts []*Alert `yaml:"alerts"`
|
|
|
|
|
|
|
|
numberOfFailuresInARow int
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) Validate() {
|
|
|
|
// Set default values
|
|
|
|
if service.Interval == 0 {
|
|
|
|
service.Interval = 10 * time.Second
|
|
|
|
}
|
|
|
|
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-08-22 20:15:08 +02:00
|
|
|
for _, alert := range service.Alerts {
|
|
|
|
if alert.Threshold <= 0 {
|
|
|
|
alert.Threshold = 3
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 02:13:06 +02:00
|
|
|
if len(service.Url) == 0 {
|
|
|
|
panic(ErrNoUrl)
|
|
|
|
}
|
|
|
|
if len(service.Conditions) == 0 {
|
|
|
|
panic(ErrNoCondition)
|
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
|
|
|
|
// Make sure that the request can be created
|
|
|
|
_, err := http.NewRequest(service.Method, service.Url, bytes.NewBuffer([]byte(service.Body)))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) EvaluateConditions() *Result {
|
|
|
|
result := &Result{Success: true, Errors: []string{}}
|
|
|
|
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()
|
2020-08-21 03:11:22 +02:00
|
|
|
if result.Success {
|
|
|
|
service.numberOfFailuresInARow = 0
|
2020-08-22 20:15:08 +02:00
|
|
|
// TODO: Send notification that alert has been resolved?
|
2020-08-21 03:11:22 +02:00
|
|
|
} else {
|
|
|
|
service.numberOfFailuresInARow++
|
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-08-21 03:11:22 +02:00
|
|
|
func (service *Service) GetAlertsTriggered() []Alert {
|
|
|
|
var alerts []Alert
|
|
|
|
if service.numberOfFailuresInARow == 0 {
|
|
|
|
return alerts
|
|
|
|
}
|
|
|
|
for _, alert := range service.Alerts {
|
|
|
|
if alert.Enabled && alert.Threshold == service.numberOfFailuresInARow {
|
|
|
|
alerts = append(alerts, *alert)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return alerts
|
|
|
|
}
|
|
|
|
|
2020-04-15 01:20:00 +02:00
|
|
|
func (service *Service) getIp(result *Result) {
|
|
|
|
urlObject, err := url.Parse(service.Url)
|
|
|
|
if err != nil {
|
|
|
|
result.Errors = append(result.Errors, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result.Hostname = urlObject.Hostname()
|
|
|
|
ips, err := net.LookupIP(urlObject.Hostname())
|
|
|
|
if err != nil {
|
|
|
|
result.Errors = append(result.Errors, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result.Ip = ips[0].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) call(result *Result) {
|
|
|
|
request := service.buildRequest()
|
|
|
|
startTime := time.Now()
|
|
|
|
response, err := client.GetHttpClient().Do(request)
|
|
|
|
if err != nil {
|
|
|
|
result.Duration = time.Since(startTime)
|
|
|
|
result.Errors = append(result.Errors, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result.Duration = time.Since(startTime)
|
|
|
|
result.HttpStatus = response.StatusCode
|
|
|
|
result.Body, err = ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
result.Errors = append(result.Errors, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (service *Service) buildRequest() *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))
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|