Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
34 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,42 @@ | ||
var aws = require("aws-lib"); | ||
var aws = require('aws-lib'); | ||
var _ = require('underscore'); | ||
|
||
module.exports = function(options) { | ||
var cw = aws.createCWClient(options.awskey, options.awssecret); | ||
|
||
var Check = function Check(params) { | ||
var that = this; | ||
this.interval = params.definition.interval; | ||
this.command = params.definition.command; | ||
this.unit = params.definition.unit; | ||
this.name = params.definition.name; | ||
this.namespace = params.definition.namespace; | ||
|
||
_(params).each(function(v, k) { | ||
that[k] = v; | ||
}); | ||
if (options.daemon) { | ||
setInterval(function() { that.emit('metric'); }, this.interval); | ||
|
||
this.on('metric', function() { | ||
this.command(function(res) { | ||
// Submit to CloudWatch | ||
that.instance = res.instance; | ||
that.value = res.value; | ||
that.submit(options, res); | ||
}); | ||
}); | ||
} else { | ||
Submit(options, Prepare(options)); | ||
this.submit(options); | ||
} | ||
}; | ||
|
||
Check.prototype = new process.EventEmitter(); | ||
|
||
Check.prototype.submit = function(config, res) { | ||
var data = {}; | ||
data.instanceid = res.instance; | ||
data.unit = this.unit; | ||
data.metricname = this.name; | ||
data.namespace = this.namespace; | ||
data.value = res.value; | ||
data = Prepare(data); | ||
Submit(config, data); | ||
}; | ||
|
||
var Prepare = function(data) { | ||
var params = {}; | ||
params['Namespace'] = data.namespace; | ||
params['MetricData.member.1.MetricName'] = data.metricname; | ||
params['Namespace'] = this.namespace; | ||
params['MetricData.member.1.MetricName'] = this.metricname; | ||
params['MetricData.member.1.Dimensions.member.1.Name'] = 'InstanceId'; | ||
params['MetricData.member.1.Dimensions.member.1.Value'] = data.instanceid; | ||
params['MetricData.member.1.Unit'] = data.unit; | ||
params['MetricData.member.1.Value'] = data.value; | ||
return params; | ||
} | ||
|
||
var Submit = function(config, data) { | ||
this.awskey = config.awskey; | ||
this.awssecret = config.awssecret; | ||
// Submit to CloudWatch | ||
cw.call("PutMetricData", data, function(result) { | ||
params['MetricData.member.1.Dimensions.member.1.Value'] = this.instanceid; | ||
params['MetricData.member.1.Unit'] = this.unit; | ||
params['MetricData.member.1.Value'] = this.value; | ||
cw.call("PutMetricData", params, function(result) { | ||
console.log(JSON.stringify(result)); | ||
}); | ||
} | ||
}; | ||
|
||
return Check; | ||
} |