Skip to content

Instantly share code, notes, and snippets.

@codemoran
Created September 4, 2013 05:23
Show Gist options
  • Save codemoran/6433040 to your computer and use it in GitHub Desktop.
Save codemoran/6433040 to your computer and use it in GitHub Desktop.
Use the harddrive /dev/sda1 and send the usage percentage to Graphdat to create a custom graph
var _exec = require('child_process').exec;
var _https = require('https');
var _os = require('os');
var _options = {
method: 'POST',
host: 'api.graphdat.com',
port: 443,
path: '/v1/measurements',
auth: '[email protected]:api.896a98bf4f',
headers: {'Content-Type' : 'application/json'},
};
function getDiskSpace(callback)
{
_exec("df -k /dev/sda1", function(err, stdout, stderr) {
// ignore errors, this is just a sample
var lines = stdout.split("\n");
var diskInfo = lines[1].replace(/[\s\n\r]+/g,' ').split(' ');
var percentage = diskInfo[4].replace('%', '') / 100;
callback(null, percentage);
});
}
function sendToGraphdat(percentage, callback)
{
var body = {
"source":"sigurd-db",
"metric":"SIGURD_DISK_SPACE",
"measure": percentage,
"timestamp": Date.now() / 1000
};
var bodyString = JSON.stringify(body);
_options.headers['Content-Length'] = Buffer.byteLength(bodyString, 'utf8');
var cb = function(response) {
response.on('end', function () {
return callback(null);
});
};
var req = _https.request(_options, callback);
req.write(bodyString);
req.end();
}
function run() {
getDiskSpace(function(err, percentage) {
// ignore errors, this is just a sample
sendToGraphdat(percentage, function(err) {
// ignore errors, this is just a sample
setTimeout(run, 5000);
});
});
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment