Don't update condition if status stays False/Unknown for custom plugin

This commit is contained in:
Zhen Wang
2019-08-01 23:40:16 -07:00
parent 599ca532e8
commit 2f5d03280a
@@ -156,14 +156,14 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
}) })
} }
} else { } else {
// For permanent error changes the condition // For permanent error that changes the condition
for i := range c.conditions { for i := range c.conditions {
condition := &c.conditions[i] condition := &c.conditions[i]
if condition.Type == result.Rule.Condition { if condition.Type == result.Rule.Condition {
status := toConditionStatus(result.ExitStatus) // The condition reason specified in the rule and the result message
// change 1: Condition status change from True to False/Unknown // represent the problem happened. We need to know the default condition
if condition.Status == types.True && status != types.True { // from the config, so that we can set the new condition reason/message
condition.Transition = timestamp // back when such problem goes away.
var defaultConditionReason string var defaultConditionReason string
var defaultConditionMessage string var defaultConditionMessage string
for j := range c.config.DefaultConditions { for j := range c.config.DefaultConditions {
@@ -175,58 +175,59 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
} }
} }
inactiveProblemEvents = append(inactiveProblemEvents, util.GenerateConditionChangeEvent( needToUpdateCondition := true
condition.Type, var newReason string
status, var newMessage string
defaultConditionReason, status := toConditionStatus(result.ExitStatus)
timestamp, if condition.Status == types.True && status != types.True {
)) // Scenario 1: Condition status changes from True to False/Unknown
newReason = defaultConditionReason
condition.Status = status if newMessage == "" {
condition.Message = defaultConditionMessage newMessage = defaultConditionMessage
condition.Reason = defaultConditionReason } else {
newMessage = result.Message
}
} else if condition.Status != types.True && status == types.True { } else if condition.Status != types.True && status == types.True {
// change 2: Condition status change from False/Unknown to True // Scenario 2: Condition status changes from False/Unknown to True
condition.Transition = timestamp newReason = result.Rule.Reason
condition.Message = result.Message newMessage = result.Message
activeProblemEvents = append(activeProblemEvents, util.GenerateConditionChangeEvent(
condition.Type,
status,
result.Rule.Reason,
timestamp,
))
condition.Status = status
condition.Reason = result.Rule.Reason
} else if condition.Status != status { } else if condition.Status != status {
// change 3: Condition status change from False to Unknown or vice versa // Scenario 3: Condition status changes from False to Unknown or vice versa
condition.Transition = timestamp newReason = defaultConditionReason
condition.Message = result.Message if newMessage == "" {
inactiveProblemEvents = append(inactiveProblemEvents, util.GenerateConditionChangeEvent( newMessage = defaultConditionMessage
condition.Type, } else {
status, newMessage = result.Message
result.Rule.Reason, }
timestamp, } else if condition.Status == types.True && status == types.True &&
))
condition.Status = status
condition.Reason = result.Rule.Reason
} else if condition.Status == status &&
(condition.Reason != result.Rule.Reason || (condition.Reason != result.Rule.Reason ||
(*c.config.PluginGlobalConfig.EnableMessageChangeBasedConditionUpdate && condition.Message != result.Message)) { (*c.config.PluginGlobalConfig.EnableMessageChangeBasedConditionUpdate && condition.Message != result.Message)) {
// change 4: Condition status do not change. // Scenario 4: Condition status does not change and it stays true.
// condition reason changes or // condition reason changes or
// condition message changes when message based condition update is enabled. // condition message changes when message based condition update is enabled.
newReason = result.Rule.Reason
newMessage = result.Message
} else {
// Scenario 5: Condition status does not change and it stays False/Unknown.
// This should just be the default reason or message (as a consequence
// of scenario 1 and scenario 3 above).
needToUpdateCondition = false
}
if needToUpdateCondition {
condition.Transition = timestamp condition.Transition = timestamp
condition.Reason = result.Rule.Reason condition.Status = status
condition.Message = result.Message condition.Reason = newReason
condition.Message = newMessage
updateEvent := util.GenerateConditionChangeEvent( updateEvent := util.GenerateConditionChangeEvent(
condition.Type, condition.Type,
status, status,
condition.Reason, newReason,
timestamp, timestamp,
) )
if condition.Status == types.True {
if status == types.True {
activeProblemEvents = append(activeProblemEvents, updateEvent) activeProblemEvents = append(activeProblemEvents, updateEvent)
} else { } else {
inactiveProblemEvents = append(inactiveProblemEvents, updateEvent) inactiveProblemEvents = append(inactiveProblemEvents, updateEvent)