Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nixwiz/sensu-go-fatigue-check-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nixwiz committed Aug 30, 2019
2 parents 7f508b5 + 583bbac commit 77a61f6
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ The Fatigue Check Filter makes use of four annotations within a check's metadata
|Annotation|Default|Usage|
|----------|-------|-----|
|fatigue_check/occurrences|1|On which occurrence to allow the initial event to pass through|
|fatigue_check/interval|1800|In seconds, at what interval to allow subsequent events to pass through|
|fatigue_check/interval|1800|In seconds, at what interval to allow subsequent events to pass through, ideally a multiple of the check interval|
|fatigue_check/allow_resolution|true|Determines whether or not a resolution event is passed through|
|fatigue_check/suppress_flapping|true|Determines whether or not to suppress events for checks that are marked as flapping|

**Notes:**
* This filter makes use of the occurrences_watermark attribute that was buggy up until Sensu Go 5.9. Your mileage may vary on prior versions.
* The interval must be a multiple of the check's interval.
* If the interval is not a multiple of the check's interval, then the actual interval is computed by rounding up the result of dividing the interval by the check's interval. For example, an interval of 180s with a check interval of 25s would pass the event through on every 8 occurrences (200s).

#### Definition Examples
Asset:
Expand Down
6 changes: 4 additions & 2 deletions lib/fatigue_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function fatigue_check(event) {
try {
if (event.check.hasOwnProperty("annotations")) {
if (event.check.annotations.hasOwnProperty("fatigue_check/occurrences")) {
occurrences = parseInt(event_no_slashes.check.annotations.fatigue_check_occurrences, 10);
occurrences = parseInt(event_no_slashes.check.annotations.fatigue_check_occurrences, 10);
}
if (event.check.annotations.hasOwnProperty("fatigue_check/interval")) {
interval = parseInt(event_no_slashes.check.annotations.fatigue_check_interval, 10);
Expand Down Expand Up @@ -56,7 +56,9 @@ function fatigue_check(event) {
if (event.check.occurrences === occurrences) {
return true;
}
if (event.check.occurrences > occurrences && (event.check.occurrences % (interval / event.check.interval)) === 0) {
// The Math.ceil rounds up in the event that the interval requested is not
// multiple of the check interval
if (event.check.occurrences > occurrences && (event.check.occurrences % (Math.ceil(interval / event.check.interval))) === 0) {
return true;
}

Expand Down
171 changes: 166 additions & 5 deletions lib/fatigue_check_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function fatigue_check(event) {
try {
if (event.check.hasOwnProperty("annotations")) {
if (event.check.annotations.hasOwnProperty("fatigue_check/occurrences")) {
occurrences = parseInt(event_no_slashes.check.annotations.fatigue_check_occurrences, 10);
occurrences = parseInt(event_no_slashes.check.annotations.fatigue_check_occurrences, 10);
}
if (event.check.annotations.hasOwnProperty("fatigue_check/interval")) {
interval = parseInt(event_no_slashes.check.annotations.fatigue_check_interval, 10);
Expand Down Expand Up @@ -56,7 +56,9 @@ function fatigue_check(event) {
if (event.check.occurrences === occurrences) {
return true;
}
if (event.check.occurrences > occurrences && (event.check.occurrences % (interval / event.check.interval)) === 0) {
// The Math.ceil rounds up in the event that the interval requested is not
// multiple of the check interval
if (event.check.occurrences > occurrences && (event.check.occurrences % (Math.ceil(interval / event.check.interval))) === 0) {
return true;
}

Expand Down Expand Up @@ -357,7 +359,7 @@ function testFatigueIntervalTrue () {
],
"interval": 60,
"occurrences": 15, // 900 / 60
"occurrences_watermark": 3,
"occurrences_watermark": 15,
},
"is_incident": false,
"is_resolution": false,
Expand Down Expand Up @@ -409,7 +411,7 @@ function testFatigueIntervalFalse () {
],
"interval": 60,
"occurrences": 14, // not 900 / 60
"occurrences_watermark": 2,
"occurrences_watermark": 14,
},
"is_incident": false,
"is_resolution": false,
Expand All @@ -424,6 +426,110 @@ function testFatigueIntervalFalse () {
}
}

function testFatigueNonMultipleIntervalTrue () {
// This event is only a stub of a real event
var event = {
"check": {
"annotations": {
"fatigue_check/occurrences": "3"
"fatigue_check/allow_resolution": "true"
"fatigue_check/interval": "360"
},
"history": [
{
"executed": 1555523962,
"status": 0
},
{
"executed": 1555524022,
"status": 2
},
{
"executed": 1555524082,
"status": 0
},
{
"executed": 1555524142,
"status": 0
},
{
"executed": 1555524202,
"status": 2
},
{
"executed": 1555524262,
"status": 2
}
],
"interval": 25,
"occurrences": 15, // Math.ceil(360/25)
"occurrences_watermark": 15,
},
"is_incident": false,
"is_resolution": false,
}

var result = fatigue_check(event);

if (result) {
console.log("testFatigueNonMultipleIntervalTrue PASSED, returned true");
} else {
console.log("testFatigueNonMultipleIntervalTrue FAILED, returned false");
}
}

function testFatigueNonMultipleIntervalFalse () {
// This event is only a stub of a real event
var event = {
"check": {
"annotations": {
"fatigue_check/occurrences": "3"
"fatigue_check/allow_resolution": "true"
"fatigue_check/interval": "360"
},
"history": [
{
"executed": 1555523962,
"status": 0
},
{
"executed": 1555524022,
"status": 2
},
{
"executed": 1555524082,
"status": 0
},
{
"executed": 1555524142,
"status": 0
},
{
"executed": 1555524202,
"status": 2
},
{
"executed": 1555524262,
"status": 2
}
],
"interval": 25,
"occurrences": 14, // not Math.ceil(360/25)
"occurrences_watermark": 14,
},
"is_incident": false,
"is_resolution": false,
}

var result = fatigue_check(event);

if (result) {
console.log("testFatigueNonMultipleIntervalFalse FAILED, returned true");
} else {
console.log("testFatigueNonMultipleIntervalFalse PASSED, returned false");
}
}

function testFatigueFlappingTrue () {
// This event is only a stub of a real event
var event = {
Expand Down Expand Up @@ -581,6 +687,60 @@ function testFatigueNoAnnotations () {
}
}

function testFatigueNonMultipleIntervalLoop () {
// This event is only a stub of a real event
var event = {
"check": {
"annotations": {
"fatigue_check/occurrences": "3"
"fatigue_check/allow_resolution": "true"
"fatigue_check/interval": "360"
},
"history": [
{
"executed": 1555523962,
"status": 0
},
{
"executed": 1555524022,
"status": 2
},
{
"executed": 1555524082,
"status": 0
},
{
"executed": 1555524142,
"status": 0
},
{
"executed": 1555524202,
"status": 2
},
{
"executed": 1555524262,
"status": 2
}
],
"interval": 25,
"occurrences": 1
"occurrences_watermark": 1,
},
"is_incident": false,
"is_resolution": false,
}

console.log("Testing 1 .. 40 occurrences for non-multiple intervals");
for (i = 1; i <= 40; i++) {
event.check.occurrences = i;
var result = fatigue_check(event);
if(result) {
console.log("Occurrence " , i , " returned " , result);
}
}
console.log("Occurrences 3, 15, and 30 should have returned true.");
}

testFatigueNoAnnotations();
testFatigueResolutionTrue();
testFatigueResolutionFalse();
Expand All @@ -589,6 +749,7 @@ testFatigueOccurrencesTrue();
testFatigueOccurrencesFalse();
testFatigueIntervalTrue();
testFatigueIntervalFalse();
testFatigueNonMultipleIntervalFalse();
testFatigueFlappingTrue();
testFatigueFlappingFalse();

testFatigueNonMultipleIntervalLoop();

0 comments on commit 77a61f6

Please sign in to comment.