2016-09-23 29 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屬性中設置值)。然後總結他們的結果,你將得到總數 –

相關問題