Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4d81a9661 | ||
|
|
81c59f02d8 | ||
|
|
8101646ba5 | ||
|
|
18d3236586 | ||
|
|
bc16ae1794 | ||
|
|
d019942278 | ||
|
|
8c5551f93b | ||
|
|
3b15feaf7d | ||
|
|
326ad2c80d | ||
|
|
b4b9f5c1be | ||
|
|
ac5ad9d173 | ||
|
|
7dcd462883 | ||
|
|
5655661778 | ||
|
|
fac7b8551a | ||
|
|
59fed008e0 | ||
|
|
286e8f8590 | ||
|
|
4daf261d95 |
No files matched your search
@@ -14,13 +14,13 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 5
|
timeout-minutes: 5
|
||||||
steps:
|
steps:
|
||||||
- name: Set up Go 1.14
|
- name: Set up Go 1.15
|
||||||
uses: actions/setup-go@v1
|
uses: actions/setup-go@v1
|
||||||
with:
|
with:
|
||||||
go-version: 1.14
|
go-version: 1.15
|
||||||
id: go
|
id: go
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v2
|
||||||
- name: Build binary to make sure it works
|
- name: Build binary to make sure it works
|
||||||
run: go build -mod vendor
|
run: go build -mod vendor
|
||||||
- name: Test
|
- name: Test
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||

|

|
||||||
|
|
||||||

|

