2016-09-23 34 views
1

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

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

回答

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)) 

问候

+0

什么是 '反' 属性的单位?字节?或者位?如果我将'summaryPeriod'设置为86400秒,那么我得到一个包含'counter'的SoftLayer_Metric_Tracking_Object_Data数组。如果我想得到9月份使用的总带宽,我是否计算所有'counter'属性?什么是计算规则?至于〜 –

+0

单位是字节,要获得MB,您必须使用此:result/1000/1000.请注意,我使用的是1000而不是1024.此外,您需要将summaryPeriod从86400更改为600,否则您将得到不同的结果。 –

+0

您需要获取PUBLICIN和PUBLICOUT的数据(在keyName属性中设置值)。然后总结他们的结果,你将得到总数 –

相关问题