summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-05-06 19:00:21 +0300
committerPaul Buetow <paul@buetow.org>2024-05-06 19:00:21 +0300
commite36a175fa09a983d3438212be8ca116adfa2b4ec (patch)
tree97b3650e2e5673f083bb15c59c6d30c931cf1df9
parentb289b98e9bd7d2fcd31452e1c1083bc5d3066e42 (diff)
initiala health.go
-rw-r--r--health.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/health.go b/health.go
new file mode 100644
index 0000000..39685b1
--- /dev/null
+++ b/health.go
@@ -0,0 +1,70 @@
+package main
+
+import "strings"
+
+type alertSeverity int
+
+const (
+ ok alertSeverity = iota
+ warning
+ critical
+ unknown
+)
+
+func (s alertSeverity) String() string {
+ switch s {
+ case ok:
+ return "OK"
+ case warning:
+ return "WARNING"
+ case critical:
+ return "CRITICAL"
+ case unknown:
+ return "UNKNOWN"
+ default:
+ panic("encountered an unknown alertSeverity")
+ }
+}
+
+type alert struct {
+ text string
+ severity alertSeverity
+}
+
+type healthStatus struct {
+ alerts map[string]alert
+}
+
+func newHealthStatus() healthStatus {
+ return healthStatus{
+ alerts: make(map[string]alert),
+ }
+}
+
+func (hs healthStatus) String() string {
+ var (
+ alertsBySeverity [4][]string
+ sb strings.Builder
+ )
+
+ for _, alert := range hs.alerts {
+ alertsBySeverity[alert.severity] = append(alertsBySeverity[alert.severity], alert.text)
+ }
+
+ possible := [4]alertSeverity{ok, warning, critical, unknown}
+ for _, severity := range possible {
+ if len(alertsBySeverity[severity]) == 0 {
+ continue
+ }
+ for _, alert := range alertsBySeverity[severity] {
+ sb.WriteString(alert)
+ sb.WriteString("\n")
+ }
+ }
+
+ result := sb.String()
+ if result == "" {
+ return "OK: all is fine"
+ }
+ return result
+}