Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dashing job for fetching CPU usage and Instance count from AWS. Create an aws-config.json, and update your AutoScalingGroupName here. It assumes a List widget named servers and a Meter widget named cpu in your dashboard. { "accessKeyId": "XXXXXX", "secretAccessKey": "XXXXX", "region": "ap-southeast-1" }
"use strict";
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + '/../aws-config.json');
var autoscaling = new AWS.AutoScaling();
var cloudwatch = new AWS.CloudWatch();
function goFigure() {
var e = new Date();
var d = new Date(Date.now() - 5*60*1000);
autoscaling.describeAutoScalingInstances({},function(err,data) {
if (!err) {
var items = data.AutoScalingInstances.map(function(srv) {
return { label: srv.InstanceId, value: srv.HealthStatus };
});
send_event('servers', { items: items });
}
});
cloudwatch.getMetricStatistics({ Namespace: 'AWS/EC2' , MetricName: 'CPUUtilization',
Dimensions: [ { Name: 'AutoScalingGroupName', Value: 'adproxyasgrp' } ],
StartTime:d , EndTime: e, Period: 60, Statistics: [ 'Average','SampleCount' ],
}, function(err,data) {
var datapoint = data.Datapoints.pop();
if (err) {
return console.log(err);
}
send_event('cpu',{ value: Math.floor(datapoint.Average),
moreinfo: 'CPU usage at ' + datapoint.Timestamp.toTimeString() });
});
}
goFigure();
setInterval(goFigure,60*1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment