diff options
| author | Paul Buetow <paul@buetow.org> | 2023-04-18 23:00:48 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-04-18 23:00:48 +0300 |
| commit | 3658638e3bb3edb0f266c65fe0e42a1cd53a5f83 (patch) | |
| tree | 53735108cc161bec79ebafaecc12803d636e1f3a | |
| parent | 7ea496151c0336414c9563613d7c1ce87e28f4ba (diff) | |
use nagiosCode and other refactorings
| -rw-r--r-- | check.go | 23 | ||||
| -rw-r--r-- | execute.go | 45 | ||||
| -rw-r--r-- | exitcodes.go | 21 | ||||
| -rw-r--r-- | main.go | 4 | ||||
| -rw-r--r-- | nagioscode.go | 23 | ||||
| -rw-r--r-- | notify.go | 1 | ||||
| -rw-r--r-- | state.go | 20 |
7 files changed, 68 insertions, 69 deletions
@@ -12,10 +12,15 @@ type check struct { Args []string } +type namedCheck struct { + check + name string +} + type checkResult struct { name string output string - status int + status nagiosCode } func (c check) execute(ctx context.Context, name string) checkResult { @@ -27,11 +32,7 @@ func (c check) execute(ctx context.Context, name string) checkResult { if err := cmd.Run(); err != nil { if ctx.Err() == context.DeadlineExceeded { - return checkResult{ - name: name, - output: "Check command timed out", - status: critical, - } + return checkResult{name, "Check command timed out", critical} } } @@ -39,9 +40,9 @@ func (c check) execute(ctx context.Context, name string) checkResult { parts := strings.Split(bytes.String(), "|") output := strings.TrimSpace(parts[0]) - return checkResult{ - name: name, - output: output, - status: cmd.ProcessState.ExitCode(), - } + return checkResult{name, output, nagiosCode(cmd.ProcessState.ExitCode())} +} + +func (c namedCheck) execute(ctx context.Context) checkResult { + return c.check.execute(ctx, c.name) } @@ -7,58 +7,53 @@ import ( "time" ) -type executionUnit struct { - name string - check check -} - -func execute(config config, state state) state { +func execute(state state, config config) state { limiterCh := make(chan struct{}, config.CheckConcurrency) - executionCh := make(chan executionUnit) - resultCh := make(chan checkResult) + inputCh := make(chan namedCheck) + outputCh := make(chan checkResult) go func() { for name, check := range config.Checks { - executionCh <- executionUnit{name, check} + inputCh <- namedCheck{check, name} } - close(executionCh) + close(inputCh) }() - var resultWg sync.WaitGroup - resultWg.Add(1) + var outputWg sync.WaitGroup + outputWg.Add(1) go func() { - for checkResult := range resultCh { + for checkResult := range outputCh { state.update(checkResult) } - resultWg.Done() + outputWg.Done() }() - var executionWg sync.WaitGroup - executionWg.Add(len(config.Checks)) + var inputWg sync.WaitGroup + inputWg.Add(len(config.Checks)) - for executionUnit := range executionCh { - go func(name string, check check) { + for check := range inputCh { + go func(check namedCheck) { limiterCh <- struct{}{} defer func() { <-limiterCh - executionWg.Done() + inputWg.Done() }() ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.CheckTimeoutS)*time.Second) defer cancel() - resultCh <- check.execute(ctx, name) - }(executionUnit.name, executionUnit.check) + outputCh <- check.execute(ctx) + }(check) } - executionWg.Wait() + inputWg.Wait() log.Println("All checks completed!") - close(resultCh) + close(outputCh) - resultWg.Wait() - log.Println("All results collected!") + outputWg.Wait() + log.Println("All outputs collected!") return state } diff --git a/exitcodes.go b/exitcodes.go deleted file mode 100644 index 18e3e19..0000000 --- a/exitcodes.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -const ( - ok = 0 - warning = 1 - critical = 2 - unknown = 3 -) - -func codeToString(state int) string { - switch state { - case 0: - return "OK" - case 1: - return "WARNING" - case 2: - return "CRITICAL" - default: - return "UNKNOWN" - } -} @@ -13,12 +13,12 @@ func main() { panic(err) } - state, err := newState(config) + state, err := readState(config) if err != nil { notifyError(config, err) } - state = execute(config, state) + state = execute(state, config) if err := state.persist(); err != nil { notifyError(config, err) diff --git a/nagioscode.go b/nagioscode.go new file mode 100644 index 0000000..8199aa1 --- /dev/null +++ b/nagioscode.go @@ -0,0 +1,23 @@ +package main + +type nagiosCode int + +const ( + ok nagiosCode = 0 + warning nagiosCode = 1 + critical nagiosCode = 2 + unknown nagiosCode = 3 +) + +func (n nagiosCode) Str() string { + switch n { + case 0: + return "OK" + case 1: + return "WARNING" + case 2: + return "CRITICAL" + default: + return "UNKNOWN" + } +} @@ -23,6 +23,7 @@ func notify(config config, subject, body string) error { message := header + "\r\n" + body log.Println("Using SMTP server", config.SMTPServer) + return smtp.SendMail(config.SMTPServer, nil, config.EmailFrom, []string{config.EmailTo}, []byte(message)) } @@ -10,8 +10,8 @@ import ( ) type checkState struct { - Status int - PrevStatus int + Status nagiosCode + PrevStatus nagiosCode output string } @@ -20,7 +20,7 @@ type state struct { checks map[string]checkState } -func newState(config config) (state, error) { +func readState(config config) (state, error) { s := state{ stateFile: fmt.Sprintf("%s/state.json", config.StateDir), checks: make(map[string]checkState), @@ -85,7 +85,7 @@ func (s state) report() (string, string, bool) { var sb strings.Builder var changed bool - f := func(filter func(i int) bool) int { + f := func(filter func(n nagiosCode) bool) int { var count int for name, checkState := range s.checks { if !filter(checkState.Status) { @@ -94,12 +94,12 @@ func (s state) report() (string, string, bool) { count++ if checkState.Status != checkState.PrevStatus { - sb.WriteString(codeToString(checkState.PrevStatus)) + sb.WriteString(nagiosCode(checkState.PrevStatus).Str()) sb.WriteString("->") changed = true } - sb.WriteString(codeToString(checkState.Status)) + sb.WriteString(nagiosCode(checkState.Status).Str()) sb.WriteString(": ") sb.WriteString(name) sb.WriteString(" ==>> ") @@ -112,22 +112,22 @@ func (s state) report() (string, string, bool) { sb.WriteString("This is the recent Gogios report!\n\n") - numCriticals := f(func(i int) bool { return i == 2 }) + numCriticals := f(func(n nagiosCode) bool { return n == 2 }) if numCriticals > 0 { sb.WriteString("\n") } - numWarnings := f(func(i int) bool { return i == 1 }) + numWarnings := f(func(n nagiosCode) bool { return n == 1 }) if numWarnings > 0 { sb.WriteString("\n") } - numUnknowns := f(func(i int) bool { return i > 2 }) + numUnknowns := f(func(n nagiosCode) bool { return n > 2 }) if numUnknowns > 0 { sb.WriteString("\n") } - numOks := f(func(i int) bool { return i == 0 }) + numOks := f(func(n nagiosCode) bool { return n == 0 }) if numOks > 0 { sb.WriteString("\n") } |
