diff --git a/config.go b/config.go index dd3f081..4406c85 100644 --- a/config.go +++ b/config.go @@ -18,7 +18,7 @@ type Config struct { Checks struct{ HDD checks.HDDConfig `yaml:"hdd"` Load checks.LoadConfig `yaml:"load"` - Memory checks.MemmoryConfig `yaml:"memory"` + Memory checks.MemoryConfig `yaml:"memory"` } `yaml:"checks"` } diff --git a/internal/pkg/checks/hdd.go b/internal/pkg/checks/hdd.go index e7169d9..5e7e920 100644 --- a/internal/pkg/checks/hdd.go +++ b/internal/pkg/checks/hdd.go @@ -13,7 +13,7 @@ type HDD struct { Config HDDConfig } -func (h HDD) Execute() (ok bool, err error) { +func (h HDD) Execute() (ok bool, data interface{}, err error) { parts, _ := disk.Partitions(true) log.Debug().Int("max_percent", h.Config.MaxPercent).Msg("Execute HDD Check") @@ -21,11 +21,11 @@ func (h HDD) Execute() (ok bool, err error) { device := p.Mountpoint s, _ := disk.Usage(device) if s.UsedPercent > float64(h.Config.MaxPercent) { - return false, nil + return false, nil,nil } } - return true, nil + return true, nil,nil } func (h HDD) Name() string { diff --git a/internal/pkg/checks/interfaces.go b/internal/pkg/checks/interfaces.go index f73c361..73cd74f 100644 --- a/internal/pkg/checks/interfaces.go +++ b/internal/pkg/checks/interfaces.go @@ -1,6 +1,6 @@ package checks type Check interface { - Execute() (ok bool, err error) + Execute() (ok bool, data interface{}, err error) Name() string } \ No newline at end of file diff --git a/internal/pkg/checks/load.go b/internal/pkg/checks/load.go index f85ff31..908ff22 100644 --- a/internal/pkg/checks/load.go +++ b/internal/pkg/checks/load.go @@ -14,21 +14,21 @@ type Load struct { Config LoadConfig } -func (h Load) Execute() (ok bool, err error) { +func (h Load) Execute() (ok bool, data interface{}, err error) { load, err := loadavg.Get() if load.Loadavg1 > h.Config.MaxLoad1 { - return false, nil + return false,load, nil } if load.Loadavg5 > h.Config.MaxLoad5 { - return false, nil + return false, load, nil } if load.Loadavg15 > h.Config.MaxLoad15 { - return false, nil + return false, load,nil } - return true, nil + return true,load, nil } func (h Load) Name() string { diff --git a/internal/pkg/checks/memory.go b/internal/pkg/checks/memory.go index a259f72..479bef0 100644 --- a/internal/pkg/checks/memory.go +++ b/internal/pkg/checks/memory.go @@ -4,22 +4,22 @@ import ( "github.com/mackerelio/go-osstat/memory" ) -type MemmoryConfig struct { +type MemoryConfig struct { Max float64 `yaml:"max"` } type Memory struct { - Config MemmoryConfig + Config MemoryConfig } -func (h Memory) Execute() (ok bool, err error) { +func (h Memory) Execute() (ok bool, data interface{}, err error) { memory, err := memory.Get() p := float64(100) / float64(memory.Total) * float64(memory.Used) if p > h.Config.Max { - return false, nil + return false, nil, nil } - return true, nil + return true, nil,nil } func (h Memory) Name() string { diff --git a/main.go b/main.go index faffd2e..15e40c6 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,10 @@ func init() { func main() { - http.HandleFunc("/", handler) + http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { + http.Redirect(writer, request, "/data.json", http.StatusSeeOther) + }) + http.HandleFunc("/data.json", handler) err := http.ListenAndServe(c.HTTP.Listen, nil) log.Fatal().Err(err).Msg("Shutdown") } @@ -28,7 +31,7 @@ func handler(w http.ResponseWriter, r *http.Request) { httpResposne := struct { - Checks map[string]bool + Checks map[string]ResultReturn Config Config }{Checks: res, Config: c} @@ -44,19 +47,27 @@ func handler(w http.ResponseWriter, r *http.Request) { } -func checkSystem() (map[string]bool, bool) { +type ResultReturn struct { + Success bool `json:"success"` + Data interface{} `json:"data"` +} + +func checkSystem() (map[string]ResultReturn, bool) { globaleResult := true wg := sync.WaitGroup{} - res := make(map[string]bool) + res := make(map[string]ResultReturn) for _, c := range checkList { wg.Add(1) go func(check checks.Check) { defer wg.Done() - s, _ := check.Execute() + s, data, _ := check.Execute() if s == false { globaleResult = false } - res[check.Name()] = s + res[check.Name()] = ResultReturn{ + Success: s, + Data: data, + } }(c) } wg.Wait()