2012年12月7日

[Azure]計算VHD被Charge的容量

最近常遇到客戶詢問為什麼VHD每天被Charge的容量不大對。

Azure VHD是儲存在Page Blob中,並且只有需要(至於怎麼樣叫做需要,目前沒有文件說明;我測試的結果,OS Disk幾乎是全部計入,Data Dis則是部分計入,但,目前沒有任何保證)的部分會被寫入。因此在計算用量時,並不是Portal上看到30G就是30G計費。我寫了一個小程式來計算每天charge的容量:(這個程式計算出的結果可能還是與報表上有誤差,因為我沒有將一些屬性所占用的容量計算進去,不過已經相當接近了)

            string accountName = string.Empty;
string keyValue = string.Empty;

Console.WriteLine("Please input Storage Name”);
accountName = Console.ReadLine();

Console.WriteLine("Please input Storage Account Key");
keyValue = Console.ReadLine();

var storage = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, keyValue), false);
var blobClient = storage.CreateCloudBlobClient();
var vhds = blobClient.GetContainerReference("vhds");
var blobs = vhds.ListBlobs();
float totalBytes = 0;
float totalGB = 0;
float summaryGB = 0;
foreach (var blob in blobs)
{
var pageBlob = vhds.GetPageBlobReference(blob.Uri.Segments[blob.Uri.Segments.Length - 1]);
pageBlob.FetchAttributes();
var pageRanges = pageBlob.GetPageRanges();
totalBytes += pageRanges.Sum(x => x.EndOffset - x.StartOffset);


                totalGB = totalBytes / (1024 * 1024 * 1024);
summaryGB += totalGB;
Console.WriteLine(string.Format("Total GB for VHD[{0}] is {1}, Daily usage is {2} GB",
blob.Uri.Segments[blob.Uri.Segments.Length - 1],
totalGB,
totalGB / 31));

}
Console.WriteLine("Total VHD Size in storage is {0} GB", summaryGB);
Console.WriteLine("Daily usage is {0} GB", summaryGB / 31);
Console.ReadLine();

沒有留言:

About Me