Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
230c8821ec | ||
|
|
ecef3d9538 | ||
|
|
3ecfe4d322 | ||
|
|
6a3f65db7f | ||
|
|
ed490669b1 | ||
|
|
187b38ee22 | ||
|
|
261fe27b2e | ||
|
|
a29f175cba | ||
|
|
f26dacaf22 | ||
|
|
b4d81a9661 | ||
|
|
81c59f02d8 | ||
|
|
8101646ba5 | ||
|
|
93b7220d5f | ||
|
|
18d3236586 | ||
|
|
bc16ae1794 | ||
|
|
d019942278 | ||
|
|
8c5551f93b | ||
|
|
3b15feaf7d | ||
|
|
326ad2c80d | ||
|
|
b4b9f5c1be |
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)
|
||||||
@@ -27,6 +29,9 @@ core applications: https://status.twinnation.org/
|
|||||||
- [Using in Production](#using-in-production)
|
- [Using in Production](#using-in-production)
|
||||||
- [FAQ](#faq)
|
- [FAQ](#faq)
|
||||||
- [Sending a GraphQL request](#sending-a-graphql-request)
|
- [Sending a GraphQL request](#sending-a-graphql-request)
|
||||||
|
- [Recommended interval](#recommended-interval)
|
||||||
|
- [Default timeouts](#default-timeouts)
|
||||||
|
- [Monitoring a TCP service](#monitoring-a-tcp-service)
|
||||||
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
@@ -75,13 +80,14 @@ 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 `[]` |
|
||||||
| `services[].name` | Name of the service. Can be anything. | Required `""` |
|
| `services[].name` | Name of the service. Can be anything. | Required `""` |
|
||||||
| `services[].url` | URL to send the request to | Required `""` |
|
| `services[].url` | URL to send the request to | Required `""` |
|
||||||
| `services[].conditions` | Conditions used to determine the health of the service | `[]` |
|
| `services[].conditions` | Conditions used to determine the health of the service | `[]` |
|
||||||
|
| `services[].insecure` | Whether to skip verifying the server's certificate chain and host name | `false` |
|
||||||
| `services[].interval` | Duration to wait between every status check | `60s` |
|
| `services[].interval` | Duration to wait between every status check | `60s` |
|
||||||
| `services[].method` | Request method | `GET` |
|
| `services[].method` | Request method | `GET` |
|
||||||
| `services[].graphql` | Whether to wrap the body in a query param (`{"query":"$body"}`) | `false` |
|
| `services[].graphql` | Whether to wrap the body in a query param (`{"query":"$body"}`) | `false` |
|
||||||
@@ -114,23 +120,48 @@ 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, ... |
|
||||||
|
| `[CONNECTED] == true` | Connection to host must've been successful | true, false | |
|
||||||
| `[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"}`
|
||||||
|
| `[CONNECTED]` | Resolves into whether a connection could be established | `true`
|
||||||
|
|
||||||
|
|
||||||
|
#### 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 +170,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 +206,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 +239,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 +276,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 +298,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
|
||||||
@@ -320,3 +351,60 @@ will send a `POST` request to `http://localhost:8080/playground` with the follow
|
|||||||
```json
|
```json
|
||||||
{"query":" {\n user(gender: \"female\") {\n id\n name\n gender\n avatar\n }\n }"}
|
{"query":" {\n user(gender: \"female\") {\n id\n name\n gender\n avatar\n }\n }"}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Recommended interval
|
||||||
|
|
||||||
|
To ensure that Gatus provides reliable and accurate results (i.e. response time), Gatus only evaluates one service at a time.
|
||||||
|
In other words, even if you have multiple services with the exact same interval, they will not execute at the same time.
|
||||||
|
|
||||||
|
You can test this yourself by running Gatus with several services configured with a very short, unrealistic interval,
|
||||||
|
such as 1ms. You'll notice that the response time does not fluctuate - that is because while services are evaluated on
|
||||||
|
different goroutines, there's a global lock that prevents multiple services from running at the same time.
|
||||||
|
|
||||||
|
Unfortunately, there is a drawback. If you have a lot of services, including some that are very slow or prone to time out (the default
|
||||||
|
time out is 10s for HTTP and 5s for TCP), then it means that for the entire duration of the request, no other services can be evaluated.
|
||||||
|
|
||||||
|
**This does mean that Gatus will be unable to evaluate the health of other services**.
|
||||||
|
The interval does not include the duration of the request itself, which means that if a service has an interval of 30s
|
||||||
|
and the request takes 2s to complete, the timestamp between two evaluations will be 32s, not 30s.
|
||||||
|
|
||||||
|
While this does not prevent Gatus' from performing health checks on all other services, it may cause Gatus to be unable
|
||||||
|
to respect the configured interval, for instance:
|
||||||
|
- Service A has an interval of 5s, and times out after 10s to complete
|
||||||
|
- Service B has an interval of 5s, and takes 1ms to complete
|
||||||
|
- Service B will be unable to run every 5s, because service A's health evaluation takes longer than its interval
|
||||||
|
|
||||||
|
To sum it up, while Gatus can really handle any interval you throw at it, you're better off having slow requests with
|
||||||
|
higher interval.
|
||||||
|
|
||||||
|
As a rule of the thumb, I personally set interval for more complex health checks to `5m` (5 minutes) and
|
||||||
|
simple health checks used for alerting (PagerDuty/Twilio) to `30s`.
|
||||||
|
|
||||||
|
|
||||||
|
### Default timeouts
|
||||||
|
|
||||||
|
| Protocol | Timeout |
|
||||||
|
|:-------- |:------- |
|
||||||
|
| HTTP | 10s
|
||||||
|
| TCP | 5s
|
||||||
|
|
||||||
|
|
||||||
|
### Monitoring a TCP service
|
||||||
|
|
||||||
|
By prefixing `services[].url` with `tcp:\\`, you can monitor TCP services at a very basic level:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
- name: redis
|
||||||
|
url: "tcp://127.0.0.1:6379"
|
||||||
|
interval: 30s
|
||||||
|
conditions:
|
||||||
|
- "[CONNECTED] == true"
|
||||||
|
```
|
||||||
|
|
||||||
|
Placeholders `[STATUS]` and `[BODY]` as well as the fields `services[].body`, `services[].insecure`,
|
||||||
|
`services[].headers`, `services[].method` and `services[].graphql` are not supported for TCP services.
|
||||||
|
|
||||||
|
**NOTE**: `[CONNECTED] == true` does not guarantee that the service itself is healthy - it only guarantees that there's
|
||||||
|
something at the given address listening to the given port, and that a connection to that address was successfully
|
||||||
|
established.
|
||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"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"`
|
||||||
@@ -17,10 +19,12 @@ 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 {
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *AlertProvider {
|
||||||
return provider
|
return provider
|
||||||
}
|
}
|
||||||
@@ -69,7 +73,7 @@ func (provider *AlertProvider) buildRequest(serviceName, alertDescription string
|
|||||||
// Send a request to the alert provider and return the body
|
// Send a request to the alert provider and return the body
|
||||||
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
||||||
request := provider.buildRequest(serviceName, alertDescription, resolved)
|
request := provider.buildRequest(serviceName, alertDescription, resolved)
|
||||||
response, err := client.GetHttpClient().Do(request)
|
response, err := client.GetHttpClient(false).Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AlertProvider is the interface that each providers should implement
|
||||||
type AlertProvider interface {
|
type AlertProvider interface {
|
||||||
|
// IsValid returns whether the provider's configuration is valid
|
||||||
IsValid() bool
|
IsValid() bool
|
||||||
|
|
||||||
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
||||||
ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *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
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"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"`
|
||||||
@@ -15,10 +16,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
if resolved {
|
if resolved {
|
||||||
|
|||||||
+32
-6
@@ -1,19 +1,45 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
client *http.Client
|
secureHttpClient *http.Client
|
||||||
|
insecureHttpClient *http.Client
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetHttpClient() *http.Client {
|
func GetHttpClient(insecure bool) *http.Client {
|
||||||
if client == nil {
|
if insecure {
|
||||||
client = &http.Client{
|
if insecureHttpClient == nil {
|
||||||
Timeout: time.Second * 10,
|
insecureHttpClient = &http.Client{
|
||||||
|
Timeout: time.Second * 10,
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return insecureHttpClient
|
||||||
|
} else {
|
||||||
|
if secureHttpClient == nil {
|
||||||
|
secureHttpClient = &http.Client{
|
||||||
|
Timeout: time.Second * 10,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return secureHttpClient
|
||||||
}
|
}
|
||||||
return client
|
}
|
||||||
|
|
||||||
|
func CanCreateConnectionToTcpService(address string) bool {
|
||||||
|
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_ = conn.Close()
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
+21
-6
@@ -1,13 +1,28 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestGetHttpClient(t *testing.T) {
|
func TestGetHttpClient(t *testing.T) {
|
||||||
if client != nil {
|
if secureHttpClient != nil {
|
||||||
t.Error("client should've been nil since it hasn't been called a single time yet")
|
t.Error("secureHttpClient should've been nil since it hasn't been called a single time yet")
|
||||||
}
|
}
|
||||||
_ = GetHttpClient()
|
if insecureHttpClient != nil {
|
||||||
if client == nil {
|
t.Error("insecureHttpClient should've been nil since it hasn't been called a single time yet")
|
||||||
t.Error("client shouldn't have been nil, since it has been called once")
|
}
|
||||||
|
_ = GetHttpClient(false)
|
||||||
|
if secureHttpClient == nil {
|
||||||
|
t.Error("secureHttpClient shouldn't have been nil, since it has been called once")
|
||||||
|
}
|
||||||
|
if insecureHttpClient != nil {
|
||||||
|
t.Error("insecureHttpClient should've been nil since it hasn't been called a single time yet")
|
||||||
|
}
|
||||||
|
_ = GetHttpClient(true)
|
||||||
|
if secureHttpClient == nil {
|
||||||
|
t.Error("secureHttpClient shouldn't have been nil, since it has been called once")
|
||||||
|
}
|
||||||
|
if insecureHttpClient == nil {
|
||||||
|
t.Error("insecureHttpClient shouldn't have been nil, since it has been called once")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,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"`
|
||||||
|
|||||||
+100
-2
@@ -2,23 +2,41 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TwinProduction/gatus/jsonpath"
|
||||||
|
"github.com/TwinProduction/gatus/pattern"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusPlaceholder = "[STATUS]"
|
||||||
|
IPPlaceHolder = "[IP]"
|
||||||
|
ResponseTimePlaceHolder = "[RESPONSE_TIME]"
|
||||||
|
BodyPlaceHolder = "[BODY]"
|
||||||
|
ConnectedPlaceHolder = "[CONNECTED]"
|
||||||
|
|
||||||
|
LengthFunctionPrefix = "len("
|
||||||
|
PatternFunctionPrefix = "pat("
|
||||||
|
FunctionSuffix = ")"
|
||||||
|
|
||||||
|
InvalidConditionElementSuffix = "(INVALID)"
|
||||||
|
)
|
||||||
|
|
||||||
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 +67,83 @@ 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sanitizeAndResolve sanitizes and resolves a list of element and returns the list of resolved elements
|
||||||
|
func sanitizeAndResolve(list []string, result *Result) []string {
|
||||||
|
var sanitizedList []string
|
||||||
|
body := strings.TrimSpace(string(result.Body))
|
||||||
|
for _, element := range list {
|
||||||
|
element = strings.TrimSpace(element)
|
||||||
|
switch strings.ToUpper(element) {
|
||||||
|
case StatusPlaceholder:
|
||||||
|
element = strconv.Itoa(result.HttpStatus)
|
||||||
|
case IPPlaceHolder:
|
||||||
|
element = result.Ip
|
||||||
|
case ResponseTimePlaceHolder:
|
||||||
|
element = strconv.Itoa(int(result.Duration.Milliseconds()))
|
||||||
|
case BodyPlaceHolder:
|
||||||
|
element = body
|
||||||
|
case ConnectedPlaceHolder:
|
||||||
|
element = strconv.FormatBool(result.Connected)
|
||||||
|
default:
|
||||||
|
// if contains the BodyPlaceHolder, then evaluate json path
|
||||||
|
if strings.Contains(element, BodyPlaceHolder) {
|
||||||
|
wantLength := false
|
||||||
|
if strings.HasPrefix(element, LengthFunctionPrefix) && strings.HasSuffix(element, FunctionSuffix) {
|
||||||
|
wantLength = true
|
||||||
|
element = strings.TrimSuffix(strings.TrimPrefix(element, LengthFunctionPrefix), FunctionSuffix)
|
||||||
|
}
|
||||||
|
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.Replace(element, fmt.Sprintf("%s.", BodyPlaceHolder), "", 1), result.Body)
|
||||||
|
if err != nil {
|
||||||
|
result.Errors = append(result.Errors, err.Error())
|
||||||
|
element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix)
|
||||||
|
} else {
|
||||||
|
if wantLength {
|
||||||
|
element = fmt.Sprintf("%d", resolvedElementLength)
|
||||||
|
} else {
|
||||||
|
element = resolvedElement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sanitizedList = append(sanitizedList, element)
|
||||||
|
}
|
||||||
|
return sanitizedList
|
||||||
|
}
|
||||||
|
|
||||||
|
func sanitizeAndResolveNumerical(list []string, result *Result) []int {
|
||||||
|
var sanitizedNumbers []int
|
||||||
|
sanitizedList := sanitizeAndResolve(list, result)
|
||||||
|
for _, element := range sanitizedList {
|
||||||
|
if number, err := strconv.Atoi(element); err != nil {
|
||||||
|
// Default to 0 if the string couldn't be converted to an integer
|
||||||
|
sanitizedNumbers = append(sanitizedNumbers, 0)
|
||||||
|
} else {
|
||||||
|
sanitizedNumbers = append(sanitizedNumbers, number)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sanitizedNumbers
|
||||||
|
}
|
||||||
@@ -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,84 @@ 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithConnected(t *testing.T) {
|
||||||
|
condition := Condition("[CONNECTED] == true")
|
||||||
|
result := &Result{Connected: true}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if !result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a success", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCondition_evaluateWithConnectedFailure(t *testing.T) {
|
||||||
|
condition := Condition("[CONNECTED] == true")
|
||||||
|
result := &Result{Connected: false}
|
||||||
|
condition.evaluate(result)
|
||||||
|
if result.ConditionResults[0].Success {
|
||||||
|
t.Errorf("Condition '%s' should have been a failure", condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
-12
@@ -9,6 +9,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,6 +47,9 @@ type Service struct {
|
|||||||
// Alerts is the alerting configuration for the service in case of failure
|
// Alerts is the alerting configuration for the service in case of failure
|
||||||
Alerts []*Alert `yaml:"alerts"`
|
Alerts []*Alert `yaml:"alerts"`
|
||||||
|
|
||||||
|
// Insecure is whether to skip verifying the server's certificate chain and host name
|
||||||
|
Insecure bool `yaml:"insecure,omitempty"`
|
||||||
|
|
||||||
NumberOfFailuresInARow int
|
NumberOfFailuresInARow int
|
||||||
NumberOfSuccessesInARow int
|
NumberOfSuccessesInARow int
|
||||||
}
|
}
|
||||||
@@ -133,19 +137,30 @@ func (service *Service) getIp(result *Result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (service *Service) call(result *Result) {
|
func (service *Service) call(result *Result) {
|
||||||
request := service.buildRequest()
|
isServiceTcp := strings.HasPrefix(service.Url, "tcp://")
|
||||||
startTime := time.Now()
|
var request *http.Request
|
||||||
response, err := client.GetHttpClient().Do(request)
|
var response *http.Response
|
||||||
if err != nil {
|
var err error
|
||||||
result.Duration = time.Since(startTime)
|
if !isServiceTcp {
|
||||||
result.Errors = append(result.Errors, err.Error())
|
request = service.buildRequest()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
result.Duration = time.Since(startTime)
|
startTime := time.Now()
|
||||||
result.HttpStatus = response.StatusCode
|
if isServiceTcp {
|
||||||
result.Body, err = ioutil.ReadAll(response.Body)
|
result.Connected = client.CanCreateConnectionToTcpService(strings.TrimPrefix(service.Url, "tcp://"))
|
||||||
if err != nil {
|
result.Duration = time.Since(startTime)
|
||||||
result.Errors = append(result.Errors, err.Error())
|
} else {
|
||||||
|
response, err = client.GetHttpClient(service.Insecure).Do(request)
|
||||||
|
result.Duration = time.Since(startTime)
|
||||||
|
if err != nil {
|
||||||
|
result.Errors = append(result.Errors, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ func TestIntegrationEvaluateHealth(t *testing.T) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
if !result.Connected {
|
||||||
|
t.Error("Because the connection has been established, result.Connected should've been true")
|
||||||
|
}
|
||||||
if !result.Success {
|
if !result.Success {
|
||||||
t.Error("Because all conditions passed, this should have been a success")
|
t.Error("Because all conditions passed, this should have been a success")
|
||||||
}
|
}
|
||||||
@@ -31,6 +34,9 @@ func TestIntegrationEvaluateHealthWithFailure(t *testing.T) {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
if !result.Connected {
|
||||||
|
t.Error("Because the connection has been established, result.Connected should've been true")
|
||||||
|
}
|
||||||
if result.Success {
|
if result.Success {
|
||||||
t.Error("Because one of the conditions failed, success should have been false")
|
t.Error("Because one of the conditions failed, success should have been false")
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-8
@@ -9,16 +9,37 @@ type HealthStatus struct {
|
|||||||
Message string `json:"message,omitempty"`
|
Message string `json:"message,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Result of the evaluation of a Service
|
||||||
type Result struct {
|
type Result struct {
|
||||||
HttpStatus int `json:"status"`
|
// HttpStatus is the HTTP response status code
|
||||||
Body []byte `json:"-"`
|
HttpStatus int `json:"status"`
|
||||||
Hostname string `json:"hostname"`
|
|
||||||
Ip string `json:"-"`
|
// Body is the response body
|
||||||
Duration time.Duration `json:"duration"`
|
Body []byte `json:"-"`
|
||||||
Errors []string `json:"errors"`
|
|
||||||
|
// Hostname extracted from the Service Url
|
||||||
|
Hostname string `json:"hostname"`
|
||||||
|
|
||||||
|
// Ip resolved from the Service Url
|
||||||
|
Ip string `json:"-"`
|
||||||
|
|
||||||
|
// Connected whether a connection to the host was established successfully
|
||||||
|
Connected bool `json:"-"`
|
||||||
|
|
||||||
|
// Duration time that the request took
|
||||||
|
Duration time.Duration `json:"duration"`
|
||||||
|
|
||||||
|
// Errors encountered during the evaluation of the service's health
|
||||||
|
Errors []string `json:"errors"`
|
||||||
|
|
||||||
|
// ConditionResults results of the service's conditions
|
||||||
ConditionResults []*ConditionResult `json:"condition-results"`
|
ConditionResults []*ConditionResult `json:"condition-results"`
|
||||||
Success bool `json:"success"`
|
|
||||||
Timestamp time.Time `json:"timestamp"`
|
// Success whether the result signifies a success or not
|
||||||
|
Success bool `json:"success"`
|
||||||
|
|
||||||
|
// Timestamp when the request was sent
|
||||||
|
Timestamp time.Time `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConditionResult struct {
|
type ConditionResult struct {
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/TwinProduction/gatus/jsonpath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
StatusPlaceholder = "[STATUS]"
|
|
||||||
IPPlaceHolder = "[IP]"
|
|
||||||
ResponseTimePlaceHolder = "[RESPONSE_TIME]"
|
|
||||||
BodyPlaceHolder = "[BODY]"
|
|
||||||
|
|
||||||
LengthFunctionPrefix = "len("
|
|
||||||
FunctionSuffix = ")"
|
|
||||||
|
|
||||||
InvalidConditionElementSuffix = "(INVALID)"
|
|
||||||
)
|
|
||||||
|
|
||||||
func sanitizeAndResolve(list []string, result *Result) []string {
|
|
||||||
var sanitizedList []string
|
|
||||||
body := strings.TrimSpace(string(result.Body))
|
|
||||||
for _, element := range list {
|
|
||||||
element = strings.TrimSpace(element)
|
|
||||||
switch strings.ToUpper(element) {
|
|
||||||
case StatusPlaceholder:
|
|
||||||
element = strconv.Itoa(result.HttpStatus)
|
|
||||||
case IPPlaceHolder:
|
|
||||||
element = result.Ip
|
|
||||||
case ResponseTimePlaceHolder:
|
|
||||||
element = strconv.Itoa(int(result.Duration.Milliseconds()))
|
|
||||||
case BodyPlaceHolder:
|
|
||||||
element = body
|
|
||||||
default:
|
|
||||||
// if starts with BodyPlaceHolder, then evaluate json path
|
|
||||||
if strings.Contains(element, BodyPlaceHolder) {
|
|
||||||
wantLength := false
|
|
||||||
if strings.HasPrefix(element, LengthFunctionPrefix) && strings.HasSuffix(element, FunctionSuffix) {
|
|
||||||
wantLength = true
|
|
||||||
element = strings.TrimSuffix(strings.TrimPrefix(element, LengthFunctionPrefix), FunctionSuffix)
|
|
||||||
}
|
|
||||||
resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.Replace(element, fmt.Sprintf("%s.", BodyPlaceHolder), "", 1), result.Body)
|
|
||||||
if err != nil {
|
|
||||||
result.Errors = append(result.Errors, err.Error())
|
|
||||||
element = fmt.Sprintf("%s %s", element, InvalidConditionElementSuffix)
|
|
||||||
} else {
|
|
||||||
if wantLength {
|
|
||||||
element = fmt.Sprintf("%d", resolvedElementLength)
|
|
||||||
} else {
|
|
||||||
element = resolvedElement
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sanitizedList = append(sanitizedList, element)
|
|
||||||
}
|
|
||||||
return sanitizedList
|
|
||||||
}
|
|
||||||
|
|
||||||
func sanitizeAndResolveNumerical(list []string, result *Result) []int {
|
|
||||||
var sanitizedNumbers []int
|
|
||||||
sanitizedList := sanitizeAndResolve(list, result)
|
|
||||||
for _, element := range sanitizedList {
|
|
||||||
if number, err := strconv.Atoi(element); err != nil {
|
|
||||||
// Default to 0 if the string couldn't be converted to an integer
|
|
||||||
sanitizedNumbers = append(sanitizedNumbers, 0)
|
|
||||||
} else {
|
|
||||||
sanitizedNumbers = append(sanitizedNumbers, number)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sanitizedNumbers
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
-3
@@ -90,6 +90,15 @@
|
|||||||
#social img:hover {
|
#social img:hover {
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
#settings {
|
||||||
|
position: fixed;
|
||||||
|
left: 5px;
|
||||||
|
bottom: 5px;
|
||||||
|
padding: 5px;
|
||||||
|
}
|
||||||
|
#settings select:focus {
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -128,9 +137,26 @@
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="settings">
|
||||||
|
<div class="input-group input-group-sm">
|
||||||
|
<div class="input-group-prepend">
|
||||||
|
<div class="input-group-text">↻</div>
|
||||||
|
</div>
|
||||||
|
<select class="form-control form-control-sm" id="refresh-rate">
|
||||||
|
<option value="10">10s</option>
|
||||||
|
<option value="30" selected>30s</option>
|
||||||
|
<option value="60">1m</option>
|
||||||
|
<option value="120">2m</option>
|
||||||
|
<option value="300">5m</option>
|
||||||
|
<option value="600">10m</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
let serviceStatuses = {};
|
let serviceStatuses = {};
|
||||||
let timerHandler = 0;
|
let timerHandler = 0;
|
||||||
|
let refreshIntervalHandler = 0;
|
||||||
let userClickedStatus = false;
|
let userClickedStatus = false;
|
||||||
|
|
||||||
function showTooltip(serviceName, index, element) {
|
function showTooltip(serviceName, index, element) {
|
||||||
@@ -291,10 +317,19 @@
|
|||||||
.replace(/'/g, ''');
|
.replace(/'/g, ''');
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshResults();
|
function setRefreshInterval(seconds) {
|
||||||
setInterval(function() {
|
|
||||||
refreshResults();
|
refreshResults();
|
||||||
}, 30000);
|
refreshIntervalHandler = setInterval(function() {
|
||||||
|
refreshResults();
|
||||||
|
}, seconds * 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
$("#refresh-rate").change(function() {
|
||||||
|
clearInterval(refreshIntervalHandler);
|
||||||
|
setRefreshInterval($(this).val())
|
||||||
|
});
|
||||||
|
setRefreshInterval(30);
|
||||||
|
$("#refresh-rate").val(30);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -30,13 +30,13 @@ 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
|
||||||
}
|
}
|
||||||
alertProvider := config.GetAlertingProviderByAlertType(cfg, alert.Type)
|
alertProvider := config.GetAlertingProviderByAlertType(cfg, alert.Type)
|
||||||
if alertProvider != nil && alertProvider.IsValid() {
|
if alertProvider != nil && alertProvider.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 %s alert because alert with description='%s' has been TRIGGERED", alert.Type, alert.Description)
|
||||||
customAlertProvider := alertProvider.ToCustomAlertProvider(service, alert, result, false)
|
customAlertProvider := alertProvider.ToCustomAlertProvider(service, alert, result, false)
|
||||||
// TODO: retry on error
|
// TODO: retry on error
|
||||||
var err error
|
var err error
|
||||||
@@ -64,7 +64,7 @@ func handleAlertsToTrigger(service *core.Service, result *core.Result, cfg *conf
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Not sending alert of type=%s despite being triggered, because the provider wasn't configured properly", alert.Type)
|
log.Printf("[watchdog][handleAlertsToResolve] Not sending alert of type=%s despite being TRIGGERED, because the provider wasn't configured properly", alert.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,7 @@ func handleAlertsToResolve(service *core.Service, result *core.Result, cfg *conf
|
|||||||
}
|
}
|
||||||
alertProvider := config.GetAlertingProviderByAlertType(cfg, alert.Type)
|
alertProvider := config.GetAlertingProviderByAlertType(cfg, alert.Type)
|
||||||
if alertProvider != nil && alertProvider.IsValid() {
|
if alertProvider != nil && alertProvider.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 %s alert because alert with description='%s' has been RESOLVED", alert.Type, alert.Description)
|
||||||
customAlertProvider := alertProvider.ToCustomAlertProvider(service, alert, result, true)
|
customAlertProvider := alertProvider.ToCustomAlertProvider(service, alert, result, true)
|
||||||
// TODO: retry on error
|
// TODO: retry on error
|
||||||
_, err := customAlertProvider.Send(service.Name, alert.Description, true)
|
_, err := customAlertProvider.Send(service.Name, alert.Description, true)
|
||||||
@@ -93,7 +93,7 @@ func handleAlertsToResolve(service *core.Service, result *core.Result, cfg *conf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Not sending alert of type=%s despite being resolved, because the provider wasn't configured properly", alert.Type)
|
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
|
||||||
|
|||||||
Reference in new issue
Block a user