Compare commits

..

3 Commits

Author SHA1 Message Date
TwinProduction
fe82465c19 Prevent multiple services from being evaluated at the same time 2020-04-06 18:58:13 -04:00
TwinProduction
ab73c4666e Minor improvements 2020-03-10 18:34:32 -04:00
TwinProduction
16837562ea Add GATUS_CONFIG_FILE env var to support custom config path 2020-03-08 18:16:39 -04:00
9 changed files with 90 additions and 71 deletions

View File

@@ -10,11 +10,15 @@ Live example: https://status.twinnation.org/
## Usage
By default, the configuration file is expected to be at `config/config.yaml`.
You can specify a custom path by setting the `GATUS_CONFIG_FILE` environment variable.
```yaml
metrics: true # Whether to expose metrics at /metrics
services:
- name: twinnation # Name of your service, can be anything
url: https://twinnation.org/actuator/health
url: https://twinnation.org/health
interval: 15s # Duration to wait between every status check (opt. default: 10s)
conditions:
- "[STATUS] == 200"

View File

@@ -1,11 +1,12 @@
metrics: true
services:
- name: twinnation
url: https://twinnation.org/actuator/health
interval: 15s
- name: Twinnation
url: https://twinnation.org/health
interval: 30s
conditions:
- "[STATUS] == 200"
- name: github
- name: GitHub API
url: https://api.github.com/healthz
interval: 30s
conditions:
- "[STATUS] == 200"

View File

@@ -23,7 +23,7 @@ type ServerMessage struct {
type Result struct {
HttpStatus int `json:"status"`
Hostname string `json:"hostname"`
Ip string `json:"ip"`
Ip string `json:"-"`
Duration time.Duration `json:"duration"`
Errors []string `json:"errors"`
ConditionResults []*ConditionResult `json:"condition-results"`
@@ -76,7 +76,7 @@ func (service *Service) EvaluateConditions() *Result {
result.Success = false
}
for _, condition := range service.Conditions {
success := condition.Evaluate(result)
success := condition.evaluate(result)
if !success {
result.Success = false
}
@@ -88,12 +88,12 @@ func (service *Service) EvaluateConditions() *Result {
type ConditionResult struct {
Condition *Condition `json:"condition"`
Success bool `json:"success"`
Explanation string `json:"explanation"`
Explanation string `json:"-"`
}
type Condition string
func (c *Condition) Evaluate(result *Result) bool {
func (c *Condition) evaluate(result *Result) bool {
condition := string(*c)
if strings.Contains(condition, "==") {
parts := sanitizeAndResolve(strings.Split(condition, "=="), result)

View File

@@ -7,7 +7,7 @@ import (
func TestEvaluateWithIp(t *testing.T) {
condition := Condition("[IP] == 127.0.0.1")
result := &Result{Ip: "127.0.0.1"}
condition.Evaluate(result)
condition.evaluate(result)
if !result.ConditionResults[0].Success {
t.Errorf("Condition '%s' should have been a success", condition)
}
@@ -16,7 +16,7 @@ func TestEvaluateWithIp(t *testing.T) {
func TestEvaluateWithStatus(t *testing.T) {
condition := Condition("[STATUS] == 201")
result := &Result{HttpStatus: 201}
condition.Evaluate(result)
condition.evaluate(result)
if !result.ConditionResults[0].Success {
t.Errorf("Condition '%s' should have been a success", condition)
}
@@ -25,7 +25,7 @@ func TestEvaluateWithStatus(t *testing.T) {
func TestEvaluateWithFailure(t *testing.T) {
condition := Condition("[STATUS] == 200")
result := &Result{HttpStatus: 500}
condition.Evaluate(result)
condition.evaluate(result)
if result.ConditionResults[0].Success {
t.Errorf("Condition '%s' should have been a failure", condition)
}

View File

@@ -1,7 +1,7 @@
metrics: true
services:
- name: TwiNNatioN
url: https://twinnation.org/actuator/health
url: https://twinnation.org/health
interval: 10s
conditions:
- "[STATUS] == 200"

View File

@@ -4,7 +4,7 @@ data:
metrics: true
services:
- name: TwiNNatioN
url: https://twinnation.org/actuator/health
url: https://twinnation.org/health
interval: 1m
conditions:
- "[STATUS] == 200"

View File

@@ -25,10 +25,10 @@ func main() {
}
func loadConfiguration() *config.Config {
args := os.Args
var err error
if len(args) == 2 {
err = config.Load(args[1])
customConfigFile := os.Getenv("GATUS_CONFIG_FILE")
if len(customConfigFile) > 0 {
err = config.Load(customConfigFile)
} else {
err = config.LoadDefaultConfiguration()
}

View File

@@ -4,11 +4,23 @@
<title>Status</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
td > span.badge {
#results div.container:first-child {
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
#results div.container:last-child {
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
border-bottom-width: 1px;
border-color: #dee2e6;
border-style: solid;
}
.status-ok {
display: inline-block;
width: 20px;
cursor: default;
margin-right: 2px;
width: 1%;
height: 20px;
margin-right: 4px;
background-color: #28a745;
}
</style>
</head>
@@ -17,19 +29,7 @@
<div class="text-center mb-3">
<div class="display-4">Status</div>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Status</th>
<th scope="col">Hostname</th>
<th scope="col">Response time</th>
</tr>
</thead>
<tbody id="results">
</tbody>
</table>
<div id="results">
</div>
</div>
@@ -38,8 +38,9 @@
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
const OK = "<span class='badge badge-success' title='__RESPONSE_TIME____CONDITIONS____ERRORS__'>&#10003;</span>";
const NOK = "<span class='badge badge-danger' title='__RESPONSE_TIME____CONDITIONS____ERRORS__'>X</span>";
//const OK = "<div class='status-ok' title='__RESPONSE_TIME____CONDITIONS____ERRORS__'></div>"
const OK = "<span class='badge badge-success ml-1' style='width: 5%' title='__RESPONSE_TIME____CONDITIONS____ERRORS__'>&#10003;</span>";
const NOK = "<span class='badge badge-danger ml-1' style='width: 5%' title='__RESPONSE_TIME____CONDITIONS____ERRORS__'>X</span>";
function generateServiceResultBox(serviceResult) {
let output = (serviceResult.success ? OK : NOK);
@@ -62,9 +63,9 @@
return output;
}
function refreshTable() {
function refreshResults() {
$.getJSON("/api/v1/results", function (data) {
let tableBody = "";
let output = "";
for (let serviceName in data) {
let serviceStatusOverTime = "";
let hostname = data[serviceName][data[serviceName].length-1].hostname
@@ -82,21 +83,30 @@
maxResponseTime = responseTime;
}
}
tableBody += ""
+ "<tr>"
+ " <td>" + serviceName + "</td>"
+ " <td>" + serviceStatusOverTime + "</td>"
+ " <td><a href=\"//" + hostname + "\">" + hostname + "</a></td>"
+ " <td>" + (minResponseTime === maxResponseTime ? minResponseTime : (minResponseTime + "-" + maxResponseTime)) + "ms</td>"
+ "</tr>";
output += ""
+ "<div class='container p-2 border-left border-right border-top border-black'>"
+ " <div class='row mb-2'>"
+ " <div class='col-8'>"
+ " <span class='font-weight-bold'>" + serviceName + "</span> <span class='text-secondary font-weight-lighter'>- " + hostname + "</span>"
+ " </div>"
+ " <div class='col-4 text-right'>"
+ " <span class='font-weight-lighter'>" + (minResponseTime === maxResponseTime ? minResponseTime : (minResponseTime + "-" + maxResponseTime)) + "ms</span>"
+ " </div>"
+ " </div>"
+ " <div class='row'>"
+ " <div class='col-12 d-flex flex-row-reverse'>"
+ " " + serviceStatusOverTime
+ " </div>"
+ " </div>"
+ "</div>";
}
$("#results").html(tableBody);
$("#results").html(output);
});
}
refreshTable();
refreshResults();
setInterval(function() {
refreshTable();
refreshResults();
}, 10000);
</script>
</body>

View File

@@ -20,28 +20,32 @@ func GetServiceResults() *map[string][]*core.Result {
func Monitor(cfg *config.Config) {
for _, service := range cfg.Services {
go func(service *core.Service) {
for {
log.Printf("[watchdog][Monitor] Monitoring serviceName=%s", service.Name)
result := service.EvaluateConditions()
metric.PublishMetricsForService(service, result)
rwLock.Lock()
serviceResults[service.Name] = append(serviceResults[service.Name], result)
if len(serviceResults[service.Name]) > 10 {
serviceResults[service.Name] = serviceResults[service.Name][1:]
}
rwLock.Unlock()
log.Printf(
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s",
service.Name,
len(result.Errors),
result.Duration.Round(time.Millisecond),
)
log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
time.Sleep(service.Interval)
}
}(service)
// To prevent multiple requests from running exactly at the same time
time.Sleep(100 * time.Millisecond)
go monitor(service)
// To prevent multiple requests from running at the same time
time.Sleep(500 * time.Millisecond)
}
}
func monitor(service *core.Service) {
for {
// By placing the lock here, we prevent multiple services from being monitored at the exact same time, which
// could cause performance issues and return inaccurate results
rwLock.Lock()
log.Printf("[watchdog][Monitor] Monitoring serviceName=%s", service.Name)
result := service.EvaluateConditions()
metric.PublishMetricsForService(service, result)
serviceResults[service.Name] = append(serviceResults[service.Name], result)
if len(serviceResults[service.Name]) > 20 {
serviceResults[service.Name] = serviceResults[service.Name][1:]
}
rwLock.Unlock()
log.Printf(
"[watchdog][Monitor] Finished monitoring serviceName=%s; errors=%d; requestDuration=%s",
service.Name,
len(result.Errors),
result.Duration.Round(time.Millisecond),
)
log.Printf("[watchdog][Monitor] Waiting interval=%s before monitoring serviceName=%s", service.Interval, service.Name)
time.Sleep(service.Interval)
}
}