Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active April 20, 2016 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmervine/d347550caa5e865c4578 to your computer and use it in GitHub Desktop.
Save jmervine/d347550caa5e865c4578 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
/*
* MaxCDN 2 DataDog
*
* Usage:
*
* $ npm install async moment maxcdn dogapi
* $ MAXCDN_ALIAS=<maxcdn_alais> \
* MAXCDN_KEY=<maxcdn_key> \
* MAXCDN_SECRET=<maxcdn_secret> \
* DATADOG_API_KEY=<datadog_api_key> \
* DATADOG_APP_KKEY=<datadog_app_key> \
* node ./maxdd.js
*
* Developed / tested with:
*
* $ jmervine@laptop:~$ node --version
* $ v5.9.0
* $ jmervine@laptop:~$ npm --version
* $ 3.7.3
* $ jmervine@laptop:~$ uname -a
* $ Linux laptop 4.2.0-34-generic #39-Ubuntu SMP Thu Mar 10 22:13:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
*/
var maxAlias = process.env.MAXCDN_ALIAS;
var maxKey = process.env.MAXCDN_KEY;
var maxSecret = process.env.MAXCDN_SECRET;
var ddApiKey = process.env.DATADOG_API_KEY;
var ddAppKey = process.env.DATADOG_APP_KEY;
var async = require('async');
var MaxCDN = require('maxcdn');
var maxcdn = new MaxCDN(maxAlias, maxKey, maxSecret);
var datadog = require("dogapi");
var report = {};
function stats(callback) {
var endpoint = 'reports/stats.json/hourly';
console.log('==> Fetching maxcdn stats payload from:', endpoint);
maxcdn.get(endpoint, function(error, results) {
if (error) {
console.log(' ERROR: s', error.data);
process.exit(error.statusCode);
}
callback(undefined, results.data.summary);
});
}
function statuscodes(callback) {
var collect = function(dataset, code) {
var results = { '2xx': 0, '3xx': 0, '4xx': 0, '5xx': 0 };
dataset.forEach(function(data) {
[ "2", "3", "4", "5" ].forEach(function(n) {
if (data.status_code.startsWith(n))
results[n + 'xx'] += parseInt(data.hit, 10);
});
});
return results;
}
var endpoint = 'reports/statuscodes.json/daily';
console.log('==> Fetching maxcdn status codes payload from:', endpoint);
maxcdn.get(endpoint, function(error, results) {
if (error) {
console.log(' ERROR: s', error.data);
process.exit(error.statusCode);
}
callback(undefined, collect(results.data.statuscodes));
});
}
async.parallel({
stats: stats,
statuscodes: statuscodes,
}, function(err, results) {
datadog.initialize({
api_key: ddApiKey,
app_key: ddAppKey
});
cache_size = parseInt(results.stats.size, 10);
cache_hit = parseInt(results.stats.cache_hit, 10);
cache_miss = parseInt(results.stats.noncache_hit, 10);
cache_hit_percent = ((cache_hit / cache_miss) * 100.0);
if (cache_hit_percent === NaN) {
cache_hit_percent = 0;
}
var metrics = [
{ metric: "maxcdn.cache_bites", points: cache_size },
{ metric: "maxcdn.cache_hits", points: cache_hit },
{ metric: "maxcdn.cache_misses", points: cache_miss },
{ metric: "maxcdn.cache_hit_percent", points: cache_hit_percent }
];
[ "2", "3", "4", "5" ].forEach(function(n) {
var metric = "maxcdn." + n + "xx_count";
metrics.push({ metric: metric, points: results.statuscodes[n+'xx'] });
});
console.log('==> Sending datadog payload...');
datadog.metric.send_all(metrics, function(error, data, code) {
if (error) {
console.log('==> ERROR:', error);
process.exit(1);
}
if (code != 200) {
console.log(' ERROR: Invalid Response code (%s) %s', code, data);
process.exit(parseInt(code, 10));
}
console.log(' Sent!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment