func Check() SystemStatus {
	status := SystemStatus{
		OK: true,
	}
	log().Debug("Running health check")
	for name, check := range checks {
		log().Trace("Polling subsystem", "check", name)
		result := check()
		status.Subsystems = append(status.Subsystems, result)
		status.OK = status.OK && result.OK
		if !result.OK && status.FirstFailure == (SubsystemStatus{}) {
			status.FirstFailure = result
		}
	}
	return status
}
Check runs all the health checks and returns the aggregate status
to the caller.
package health
import (
	"fmt"
	"github.com/hashicorp/go-hclog"
	pb "github.com/netauth/protocol/v2"
)
var (
	checks map[string]SubsystemCheck
	lb hclog.Logger
)
type SubsystemStatus struct {
	OK     bool
	Name   string
	Status string
}
func (s SubsystemStatus) String() string {
	statword := "FAIL"
	if s.OK {
		statword = "PASS"
	}
	return fmt.Sprintf("[%s]  %s\t%s",
		statword,
		s.Name,
		s.Status)
}
func (s SubsystemStatus) Proto() *pb.SubSystemStatus {
	sub := pb.SubSystemStatus{
		Name:         &s.Name,
		OK:           &s.OK,
		FaultMessage: &s.Status,
	}
	return &sub
}
type SubsystemCheck func() SubsystemStatus
type SystemStatus struct {
	OK           bool
	FirstFailure SubsystemStatus
	Subsystems   []SubsystemStatus
}
func (s SystemStatus) String() string {
	out := ""
	statword := "FAIL"
	if s.OK {
		statword = "PASS"
	}
	out += fmt.Sprintf("System Check Status: %s\n", statword)
	if s.FirstFailure != (SubsystemStatus{}) {
		out += fmt.Sprintf("\nFirst Failure:\n%s\n", s.FirstFailure)
	}
	if len(s.Subsystems) > 0 {
		out += "\nSubsystems:\n"
	}
	for _, st := range s.Subsystems {
		out += fmt.Sprintf("%s\n", st)
	}
	return out
}
func (s SystemStatus) Proto() *pb.ServerStatus {
	stat := pb.ServerStatus{}
	stat.SystemOK = func(b bool) *bool { return &b }(true)
	for i := range s.Subsystems {
		if !s.Subsystems[i].OK && stat.FirstFailure == nil {
			stat.FirstFailure = s.Subsystems[i].Proto()
			stat.SystemOK = func(b bool) *bool { return &b }(false)
		}
		stat.SubSystems = append(stat.SubSystems, s.Subsystems[i].Proto())
	}
	return &stat
}
func init() {
	checks = make(map[string]SubsystemCheck)
}
func RegisterCheck(name string, check SubsystemCheck) {
	if _, ok := checks[name]; ok {
		log().Warn("Refusing to overwrite existing check", "check", name)
		return
	}
	checks[name] = check
}