From 8d6b4d6db6b7784c984e79aebfe81b854e2e331c Mon Sep 17 00:00:00 2001 From: kekskurse Date: Thu, 21 Oct 2021 12:15:55 +0200 Subject: [PATCH] Add Systemd Check --- config.go | 1 + config.yml | 5 +++ internal/pkg/checks/systemd.go | 59 ++++++++++++++++++++++++++++++++++ main.go | 8 +++-- template/index.html | 29 +++++++++++++++++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 internal/pkg/checks/systemd.go diff --git a/config.go b/config.go index a986378..26b542a 100644 --- a/config.go +++ b/config.go @@ -18,6 +18,7 @@ type Config struct { HDD checks.HDDConfig `yaml:"hdd"` Load checks.LoadConfig `yaml:"load"` Memory checks.MemoryConfig `yaml:"memory"` + Systemd checks.SystemdConf `yaml:"systemd"` } `yaml:"checks"` } diff --git a/config.yml b/config.yml index 7fc2e04..cd64d3a 100644 --- a/config.yml +++ b/config.yml @@ -11,3 +11,8 @@ checks: memory: max: 100 max_swap: 80 + systemd: + services: + - sshd + - test + - docker diff --git a/internal/pkg/checks/systemd.go b/internal/pkg/checks/systemd.go new file mode 100644 index 0000000..4b53954 --- /dev/null +++ b/internal/pkg/checks/systemd.go @@ -0,0 +1,59 @@ +package checks + +import ( + "github.com/rs/zerolog/log" + "os/exec" + "strings" +) + +type SystemdConf struct { + Services []string `yaml:"services"` +} + +type Systemd struct { + Config SystemdConf +} + +func (h Systemd) Execute() (ok bool, data interface{}, err error) { + success := true + servicelist := make(map[string]bool) + + for _, service := range h.Config.Services { + res, err := h.getStatus(service) + if err != nil { + return false, nil, err + } + if res == false { + success = false + } + servicelist[service] = res + } + + return success, servicelist,nil +} + +func (h Systemd) Name() string { + return "Systemd Status" +} + +func (h Systemd) getStatus(name string) (bool, error) { + cmd := exec.Command("systemctl", "check", name) + out, err := cmd.CombinedOutput() + if err != nil { + log.Debug().Err(err).Msg("Error in systemd communication") + if exitErr, ok := err.(*exec.ExitError); ok { + if exitErr.ExitCode() == 3 { + return false, nil + } + } + return false, err + } + + + s := string(out) + s = strings.Trim(s, "\n") + if s == "active" { + return true, nil + } + return false, nil +} diff --git a/main.go b/main.go index 94e8c36..01898c1 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ var checkList []checks.Check func init() { 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}) + 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}) } func main() { @@ -69,7 +69,11 @@ func checkSystem() (map[string]ResultReturn, bool) { wg.Add(1) go func(check checks.Check) { defer wg.Done() - s, data, _ := check.Execute() + s, data, err := check.Execute() + if err != nil { + log.Error().Err(err).Msg("Cant execute check") + return + } log.Debug().Str("check", check.Name()).Bool("status", s).Interface("Data", data).Msg("Executed Check, got Results") if s == false { globaleResult = false diff --git a/template/index.html b/template/index.html index f069d98..74ca228 100644 --- a/template/index.html +++ b/template/index.html @@ -54,6 +54,16 @@ +
+
+
+ Systemd Check +
+
+

...

+
+
+