|
||||||
[](https://goreportcard.com/report/github.com/TwinProduction/gatus)
|
[](https://goreportcard.com/report/github.com/TwinProduction/gatus)
|
||||||
[](https://cloud.docker.com/repository/docker/twinproduction/gatus)
|
[](https://cloud.docker.com/repository/docker/twinproduction/gatus)
|
||||||
|
|
||||||
A service health dashboard in Go that is meant to be used as a docker
|
A service health dashboard in Go that is meant to be used as a docker
|
||||||
image with a custom configuration file.
|
image with a custom configuration file.
|
||||||
|
|
||||||
I personally deploy it in my Kubernetes cluster and have it monitor the status of my
|
I personally deploy it in my Kubernetes cluster and let it monitor the status of my
|
||||||
core applications: https://status.twinnation.org/
|
core applications: https://status.twinnation.org/
|
||||||
|
|
||||||
|
|
||||||
@@ -17,6 +17,8 @@ core applications: https://status.twinnation.org/
|
|||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Configuration](#configuration)
|
- [Configuration](#configuration)
|
||||||
- [Conditions](#conditions)
|
- [Conditions](#conditions)
|
||||||
|
- [Placeholders](#placeholders)
|
||||||
|
- [Functions](#functions)
|
||||||
- [Alerting](#alerting)
|
- [Alerting](#alerting)
|
||||||
- [Configuring Slack alerts](#configuring-slack-alerts)
|
- [Configuring Slack alerts](#configuring-slack-alerts)
|
||||||
- [Configuring PagerDuty alerts](#configuring-pagerduty-alerts)
|
- [Configuring PagerDuty alerts](#configuring-pagerduty-alerts)
|
||||||
@@ -75,7 +77,7 @@ Note that you can also add environment variables in the configuration file (i.e.
|
|||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
| Parameter | Description | Default |
|
| Parameter | Description | Default |
|
||||||
| ---------------------------------------- | ----------------------------------------------------------------------------- | -------------- |
|
|:---------------------------------------- |:----------------------------------------------------------------------------- |:-------------- |
|
||||||
| `debug` | Whether to enable debug logs | `false` |
|
| `debug` | Whether to enable debug logs | `false` |
|
||||||
| `metrics` | Whether to expose metrics at /metrics | `false` |
|
| `metrics` | Whether to expose metrics at /metrics | `false` |
|
||||||
| `services` | List of services to monitor | Required `[]` |
|
| `services` | List of services to monitor | Required `[]` |
|
||||||
@@ -114,23 +116,46 @@ Note that you can also add environment variables in the configuration file (i.e.
|
|||||||
Here are some examples of conditions you can use:
|
Here are some examples of conditions you can use:
|
||||||
|
|
||||||
| Condition | Description | Passing values | Failing values |
|
| Condition | Description | Passing values | Failing values |
|
||||||
| -----------------------------| ------------------------------------------------------- | -------------------------- | -------------- |
|
|:-----------------------------|:------------------------------------------------------- |:-------------------------- | -------------- |
|
||||||
| `[STATUS] == 200` | Status must be equal to 200 | 200 | 201, 404, ... |
|
| `[STATUS] == 200` | Status must be equal to 200 | 200 | 201, 404, ... |
|
||||||
| `[STATUS] < 300` | Status must lower than 300 | 200, 201, 299 | 301, 302, ... |
|
| `[STATUS] < 300` | Status must lower than 300 | 200, 201, 299 | 301, 302, ... |
|
||||||
| `[STATUS] <= 299` | Status must be less than or equal to 299 | 200, 201, 299 | 301, 302, ... |
|
| `[STATUS] <= 299` | Status must be less than or equal to 299 | 200, 201, 299 | 301, 302, ... |
|
||||||
| `[STATUS] > 400` | Status must be greater than 400 | 401, 402, 403, 404 | 400, 200, ... |
|
| `[STATUS] > 400` | Status must be greater than 400 | 401, 402, 403, 404 | 400, 200, ... |
|
||||||
| `[RESPONSE_TIME] < 500` | Response time must be below 500ms | 100ms, 200ms, 300ms | 500ms, 501ms |
|
| `[RESPONSE_TIME] < 500` | Response time must be below 500ms | 100ms, 200ms, 300ms | 500ms, 501ms |
|
||||||
| `[BODY] == 1` | The body must be equal to 1 | 1 | Anything else |
|
| `[IP] == 127.0.0.1` | Target IP must be 127.0.0.1 | 127.0.0.1 | 0.0.0.0 |
|
||||||
|
| `[BODY] == 1` | The body must be equal to 1 | 1 | `{}`, `2`, ... |
|
||||||
| `[BODY].user.name == john` | JSONPath value of `$.user.name` is equal to `john` | `{"user":{"name":"john"}}` | |
|
| `[BODY].user.name == john` | JSONPath value of `$.user.name` is equal to `john` | `{"user":{"name":"john"}}` | |
|
||||||
| `[BODY].data[0].id == 1` | JSONPath value of `$.data[0].id` is equal to 1 | `{"data":[{"id":1}]}` | |
|
| `[BODY].data[0].id == 1` | JSONPath value of `$.data[0].id` is equal to 1 | `{"data":[{"id":1}]}` | |
|
||||||
|
| `[BODY].age == [BODY].id` | JSONPath value of `$.age` is equal JSONPath `$.id` | `{"age":1,"id":1}` | |
|
||||||
| `len([BODY].data) < 5` | Array at JSONPath `$.data` has less than 5 elements | `{"data":[{"id":1}]}` | |
|
| `len([BODY].data) < 5` | Array at JSONPath `$.data` has less than 5 elements | `{"data":[{"id":1}]}` | |
|
||||||
| `len([BODY].name) == 8` | String at JSONPath `$.name` has a length of 8 | `{"name":"john.doe"}` | `{"name":"bob"}` |
|
| `len([BODY].name) == 8` | String at JSONPath `$.name` has a length of 8 | `{"name":"john.doe"}` | `{"name":"bob"}` |
|
||||||
|
| `[BODY].name == pat(john*)` | String at JSONPath `$.name` matches pattern `john*` | `{"name":"john.doe"}` | `{"name":"bob"}` |
|
||||||
|
|
||||||
|
|
||||||
|
#### Placeholders
|
||||||
|
|
||||||
|
| Placeholder | Description | Example of resolved value |
|
||||||
|
|:---------------------------- |:------------------------------------------------------- |:-------------------------- |
|
||||||
|
| `[STATUS]` | Resolves into the HTTP status of the request | 404
|
||||||
|
| `[RESPONSE_TIME]` | Resolves into the response time the request took, in ms | 10
|
||||||
|
| `[IP]` | Resolves into the IP of the target host | 192.168.0.232
|
||||||
|
| `[BODY]` | Resolves into the response body. Supports JSONPath. | `{"name":"john.doe"}`
|
||||||
|
|
||||||
|
|
||||||
|
#### Functions
|
||||||
|
|
||||||
|
| Function | Description | Example |
|
||||||
|
|:-----------|:---------------------------------------------------------------------------------------------------------------- |:-------------------------- |
|
||||||
|
| `len` | Returns the length of the object/slice. Works only with the `[BODY]` placeholder. | `len([BODY].username) > 8`
|
||||||
|
| `pat` | Specifies that the string passed as parameter should be evaluated as a pattern. Works only with `==` and `!=`. | `[IP] == pat(192.168.*)`
|
||||||
|
|
||||||
|
**NOTE**: Use `pat` only when you need to. `[STATUS] == pat(2*)` is a lot more expensive than `[STATUS] < 300`.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Alerting
|
### Alerting
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### Configuring Slack alerts
|
#### Configuring Slack alerts
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@@ -139,8 +164,8 @@ alerting:
|
|||||||
webhook-url: "https://hooks.slack.com/services/**********/**********/**********"
|
webhook-url: "https://hooks.slack.com/services/**********/**********/**********"
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
interval: 30s
|
|
||||||
url: "https://twinnation.org/health"
|
url: "https://twinnation.org/health"
|
||||||
|
interval: 30s
|
||||||
alerts:
|
alerts:
|
||||||
- type: slack
|
- type: slack
|
||||||
enabled: true
|
enabled: true
|
||||||
@@ -175,15 +200,15 @@ alerting:
|
|||||||
integration-key: "********************************"
|
integration-key: "********************************"
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
interval: 30s
|
|
||||||
url: "https://twinnation.org/health"
|
url: "https://twinnation.org/health"
|
||||||
|
interval: 30s
|
||||||
alerts:
|
alerts:
|
||||||
- type: pagerduty
|
- type: pagerduty
|
||||||
enabled: true
|
enabled: true
|
||||||
failure-threshold: 3
|
failure-threshold: 3
|
||||||
success-threshold: 5
|
success-threshold: 5
|
||||||
description: "healthcheck failed 3 times in a row"
|
|
||||||
send-on-resolved: true
|
send-on-resolved: true
|
||||||
|
description: "healthcheck failed 3 times in a row"
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
- "[BODY].status == UP"
|
- "[BODY].status == UP"
|
||||||
@@ -208,8 +233,8 @@ services:
|
|||||||
- type: twilio
|
- type: twilio
|
||||||
enabled: true
|
enabled: true
|
||||||
failure-threshold: 5
|
failure-threshold: 5
|
||||||
description: "healthcheck failed 5 times in a row"
|
|
||||||
send-on-resolved: true
|
send-on-resolved: true
|
||||||
|
description: "healthcheck failed 5 times in a row"
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
- "[BODY].status == UP"
|
- "[BODY].status == UP"
|
||||||
@@ -245,8 +270,8 @@ alerting:
|
|||||||
}
|
}
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
interval: 30s
|
|
||||||
url: "https://twinnation.org/health"
|
url: "https://twinnation.org/health"
|
||||||
|
interval: 30s
|
||||||
alerts:
|
alerts:
|
||||||
- type: custom
|
- type: custom
|
||||||
enabled: true
|
enabled: true
|
||||||
@@ -267,12 +292,12 @@ Other than using one of the examples provided in the `examples` folder, you can
|
|||||||
creating a configuration file - we'll call it `config.yaml` for this example - and running the following
|
creating a configuration file - we'll call it `config.yaml` for this example - and running the following
|
||||||
command:
|
command:
|
||||||
```
|
```
|
||||||
docker run -p 8080:8080 --mount type=bind,source="$(pwd)"/test.yaml,target=/config/config.yaml --name gatus twinproduction/gatus
|
docker run -p 8080:8080 --mount type=bind,source="$(pwd)"/config.yaml,target=/config/config.yaml --name gatus twinproduction/gatus
|
||||||
```
|
```
|
||||||
|
|
||||||
If you're on Windows, replace `"$(pwd)"` by the absolute path to your current directory, e.g.:
|
If you're on Windows, replace `"$(pwd)"` by the absolute path to your current directory, e.g.:
|
||||||
```
|
```
|
||||||
docker run -p 8080:8080 --mount type=bind,source=E:/Go/src/github.com/TwinProduction/gatus/test.yaml,target=/config/config.yaml --name gatus twinproduction/gatus
|
docker run -p 8080:8080 --mount type=bind,source=C:/Users/Chris/Desktop/config.yaml,target=/config/config.yaml --name gatus twinproduction/gatus
|
||||||
```
|
```
|
||||||
|
|
||||||
## Running the tests
|
## Running the tests
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TwinProduction/gatus/client"
|
"github.com/TwinProduction/gatus/client"
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AlertProvider is the configuration necessary for sending an alert using a custom HTTP request
|
||||||
|
// Technically, all alert providers should be reachable using the custom alert provider
|
||||||
type AlertProvider struct {
|
type AlertProvider struct {
|
||||||
Url string `yaml:"url"`
|
Url string `yaml:"url"`
|
||||||
Method string `yaml:"method,omitempty"`
|
Method string `yaml:"method,omitempty"`
|
||||||
@@ -16,10 +19,16 @@ type AlertProvider struct {
|
|||||||
Headers map[string]string `yaml:"headers,omitempty"`
|
Headers map[string]string `yaml:"headers,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValid returns whether the provider's configuration is valid
|
||||||
func (provider *AlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.Url) > 0
|
return len(provider.Url) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
||||||
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *AlertProvider {
|
||||||
|
return provider
|
||||||
|
}
|
||||||
|
|
||||||
func (provider *AlertProvider) buildRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
func (provider *AlertProvider) buildRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
||||||
body := provider.Body
|
body := provider.Body
|
||||||
providerUrl := provider.Url
|
providerUrl := provider.Url
|
||||||
|
|||||||
@@ -6,16 +6,28 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AlertProvider is the configuration necessary for sending an alert using PagerDuty
|
||||||
type AlertProvider struct {
|
type AlertProvider struct {
|
||||||
IntegrationKey string `yaml:"integration-key"`
|
IntegrationKey string `yaml:"integration-key"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValid returns whether the provider's configuration is valid
|
||||||
func (provider *AlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.IntegrationKey) == 32
|
return len(provider.IntegrationKey) == 32
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://developer.pagerduty.com/docs/events-api-v2/trigger-events/
|
// https://developer.pagerduty.com/docs/events-api-v2/trigger-events/
|
||||||
func (provider *AlertProvider) ToCustomAlertProvider(eventAction, resolveKey string, service *core.Service, message string) *custom.AlertProvider {
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *custom.AlertProvider {
|
||||||
|
var message, eventAction, resolveKey string
|
||||||
|
if resolved {
|
||||||
|
message = fmt.Sprintf("RESOLVED: %s - %s", service.Name, alert.Description)
|
||||||
|
eventAction = "resolve"
|
||||||
|
resolveKey = alert.ResolveKey
|
||||||
|
} else {
|
||||||
|
message = fmt.Sprintf("TRIGGERED: %s - %s", service.Name, alert.Description)
|
||||||
|
eventAction = "trigger"
|
||||||
|
resolveKey = ""
|
||||||
|
}
|
||||||
return &custom.AlertProvider{
|
return &custom.AlertProvider{
|
||||||
Url: "https://events.pagerduty.com/v2/enqueue",
|
Url: "https://events.pagerduty.com/v2/enqueue",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AlertProvider is the interface that each providers should implement
|
||||||
|
type AlertProvider interface {
|
||||||
|
// IsValid returns whether the provider's configuration is valid
|
||||||
|
IsValid() bool
|
||||||
|
|
||||||
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
||||||
|
ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *custom.AlertProvider
|
||||||
|
}
|
||||||
@@ -6,14 +6,17 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AlertProvider is the configuration necessary for sending an alert using Slack
|
||||||
type AlertProvider struct {
|
type AlertProvider struct {
|
||||||
WebhookUrl string `yaml:"webhook-url"`
|
WebhookUrl string `yaml:"webhook-url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValid returns whether the provider's configuration is valid
|
||||||
func (provider *AlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.WebhookUrl) > 0
|
return len(provider.WebhookUrl) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
||||||
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *custom.AlertProvider {
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *custom.AlertProvider {
|
||||||
var message string
|
var message string
|
||||||
var color string
|
var color string
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
|
"github.com/TwinProduction/gatus/core"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AlertProvider is the configuration necessary for sending an alert using Twilio
|
||||||
type AlertProvider struct {
|
type AlertProvider struct {
|
||||||
SID string `yaml:"sid"`
|
SID string `yaml:"sid"`
|
||||||
Token string `yaml:"token"`
|
Token string `yaml:"token"`
|
||||||
@@ -14,11 +16,19 @@ type AlertProvider struct {
|
|||||||
To string `yaml:"to"`
|
To string `yaml:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValid returns whether the provider's configuration is valid
|
||||||
func (provider *AlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.Token) > 0 && len(provider.SID) > 0 && len(provider.From) > 0 && len(provider.To) > 0
|
return len(provider.Token) > 0 && len(provider.SID) > 0 && len(provider.From) > 0 && len(provider.To) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *AlertProvider) ToCustomAlertProvider(message string) *custom.AlertProvider {
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
||||||
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *custom.AlertProvider {
|
||||||
|
var message string
|
||||||
|
if resolved {
|
||||||
|
message = fmt.Sprintf("RESOLVED: %s - %s", service.Name, alert.Description)
|
||||||
|
} else {
|
||||||
|
message = fmt.Sprintf("TRIGGERED: %s - %s", service.Name, alert.Description)
|
||||||
|
}
|
||||||
return &custom.AlertProvider{
|
return &custom.AlertProvider{
|
||||||
Url: fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", provider.SID),
|
Url: fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", provider.SID),
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGetHttpClient(t *testing.T) {
|
||||||
|
if client != nil {
|
||||||
|
t.Error("client should've been nil since it hasn't been called a single time yet")
|
||||||
|
}
|
||||||
|
_ = GetHttpClient()
|
||||||
|
if client == nil {
|
||||||
|
t.Error("client shouldn't have been nil, since it has been called once")
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -1,15 +1,15 @@
|
|||||||
metrics: true
|
metrics: true
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
|
url: "https://twinnation.org/health"
|
||||||
interval: 30s
|
interval: 30s
|
||||||
url: https://twinnation.org/health
|
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
- "[BODY].status == UP"
|
- "[BODY].status == UP"
|
||||||
- "[RESPONSE_TIME] < 1000"
|
- "[RESPONSE_TIME] < 1000"
|
||||||
- name: cat-fact
|
- name: cat-fact
|
||||||
interval: 1m
|
|
||||||
url: "https://cat-fact.herokuapp.com/facts/random"
|
url: "https://cat-fact.herokuapp.com/facts/random"
|
||||||
|
interval: 1m
|
||||||
conditions:
|
conditions:
|
||||||
- "[STATUS] == 200"
|
- "[STATUS] == 200"
|
||||||
- "[BODY].deleted == false"
|
- "[BODY].deleted == false"
|
||||||
|
|||||||
+76
-6
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/TwinProduction/gatus/alerting"
|
"github.com/TwinProduction/gatus/alerting"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider"
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -11,6 +12,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// DefaultConfigurationFilePath is the default path that will be used to search for the configuration file
|
||||||
|
// if a custom path isn't configured through the GATUS_CONFIG_FILE environment variable
|
||||||
DefaultConfigurationFilePath = "config/config.yaml"
|
DefaultConfigurationFilePath = "config/config.yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,6 +24,7 @@ var (
|
|||||||
config *Config
|
config *Config
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config is the main configuration structure
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Metrics bool `yaml:"metrics"`
|
Metrics bool `yaml:"metrics"`
|
||||||
Debug bool `yaml:"debug"`
|
Debug bool `yaml:"debug"`
|
||||||
@@ -36,7 +40,7 @@ func Get() *Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Load(configFile string) error {
|
func Load(configFile string) error {
|
||||||
log.Printf("[config][Load] Attempting to load config from configFile=%s", configFile)
|
log.Printf("[config][Load] Reading configuration from configFile=%s", configFile)
|
||||||
cfg, err := readConfigurationFile(configFile)
|
cfg, err := readConfigurationFile(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -75,13 +79,79 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
|
|||||||
// Parse configuration file
|
// Parse configuration file
|
||||||
err = yaml.Unmarshal(yamlBytes, &config)
|
err = yaml.Unmarshal(yamlBytes, &config)
|
||||||
// Check if the configuration file at least has services.
|
// Check if the configuration file at least has services.
|
||||||
if config == nil || len(config.Services) == 0 {
|
if config == nil || config.Services == nil || len(config.Services) == 0 {
|
||||||
err = ErrNoServiceInConfig
|
err = ErrNoServiceInConfig
|
||||||
} else {
|
} else {
|
||||||
// Set the default values if they aren't set
|
validateAlertingConfig(config)
|
||||||
for _, service := range config.Services {
|
validateServicesConfig(config)
|
||||||
service.Validate()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateServicesConfig(config *Config) {
|
||||||
|
for _, service := range config.Services {
|
||||||
|
if config.Debug {
|
||||||
|
log.Printf("[config][validateServicesConfig] Validating service '%s'", service.Name)
|
||||||
|
}
|
||||||
|
service.ValidateAndSetDefaults()
|
||||||
|
}
|
||||||
|
log.Printf("[config][validateServicesConfig] Validated %d services", len(config.Services))
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateAlertingConfig(config *Config) {
|
||||||
|
if config.Alerting == nil {
|
||||||
|
log.Printf("[config][validateAlertingConfig] Alerting is not configured")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
alertTypes := []core.AlertType{
|
||||||
|
core.SlackAlert,
|
||||||
|
core.TwilioAlert,
|
||||||
|
core.PagerDutyAlert,
|
||||||
|
core.CustomAlert,
|
||||||
|
}
|
||||||
|
var validProviders, invalidProviders []core.AlertType
|
||||||
|
for _, alertType := range alertTypes {
|
||||||
|
alertProvider := GetAlertingProviderByAlertType(config, alertType)
|
||||||
|
if alertProvider != nil {
|
||||||
|
if alertProvider.IsValid() {
|
||||||
|
validProviders = append(validProviders, alertType)
|
||||||
|
} else {
|
||||||
|
log.Printf("[config][validateAlertingConfig] Ignoring provider=%s because configuration is invalid", alertType)
|
||||||
|
invalidProviders = append(invalidProviders, alertType)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
invalidProviders = append(invalidProviders, alertType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("[config][validateAlertingConfig] configuredProviders=%s; ignoredProviders=%s", validProviders, invalidProviders)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAlertingProviderByAlertType(config *Config, alertType core.AlertType) provider.AlertProvider {
|
||||||
|
switch alertType {
|
||||||
|
case core.SlackAlert:
|
||||||
|
if config.Alerting.Slack == nil {
|
||||||
|
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return config.Alerting.Slack
|
||||||
|
case core.TwilioAlert:
|
||||||
|
if config.Alerting.Twilio == nil {
|
||||||
|
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return config.Alerting.Twilio
|
||||||
|
case core.PagerDutyAlert:
|
||||||
|
if config.Alerting.PagerDuty == nil {
|
||||||
|
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return config.Alerting.PagerDuty
|
||||||
|
case core.CustomAlert:
|
||||||
|
if config.Alerting.Custom == nil {
|
||||||
|
// Since we're returning an interface, we need to explicitly return nil, even if the provider itself is nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return config.Alerting.Custom
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
+41
-4
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -36,7 +35,6 @@ services:
|
|||||||
if config.Services[1].Url != "https://api.github.com/healthz" {
|
if config.Services[1].Url != "https://api.github.com/healthz" {
|
||||||
t.Errorf("URL should have been %s", "https://api.github.com/healthz")
|
t.Errorf("URL should have been %s", "https://api.github.com/healthz")
|
||||||
}
|
}
|
||||||
fmt.Println(config.Services[0].Interval)
|
|
||||||
if config.Services[0].Interval != 15*time.Second {
|
if config.Services[0].Interval != 15*time.Second {
|
||||||
t.Errorf("Interval should have been %s", 15*time.Second)
|
t.Errorf("Interval should have been %s", 15*time.Second)
|
||||||
}
|
}
|
||||||
@@ -123,6 +121,8 @@ func TestParseAndValidateConfigBytesWithAlerting(t *testing.T) {
|
|||||||
alerting:
|
alerting:
|
||||||
slack:
|
slack:
|
||||||
webhook-url: "http://example.com"
|
webhook-url: "http://example.com"
|
||||||
|
pagerduty:
|
||||||
|
integration-key: "00000000000000000000000000000000"
|
||||||
services:
|
services:
|
||||||
- name: twinnation
|
- name: twinnation
|
||||||
url: https://twinnation.org/actuator/health
|
url: https://twinnation.org/actuator/health
|
||||||
@@ -144,10 +144,19 @@ services:
|
|||||||
t.Error("Metrics should've been false by default")
|
t.Error("Metrics should've been false by default")
|
||||||
}
|
}
|
||||||
if config.Alerting == nil {
|
if config.Alerting == nil {
|
||||||
t.Fatal("config.AlertingConfig shouldn't have been nil")
|
t.Fatal("config.Alerting shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting.Slack == nil || !config.Alerting.Slack.IsValid() {
|
||||||
|
t.Fatal("Slack alerting config should've been valid")
|
||||||
}
|
}
|
||||||
if config.Alerting.Slack.WebhookUrl != "http://example.com" {
|
if config.Alerting.Slack.WebhookUrl != "http://example.com" {
|
||||||
t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack)
|
t.Errorf("Slack webhook should've been %s, but was %s", "http://example.com", config.Alerting.Slack.WebhookUrl)
|
||||||
|
}
|
||||||
|
if config.Alerting.PagerDuty == nil || !config.Alerting.PagerDuty.IsValid() {
|
||||||
|
t.Fatal("PagerDuty alerting config should've been valid")
|
||||||
|
}
|
||||||
|
if config.Alerting.PagerDuty.IntegrationKey != "00000000000000000000000000000000" {
|
||||||
|
t.Errorf("PagerDuty integration key should've been %s, but was %s", "00000000000000000000000000000000", config.Alerting.PagerDuty.IntegrationKey)
|
||||||
}
|
}
|
||||||
if len(config.Services) != 1 {
|
if len(config.Services) != 1 {
|
||||||
t.Error("There should've been 1 service")
|
t.Error("There should've been 1 service")
|
||||||
@@ -180,3 +189,31 @@ services:
|
|||||||
t.Errorf("The type of the alert should've been %s, but it was %s", "Healthcheck failed 7 times in a row", config.Services[0].Alerts[0].Description)
|
t.Errorf("The type of the alert should've been %s, but it was %s", "Healthcheck failed 7 times in a row", config.Services[0].Alerts[0].Description)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseAndValidateConfigBytesWithInvalidPagerDutyAlertingConfig(t *testing.T) {
|
||||||
|
config, err := parseAndValidateConfigBytes([]byte(`
|
||||||
|
alerting:
|
||||||
|
pagerduty:
|
||||||
|
integration-key: "INVALID_KEY"
|
||||||
|
services:
|
||||||
|
- name: twinnation
|
||||||
|
url: https://twinnation.org/actuator/health
|
||||||
|
conditions:
|
||||||
|
- "[STATUS] == 200"
|
||||||
|
`))
|
||||||
|
if err != nil {
|
||||||
|
t.Error("No error should've been returned")
|
||||||
|
}
|
||||||
|
if config == nil {
|
||||||
|
t.Fatal("Config shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting == nil {
|
||||||
|
t.Fatal("config.Alerting shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting.PagerDuty == nil {
|
||||||
|
t.Fatal("PagerDuty alerting config shouldn't have been nil")
|
||||||
|
}
|
||||||
|
if config.Alerting.PagerDuty.IsValid() {
|
||||||
|
t.Fatal("PagerDuty alerting config should've been invalid")
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
-2
@@ -2,23 +2,25 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TwinProduction/gatus/pattern"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Condition string
|
type Condition string
|
||||||
|
|
||||||
|
// evaluate the Condition with the Result of the health check
|
||||||
func (c *Condition) evaluate(result *Result) bool {
|
func (c *Condition) evaluate(result *Result) bool {
|
||||||
condition := string(*c)
|
condition := string(*c)
|
||||||
success := false
|
success := false
|
||||||
var resolvedCondition string
|
var resolvedCondition string
|
||||||
if strings.Contains(condition, "==") {
|
if strings.Contains(condition, "==") {
|
||||||
parts := sanitizeAndResolve(strings.Split(condition, "=="), result)
|
parts := sanitizeAndResolve(strings.Split(condition, "=="), result)
|
||||||
success = parts[0] == parts[1]
|
success = isEqual(parts[0], parts[1])
|
||||||
resolvedCondition = fmt.Sprintf("%v == %v", parts[0], parts[1])
|
resolvedCondition = fmt.Sprintf("%v == %v", parts[0], parts[1])
|
||||||
} else if strings.Contains(condition, "!=") {
|
} else if strings.Contains(condition, "!=") {
|
||||||
parts := sanitizeAndResolve(strings.Split(condition, "!="), result)
|
parts := sanitizeAndResolve(strings.Split(condition, "!="), result)
|
||||||
success = parts[0] != parts[1]
|
success = !isEqual(parts[0], parts[1])
|
||||||
resolvedCondition = fmt.Sprintf("%v != %v", parts[0], parts[1])
|
resolvedCondition = fmt.Sprintf("%v != %v", parts[0], parts[1])
|
||||||
} else if strings.Contains(condition, "<=") {
|
} else if strings.Contains(condition, "<=") {
|
||||||
parts := sanitizeAndResolveNumerical(strings.Split(condition, "<="), result)
|
parts := sanitizeAndResolveNumerical(strings.Split(condition, "<="), result)
|
||||||
@@ -49,3 +51,26 @@ func (c *Condition) evaluate(result *Result) bool {
|
|||||||
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
||||||
return success
|
return success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isEqual compares two strings.
|
||||||
|
//
|
||||||
|
// It also supports the pattern function. That is to say, if one of the strings starts with PatternFunctionPrefix
|
||||||
|
// and ends with FunctionSuffix, it will be treated like a pattern.
|
||||||
|
func isEqual(first, second string) bool {
|
||||||
|
var isFirstPattern, isSecondPattern bool
|
||||||
|
if strings.HasPrefix(first, PatternFunctionPrefix) && strings.HasSuffix(first, FunctionSuffix) {
|
||||||
|
isFirstPattern = true
|
||||||
|
first = strings.TrimSuffix(strings.TrimPrefix(first, PatternFunctionPrefix), FunctionSuffix)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(second, PatternFunctionPrefix) && strings.HasSuffix(second, FunctionSuffix) {
|
||||||
|
isSecondPattern = true
|
||||||
|
second = strings.TrimSuffix(strings.TrimPrefix(second, PatternFunctionPrefix), FunctionSuffix)
|
||||||
|
}
|
||||||
|
if isFirstPattern && !isSecondPattern {
|
||||||
|
return pattern.Match(first, second)
|
||||||
|
} else if !isFirstPattern && isSecondPattern {
|
||||||
|
return pattern.Match(second, first)
|
||||||
|
} else {
|
||||||
|
return first == second
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -113,6 +113,24 @@ func TestCondition_evaluateWithBodyJsonPathComplex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyJsonPathDoublePlaceholders(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].user.firstName != [BODY].user.lastName")
|
||||||
|
result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyJsonPathDoublePlaceholdersFailure(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].user.firstName == [BODY].user.lastName")
|
||||||
|
result := &Result{Body: []byte("{\"user\": {\"firstName\": \"john\", \"lastName\": \"doe\"}}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCondition_evaluateWithBodyJsonPathLongInt(t *testing.T) {
|
func TestCondition_evaluateWithBodyJsonPathLongInt(t *testing.T) {
|
||||||
condition := Condition("[BODY].data.id == 1")
|
condition := Condition("[BODY].data.id == 1")
|
||||||
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
result := &Result{Body: []byte("{\"data\": {\"id\": 1}}")}
|
||||||
@@ -184,3 +202,66 @@ func TestCondition_evaluateWithBodyStringLength(t *testing.T) {
|
|||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyStringPattern(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].name == pat(*ohn*)")
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyStringPatternFailure(t *testing.T) {
|
||||||
|
condition := Condition("[BODY].name == pat(bob*)")
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithBodyPatternFailure(t *testing.T) {
|
||||||
|
condition := Condition("[BODY] == pat(*john*)")
|
||||||
|
result := &Result{Body: []byte("{\"name\": \"john.doe\"}")}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithIPPattern(t *testing.T) {
|
||||||
|
condition := Condition("[IP] == pat(10.*)")
|
||||||
|
result := &Result{Ip: "10.0.0.0"}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithIPPatternFailure(t *testing.T) {
|
||||||
|
condition := Condition("[IP] == pat(10.*)")
|
||||||
|
result := &Result{Ip: "255.255.255.255"}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithStatusPattern(t *testing.T) {
|
||||||
|
condition := Condition("[STATUS] == pat(4*)")
|
||||||
|
result := &Result{HttpStatus: 404}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithStatusPatternFailure(t *testing.T) {
|
||||||
|
condition := Condition("[STATUS] != pat(4*)")
|
||||||
|
result := &Result{HttpStatus: 404}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-2
@@ -50,7 +50,8 @@ type Service struct {
|
|||||||
NumberOfSuccessesInARow int
|
NumberOfSuccessesInARow int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *Service) Validate() {
|
// ValidateAndSetDefaults validates the service's configuration and sets the default value of fields that have one
|
||||||
|
func (service *Service) ValidateAndSetDefaults() {
|
||||||
// Set default values
|
// Set default values
|
||||||
if service.Interval == 0 {
|
if service.Interval == 0 {
|
||||||
service.Interval = 1 * time.Minute
|
service.Interval = 1 * time.Minute
|
||||||
@@ -83,7 +84,8 @@ func (service *Service) Validate() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *Service) EvaluateConditions() *Result {
|
// 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{}}
|
result := &Result{Success: true, Errors: []string{}}
|
||||||
service.getIp(result)
|
service.getIp(result)
|
||||||
if len(result.Errors) == 0 {
|
if len(result.Errors) == 0 {
|
||||||
|
|||||||
@@ -4,14 +4,14 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIntegrationEvaluateConditions(t *testing.T) {
|
func TestIntegrationEvaluateHealth(t *testing.T) {
|
||||||
condition := Condition("[STATUS] == 200")
|
condition := Condition("[STATUS] == 200")
|
||||||
service := Service{
|
service := Service{
|
||||||
Name: "TwiNNatioN",
|
Name: "TwiNNatioN",
|
||||||
Url: "https://twinnation.org/health",
|
Url: "https://twinnation.org/health",
|
||||||
Conditions: []*Condition{&condition},
|
Conditions: []*Condition{&condition},
|
||||||
}
|
}
|
||||||
result := service.EvaluateConditions()
|
result := service.EvaluateHealth()
|
||||||
if !result.ConditionResults[0].Success {
|
if !result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a success", condition)
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
}
|
}
|
||||||
@@ -20,14 +20,14 @@ func TestIntegrationEvaluateConditions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntegrationEvaluateConditionsWithFailure(t *testing.T) {
|
func TestIntegrationEvaluateHealthWithFailure(t *testing.T) {
|
||||||
condition := Condition("[STATUS] == 500")
|
condition := Condition("[STATUS] == 500")
|
||||||
service := Service{
|
service := Service{
|
||||||
Name: "TwiNNatioN",
|
Name: "TwiNNatioN",
|
||||||
Url: "https://twinnation.org/health",
|
Url: "https://twinnation.org/health",
|
||||||
Conditions: []*Condition{&condition},
|
Conditions: []*Condition{&condition},
|
||||||
}
|
}
|
||||||
result := service.EvaluateConditions()
|
result := service.EvaluateHealth()
|
||||||
if result.ConditionResults[0].Success {
|
if result.ConditionResults[0].Success {
|
||||||
t.Errorf("Condition '%s' should have been a failure", condition)
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -13,12 +13,14 @@ const (
|
|||||||
ResponseTimePlaceHolder = "[RESPONSE_TIME]"
|
ResponseTimePlaceHolder = "[RESPONSE_TIME]"
|
||||||
BodyPlaceHolder = "[BODY]"
|
BodyPlaceHolder = "[BODY]"
|
||||||
|
|
||||||
LengthFunctionPrefix = "len("
|
LengthFunctionPrefix = "len("
|
||||||
FunctionSuffix = ")"
|
PatternFunctionPrefix = "pat("
|
||||||
|
FunctionSuffix = ")"
|
||||||
|
|
||||||
InvalidConditionElementSuffix = "(INVALID)"
|
InvalidConditionElementSuffix = "(INVALID)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// sanitizeAndResolve sanitizes and resolves a list of element and returns the list of resolved elements
|
||||||
func sanitizeAndResolve(list []string, result *Result) []string {
|
func sanitizeAndResolve(list []string, result *Result) []string {
|
||||||
var sanitizedList []string
|
var sanitizedList []string
|
||||||
body := strings.TrimSpace(string(result.Body))
|
body := strings.TrimSpace(string(result.Body))
|
||||||
@@ -34,7 +36,7 @@ func sanitizeAndResolve(list []string, result *Result) []string {
|
|||||||
case BodyPlaceHolder:
|
case BodyPlaceHolder:
|
||||||
element = body
|
element = body
|
||||||
default:
|
default:
|
||||||
// if starts with BodyPlaceHolder, then evaluate json path
|
// if contains the BodyPlaceHolder, then evaluate json path
|
||||||
if strings.Contains(element, BodyPlaceHolder) {
|
if strings.Contains(element, BodyPlaceHolder) {
|
||||||
wantLength := false
|
wantLength := false
|
||||||
if strings.HasPrefix(element, LengthFunctionPrefix) && strings.HasSuffix(element, FunctionSuffix) {
|
if strings.HasPrefix(element, LengthFunctionPrefix) && strings.HasSuffix(element, FunctionSuffix) {
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ spec:
|
|||||||
protocol: TCP
|
protocol: TCP
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 50m
|
cpu: 200m
|
||||||
memory: 50M
|
memory: 50M
|
||||||
requests:
|
requests:
|
||||||
cpu: 20m
|
cpu: 50m
|
||||||
memory: 20M
|
memory: 20M
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- mountPath: /config
|
- mountPath: /config
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package pattern
|
||||||
|
|
||||||
|
import "path/filepath"
|
||||||
|
|
||||||
|
// Match checks whether a string matches a pattern
|
||||||
|
func Match(pattern, s string) bool {
|
||||||
|
if pattern == "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
matched, _ := filepath.Match(pattern, s)
|
||||||
|
return matched
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package pattern
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestMatch(t *testing.T) {
|
||||||
|
testMatch(t, "*", "livingroom_123", true)
|
||||||
|
testMatch(t, "**", "livingroom_123", true)
|
||||||
|
testMatch(t, "living*", "livingroom_123", true)
|
||||||
|
testMatch(t, "*living*", "livingroom_123", true)
|
||||||
|
testMatch(t, "*123", "livingroom_123", true)
|
||||||
|
testMatch(t, "*_*", "livingroom_123", true)
|
||||||
|
testMatch(t, "living*_*3", "livingroom_123", true)
|
||||||
|
testMatch(t, "living*room_*3", "livingroom_123", true)
|
||||||
|
testMatch(t, "living*room_*3", "livingroom_123", true)
|
||||||
|
testMatch(t, "*vin*om*2*", "livingroom_123", true)
|
||||||
|
testMatch(t, "livingroom_123", "livingroom_123", true)
|
||||||
|
testMatch(t, "*livingroom_123*", "livingroom_123", true)
|
||||||
|
testMatch(t, "livingroom", "livingroom_123", false)
|
||||||
|
testMatch(t, "livingroom123", "livingroom_123", false)
|
||||||
|
testMatch(t, "what", "livingroom_123", false)
|
||||||
|
testMatch(t, "*what*", "livingroom_123", false)
|
||||||
|
testMatch(t, "*.*", "livingroom_123", false)
|
||||||
|
testMatch(t, "room*123", "livingroom_123", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testMatch(t *testing.T, pattern, key string, expectedToMatch bool) {
|
||||||
|
matched := Match(pattern, key)
|
||||||
|
if expectedToMatch {
|
||||||
|
if !matched {
|
||||||
|
t.Errorf("%s should've matched pattern '%s'", key, pattern)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if matched {
|
||||||
|
t.Errorf("%s shouldn't have matched pattern '%s'", key, pattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
-73
@@ -2,8 +2,6 @@ package watchdog
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
|
||||||
"github.com/TwinProduction/gatus/config"
|
"github.com/TwinProduction/gatus/config"
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"log"
|
"log"
|
||||||
@@ -32,46 +30,20 @@ func handleAlertsToTrigger(service *core.Service, result *core.Result, cfg *conf
|
|||||||
}
|
}
|
||||||
if alert.Triggered {
|
if alert.Triggered {
|
||||||
if cfg.Debug {
|
if cfg.Debug {
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Alert with description='%s' has already been triggered, skipping", alert.Description)
|
log.Printf("[watchdog][handleAlertsToTrigger] Alert with description='%s' has already been TRIGGERED, skipping", alert.Description)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var alertProvider *custom.AlertProvider
|
alertProvider := config.GetAlertingProviderByAlertType(cfg, alert.Type)
|
||||||
if alert.Type == core.SlackAlert {
|
if alertProvider != nil && alertProvider.IsValid() {
|
||||||
if cfg.Alerting.Slack != nil && cfg.Alerting.Slack.IsValid() {
|
log.Printf("[watchdog][handleAlertsToTrigger] Sending %s alert because alert with description='%s' has been TRIGGERED", alert.Type, alert.Description)
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Sending Slack alert because alert with description='%s' has been triggered", alert.Description)
|
customAlertProvider := alertProvider.ToCustomAlertProvider(service, alert, result, false)
|
||||||
alertProvider = cfg.Alerting.Slack.ToCustomAlertProvider(service, alert, result, false)
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Not sending Slack alert despite being triggered, because there is no Slack webhook configured")
|
|
||||||
}
|
|
||||||
} else if alert.Type == core.PagerDutyAlert {
|
|
||||||
if cfg.Alerting.PagerDuty != nil && cfg.Alerting.PagerDuty.IsValid() {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Sending PagerDuty alert because alert with description='%s' has been triggered", alert.Description)
|
|
||||||
alertProvider = cfg.Alerting.PagerDuty.ToCustomAlertProvider("trigger", "", service, fmt.Sprintf("TRIGGERED: %s - %s", service.Name, alert.Description))
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Not sending PagerDuty alert despite being triggered, because PagerDuty isn't configured properly")
|
|
||||||
}
|
|
||||||
} else if alert.Type == core.TwilioAlert {
|
|
||||||
if cfg.Alerting.Twilio != nil && cfg.Alerting.Twilio.IsValid() {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Sending Twilio alert because alert with description='%s' has been triggered", alert.Description)
|
|
||||||
alertProvider = cfg.Alerting.Twilio.ToCustomAlertProvider(fmt.Sprintf("TRIGGERED: %s - %s", service.Name, alert.Description))
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Not sending Twilio alert despite being triggered, because Twilio config settings missing")
|
|
||||||
}
|
|
||||||
} else if alert.Type == core.CustomAlert {
|
|
||||||
if cfg.Alerting.Custom != nil && cfg.Alerting.Custom.IsValid() {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Sending custom alert because alert with description='%s' has been triggered", alert.Description)
|
|
||||||
alertProvider = cfg.Alerting.Custom
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Not sending custom alert despite being triggered, because there is no custom url configured")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if alertProvider != nil {
|
|
||||||
// TODO: retry on error
|
// TODO: retry on error
|
||||||
var err error
|
var err error
|
||||||
|
// We need to extract the DedupKey from PagerDuty's response
|
||||||
if alert.Type == core.PagerDutyAlert {
|
if alert.Type == core.PagerDutyAlert {
|
||||||
var body []byte
|
var body []byte
|
||||||
body, err = alertProvider.Send(service.Name, alert.Description, true)
|
body, err = customAlertProvider.Send(service.Name, alert.Description, false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var response pagerDutyResponse
|
var response pagerDutyResponse
|
||||||
err = json.Unmarshal(body, &response)
|
err = json.Unmarshal(body, &response)
|
||||||
@@ -82,13 +54,17 @@ func handleAlertsToTrigger(service *core.Service, result *core.Result, cfg *conf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err = alertProvider.Send(service.Name, alert.Description, false)
|
// All other alert types don't need to extract anything from the body, so we can just send the request right away
|
||||||
|
_, err = customAlertProvider.Send(service.Name, alert.Description, false)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Ran into error sending an alert: %s", err.Error())
|
log.Printf("[watchdog][handleAlertsToTrigger] Ran into error sending an alert: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
alert.Triggered = true
|
alert.Triggered = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
log.Printf("[watchdog][handleAlertsToResolve] Not sending alert of type=%s despite being TRIGGERED, because the provider wasn't configured properly", alert.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,44 +79,12 @@ func handleAlertsToResolve(service *core.Service, result *core.Result, cfg *conf
|
|||||||
if !alert.SendOnResolved {
|
if !alert.SendOnResolved {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var alertProvider *custom.AlertProvider
|
alertProvider := config.GetAlertingProviderByAlertType(cfg, alert.Type)
|
||||||
if alert.Type == core.SlackAlert {
|
if alertProvider != nil && alertProvider.IsValid() {
|
||||||
if cfg.Alerting.Slack != nil && cfg.Alerting.Slack.IsValid() {
|
log.Printf("[watchdog][handleAlertsToResolve] Sending %s alert because alert with description='%s' has been RESOLVED", alert.Type, alert.Description)
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Sending Slack alert because alert with description='%s' has been resolved", alert.Description)
|
customAlertProvider := alertProvider.ToCustomAlertProvider(service, alert, result, true)
|
||||||
alertProvider = cfg.Alerting.Slack.ToCustomAlertProvider(service, alert, result, true)
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Not sending Slack alert despite being resolved, because there is no Slack webhook configured")
|
|
||||||
}
|
|
||||||
} else if alert.Type == core.PagerDutyAlert {
|
|
||||||
if cfg.Alerting.PagerDuty != nil && cfg.Alerting.PagerDuty.IsValid() {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Sending PagerDuty alert because alert with description='%s' has been resolved", alert.Description)
|
|
||||||
alertProvider = cfg.Alerting.PagerDuty.ToCustomAlertProvider("resolve", alert.ResolveKey, service, fmt.Sprintf("RESOLVED: %s - %s", service.Name, alert.Description))
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Not sending PagerDuty alert despite being resolved, because PagerDuty isn't configured properly")
|
|
||||||
}
|
|
||||||
} else if alert.Type == core.TwilioAlert {
|
|
||||||
if cfg.Alerting.Twilio != nil && cfg.Alerting.Twilio.IsValid() {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Sending Twilio alert because alert with description='%s' has been resolved", alert.Description)
|
|
||||||
alertProvider = cfg.Alerting.Twilio.ToCustomAlertProvider(fmt.Sprintf("RESOLVED: %s - %s", service.Name, alert.Description))
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Not sending Twilio alert despite being resolved, because Twilio isn't configured properly")
|
|
||||||
}
|
|
||||||
} else if alert.Type == core.CustomAlert {
|
|
||||||
if cfg.Alerting.Custom != nil && cfg.Alerting.Custom.IsValid() {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Sending custom alert because alert with description='%s' has been resolved", alert.Description)
|
|
||||||
alertProvider = &custom.AlertProvider{
|
|
||||||
Url: cfg.Alerting.Custom.Url,
|
|
||||||
Method: cfg.Alerting.Custom.Method,
|
|
||||||
Body: cfg.Alerting.Custom.Body,
|
|
||||||
Headers: cfg.Alerting.Custom.Headers,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Not sending custom alert despite being resolved, because the custom provider isn't configured properly")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if alertProvider != nil {
|
|
||||||
// TODO: retry on error
|
// TODO: retry on error
|
||||||
_, err := alertProvider.Send(service.Name, alert.Description, true)
|
_, err := customAlertProvider.Send(service.Name, alert.Description, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Ran into error sending an alert: %s", err.Error())
|
log.Printf("[watchdog][handleAlertsToResolve] Ran into error sending an alert: %s", err.Error())
|
||||||
} else {
|
} else {
|
||||||
@@ -148,6 +92,8 @@ func handleAlertsToResolve(service *core.Service, result *core.Result, cfg *conf
|
|||||||
alert.ResolveKey = ""
|
alert.ResolveKey = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Printf("[watchdog][handleAlertsToResolve] Not sending alert of type=%s despite being RESOLVED, because the provider wasn't configured properly", alert.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
service.NumberOfFailuresInARow = 0
|
service.NumberOfFailuresInARow = 0
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func monitor(service *core.Service) {
|
|||||||
if cfg.Debug {
|
if cfg.Debug {
|
||||||
log.Printf("[watchdog][monitor] Monitoring serviceName=%s", service.Name)
|
log.Printf("[watchdog][monitor] Monitoring serviceName=%s", service.Name)
|
||||||
}
|
}
|
||||||
result := service.EvaluateConditions()
|
result := service.EvaluateHealth()
|
||||||
metric.PublishMetricsForService(service, result)
|
metric.PublishMetricsForService(service, result)
|
||||||
serviceResultsMutex.Lock()
|
serviceResultsMutex.Lock()
|
||||||
serviceResults[service.Name] = append(serviceResults[service.Name], result)
|
serviceResults[service.Name] = append(serviceResults[service.Name], result)
|
||||||
|
|||||||
Reference in new issue
Block a user