Return Data from Check possible and redirect to data.json
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Kekskurse 2021-09-20 13:19:05 +02:00
parent 01fac32265
commit df1f08922b
Signed by: kekskurse
GPG Key ID: 728ACCB59341E7E4
6 changed files with 32 additions and 21 deletions

View File

@ -18,7 +18,7 @@ type Config struct {
Checks struct{ Checks struct{
HDD checks.HDDConfig `yaml:"hdd"` HDD checks.HDDConfig `yaml:"hdd"`
Load checks.LoadConfig `yaml:"load"` Load checks.LoadConfig `yaml:"load"`
Memory checks.MemmoryConfig `yaml:"memory"` Memory checks.MemoryConfig `yaml:"memory"`
} `yaml:"checks"` } `yaml:"checks"`
} }

View File

@ -13,7 +13,7 @@ type HDD struct {
Config HDDConfig Config HDDConfig
} }
func (h HDD) Execute() (ok bool, err error) { func (h HDD) Execute() (ok bool, data interface{}, err error) {
parts, _ := disk.Partitions(true) parts, _ := disk.Partitions(true)
log.Debug().Int("max_percent", h.Config.MaxPercent).Msg("Execute HDD Check") 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 device := p.Mountpoint
s, _ := disk.Usage(device) s, _ := disk.Usage(device)
if s.UsedPercent > float64(h.Config.MaxPercent) { 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 { func (h HDD) Name() string {

View File

@ -1,6 +1,6 @@
package checks package checks
type Check interface { type Check interface {
Execute() (ok bool, err error) Execute() (ok bool, data interface{}, err error)
Name() string Name() string
} }

View File

@ -14,21 +14,21 @@ type Load struct {
Config LoadConfig Config LoadConfig
} }
func (h Load) Execute() (ok bool, err error) { func (h Load) Execute() (ok bool, data interface{}, err error) {
load, err := loadavg.Get() load, err := loadavg.Get()
if load.Loadavg1 > h.Config.MaxLoad1 { if load.Loadavg1 > h.Config.MaxLoad1 {
return false, nil return false,load, nil
} }
if load.Loadavg5 > h.Config.MaxLoad5 { if load.Loadavg5 > h.Config.MaxLoad5 {
return false, nil return false, load, nil
} }
if load.Loadavg15 > h.Config.MaxLoad15 { 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 { func (h Load) Name() string {

View File

@ -4,22 +4,22 @@ import (
"github.com/mackerelio/go-osstat/memory" "github.com/mackerelio/go-osstat/memory"
) )
type MemmoryConfig struct { type MemoryConfig struct {
Max float64 `yaml:"max"` Max float64 `yaml:"max"`
} }
type Memory struct { 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() memory, err := memory.Get()
p := float64(100) / float64(memory.Total) * float64(memory.Used) p := float64(100) / float64(memory.Total) * float64(memory.Used)
if p > h.Config.Max { if p > h.Config.Max {
return false, nil return false, nil, nil
} }
return true, nil return true, nil,nil
} }
func (h Memory) Name() string { func (h Memory) Name() string {

23
main.go
View File

@ -18,7 +18,10 @@ func init() {
func main() { 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) err := http.ListenAndServe(c.HTTP.Listen, nil)
log.Fatal().Err(err).Msg("Shutdown") log.Fatal().Err(err).Msg("Shutdown")
} }
@ -28,7 +31,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
httpResposne := struct { httpResposne := struct {
Checks map[string]bool Checks map[string]ResultReturn
Config Config Config Config
}{Checks: res, Config: c} }{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 globaleResult := true
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
res := make(map[string]bool) res := make(map[string]ResultReturn)
for _, c := range checkList { for _, c := range checkList {
wg.Add(1) wg.Add(1)
go func(check checks.Check) { go func(check checks.Check) {
defer wg.Done() defer wg.Done()
s, _ := check.Execute() s, data, _ := check.Execute()
if s == false { if s == false {
globaleResult = false globaleResult = false
} }
res[check.Name()] = s res[check.Name()] = ResultReturn{
Success: s,
Data: data,
}
}(c) }(c)
} }
wg.Wait() wg.Wait()