I tried querying the API for CPU usage on one of our virtual servers, using the PHP client:
$user = 'my_user';
$key = 'my_key';
$server = 24819101;
$client = \SoftLayer\SoapClient::getClient('SoftLayer_Virtual_Guest', $server, $user, $key);
$usage = $client->getCpuMetricDataByDate(date('Y-m-d H:i:s', strtotime('-2 HOUR')));
The call to the API is ok, and one of the results is like this:
array(
'counter' => 2.096,
'dateTime' => '2017-01-24T14:00:00+01:00',
'type' => 'cpu0',
)
I read this as "the first CPU, at 2 PM today, was at about 2% load".
However, if I go see the CPU chart from the control panel, I see that at 2 PM today the CPU was at 76.5%
Shouldn't the two values be equal be equal?
Firt of all the API method that the Softlayer's portal uses to display the CPU usage is not SoftLayer_Virtual_Guest:getCpuMetricDataByDate this method displays the average CPU usage, in another hand the Softlayer's portal displays the max CPU usages and it uses this method:
http://sldn.softlayer.com/reference/services/SoftLayer_Metric_Tracking_Object/getSummaryData
This is an example using RESTful:
POST https://$USER:$APIKEY@api.softlayer.com/rest/v3.1/SoftLayer_Metric_Tracking_Object/$METIRCID/getSummaryData
PAYLOAD:
{
"parameters": [
"2017-01-24T00:00:00-00:00",
"2017-01-25T00:00:00-00:00",
[
{
"keyName": "CPU0",
"summaryType": "max"
}
],
600
]
}
using PHP it would be something like this:
$metricID = 11111;
$client = \SoftLayer\SoapClient::getClient('SoftLayer_Metric_Tracking_Object', $metricID, $user, $key);
$types = array();
$type = new stdClass();
$type -> keyName = "CPU0";
$type -> summaryType= "max";
$types[] = $type
$usage = $client->getCpuMetricDataByDate("2017-01-24T00:00:00-00:00", "2017-01-25T00:00:00-00:00",$types,600);
So you need to get the metricID for that you can use this method:
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/getMetricTrackingObjectId
here an example using RESTFUl:
GET https://api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest/$VirtualGuestId/getMetricTrackingObjectId
Another thing to point out is the time, you need to make sure that the portal and your code are using the same timezone otherwise you will see different data for the same time in portal and API. The API will show the time using the timezone of the user that uses to perform the request, you can see the user timezone by:
- going to Softlayer's portal
- Click Account menu item->users
- Click over your user
- See the timezone
i suggest you first display all the data then verify if the data are the same for a determinate timezone, if they are not you need to review the timezone and make the changes until they display the same data
Regards