http-server-status/internal/pkg/checks/memory.go

36 lines
725 B
Go
Raw Normal View History

2021-09-20 09:28:00 +00:00
package checks
import (
"github.com/mackerelio/go-osstat/memory"
)
type MemoryConfig struct {
2021-09-20 10:38:41 +00:00
Max float64 `yaml:"max"`
2021-09-20 14:45:34 +00:00
MaxSwap float64 `yaml:"max_swap"`
2021-09-20 10:38:41 +00:00
}
2021-09-20 09:28:00 +00:00
2021-09-20 10:38:41 +00:00
type Memory struct {
Config MemoryConfig
2021-09-20 09:28:00 +00:00
}
func (h Memory) Execute() (ok bool, data interface{}, err error) {
2021-09-20 09:28:00 +00:00
memory, err := memory.Get()
p := float64(100) / float64(memory.Total) * float64(memory.Used)
2021-09-20 14:45:34 +00:00
ps := float64(100) / float64(memory.SwapTotal) * float64(memory.SwapUsed)
2021-09-20 14:27:05 +00:00
res := make(map[string]interface{})
res["row"] = memory
res["ram"] = p
2021-09-20 14:45:34 +00:00
res["swap"] = ps
2021-09-20 10:38:41 +00:00
if p > h.Config.Max {
2021-09-20 14:27:05 +00:00
return false, res, nil
2021-09-20 09:28:00 +00:00
}
2021-09-20 14:45:34 +00:00
if ps > h.Config.MaxSwap {
return false, res, nil
}
2021-09-20 09:28:00 +00:00
2021-09-20 14:27:05 +00:00
return true, res,nil
2021-09-20 09:28:00 +00:00
}
func (h Memory) Name() string {
return "Memory usage"
}