summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-10-27 23:36:49 +0200
committerPaul Buetow <paul@buetow.org>2025-10-27 23:36:49 +0200
commit81d1550df55318beff8e9f762952a33daaa7c0cf (patch)
tree897e3c044c8e3bf5c9d71d98345fde9a645e8c7a /internal
parent6352e8c33c1c22af382093d406d477d1530950db (diff)
feat: Add randomSpread and RunInterval to checks
This commit introduces two new optional parameters to the check configuration: - `randomSpread`: This parameter allows specifying a random sleep time up to N seconds before a check is executed. This is useful to avoid all checks running at the same time. - `RunInterval`: This parameter defines the minimum interval in seconds between two executions of a check. This is useful if gogios is run more frequently than a specific check should be. The `README.md` has been updated to document these new features. fix: Fix deadlock when skipping checks This commit also fixes a deadlock that occurred when a check was skipped due to the `RunInterval` setting. The `inputWg.Done()` was not being called, causing the main goroutine to wait forever. build: Replace Taskfile with Magefile The `Taskfile.yml` has been replaced with a `Magefile.go` to manage the build process. This provides more flexibility and is more idiomatic for Go projects.
Diffstat (limited to 'internal')
-rw-r--r--internal/check.go4
-rw-r--r--internal/runchecks.go34
-rw-r--r--internal/state.go20
-rw-r--r--internal/state_test.go24
4 files changed, 71 insertions, 11 deletions
diff --git a/internal/check.go b/internal/check.go
index 70f0044..3f2e4cc 100644
--- a/internal/check.go
+++ b/internal/check.go
@@ -14,6 +14,8 @@ type check struct {
DependsOn []string `json:"DependsOn,omitempty"`
Retries int `json:"Retries,omitempty"`
RetryInterval int `json:"RetryInterval,omitempty"`
+ RunInterval int `json:"RunInterval,omitempty"`
+ RandomSpread int `json:"RandomSpread,omitempty"`
}
type namedCheck struct {
@@ -65,4 +67,4 @@ func (c namedCheck) run(ctx context.Context) checkResult {
func (c namedCheck) skip(output string) checkResult {
return c.check.skip(c.name, output)
-}
+} \ No newline at end of file
diff --git a/internal/runchecks.go b/internal/runchecks.go
index 788e77d..fb7a9c4 100644
--- a/internal/runchecks.go
+++ b/internal/runchecks.go
@@ -3,6 +3,7 @@ package internal
import (
"context"
"log"
+ "math/rand"
"sync"
"time"
)
@@ -36,6 +37,25 @@ func runChecks(ctx context.Context, state state, conf config) state {
inputWg.Add(len(conf.Checks))
for check := range inputCh {
+ if age := state.age(check.name); check.RunInterval > int(age.Seconds()) {
+ lastCheckState, ok := state.checks[check.name]
+ if ok {
+ log.Printf("Skipping %s: interval not yet reached (%v (%v) <= %v)", check.name,
+ int(age.Seconds()), age, check.RunInterval)
+ outputCh <- checkResult{
+ name: check.name,
+ output: lastCheckState.output,
+ epoch: lastCheckState.Epoch,
+ status: lastCheckState.Status,
+ federated: lastCheckState.federated,
+ }
+ inputWg.Done()
+ continue
+ }
+ log.Println("Something went wrong... expected check state for", check,
+ "bug got nothing! Proceeding anyway")
+ }
+
go func(check namedCheck) {
outputCh <- runCheck(ctx, limitCh, deps, check, conf, check.Retries)
inputWg.Done()
@@ -52,14 +72,20 @@ func runChecks(ctx context.Context, state state, conf config) state {
return state
}
-func runCheck(ctx context.Context, limitCh chan struct{},
- deps dependency, check namedCheck, conf config, retries int) checkResult {
-
+func runCheck(ctx context.Context, limitCh chan struct{}, deps dependency,
+ check namedCheck, conf config, retries int,
+) checkResult {
if err := deps.wait(ctx, check.DependsOn); err != nil {
deps.notOk(check.name)
return check.skip(err.Error())
}
+ if check.RandomSpread > 0 {
+ d := time.Duration(rand.Intn(check.RandomSpread)) * time.Second
+ log.Printf("Sleeping %v before running %s", d, check.name)
+ time.Sleep(d)
+ }
+
limitCh <- struct{}{}
checkCtx, cancel := context.WithTimeout(ctx,
@@ -84,4 +110,4 @@ func runCheck(ctx context.Context, limitCh chan struct{},
<-limitCh
return checkResult
-}
+} \ No newline at end of file
diff --git a/internal/state.go b/internal/state.go
index 8de7f15..dceb108 100644
--- a/internal/state.go
+++ b/internal/state.go
@@ -83,6 +83,14 @@ func (s state) update(result checkResult) {
log.Println(result.name, cs)
}
+func (s state) age(name string) time.Duration {
+ if prevState, ok := s.checks[name]; ok {
+ return time.Since(time.Unix(prevState.Epoch, 0))
+ }
+
+ return time.Duration(0)
+}
+
// To be used to merge the state of another server running Gogios
func (s state) merge(other state) error {
for name, cs := range other.checks {
@@ -105,7 +113,7 @@ func (s state) mergeFromBytes(bytes []byte) error {
func (s state) persist() error {
stateDir := filepath.Dir(s.stateFile)
if _, err := os.Stat(stateDir); os.IsNotExist(err) {
- if err := os.MkdirAll(stateDir, 0755); err != nil {
+ if err := os.MkdirAll(stateDir, 0o755); err != nil {
return err
}
}
@@ -180,8 +188,8 @@ func (s state) reportChanged(sb *strings.Builder) (changed bool) {
}
func (s state) reportUnhandled(sb *strings.Builder) (numCriticals, numWarnings,
- numUnknown, numOK int) {
-
+ numUnknown, numOK int,
+) {
numCriticals = s.reportBy(sb, false, false, func(cs checkState) bool {
return cs.Status == nagiosCritical
})
@@ -208,8 +216,8 @@ func (s state) reportStaleAlerts(sb *strings.Builder) int {
}
func (s state) reportBy(sb *strings.Builder, showStatusChange, isStaleReport bool,
- filter func(cs checkState) bool) (count int) {
-
+ filter func(cs checkState) bool,
+) (count int) {
for name, cs := range s.checks {
if !filter(cs) {
continue
@@ -254,4 +262,4 @@ func (s state) countBy(filter func(cs checkState) bool) (count int) {
}
}
return
-}
+} \ No newline at end of file
diff --git a/internal/state_test.go b/internal/state_test.go
new file mode 100644
index 0000000..aacc023
--- /dev/null
+++ b/internal/state_test.go
@@ -0,0 +1,24 @@
+package internal
+
+import (
+ "testing"
+ "time"
+)
+
+func TestAge(t *testing.T) {
+ state := state{checks: make(map[string]checkState)}
+
+ state.checks["Check Foo"] = checkState{Epoch: 0}
+ minAge := time.Duration(time.Now().Unix())
+
+ if reportedAge := state.age("Check Foo"); reportedAge < minAge {
+ t.Errorf("expected age >= %v, got %v", minAge, reportedAge)
+ }
+
+ maxAge := time.Duration(time.Now().Unix())
+ state.checks["Check Bar"] = checkState{Epoch: time.Now().Unix()}
+
+ if reportedAge := state.age("Check Bar"); reportedAge >= minAge {
+ t.Errorf("expected age < %v, got %v", maxAge, reportedAge)
+ }
+}