SoftLayer_Virtual_Guest :: getBillingCyclePublicBandwidthUsage的返回值中的转换规则是什么?

时间:2016-09-23 07:57:29

标签: python api bandwidth ibm-cloud-infrastructure

我正在开发带宽的总使用量。我尝试了很多方法来获得带宽的总使用量。结果总是与门户网站不同,而它们差不多。我不知道规则是否错误。因为API SoftLayer_Virtual_Guest :: getBillingCyclePublicBandwidthUsage(amountIn和amountOut)的返回值是十进制的。所以我做了如下:

result = bandwidth_mgt.sl_virtual_guest.getBillingCyclePublicBandwidthUsage(id=instance_id)
amountOut = Decimal(result['amountOut'])*1024   #GB to MB
amountIn = Decimal(result['amountIn'])*1024  #GB to MB
print 'amountOut=%s MB amountIn=%s MB' %(amountOut, amountIn)

结果是'amountOut = 31.75424 MB amountIn = 30.6176 MB'。 但门户网站的结果是33.27 MB和32.1 MB。有1.5MB不同。为什么?方面〜

picture of portal site

1 个答案:

答案 0 :(得分:0)

这是预期的行为值不完全相同,这是因为门户网站使用另一种方法来获取该值,如果您想获得使用相同方法所需的相同值。

查看这些相关论坛。

您需要使用以下方法:http://sldn.softlayer.com/reference/services/SoftLayer_Metric_Tracking_Object/getSummaryData

该方法返回的值用于在控制门户中创建图表。

我用Python编写了这段代码

#!/usr/bin/env python3

import SoftLayer

# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'
vsId = 24340313

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
vgService = client['SoftLayer_Virtual_Guest']
mtoService = client['SoftLayer_Metric_Tracking_Object']


try:
    idTrack = vgService.getMetricTrackingObjectId(id = vsId)
    object_template = [{
      'keyName': 'PUBLICIN',
      'summaryType': 'sum'
    }]

    counters = mtoService.getSummaryData('2016-09-04T00:00:00-05:00','2016-09-26T23:59:59-05:00',object_template,600, id = idTrack)

    totalIn = 0
    for counter in counters:
        totalIn = totalIn + counter["counter"]

    object_template = [{
      'keyName': 'PUBLICOUT',
      'summaryType': 'sum'
    }]

    counters = mtoService.getSummaryData('2016-09-04T00:00:00-05:00','2016-09-26T23:59:59-05:00',object_template,600, id = idTrack)

    totalOut = 0
    for counter in counters:
        totalOut = totalOut + counter["counter"]

    print( "The total INBOUND in GB: ")
    print(totalIn / 1000 / 1000 / 1000 )

    print( "The total OUTBOUND in MB: ")
    print(totalOut / 1000 / 1000 )

    print( "The total GB")
    print((totalOut + totalIn) / 1000 / 1000 / 1000 )

except SoftLayer.SoftLayerAPIError as e:    
    print("Unable get the bandwidth. "
          % (e.faultCode, e.faultString))

问候