diagd: expose machine-readable healthiness

This commit is contained in:
Michael Stapelberg 2018-06-26 21:52:29 +02:00
parent 72661b13ec
commit c24eefbb51

View File

@ -2,6 +2,7 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"html" "html"
@ -46,6 +47,18 @@ func dump(w io.Writer, re *diag.EvalResult) {
fmt.Fprintf(w, "</ul></li>") fmt.Fprintf(w, "</ul></li>")
} }
func firstError(re *diag.EvalResult) string {
if re.Error {
return fmt.Sprintf("%s: %s", re.Name, re.Status)
}
for _, ch := range re.Children {
if msg := firstError(ch); msg != "" {
return msg
}
}
return ""
}
func logic() error { func logic() error {
const ( const (
uplink = "uplink0" /* enp0s31f6 */ uplink = "uplink0" /* enp0s31f6 */
@ -71,6 +84,22 @@ func logic() error {
fmt.Fprintf(w, `<!DOCTYPE html><style type="text/css">ul { list-style-type: none; }</style><ul>`) fmt.Fprintf(w, `<!DOCTYPE html><style type="text/css">ul { list-style-type: none; }</style><ul>`)
dump(w, re) dump(w, re)
}) })
http.HandleFunc("/health.json", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
re := m.Evaluate()
mu.Unlock()
reply := struct {
FirstError string `json:"first_error"`
}{
FirstError: firstError(re),
}
b, err := json.Marshal(&reply)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
})
updateListeners() updateListeners()
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1) signal.Notify(ch, syscall.SIGUSR1)