phpperformancemonitoringibm-cloudibm-cloud-tools

Remotely check CPU, memory and disk space IBM Bluemix PHP instance


Remotely check CPU, memory and disk space IBM Bluemix PHP instance.

I have a php instance running in IBM Bluemix.

Now I want to check the CPU, Memory and Disk Space from an external program by calling a php web page.

For CPU I tried the following function:

function get_server_cpu_usage(){
    $load = sys_getloadavg();
    $cores = shell_exec("grep 'model name' /proc/cpuinfo | wc -l");
    $load[2] = ($load[2] / $cores) * 100;
    return $load[2];
}

For Memory I use the following function:

memory_get_usage(true)

For Disk Space I use the following function:

disk_free_space("/") 

But when I compare these results with the results provided by the IBM Bluemix Console, they are different.

Is there a correct way to externally monitor these values?


Solution

  • You can retrieve that information using CF API REST call. You can find the CF APIs documentation here:

    In this specific case you could do a curl call from your php application and parse the JSON response for CPU, Memory and Disk Space information.

    curl "https://api.ng.bluemix.net/v2/apps/YOURAPP_GUID/summary" -X GET -H "Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoidWFhLWlkLTQyNCIsImVtYWlsIjoiZW1haWwtMjkzQHNvbWVkb21haW4uY29tIiwic2NvcGUiOlsiY2xvdWRfY29udHJvbGxlci5hZG1pbiJdLCJhdWQiOlsiY2xvdWRfY29udHJvbGxlciJdLCJleHAiOjE0NjA1MDY2NjF9.iUpeFnPKDWf3sxvDB0RF2_nSLAkqLZP7iN6Nx0bWE-Q"

    You can retrieve the Authorization header with:

    cf oauth-token
    

    after login to IBM Bluemix (cf login)

    If you want retrieve the auth-token from your application you should use another REST API before running the first curl get.

    curl -s -X POST -H "Accept-Encoding: application/json" -d "grant_type=password&password=YOURPASSWORD&scope=&username=YOURUSERNAME" -u "cf:" https://login.ng.bluemix.net/UAALoginServerWAR/oauth/token