http-server-status/main.go

65 lines
1.3 KiB
Go
Raw Normal View History

2021-09-20 09:28:00 +00:00
package main
import (
"encoding/json"
"github.com/rs/zerolog/log"
checks "http-server-status/internal/pkg/checks"
"net/http"
"sync"
)
var checkList []checks.Check
func init() {
2021-09-20 10:38:41 +00:00
readConfig()
log.Debug().Int("max_percent", c.Checks.HDD.MaxPercent).Msg("HDD CHECK")
checkList = append(checkList, checks.HDD{c.Checks.HDD}, checks.Memory{Config: c.Checks.Memory}, checks.Load{Config: c.Checks.Load})
2021-09-20 09:28:00 +00:00
}
func main() {
http.HandleFunc("/", handler)
2021-09-20 10:38:41 +00:00
err := http.ListenAndServe(c.HTTP.Listen, nil)
2021-09-20 09:28:00 +00:00
log.Fatal().Err(err).Msg("Shutdown")
}
func handler(w http.ResponseWriter, r *http.Request) {
res, gloableRes := checkSystem()
2021-09-20 10:38:41 +00:00
httpResposne := struct {
Checks map[string]bool
Config Config
}{Checks: res, Config: c}
resJson, err := json.Marshal(httpResposne)
2021-09-20 09:28:00 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Check Error")
}
if gloableRes {
w.Write(resJson)
return
}
http.Error(w, string(resJson), http.StatusInternalServerError)
}
func checkSystem() (map[string]bool, bool) {
globaleResult := true
wg := sync.WaitGroup{}
res := make(map[string]bool)
for _, c := range checkList {
wg.Add(1)
go func(check checks.Check) {
defer wg.Done()
s, _ := check.Execute()
if s == false {
globaleResult = false
}
res[check.Name()] = s
}(c)
}
wg.Wait()
return res, globaleResult
}