Skip to content

Instantly share code, notes, and snippets.

@rushipkar90
Created September 25, 2015 16:25
Show Gist options
  • Save rushipkar90/b700fe7bec824d2b9d22 to your computer and use it in GitHub Desktop.
Save rushipkar90/b700fe7bec824d2b9d22 to your computer and use it in GitHub Desktop.
bandwidthusage.pl
#!/usr/bin/perl
use lib '/root/sysutils/modules';
use XML::Simple;
use Data::Dumper;
use XMLAPIRequest;
chomp(my $hostname = `hostname`);
$xmlapi = XMLAPIRequest->new(
'HOSTNAME' => $hostname
);
$command = '/xml-api/showbw';
%request = $xmlapi->dispatch($command);
$xml = XML::Simple->new();
$requestparsed = $xml->XMLin($request{PAGE});
%accounts = {};
#print Dumper $requestparsed;
if ($request{RESULT} =~ /HTTP\/1.0 200 OK/) {
print "\nTOP 20 BANDWIDTH\n\n";
foreach $account (@{$requestparsed->{bandwidth}{acct}}) {
$accounts{$account->{user}} = $account->{totalbytes};
}
@sorted = sort { $accounts{$b} <=> $accounts{$a} } (keys(%accounts));
foreach $key (@sorted[0..19]) {
$mbytes = int (($accounts{$key} / 1024) / 1024);
print "$key\t\t $mbytes M\n";
}
} else {
print "Bandwidth Usage Request failed.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment