From bdf7355fa75bdda41b980c0bc8c0ef383d3fe3bc Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Mon, 10 May 2021 21:39:52 -0700 Subject: [PATCH 1/4] Add duration parsing for intervals --- README.md | 5 ++--- config.go | 3 ++- main.go | 3 +-- monitor.go | 8 ++++---- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 65be87d..107fbf5 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ The global configurations are: |key|value| |---|---| -|`check_interval`|Maximum frequency to run checks for each monitor| +|`check_interval`|Maximum frequency to run checks for each monitor (as duration, eg. 1m2s)| |`monitors`|List of all monitors. Detailed description below| |`alerts`|List of all alerts. Detailed description below| @@ -111,7 +111,7 @@ minitor -metrics -metrics-port 3000 ## Contributing -Whether you're looking to submit a patch or just tell me I broke something, you can contribute through the Github mirror and I can merge PRs back to the source repository. +Whether you're looking to submit a patch or tell me I broke something, you can contribute through the Github mirror and I can merge PRs back to the source repository. Primary Repo: https://git.iamthefij.com/iamthefij/minitor.git @@ -153,4 +153,3 @@ Future, potentially breaking changes - [ ] Async checking - [ ] Revisit metrics and see if they all make sense - [ ] Consider dropping `alert_up` and `alert_down` in favor of using Go templates that offer more control of messaging (Breaking) - - [ ] Use durations rather than seconds checked in event loop (Potentially breaking) diff --git a/config.go b/config.go index ef67de7..7d2d5b3 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "errors" "io/ioutil" + "time" "git.iamthefij.com/iamthefij/slog" "gopkg.in/yaml.v2" @@ -12,7 +13,7 @@ var errInvalidConfig = errors.New("Invalid configuration") // Config type is contains all provided user configuration type Config struct { - CheckInterval int64 `yaml:"check_interval"` + CheckInterval time.Duration `yaml:"check_interval"` Monitors []*Monitor Alerts map[string]*Alert } diff --git a/main.go b/main.go index 3df6d24..1eecd43 100644 --- a/main.go +++ b/main.go @@ -120,7 +120,6 @@ func main() { panic(err) } - sleepTime := time.Duration(config.CheckInterval) * time.Second - time.Sleep(sleepTime) + time.Sleep(config.CheckInterval) } } diff --git a/monitor.go b/monitor.go index 059d707..6aea6f3 100644 --- a/monitor.go +++ b/monitor.go @@ -11,9 +11,9 @@ import ( // Monitor represents a particular periodic check of a command type Monitor struct { //nolint:maligned // Config values - AlertAfter int16 `yaml:"alert_after"` - AlertEvery int16 `yaml:"alert_every"` - CheckInterval float64 `yaml:"check_interval"` + AlertAfter int16 `yaml:"alert_after"` + AlertEvery int16 `yaml:"alert_every"` + CheckInterval time.Duration `yaml:"check_interval"` Name string AlertDown []string `yaml:"alert_down"` AlertUp []string `yaml:"alert_up"` @@ -43,7 +43,7 @@ func (monitor Monitor) ShouldCheck() bool { return true } - sinceLastCheck := time.Since(monitor.lastCheck).Seconds() + sinceLastCheck := time.Since(monitor.lastCheck) return sinceLastCheck >= monitor.CheckInterval } From 04395fa69337dc4a9e9627f130619d3c56482583 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 11 May 2021 10:40:54 -0700 Subject: [PATCH 2/4] Add duration parsing tests --- config_test.go | 31 +++++++++++++++++++++++++++++-- monitor_test.go | 6 +++--- test/valid-config.yml | 10 ++++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/config_test.go b/config_test.go index 808a194..8c5fab9 100644 --- a/config_test.go +++ b/config_test.go @@ -3,6 +3,7 @@ package main import ( "log" "testing" + "time" ) func TestLoadConfig(t *testing.T) { @@ -35,11 +36,37 @@ func TestLoadConfig(t *testing.T) { // Set PyCompat to default value PyCompat = false - - log.Println("-----") } } +func TestIntervalParsing(t *testing.T) { + log.Printf("Testing case TestIntervalParsing") + + config, err := LoadConfig("./test/valid-config.yml") + if err != nil { + t.Errorf("Failed loading config: %v", err) + } + + oneSecond := time.Second + tenSeconds := 10 * time.Second + oneMinute := time.Minute + + // validate top level interval seconds represented as an int + if config.CheckInterval != oneSecond { + t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval) + } + + if config.Monitors[0].CheckInterval != tenSeconds { + t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) + } + + if config.Monitors[1].CheckInterval != oneMinute { + t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) + } + + log.Println("-----") +} + // TestMultiLineConfig is a more complicated test stepping through the parsing // and execution of mutli-line strings presented in YAML func TestMultiLineConfig(t *testing.T) { diff --git a/monitor_test.go b/monitor_test.go index cde021f..5988b91 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -45,9 +45,9 @@ func TestMonitorShouldCheck(t *testing.T) { name string }{ {Monitor{}, true, "Empty"}, - {Monitor{lastCheck: timeNow, CheckInterval: 15}, false, "Just checked"}, - {Monitor{lastCheck: timeTenSecAgo, CheckInterval: 15}, false, "-10s"}, - {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: 15}, true, "-20s"}, + {Monitor{lastCheck: timeNow, CheckInterval: time.Second * 15}, false, "Just checked"}, + {Monitor{lastCheck: timeTenSecAgo, CheckInterval: time.Second * 15}, false, "-10s"}, + {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: time.Second * 15}, true, "-20s"}, } for _, c := range cases { diff --git a/test/valid-config.yml b/test/valid-config.yml index e192977..3860e95 100644 --- a/test/valid-config.yml +++ b/test/valid-config.yml @@ -3,21 +3,23 @@ check_interval: 1 monitors: - name: Command - command: ['echo', '$PATH'] - alert_down: ['log_command', 'log_shell'] + command: ["echo", "$PATH"] + alert_down: ["log_command", "log_shell"] alert_every: 0 + check_interval: 10s - name: Shell command: > echo 'Some string with stuff'; echo 'another line'; echo $PATH; exit 1 - alert_down: ['log_command', 'log_shell'] + alert_down: ["log_command", "log_shell"] alert_after: 5 alert_every: 0 + check_interval: 1m alerts: log_command: - command: ['echo', 'regular', '"command!!!"', "{{.MonitorName}}"] + command: ["echo", "regular", '"command!!!"', "{{.MonitorName}}"] log_shell: command: echo "Failure on {{.MonitorName}} User is $USER" From 860c2cdf43abb52d308059ca103f0fee620afe07 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Wed, 12 May 2021 10:33:42 -0700 Subject: [PATCH 3/4] Add custom type to parse out seconds as int and durations as strings --- config.go | 30 +++++++++++++++++++++++++++++- config_test.go | 6 +++--- main.go | 2 +- monitor.go | 8 ++++---- monitor_test.go | 6 +++--- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index 7d2d5b3..a389b2b 100644 --- a/config.go +++ b/config.go @@ -13,7 +13,7 @@ var errInvalidConfig = errors.New("Invalid configuration") // Config type is contains all provided user configuration type Config struct { - CheckInterval time.Duration `yaml:"check_interval"` + CheckInterval SecondsOrDuration `yaml:"check_interval"` Monitors []*Monitor Alerts map[string]*Alert } @@ -52,6 +52,34 @@ func (cos *CommandOrShell) UnmarshalYAML(unmarshal func(interface{}) error) erro return nil } +// SecondsOrDuration wraps a duration value for parsing a duration or seconds from YAML +// NOTE: This should be removed in favor of only parsing durations once compatibility is broken +type SecondsOrDuration struct { + value time.Duration +} + +// Value returns a duration value +func (sod SecondsOrDuration) Value() time.Duration { + return sod.value +} + +// UnmarshalYAML allows unmarshalling a duration value or seconds if an int was provided +func (sod *SecondsOrDuration) UnmarshalYAML(unmarshal func(interface{}) error) error { + var seconds int64 + err := unmarshal(&seconds) + + if err == nil { + sod.value = time.Second * time.Duration(seconds) + + return nil + } + + // Error indicates that we don't have an int + err = unmarshal(&sod.value) + + return err +} + // IsValid checks config validity and returns true if valid func (config Config) IsValid() (isValid bool) { isValid = true diff --git a/config_test.go b/config_test.go index 8c5fab9..6945b40 100644 --- a/config_test.go +++ b/config_test.go @@ -52,15 +52,15 @@ func TestIntervalParsing(t *testing.T) { oneMinute := time.Minute // validate top level interval seconds represented as an int - if config.CheckInterval != oneSecond { + if config.CheckInterval.Value() != oneSecond { t.Errorf("Incorrectly parsed int seconds. expected=%v actual=%v", oneSecond, config.CheckInterval) } - if config.Monitors[0].CheckInterval != tenSeconds { + if config.Monitors[0].CheckInterval.Value() != tenSeconds { t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) } - if config.Monitors[1].CheckInterval != oneMinute { + if config.Monitors[1].CheckInterval.Value() != oneMinute { t.Errorf("Incorrectly parsed seconds duration. expected=%v actual=%v", oneSecond, config.CheckInterval) } diff --git a/main.go b/main.go index 1eecd43..9121480 100644 --- a/main.go +++ b/main.go @@ -120,6 +120,6 @@ func main() { panic(err) } - time.Sleep(config.CheckInterval) + time.Sleep(config.CheckInterval.Value()) } } diff --git a/monitor.go b/monitor.go index 6aea6f3..a289897 100644 --- a/monitor.go +++ b/monitor.go @@ -11,9 +11,9 @@ import ( // Monitor represents a particular periodic check of a command type Monitor struct { //nolint:maligned // Config values - AlertAfter int16 `yaml:"alert_after"` - AlertEvery int16 `yaml:"alert_every"` - CheckInterval time.Duration `yaml:"check_interval"` + AlertAfter int16 `yaml:"alert_after"` + AlertEvery int16 `yaml:"alert_every"` + CheckInterval SecondsOrDuration `yaml:"check_interval"` Name string AlertDown []string `yaml:"alert_down"` AlertUp []string `yaml:"alert_up"` @@ -45,7 +45,7 @@ func (monitor Monitor) ShouldCheck() bool { sinceLastCheck := time.Since(monitor.lastCheck) - return sinceLastCheck >= monitor.CheckInterval + return sinceLastCheck >= monitor.CheckInterval.Value() } // Check will run the command configured by the Monitor and return a status diff --git a/monitor_test.go b/monitor_test.go index 5988b91..7d8f7ed 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -45,9 +45,9 @@ func TestMonitorShouldCheck(t *testing.T) { name string }{ {Monitor{}, true, "Empty"}, - {Monitor{lastCheck: timeNow, CheckInterval: time.Second * 15}, false, "Just checked"}, - {Monitor{lastCheck: timeTenSecAgo, CheckInterval: time.Second * 15}, false, "-10s"}, - {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: time.Second * 15}, true, "-20s"}, + {Monitor{lastCheck: timeNow, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "Just checked"}, + {Monitor{lastCheck: timeTenSecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, false, "-10s"}, + {Monitor{lastCheck: timeTwentySecAgo, CheckInterval: SecondsOrDuration{time.Second * 15}}, true, "-20s"}, } for _, c := range cases { From 49e363581938b6c86549d13d002ea4e8d8ff0473 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Wed, 12 May 2021 16:37:59 -0700 Subject: [PATCH 4/4] Add backwards compatility explanation in Readme --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 107fbf5..4db92d7 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ The global configurations are: |key|value| |---|---| -|`check_interval`|Maximum frequency to run checks for each monitor (as duration, eg. 1m2s)| +|`check_interval`|Maximum frequency to run checks for each monitor as duration, eg. 1m2s.| |`monitors`|List of all monitors. Detailed description below| |`alerts`|List of all alerts. Detailed description below| @@ -143,6 +143,18 @@ alerts: command: 'echo {{.MonitorName}}' ``` +Interval durations have changed from being an integer number of seconds to a duration string supported by Go, for example: + +minitor-py: +```yaml +check_interval: 90 +``` + +minitor-go: +```yaml +check_interval: 1m30s +``` + For the time being, legacy configs for the Python version of Minitor should be compatible if you apply the `-py-compat` flag when running Minitor. Eventually, this flag will go away when later breaking changes are introduced. ## Future