Add Config
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Kekskurse 2021-09-20 12:38:41 +02:00
parent c8bae3a5a8
commit 07fccbcc29
Signed by: kekskurse
GPG Key ID: 728ACCB59341E7E4
9 changed files with 101 additions and 12 deletions

42
config.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
"http-server-status/internal/pkg/checks"
"io/ioutil"
"os"
)
var c Config
type Config struct {
HTTP struct{
Listen string `yaml:"listen"`
} `yaml:"http"`
Checks struct{
HDD checks.HDDConfig `yaml:"hdd"`
Load checks.LoadConfig `yaml:"load"`
Memory checks.MemmoryConfig `yaml:"memory"`
} `yaml:"checks"`
}
func readConfig() {
fmt.Println(os.Args)
filename := "/etc/http-server-status/config.yml"
if len(os.Args) > 1 {
filename = os.Args[1]
}
log.Debug().Str("filename", filename).Msg("Load Config")
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal().Err(err).Msg("Cant get config file")
}
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
log.Fatal().Err(err).Msg("Cant parse yaml file")
}
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/mackerelio/go-osstat v0.2.0
github.com/rs/zerolog v1.25.0
github.com/shirou/gopsutil v3.21.8+incompatible
gopkg.in/yaml.v2 v2.4.0
)
require (

4
go.sum
View File

@ -39,3 +39,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

View File

@ -1,18 +1,26 @@
package checks
import "github.com/shirou/gopsutil/disk"
import (
"github.com/rs/zerolog/log"
"github.com/shirou/gopsutil/disk"
)
type HDDConfig struct {
MaxPercent int `yaml:"max_percent"`
}
type HDD struct {
Config HDDConfig
}
func (h HDD) Execute() (ok bool, err error) {
parts, _ := disk.Partitions(true)
log.Debug().Int("max_percent", h.Config.MaxPercent).Msg("Execute HDD Check")
for _, p := range parts {
device := p.Mountpoint
s, _ := disk.Usage(device)
if s.UsedPercent > 80 {
if s.UsedPercent > float64(h.Config.MaxPercent) {
return false, nil
}
}

View File

@ -4,21 +4,27 @@ import (
"github.com/mackerelio/go-osstat/loadavg"
)
type Load struct {
type LoadConfig struct {
MaxLoad1 float64 `yaml:"max_load_1"`
MaxLoad5 float64 `yaml:"max_load_5"`
MaxLoad15 float64 `yaml:"max_load_15"`
}
type Load struct {
Config LoadConfig
}
func (h Load) Execute() (ok bool, err error) {
load, err := loadavg.Get()
if load.Loadavg1 > 10 {
if load.Loadavg1 > h.Config.MaxLoad1 {
return false, nil
}
if load.Loadavg5 > 8 {
if load.Loadavg5 > h.Config.MaxLoad5 {
return false, nil
}
if load.Loadavg1 > 5 {
if load.Loadavg15 > h.Config.MaxLoad15 {
return false, nil
}

View File

@ -4,14 +4,18 @@ import (
"github.com/mackerelio/go-osstat/memory"
)
type Memory struct {
type MemmoryConfig struct {
Max float64 `yaml:"max"`
}
type Memory struct {
Config MemmoryConfig
}
func (h Memory) Execute() (ok bool, err error) {
memory, err := memory.Get()
p := float64(100) / float64(memory.Total) * float64(memory.Used)
if p > 80 {
if p > h.Config.Max {
return false, nil
}

15
main.go
View File

@ -11,19 +11,28 @@ import (
var checkList []checks.Check
func init() {
checkList = append(checkList, checks.HDD{}, checks.Memory{}, checks.Load{})
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})
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":3003", nil)
err := http.ListenAndServe(c.HTTP.Listen, nil)
log.Fatal().Err(err).Msg("Shutdown")
}
func handler(w http.ResponseWriter, r *http.Request) {
res, gloableRes := checkSystem()
resJson, err := json.Marshal(res)
httpResposne := struct {
Checks map[string]bool
Config Config
}{Checks: res, Config: c}
resJson, err := json.Marshal(httpResposne)
if err != nil {
log.Fatal().Err(err).Msg("Check Error")
}

12
test.yml Normal file
View File

@ -0,0 +1,12 @@
http:
listen: ":3003"
checks:
hdd:
max_percent: 100
load:
max_load_1: 10
max_load_5: 8
max_load_15: 5
memory:
max: 70

3
vendor/modules.txt vendored
View File

@ -24,3 +24,6 @@ github.com/shirou/gopsutil/internal/common
golang.org/x/sys/internal/unsafeheader
golang.org/x/sys/unix
golang.org/x/sys/windows
# gopkg.in/yaml.v2 v2.4.0
## explicit; go 1.15
gopkg.in/yaml.v2