Add Systemd Check
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Kekskurse 2021-10-21 12:15:55 +02:00
parent 193d863ef4
commit 8d6b4d6db6
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
5 changed files with 100 additions and 2 deletions

View file

@ -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"`
}

View file

@ -11,3 +11,8 @@ checks:
memory:
max: 100
max_swap: 80
systemd:
services:
- sshd
- test
- docker

View file

@ -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
}

View file

@ -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

View file

@ -54,6 +54,16 @@
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header" id="systemdtitle">
Systemd Check
</div>
<div class="card-body">
<p class="card-text" id="systemdcontent">...</p>
</div>
</div>
</div>
</div>
</div>
<script>
@ -129,6 +139,25 @@
$("#loadcontent").append(html)
//systemd
if (o.Checks["Systemd Status"].success) {
$("#systemdtitle").addClass("bg-success");
$("#systemdtitle").removeClass("bg-error");
} else {
$("#systemdtitle").addClass("bg-danger");
$("#systemdtitle").removeClass("bg-success");
}
$("#systemdcontent").empty()
html = "<table class='table'>"
keys = Object.keys(o.Checks["Systemd Status"].data)
for (var i = 0; i < keys.length; i++) {
name = keys[i]
value = o.Checks["Systemd Status"].data[keys[i]]
html += "<tr><td>"+name+"</td><td>"+value+"</td></tr>"
}
html += "</table>"
$("#systemdcontent").append(html)
}
getData();