http-server-status/main.go

132 lines
2.9 KiB
Go
Raw Normal View History

2021-09-20 09:28:00 +00:00
package main
import (
2021-09-20 13:45:01 +00:00
_ "embed"
2021-09-20 09:28:00 +00:00
"encoding/json"
2023-07-21 09:52:30 +00:00
"github.com/rs/zerolog"
2021-09-20 09:28:00 +00:00
"github.com/rs/zerolog/log"
checks "http-server-status/internal/pkg/checks"
"net/http"
2023-07-21 09:52:30 +00:00
"os"
2021-09-20 09:28:00 +00:00
"sync"
2021-11-25 01:01:27 +00:00
"text/template"
2021-09-20 09:28:00 +00:00
)
2021-09-20 13:45:01 +00:00
//go:embed template/index.html
var s string
2021-11-25 01:35:17 +00:00
var (
version = "development"
)
2021-09-20 09:28:00 +00:00
var checkList []checks.Check
func init() {
2021-09-20 10:38:41 +00:00
readConfig()
2023-07-21 09:52:30 +00:00
pp := log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
multi := zerolog.MultiLevelWriter(pp)
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
2021-11-29 06:31:30 +00:00
checkList = append(checkList, checks.HDD{c.Checks.HDD}, checks.Memory{Config: c.Checks.Memory}, checks.Load{Config: c.Checks.Load}, checks.Systemd{Config: c.Checks.Systemd}, checks.Version{Config: c.Checks.Version})
2021-09-20 09:28:00 +00:00
}
2021-11-29 04:53:12 +00:00
func auth(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if c.Auth.Enabled {
user, pass, _ := r.BasicAuth()
if !check(user, pass) {
w.Header().Set("WWW-Authenticate", `Basic realm="MY REALM"`)
http.Error(w, "Unauthorized.", 401)
return
}
}
fn(w, r)
}
}
func check(u, p string) bool {
if u == c.Auth.Username && p == c.Auth.Password {
return true
}
return false
}
2021-09-20 09:28:00 +00:00
func main() {
2021-11-29 04:53:12 +00:00
http.HandleFunc("/", auth(func(writer http.ResponseWriter, request *http.Request) {
2021-11-25 01:01:27 +00:00
res, gloableRes := checkSystem()
2021-09-20 21:52:07 +00:00
if gloableRes == false {
writer.WriteHeader(http.StatusInternalServerError)
2021-09-20 21:28:24 +00:00
}
2021-11-25 01:01:27 +00:00
t, err := template.New("todos").Parse(s)
if err != nil {
panic(err)
}
2021-11-25 01:35:17 +00:00
t.Execute(writer, map[string]interface{}{"checks":res, "version": version})
2021-11-25 01:01:27 +00:00
//writer.Write([]byte(s))
2021-11-29 04:53:12 +00:00
}))
http.HandleFunc("/data.json", auth(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]ResultReturn
2021-09-20 10:38:41 +00:00
Config Config
}{Checks: res, Config: c}
resJson, err := json.Marshal(httpResposne)
2021-09-20 09:28:00 +00:00
if err != nil {
2021-09-20 21:14:52 +00:00
log.Fatal().Err(err).Interface("httpResponse", ResultReturn{}).Msg("Check Error")
2021-09-20 09:28:00 +00:00
}
if gloableRes {
w.Write(resJson)
return
}
http.Error(w, string(resJson), http.StatusInternalServerError)
}
2021-09-20 13:45:01 +00:00
type ResultReturn struct {
Success bool `json:"success"`
Data interface{} `json:"data"`
}
func checkSystem() (map[string]ResultReturn, bool) {
2021-09-20 09:28:00 +00:00
globaleResult := true
wg := sync.WaitGroup{}
res := make(map[string]ResultReturn)
2021-11-29 05:02:41 +00:00
mutex := sync.Mutex{}
2021-09-20 09:28:00 +00:00
for _, c := range checkList {
wg.Add(1)
go func(check checks.Check) {
defer wg.Done()
2021-10-21 10:15:55 +00:00
s, data, err := check.Execute()
if err != nil {
log.Error().Err(err).Msg("Cant execute check")
return
}
2021-09-20 21:12:07 +00:00
log.Debug().Str("check", check.Name()).Bool("status", s).Interface("Data", data).Msg("Executed Check, got Results")
2021-09-20 09:28:00 +00:00
if s == false {
globaleResult = false
}
2021-11-29 05:02:41 +00:00
mutex.Lock()
res[check.Name()] = ResultReturn{
Success: s,
Data: data,
}
2021-11-29 05:02:41 +00:00
mutex.Unlock()
2021-09-20 09:28:00 +00:00
}(c)
}
wg.Wait()
return res, globaleResult
}