The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help in retreving account stats

Discussion in 'cPanel Developers' started by z-hoster, Sep 13, 2013.

  1. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Hey guys i would like to retreive the hosting stats for a giving username/password on my server.

    The exact info that we have in the cpanel left side would be greate.
    Does anybody have a code that generates this kind of info?

    Main Domain ***
    Home Directory /home/***
    Last login from 70.53.253.54
    Disk Space Usage 20.84 / 2,000 MB
    Monthly Bandwidth Transfer 173.55 / 20,000 MB
    Email Accounts 1 / 100
    Subdomains 0 / 10
    Parked Domains 0 / 0
    Addon Domains 0 / 0
    FTP Accounts 0 / 50
    Mailing Lists 0 / 10
    All SQL Databases 0 / 20
    MySQL Databases 0
    MySQL Disk Space 0 MB
    Mailing List Disk Space 0 MB
    Hosting package 2 Gig
    Server Name serveur
    cPanel Version 11.38.2 (build 6)
    Theme x3
    Apache version 2.2.24
    PHP version 5.3.26
    MySQL version 5.1.70-cll
    Architecture x86_64
    Operating system linux
    Shared IP Address ***
    Path to sendmail /usr/sbin/sendmail
    Path to Perl /usr/bin/perl
    Perl version 5.8.8
    Kernel version 2.6.32-358.6.1.el6.x86_64
    cPanel Pro 1.0 (RC1)
    Service Status Click to View
     
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    You can use the API2 call StatsBar::stat() for this:

    StatsBar Module Documentation

    A sample call to it looks like:

    https://example.com:2083/json-api/c...accounts|perlversion|hostname|operatingsystem

    This won't show the main domain or home directory (and you need to specifically list every piece of info you want back in the display variable).

    For whatever reason, it's a huge pain to get a user's domain via the API with their username and password. To get the main domain, I've actually created a custom API call. To use it, drop this code into /usr/local/cpanel/Cpanel/MainDomain.pm:

    Code:
    #!/usr/bin/perl
    #
    #  MainDomain -- gets the main domain from a cPanel user's account
    #
    #  Created by David Koston (dave at kostonconsulting dot com) on 2013-06-14.
    #  No warranty of any kind provided.
    #
    
    package Cpanel::MainDomain;
    
    BEGIN { push( @INC, '/usr/local/cpanel' ); }
    use strict;
    use Cpanel::AcctUtils::Domain ();
    
    
    sub api2 {
        my $func = shift;
        my %API;
    
        $API{'get'}{'func'}   = "api2_get";
        $API{'get'}{'engine'} = 'hasharray';
    
        return \%{ $API{$func} };
    }
    
    sub api2_get {
        my %OPTS = @_;
    
    	my $return_hash = {
    		status => 0,
    		message => '',
    		domain => '',
    	};
    	my $user;
    	
    	if( $OPTS{'user'} ){
    		$user = isCpanelUsername( $OPTS{'user'} ) ? $OPTS{'user'} : '';
    	}else{
    		$user = $ENV{'REMOTE_USER'};
    	}
    	
    	if( $user eq '' ){
    		$return_hash->{message} = 'Invalid or empty {&user=} parameter';
    	}
    
    
    	my $domain = Cpanel::AcctUtils::Domain::getdomain( $user );
    	if( $domain ){
    		$return_hash->{status} = 1;
    		$return_hash->{domain} = $domain;
    		$return_hash->{message} = 'Success';
    	} 
    
    
        return $return_hash;
    }
    
    
    sub isCpanelUsername {
        unless ( $_[0] ) { return 0; }
        return $_[0] =~ /^[a-zA-Z][a-zA-Z0-9\-]{0,7}$/
          ? 1
          : 0;                                               #alpha-numeric + dashes. starts with letter, max 8 chars
    }
    
    1;
    
    Then call:

    https://hostname:2083/json-api/cpanel?cpanel_jsonapi_module=MainDomain&cpanel_jsonapi_func=get

    If you need the home directory, let me know and I'll add an API call for that too.


    You'll probably want to use cPanel's PHP library to interact with the API, unless you have another preferred language:

    https://github.com/CpanelInc/xmlapi-php
     
  3. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    #3 z-hoster, Oct 15, 2013
    Last edited: Oct 15, 2013
  4. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    If you're using PHP, use cPanel's PHP XML API library to call it: https://github.com/CpanelInc/xmlapi-php

    like so:

    Code:
    $args = array('display' => 'ftpaccounts|perlversion|hostname|operatingsystem');
    $api_result = api2_query($user, 'StatsBar', 'stat', $args);
    
    Otherwise, use whatever functions/libraries make a HTTPS request and can handle JSON.

    That is a terrible idea, definitely don't give out your root password to the customer. You can use their cPanel password to query the API when connecting to port 2083 (cPanel) for the API.
     
  5. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Hmm i get an error ...

    Code:
    $ip = getenv('**.**.**.**');
    $root_pass = getenv('***');
    
    $account = "***";
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($account,$root_pass);
    
    $xmlapi->set_debug(1);
    $args = array('display' => 'ftpaccounts|perlversion|hostname|operatingsystem');
    print $xmlapi->api2_query($account, "StatsBar", "stat", $args );
    and i get:

    Fatal error: Uncaught exception 'Exception' with message 'No host defined' in /home/***/public_html/wma/include/xmlapi.php:237 Stack trace: #0 /home/***/public_html/wma/module/accueil/accueil.php(27): xmlapi->__construct(false) #1 /home/***/public_html/wma/index.php(44): include('/home/***/...') #2 {main} thrown in /home/***/public_html/wma/include/xmlapi.php on line 237
     
    #5 z-hoster, Oct 15, 2013
    Last edited: Oct 15, 2013
  6. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    The source for the PHP library shows that the error on line 237 will be thrown when the host is not set. You should ensure that you are pulling in the IP from getenv (which I'm not sure why you would use getenv() to define the cPanel server's IP address unless the website is on the same server where you'd use something like $_SERVER['HTTP_HOST']):

    Code:
         /**
            * Connection
            *
            * $host/XMLAPI_HOST should always be equal to either the IP of the server or it's hostname
            */
    
            // Set the host, error if not defined
            if ($host == null) {
                if ( (defined('XMLAPI_HOST')) && (strlen(XMLAPI_HOST) > 0) ) {
                    $this->host = XMLAPI_HOST;
                } else {
                    throw new Exception("No host defined");
                }
            } else {
                $this->host = $host;
            }
    
     
  7. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Yeah thats what i thought but i tryed both ... giving the IP

    $ip = getenv('**.**.**.**');

    and the var like this:

    $ip = getenv($_SERVER['HTTP_HOST']);

    both of them return the same error ...
    btw, thx for your help koston. It's really apreciated ;-)
     
  8. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    You shouldn't need the getenv() part. Try one of these:

    Code:
    $ip = '**.**.**.**';
    $ip = $_SERVER['HTTP_POST'];
    
     
  9. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    Your right :)
    no more error ... but nothing is showing ... how can i retrive the stats i requested to the API?

    My code looks like this so fare
    Code:
    $ip = '**.**.**.**';
    $root_pass = '***';
    $account = "***";
    $args = array('display' => 'ftpaccounts|perlversion|hostname|operatingsystem');
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($account,$root_pass);
    $xmlapi->set_debug(1);
    
    print $xmlapi->api2_query($account, "StatsBar", "stat", $args );
    
    Is the print at the end supose to print out the output? because right now it's sending back nothing.

    - - - Updated - - -

    i just noticed the error file. looks like the access was denied ... the user/password was good (100% certain of that) is there something i need to activate in order for the API to work?

    Code:
    [15-Oct-2013 10:27:47 America/Toronto] RESPONSE:
     <?xml version="1.0" ?>
    <cpanelresult>
        <error>Access denied</error>
        <data>
            <result>0</result>
            <reason>Access denied</reason>
        </data>
    </cpanelresult>
    
    [15-Oct-2013 10:27:47 America/Toronto] SimpleXML var_dump:
    SimpleXMLElement Object
    (
        [error] => Access denied
        [data] => SimpleXMLElement Object
            (
                [result] => 0
                [reason] => Access denied
            )
    
    )
     
  10. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    I just noticed that it works with my root user/password ... is it normal that normal users don't have access to the API?

    When i tryed with the root info i got an other error:

    Fatal error: Uncaught exception 'Exception' with message 'curl_exec threw error "SSL read: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac, errno 0" for https://***:2087/xml-api/cpanel?dis...l_xmlapi_func=stat&cpanel_xmlapi_apiversion=2' in /home/***/public_html/wma/include/xmlapi.php:758 Stack trace: #0 /home/***/public_html/wma/include/xmlapi.php(676): xmlapi->curl_query('https://***', 'display=ftpacco...', 'Authorization: ...') #1 /home/***/public_html/wma/include/xmlapi.php(915): xmlapi->xmlapi_query('cpanel', Array) #2 /home/***/public_html/wma/module/accueil/accueil.php(34): xmlapi->api2_query('root', 'StatsBar', 'stat', Array) #3 /home/***/public_html/wma/index.php(44): include('/home/***/...') #4 {main} thrown in /home/***/public_html/wma/include/xmlapi.php on line 758
     
  11. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    Since the output will be an object, you should do something like this to visualize it:

    Code:
    $result = $xmlapi->api2_query($account, "StatsBar", "stat", $args );
    var_dump($result);
    
    The user should have access to api2 but it looks like the library sets the port to 2087 (WHM) by default. You'll need to use the set_port( $port ) function to set it to 2083 (cPanel) to access the API as the user.
     
  12. z-hoster

    z-hoster Member

    Joined:
    Aug 8, 2005
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    1
    It worked. The SSL message i had when trying to log in with root was because the root user does not have a cpanel account. And the access denied i had was because of the port number. Thanks a lot for your help solving this issue. I should have everything i need to query cpanel and put up some nice graph on the users account now. thx a lot!
     
Loading...

Share This Page