perl <<'EOF'
use lib qw( /usr/local/cpanel /usr/local/cpanel/3rdparty/perl/514/lib64/perl5/cpanel_lib );
use Cpanel::PublicAPI;
use YAML;
use XML::Simple;

# Cause we need to authenticate
sub get_accesshash {
        my $accesshash;
        if ( ! -f '/root/.accesshash' ) {
                $ENV{REMOTE_USER} = 'root';
                system('/usr/local/cpanel/bin/realmkaccesshash');
        }
        if ( -f '/root/.accesshash' ) {
                open my $hash, "<", "/root/.accesshash";
                my @accesshash = <$hash>;
                close $hash;
                foreach(@accesshash){
                        $accesshash .= $_
                }
                return $accesshash;
        }
}

# error checking it's all cool like
sub check_user {
        my $domain = shift;
        my $user;
        open(my $DAT, "<", "/etc/trueuserdomains");
                while(<$DAT>) {
                        if ( /^$domain:\s([a-zA-Z0-9]+)$/ ) {
                                $user = $1;
                                last;
                        }
                }
        close($DAT);
        my $file = "/var/cpanel/users/$user";
        unless ( -f $file ) {
                       die "[!] Invalid user: $user does not exist.\n";
        }
        open(my $DAT, "<", "/var/cpanel/resellers") || die "[!] /var/cpanel/resellers not found, is this being run in a reseller server?\n";
                my @resellers = <$DAT>;
        close($DAT);
        if ( ! grep(/^$user/, @resellers) ) {
                die "[!] Invalid user: $user does not appear to be a reseller.\n";
        }
        return($user);
}

# get accounts owned by reseller
sub get_accounts {
        my $user = shift;
        my @accounts;
        my $file = '/etc/trueuserowners';
        open my $fh, '<', $file or die "File does not exist: $file\n";
        while (<$fh>) {
                if ( $_ =~ /(\S+): $user$/ ) {
                            push @accounts, $1;
                }
            }
        close $fh;
        return @accounts;
}


# load access hash
my $accesshash = get_accesshash;

# create cpanel object
my $cp = Cpanel::PublicAPI->new( 'user' => 'root', 'accesshash' => $accesshash );

# load user with checks
my $domain = 'domain_placeholder';
my $user = check_user($domain);

# get diskusage info
sub get_du {
        my $acct = shift;
        my $disk = $cp->cpanel_api2_request('whostmgr', { 'module' => 'StatsBar', 'func' => 'stat', 'user' => "$acct" },{'display' => 'diskusage'});
        my $disk_usage = $disk->{cpanelresult}{data}[0]{count};
	$disk_usage =~ s/,//g;
        return $disk_usage;
}

# get bandwidth info
sub get_bw {
        my $acct = shift;
        my $bandwidth = $cp->cpanel_api2_request('whostmgr', { 'module' => 'StatsBar', 'func' => 'stat', 'user' => "$acct" },{'display' => 'bandwidthusage'});
        my $bw_usage = $bandwidth->{cpanelresult}{data}[0]{count};
        return $bw_usage;
}

# get reseller stats
sub get_stats {
	my $acct = shift;
	my $reseller_stats = $cp->api_request('whostmgr', '/xml-api/resellerstats', 'GET', { 'api.version' => 1, 'user' => $acct });
	my $xml = XML::Simple->new();
	$reseller_stats = $xml->XMLin($reseller_stats);
	my $disk_alloc = $reseller_stats->{'data'}->{'reseller'}->{'totaldiskalloc'};
	my $bw_alloc = $reseller_stats->{'data'}->{'reseller'}->{'totalbwalloc'};
	return($disk_alloc, $bw_alloc);
}

my @accounts = get_accounts($user);

my $reseller_stats = $cp->api_request('whostmgr', '/xml-api/resellerstats', 'GET', { 'api.version' => 1, 'user' => $user });
my $xml = XML::Simple->new();
$reseller_stats = $xml->XMLin($reseller_stats);
my $disk_alloc = $reseller_stats->{'data'}->{'reseller'}->{'totaldiskalloc'};
my $bw_alloc = $reseller_stats->{'data'}->{'reseller'}->{'totalbwalloc'};

# Load reseller limits
my $yaml = YAML::LoadFile('/var/cpanel/reseller-limits.yaml');
my ($bw_limit) = split(/\./, $yaml->{$user}->{limits}->{resources}->{type}->{bw});
my ($du_limit) = split(/\./, $yaml->{$user}->{limits}->{resources}->{type}->{disk});

# hashref for saving data
my $data = {};
my ($bw_total, $du_total);

# build hash of usage data for reseller
foreach my $acct ( @accounts ) {
        $data->{$acct}->{disk} = get_du($acct);
        $data->{$acct}->{bw} = get_bw($acct);
}

# do math.... ewwww
foreach my $key ( keys %$data ) {
        $bw_total += $data->{$key}->{bw};
        $du_total += $data->{$key}->{disk};
}

my ( $disk_alloc, $bw_alloc ) = get_stats($user);

print "[*] Reseller usage for $user:\n";
print "\tDisk: $du_total MB | $du_limit MB | " . (split(/\./, ( $du_total / $du_limit * 100 )))[0] . " %\n";
print "\tBandwidth: $bw_total MB | $bw_limit MB | " . (split(/\./, ( $bw_total / $bw_limit * 100 )))[0] . " %\n";
print "[*] Total Allocation:\n";
print "\tDisk: $disk_alloc MB | $du_limit MB | " . (split(/\./, ( $disk_alloc / $du_limit * 100 )))[0] . " %\n";
print "\tBandwidth: $bw_alloc MB | $bw_limit MB | " . (split(/\./, ( $bw_alloc / $bw_limit * 100 )))[0] . " %\n\n";
EOF
