Permalink
Browse files

Merge pull request #91 from fzaninotto/alert-threshold-for-checkevents

Alert threshold for checkevents
  • Loading branch information...
fzaninotto committed Jan 20, 2013
2 parents d18fccc + dcca4ce commit 5242b7f6cfc6cabf1da07a10f78fd95de7071e03
Showing with 60 additions and 5 deletions.
  1. +10 −1 app/dashboard/views/_checkDetails.ejs
  2. +50 −4 models/check.js
@@ -32,11 +32,20 @@
<label class="control-label">Slow threshold</label>
<div class="controls">
<div class="input-append">
- <input type="text" name="check[maxTime]" value="<%= check.maxTime %>" class="span2" />
+ <input type="text" name="check[maxTime]" value="<%= check.maxTime %>" class="span1" />
<span class="add-on">ms</span>
</div>
</div>
</div>
+ <div class="control-group">
+ <label class="control-label">Alert Treshold</label>
+ <div class="controls">
+ <div class="input-append">
+ <input type="text" name="check[nbErrors]" value="<%= check.nbErrors %>" class="span1" />
+ <span class="add-on">failed pings</span>
+ </div>
+ </div>
+ </div>
<div class="control-group">
<label class="control-label">Tags</label>
<div class="controls">
View
@@ -19,6 +19,8 @@ var Check = new Schema({
url : String,
interval : { type: Number, default: 60000 }, // interval between two pings
maxTime : { type: Number, default: 1500 }, // time under which a ping is considered responsive
+ alertTreshold : { type: Number, default: 1 }, // nb of errors from which to trigger a new CheckEvent
+ errorCount : { type: Number, default: 0 }, // count number of errors
tags : [String],
lastChanged : Date,
firstTested : Date,
@@ -73,11 +75,22 @@ Check.methods.togglePause = function() {
Check.methods.setLastTest = function(status, time, error) {
var now = time ? new Date(time) : new Date();
+ var mustNotifyEvent = this.mustNotifyEvent(status);
+
if (!this.firstTested) {
this.firstTested = now;
}
+
this.lastTested = now;
+
if (this.isUp != status) {
+ this.lastChanged = now;
+ this.isUp = status;
+ this.uptime = 0;
+ this.downtime = 0;
+ }
+
+ if (mustNotifyEvent) {
var event = new CheckEvent({
timestamp: now,
check: this,
@@ -90,10 +103,7 @@ Check.methods.setLastTest = function(status, time, error) {
event.downtime = now.getTime() - this.lastChanged.getTime();
}
event.save();
- this.lastChanged = now;
- this.isUp = status;
- this.uptime = 0;
- this.downtime = 0;
+ this.markEventNotified();
}
var durationSinceLastChange = now.getTime() - this.lastChanged.getTime();
if (status) {
@@ -104,6 +114,42 @@ Check.methods.setLastTest = function(status, time, error) {
return this;
};
+Check.methods.mustNotifyEvent = function(status) {
+ if (!this.firstTested) {
+ return true;
+ }
+ if (!status) {
+ // check is down
+ if (this.isUp != status) {
+ // check goes down for the first time
+ this.errorCount = 1;
+ }
+ if (this.errorCount < this.alertTreshold) {
+ // repeated down pings - increase error count until reaching the down alert treshold
+ this.errorCount++;
+ return false;
+ }
+ if (this.errorCount === this.alertTreshold) {
+ // enough down pings to trigger notofication
+ return true;
+ }
+ // error count higher than treshold, that means the alert was already sent
+ return false;
+ }
+ // check is up
+ if (this.isUp != status && this.errorCount > this.alertTreshold) {
+ // check goes up after reaching the down alert treshold before
+ return true;
+ }
+ // check either goes up after less than alertTreshold down pings, or is already up for long
+ return false;
+}
+
+Check.methods.markEventNotified = function() {
+ // increase error count to disable notification if the next ping has the same status
+ this.errorCount = this.alertTreshold + 1;
+}
+
Check.methods.getQosPercentage = function() {
if (!this.qos) return false;
return (this.qos.ups / this.qos.count) * 100;

0 comments on commit 5242b7f

Please sign in to comment.