This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
/
node-status.go
94 lines (79 loc) · 2.58 KB
/
node-status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Copyright © 2020 Alessandro Segala (@ItalyPaleAle)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package utils
import (
"encoding/json"
"time"
)
// SiteHealth contains the results of the health checks for each individual app
type SiteHealth struct {
Domain string `json:"domain"`
App *string `json:"app"`
StatusCode *int `json:"-"`
ResponseSize *int `json:"-"`
Error string `json:"error,omitempty"`
Time *time.Time `json:"time,omitempty"`
}
// IsHealthy returns true if the site is in a healthy state
func (h *SiteHealth) IsHealthy() bool {
// If there's an error, it's unhealthy by default
if h.Error != "" {
return false
}
// If there's no app, it's healthy by default
if h.App == nil {
return true
}
// Check response status code and size
if h.StatusCode != nil && *h.StatusCode >= 200 && *h.StatusCode < 300 &&
h.ResponseSize != nil && *h.ResponseSize > 0 {
return true
}
// Otherwise, false
return false
}
// MarshalJSON implements a custom JSON serializer for the SiteHealth object
func (h *SiteHealth) MarshalJSON() ([]byte, error) {
// Marshal the JSON object
type Alias SiteHealth
return json.Marshal(&struct {
*Alias
Healthy bool `json:"healthy"`
}{
Alias: (*Alias)(h),
Healthy: h.IsHealthy(),
})
}
// NodeSync contains information on the sync status
type NodeSync struct {
Running bool `json:"running"`
LastSync *time.Time `json:"lastSync"`
SyncError string `json:"syncError,omitempty"`
}
// NginxStatus contains information on the status of the Nginx server
type NginxStatus struct {
Running bool `json:"running"`
}
// NodeStore contains information on the status of the store
// Particularly useful if using etcd as store
type NodeStore struct {
Healthy bool `json:"healthy"`
}
// NodeStatus contains the current status of the node
type NodeStatus struct {
NodeName string `json:"name"`
Nginx NginxStatus `json:"nginx"`
Sync NodeSync `json:"sync"`
Store NodeStore `json:"store"`
Health []SiteHealth `json:"health"`
}