Skip to content

Instantly share code, notes, and snippets.

@gido
Created Jan 15, 2015
Embed
What would you like to do?
Quick&Dirty script to get total disk usages of each account by crawling the Alwaysdata web administration
/**
* run:
* $ casperjs diskusage.js --user=myaccount --password=mypassword
*
* This script can lookup for env variables:
* ALWAYSDATA_USER
* ALWAYSDATA_PASSWORD
*/
var casper = require('casper').create({
//verbose: true,
//logLevel: "debug"
});
var system = require('system');
var utils = require('utils');
var user = casper.cli.has('user') ? casper.cli.get('user') : system.env.ALWAYSDATA_USER;
var password = casper.cli.has('password') ? casper.cli.get('password') : system.env.ALWAYSDATA_PASSWORD;
var accounts = [];
var diskUsages = [];
if (!user || !password) {
casper.echo("Usage: casperjs diskusage.js --user=myaccount --password=mypassword");
casper.exit();
}
function convertToMegaBytes(totalString) {
var totalDiskSpace = totalString.replace('Total : ', '');
var totals = totalDiskSpace.match(/([0-9,]+)\s([a-z]+)/i);
if (totals.length > 0) {
value = totals[1].replace(',', '.');
value = parseFloat(value);
units = totals[2];
switch(units) {
case 'Mio':
totalDiskSpace = value;
break;
case 'octet':
totalDiskSpace = value / 1024.0 / 1024.0;
break;
case 'Gio':
totalDiskSpace = value * 1024.0;
break;
}
} else {
return 0;
}
return totalDiskSpace;
}
function getAccounts() {
var accounts = document.querySelectorAll('#change-object optgroup[label="Comptes"] option');
return Array.prototype.map.call(accounts, function(a) {
return a.getAttribute('value');
});
}
casper.start('https://admin.alwaysdata.com/login/', function() {
this.echo('login...');
this.fill('form[action="/login/"]', {
login: user,
password: password,
alive: false
}, true);
});
casper.thenOpen('https://admin.alwaysdata.com/usage/storage/', function() {
this.echo("authenticated. Calculating total disk usage...");
accounts = this.evaluate(getAccounts);
casper.eachThen(accounts, function forEachAccountDo(response) {
var account = response.data;
// self.echo("calculating disk usage for " + account + " ...");
casper.fill('#object-choice', {
"change-object": account
}, true);
casper.then(function() {
var totalDiskSpaceText = this.evaluate(function() {
// there is jQuery on the page, so use it :)
return $(".contentmenu.left ul li").last().text();
});
var totalDiskSpace = convertToMegaBytes(totalDiskSpaceText);
diskUsages.push([account, totalDiskSpace]);
});
});
});
casper.run(function() {
var self = this;
// echo results in some pretty fashion
this.echo(accounts.length + ' accounts found:');
sorted = diskUsages.sort(function(a, b) { return b[1] - a[1]; });
this.echo("List of account ordered:");
this.echo("------------------------");
var total = 0;
sorted.forEach(function(item) {
casper.echo(item[0] + ": " + item[1] + " MB");
total = total + item[1];
});
this.echo('');
this.echo('Total: '+ total + ' MB');
this.echo('Total: '+ (total/1024) + ' GB');
this.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment