Update to fix issues with AlertAfter. Disallow 0

This commit is contained in:
IamTheFij 2019-10-02 20:52:57 -07:00
parent 67d738a33c
commit 1280fbb7bd

View File

@ -31,7 +31,7 @@ type Monitor struct {
func (monitor Monitor) IsValid() bool { func (monitor Monitor) IsValid() bool {
atLeastOneCommand := (monitor.CommandShell != "" || monitor.Command != nil) atLeastOneCommand := (monitor.CommandShell != "" || monitor.Command != nil)
atMostOneCommand := (monitor.CommandShell == "" || monitor.Command == nil) atMostOneCommand := (monitor.CommandShell == "" || monitor.Command == nil)
return atLeastOneCommand && atMostOneCommand && monitor.AlertAfter >= 0 return atLeastOneCommand && atMostOneCommand && monitor.getAlertAfter() > 0
} }
// ShouldCheck returns a boolean indicating if the Monitor is ready to be // ShouldCheck returns a boolean indicating if the Monitor is ready to be
@ -105,29 +105,25 @@ func (monitor *Monitor) failure() (notice *AlertNotice) {
log.Printf("Devastating failure. :(") log.Printf("Devastating failure. :(")
monitor.failureCount++ monitor.failureCount++
// If we haven't hit the minimum failures, we can exit // If we haven't hit the minimum failures, we can exit
if monitor.failureCount < monitor.AlertAfter { if monitor.failureCount < monitor.getAlertAfter() {
// TODO: Turn into a debug
log.Printf( log.Printf(
"Have not hit minimum failures. failures: %v alert after: %v", "Have not hit minimum failures. failures: %v alert after: %v",
monitor.failureCount, monitor.failureCount,
monitor.AlertAfter, monitor.getAlertAfter(),
) )
return return
} }
failureCount := (monitor.failureCount - monitor.AlertAfter) failureCount := (monitor.failureCount - monitor.getAlertAfter())
log.Printf("Total fail %v, this fail %v", monitor.failureCount, failureCount)
if monitor.AlertEvery > 0 { if monitor.AlertEvery > 0 {
// Handle integer number of failures before alerting // Handle integer number of failures before alerting
modVal := failureCount % monitor.AlertEvery
log.Printf("Alert every > 0: Mod val: %v", modVal)
if failureCount%monitor.AlertEvery == 0 { if failureCount%monitor.AlertEvery == 0 {
notice = monitor.createAlertNotice(false) notice = monitor.createAlertNotice(false)
} }
} else if monitor.AlertEvery == 0 { } else if monitor.AlertEvery == 0 {
// Handle alerting on first failure only // Handle alerting on first failure only
if failureCount == 1 { if failureCount == 0 {
notice = monitor.createAlertNotice(false) notice = monitor.createAlertNotice(false)
} }
} else { } else {
@ -144,6 +140,16 @@ func (monitor *Monitor) failure() (notice *AlertNotice) {
return return
} }
func (monitor Monitor) getAlertAfter() int16 {
// TODO: Come up with a better way than this method
// Zero is one!
if monitor.AlertAfter == 0 {
return 1
} else {
return monitor.AlertAfter
}
}
func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice { func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice {
// TODO: Maybe add something about recovery status here // TODO: Maybe add something about recovery status here
return &AlertNotice{ return &AlertNotice{