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.

[Resolved] WHM API question

Discussion in 'cPanel Developers' started by rustyhex, Jul 19, 2013.

  1. rustyhex

    rustyhex Member

    Joined:
    Jul 18, 2013
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hi!

    I am using Whostmgr::XMLUI::cPanel to get some info in the WHM plugin (with cpanel_exec).
    For example, i set:
    Code:
    cpanel_xmlapi_module = 'MysqlFE'
    cpanel_xmlapi_func = 'listdbs'
    to get mysql databases.

    Everything works fine, but i have 2 questions:
    1. What module and func should i use to retrieve service status? (xml-api/servicestatus)
    Nothing works, no info in the docs.

    2. api2_stat from StatsBar is marked as deprecated. What should i use instead?

    Thanks.

    p.s. Api call tracer shows absolutely nothing on https://cpanel:2087/cpsess123456/xml-api/servicestatus.
     
    #1 rustyhex, Jul 19, 2013
    Last edited: Jul 19, 2013
  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
    I have no issues with this call on test servers. Is it shown as available by the 'applist' call?

    https://my.hostname.com:2087/cpsess7078929775/xml-api/applist

    If not, what version of cPanel/WHM are you on?

    That is likely a mistake. It's being used all over the place in X3. Execute this grep and you'll see that it's being used everywhere. If it were deprecated, X3 should be updated to use a new call.


    Code:
    [/usr/local/cpanel/base/frontend/x3]# grep -R 'StatsBar::stat' *
    
     
  3. rustyhex

    rustyhex Member

    Joined:
    Jul 18, 2013
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Sorry about the confusion.

    It is definitely in the applist:
    https://my.hostname.com:2087/cpsess7...ml-api/applist

    And xml-api call works fine:
    https://cpanel:2087/cpsess123456/xml-api/servicestatus

    I wish to make this call through the Whostmgr::XMLUI::cPanel::cpanel_exec. I don't know how, because it requires 2 arguments:
    module and function. if the servicestatus is a function, then what's the module name? Status? Serverinfo?

    I am asking, because i am not completely sure, that it's possible.
     
    #3 rustyhex, Jul 22, 2013
    Last edited: Jul 22, 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
    Sorry, I didn't read clearly enough. cpanel_exec() only attaches to API2 and API1, not the XML-API. It's really just a wrapper around calling /usr/local/cpanel/cpanel --json-fast-connect or /usr/local/cpanel/cpanel --xml-fast-connect which provide access to API1/API2.

    You'll have to query the XML-API directly in order to access 'servicestatus'. That can be done with the Cpanel::PublicAPI.pm module. Here's some sample code to access it on localhost:

    Code:
    #!/usr/bin/perl
    
    BEGIN { unshift( @INC, '/usr/local/cpanel' ); }
    
    use Cpanel::PublicAPI ();
    use Data::Dumper      ();
    use IPC::Open3        ();
    use JSON::PP          ();
    
    my $accessHash = load_access_hash();
    my $func       = 'servicestatus';
    my $formdata   = {};
    
    my $publicAPI = Cpanel::PublicAPI->new(
        usessl     => 1,
        user       => $ENV{'REMOTE_USER'} || getpwuid($<),
        accesshash => $accessHash
    );
    
    my $response = $publicAPI->whm_api( $func, $formdata, 'json' );
    my $jsonObject = JSON::PP::decode_json($response);
    
    print "JSON API RESPONSE: \n" . Data::Dumper::Dumper($jsonObject) . "\n";
    
    
    
    sub load_access_hash {
        my $accessHash = '';
    
        #Ensure access hash exists
        my $theHash = "/root/.accesshash";
        unless ( -f $theHash ) {
            my $pid = IPC::Open3::open3( my $wh, my $rh, my $eh, '/usr/local/cpanel/whostmgr/bin/whostmgr setrhash' );
            waitpid( $pid, 0 );
        }
    
        #get accesshash
        open( my $hashFh, "<", $theHash ) || die "Cannot open access hash: " . $theHash;
        $accessHash = do { local $/; <$hashFh>; };
        close($hashFh);
        $accessHash =~ s/\n//g;
    
        return $accessHash;
    }
    
     
Loading...

Share This Page