summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-06-07 09:31:35 +0300
committerPaul Buetow <paul@buetow.org>2023-06-07 09:31:35 +0300
commitfaf8b0f8715a280c7f7f98595042fd4897e8da0b (patch)
treeb4fbe2d477b7f0f1b95bfe3afbcb852a007f9e49
parent9ecc857cf39c8d110da9a0c99830d2a0d15efed8 (diff)
restyle
-rwxr-xr-xgogiosbin6001368 -> 6006374 bytes
-rw-r--r--internal/check.go4
-rw-r--r--internal/config.go30
-rw-r--r--internal/dependency.go8
-rw-r--r--internal/nagioscode.go14
-rw-r--r--internal/notify.go25
-rw-r--r--internal/run.go16
-rw-r--r--internal/runchecks.go28
-rw-r--r--internal/state.go35
9 files changed, 87 insertions, 73 deletions
diff --git a/gogios b/gogios
index 1931341..332c127 100755
--- a/gogios
+++ b/gogios
Binary files differ
diff --git a/internal/check.go b/internal/check.go
index 47dc6f9..d762b66 100644
--- a/internal/check.go
+++ b/internal/check.go
@@ -35,7 +35,7 @@ func (c check) run(ctx context.Context, name string) checkResult {
if err := cmd.Run(); err != nil {
if ctx.Err() == context.DeadlineExceeded {
- return checkResult{name, "Check command timed out", critical}
+ return checkResult{name, "Check command timed out", nagiosCritical}
}
}
@@ -47,7 +47,7 @@ func (c check) run(ctx context.Context, name string) checkResult {
}
func (c check) skip(name, output string) checkResult {
- return checkResult{name, output, unknown}
+ return checkResult{name, output, nagiosUnknown}
}
func (c namedCheck) run(ctx context.Context) checkResult {
diff --git a/internal/config.go b/internal/config.go
index eb8fa81..a57ea9e 100644
--- a/internal/config.go
+++ b/internal/config.go
@@ -19,45 +19,45 @@ type config struct {
}
func newConfig(configFile string) (config, error) {
- var config config
+ var conf config
file, err := os.Open(configFile)
if err != nil {
- return config, err
+ return conf, err
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
- return config, err
+ return conf, err
}
- err = json.Unmarshal(bytes, &config)
+ err = json.Unmarshal(bytes, &conf)
if err != nil {
- return config, err
+ return conf, err
}
- if config.SMTPServer == "" {
+ if conf.SMTPServer == "" {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
- config.SMTPServer = fmt.Sprintf("%s:25", hostname)
- log.Println("Set SMTPServer to " + config.SMTPServer)
+ conf.SMTPServer = fmt.Sprintf("%s:25", hostname)
+ log.Println("Set SMTPServer to " + conf.SMTPServer)
}
- if config.StateDir == "" {
- config.StateDir = "."
- log.Println("Set StateDir to " + config.StateDir)
+ if conf.StateDir == "" {
+ conf.StateDir = "."
+ log.Println("Set StateDir to " + conf.StateDir)
}
- return config, nil
+ return conf, nil
}
-func (c config) sanityCheck() error {
- for name, check := range c.Checks {
+func (conf config) sanityCheck() error {
+ for name, check := range conf.Checks {
for _, depName := range check.DependsOn {
- if _, ok := c.Checks[depName]; !ok {
+ if _, ok := conf.Checks[depName]; !ok {
return fmt.Errorf("Check '%s' depends on non existant check '%s'", name, depName)
}
}
diff --git a/internal/dependency.go b/internal/dependency.go
index 020b4f5..4e7b56a 100644
--- a/internal/dependency.go
+++ b/internal/dependency.go
@@ -10,13 +10,13 @@ type dependency struct {
nokMap map[string]chan struct{}
}
-func newDependency(config config) dependency {
+func newDependency(conf config) dependency {
d := dependency{
- okMap: make(map[string]chan struct{}, len(config.Checks)),
- nokMap: make(map[string]chan struct{}, len(config.Checks)),
+ okMap: make(map[string]chan struct{}, len(conf.Checks)),
+ nokMap: make(map[string]chan struct{}, len(conf.Checks)),
}
- for name := range config.Checks {
+ for name := range conf.Checks {
d.okMap[name] = make(chan struct{})
d.nokMap[name] = make(chan struct{})
}
diff --git a/internal/nagioscode.go b/internal/nagioscode.go
index 65842be..96c11e3 100644
--- a/internal/nagioscode.go
+++ b/internal/nagioscode.go
@@ -3,19 +3,19 @@ package internal
type nagiosCode int
const (
- ok nagiosCode = 0
- warning nagiosCode = 1
- critical nagiosCode = 2
- unknown nagiosCode = 3
+ nagiosOk nagiosCode = 0
+ nagiosWarning nagiosCode = 1
+ nagiosCritical nagiosCode = 2
+ nagiosUnknown nagiosCode = 3
)
func (n nagiosCode) Str() string {
switch n {
- case ok:
+ case nagiosOk:
return "OK"
- case warning:
+ case nagiosWarning:
return "WARNING"
- case critical:
+ case nagiosCritical:
return "CRITICAL"
default:
return "UNKNOWN"
diff --git a/internal/notify.go b/internal/notify.go
index 7d8fc5a..991743d 100644
--- a/internal/notify.go
+++ b/internal/notify.go
@@ -6,15 +6,16 @@ import (
"net/smtp"
)
-func notify(config config, subject, body string) error {
+func notify(conf config, subject, body string) error {
log.Println("notify", subject, body)
- headers := make(map[string]string)
- headers["From"] = config.EmailFrom
- headers["To"] = config.EmailTo
- headers["Subject"] = subject
- headers["MIME-Version"] = "1.0"
- headers["Content-Type"] = "text/plain; charset=\"utf-8\""
+ headers := map[string]string{
+ "From": conf.EmailFrom,
+ "To": conf.EmailTo,
+ "Subject": subject,
+ "MIME-Version": "1.0",
+ "Content-Type": "text/plain; charset=\"utf-8\"",
+ }
header := ""
for k, v := range headers {
@@ -22,12 +23,12 @@ func notify(config config, subject, body string) error {
}
message := header + "\r\n" + body
- log.Println("Using SMTP server", config.SMTPServer)
+ log.Println("Using SMTP server", conf.SMTPServer)
- return smtp.SendMail(config.SMTPServer, nil, config.EmailFrom,
- []string{config.EmailTo}, []byte(message))
+ return smtp.SendMail(conf.SMTPServer, nil, conf.EmailFrom,
+ []string{conf.EmailTo}, []byte(message))
}
-func notifyError(config config, err error) error {
- return notify(config, fmt.Sprintf("GOGIOS: An error occured: %v", err), err.Error())
+func notifyError(conf config, err error) error {
+ return notify(conf, fmt.Sprintf("GOGIOS: An error occured: %v", err), err.Error())
}
diff --git a/internal/run.go b/internal/run.go
index 9583a69..586ebbd 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -3,27 +3,27 @@ package internal
import "context"
func Run(ctx context.Context, configFile string, renotify bool) {
- config, err := newConfig(configFile)
+ conf, err := newConfig(configFile)
if err != nil {
panic(err)
}
- if err := config.sanityCheck(); err != nil {
- notifyError(config, err)
+ if err := conf.sanityCheck(); err != nil {
+ notifyError(conf, err)
}
- state, err := readState(config)
+ state, err := readState(conf)
if err != nil {
- notifyError(config, err)
+ notifyError(conf, err)
}
- state = runChecks(ctx, state, config)
+ state = runChecks(ctx, state, conf)
if err := state.persist(); err != nil {
- notifyError(config, err)
+ notifyError(conf, err)
}
if subject, body, doNotify := state.report(renotify); doNotify {
- notify(config, subject, body)
+ notify(conf, subject, body)
}
}
diff --git a/internal/runchecks.go b/internal/runchecks.go
index fdd747a..788e77d 100644
--- a/internal/runchecks.go
+++ b/internal/runchecks.go
@@ -7,14 +7,16 @@ import (
"time"
)
-func runChecks(ctx context.Context, state state, config config) state {
- limitCh := make(chan struct{}, config.CheckConcurrency)
- inputCh := make(chan namedCheck)
- outputCh := make(chan checkResult)
- deps := newDependency(config)
+func runChecks(ctx context.Context, state state, conf config) state {
+ var (
+ limitCh = make(chan struct{}, conf.CheckConcurrency)
+ inputCh = make(chan namedCheck)
+ outputCh = make(chan checkResult)
+ deps = newDependency(conf)
+ )
go func() {
- for name, check := range config.Checks {
+ for name, check := range conf.Checks {
inputCh <- namedCheck{check, name}
}
close(inputCh)
@@ -31,11 +33,11 @@ func runChecks(ctx context.Context, state state, config config) state {
}()
var inputWg sync.WaitGroup
- inputWg.Add(len(config.Checks))
+ inputWg.Add(len(conf.Checks))
for check := range inputCh {
go func(check namedCheck) {
- outputCh <- runCheck(ctx, limitCh, deps, check, config, check.Retries)
+ outputCh <- runCheck(ctx, limitCh, deps, check, conf, check.Retries)
inputWg.Done()
}(check)
}
@@ -51,7 +53,7 @@ func runChecks(ctx context.Context, state state, config config) state {
}
func runCheck(ctx context.Context, limitCh chan struct{},
- deps dependency, check namedCheck, config config, retries int) checkResult {
+ deps dependency, check namedCheck, conf config, retries int) checkResult {
if err := deps.wait(ctx, check.DependsOn); err != nil {
deps.notOk(check.name)
@@ -61,20 +63,20 @@ func runCheck(ctx context.Context, limitCh chan struct{},
limitCh <- struct{}{}
checkCtx, cancel := context.WithTimeout(ctx,
- time.Duration(config.CheckTimeoutS)*time.Second)
+ time.Duration(conf.CheckTimeoutS)*time.Second)
defer cancel()
checkResult := check.run(checkCtx)
- if checkResult.status != ok && retries > 0 {
+ if checkResult.status != nagiosOk && retries > 0 {
<-limitCh
retryDuration := time.Duration(check.RetryInterval) * time.Second
time.Sleep(retryDuration)
log.Printf("Retrying %s after %v", check.name, retryDuration)
- return runCheck(ctx, limitCh, deps, check, config, retries-1)
+ return runCheck(ctx, limitCh, deps, check, conf, retries-1)
}
- if checkResult.status == critical {
+ if checkResult.status == nagiosCritical {
deps.notOk(check.name)
} else {
deps.ok(check.name)
diff --git a/internal/state.go b/internal/state.go
index 3ac8e7d..3735a52 100644
--- a/internal/state.go
+++ b/internal/state.go
@@ -24,9 +24,9 @@ type state struct {
checks map[string]checkState
}
-func readState(config config) (state, error) {
+func readState(conf config) (state, error) {
s := state{
- stateFile: fmt.Sprintf("%s/state.json", config.StateDir),
+ stateFile: fmt.Sprintf("%s/state.json", conf.StateDir),
checks: make(map[string]checkState),
}
@@ -52,7 +52,7 @@ func readState(config config) (state, error) {
var obsolete []string
for name := range s.checks {
- if _, ok := config.Checks[name]; !ok {
+ if _, ok := conf.Checks[name]; !ok {
obsolete = append(obsolete, name)
}
}
@@ -66,7 +66,7 @@ func readState(config config) (state, error) {
}
func (s state) update(result checkResult) {
- prevStatus := unknown
+ prevStatus := nagiosUnknown
prevState, ok := s.checks[result.name]
if ok {
prevStatus = prevState.Status
@@ -113,25 +113,25 @@ func (s state) report(renotify bool) (string, string, bool) {
func (s state) reportChanged(sb *strings.Builder) (changed bool) {
if 0 < s.reportBy(sb, true, func(cs checkState) bool {
- return cs.Status == critical && cs.changed()
+ return cs.Status == nagiosCritical && cs.changed()
}) {
changed = true
}
if 0 < s.reportBy(sb, true, func(cs checkState) bool {
- return cs.Status == warning && cs.changed()
+ return cs.Status == nagiosWarning && cs.changed()
}) {
changed = true
}
if 0 < s.reportBy(sb, true, func(cs checkState) bool {
- return cs.Status == unknown && cs.changed()
+ return cs.Status == nagiosUnknown && cs.changed()
}) {
changed = true
}
if 0 < s.reportBy(sb, true, func(cs checkState) bool {
- return cs.Status == ok && cs.changed()
+ return cs.Status == nagiosOk && cs.changed()
}) {
changed = true
}
@@ -142,10 +142,21 @@ func (s state) reportChanged(sb *strings.Builder) (changed bool) {
func (s state) reportUnhandled(sb *strings.Builder) (numCriticals, numWarnings,
numUnknown, numOK int) {
- numCriticals = s.reportBy(sb, false, func(cs checkState) bool { return cs.Status == critical })
- numWarnings = s.reportBy(sb, false, func(cs checkState) bool { return cs.Status == warning })
- numUnknown = s.reportBy(sb, false, func(cs checkState) bool { return cs.Status == unknown })
- numOK = s.countBy(func(cs checkState) bool { return cs.Status == ok })
+ numCriticals = s.reportBy(sb, false, func(cs checkState) bool {
+ return cs.Status == nagiosCritical
+ })
+
+ numWarnings = s.reportBy(sb, false, func(cs checkState) bool {
+ return cs.Status == nagiosWarning
+ })
+
+ numUnknown = s.reportBy(sb, false, func(cs checkState) bool {
+ return cs.Status == nagiosUnknown
+ })
+
+ numOK = s.countBy(func(cs checkState) bool {
+ return cs.Status == nagiosOk
+ })
return
}