远程检查IBM Bluemix PHP实例的CPU,内存和磁盘空间
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?
远程检查CPU,内存和磁盘空间IBM Bluemix PHP实例。 p>
我有一个在IBM Bluemix中运行的php实例。 p>
现在我想通过调用php网页从外部程序检查CPU,内存和磁盘空间。 p>
对于CPU,我尝试了以下功能: p>
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] ;
}
code> pre>
对于内存我使用以下函数: p>
memory_get_usage(true)
pre>
对于磁盘空间,我使用以下函数: p>
disk_free_space(“/”)
code>
但是,当我将这些结果与IBM Bluemix控制台提供的结果进行比较时,它们是不同的。 p>
是否有正确的方法来从外部监控这些结果 值? p>
div>
You can retrieve that information using CF API REST call. You can find the CF APIs documentation here:
- Application summary: https://apidocs.cloudfoundry.org/234/apps/get_app_summary.html
- Detailed application stats: https://apidocs.cloudfoundry.org/234/apps/get_detailed_stats_for_a_started_app.html
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
The values you see within the running application should be related to the running virtual process executing the PHP runtime and for this reason the result could be different from what you see from the IBM Bluemix Console in the application dashboard, since the numbers you see don't consider the resources used by the entire runtime.
Moreover you cannot be sure that the values retrieved are referring at the same moment between the two test.
If you need a more powerful tool for performance monitoring you can use the Monitoring and Analytics service on IBM Bluemix. Unfortunately the Monitoring & Analytics service does not offer an API to retrieve its collected metrics. We realize that supporting a programmatic interface would be helpful and it's in our backlog to add that capability.
try:
bx cf app <app-name>