sabato, dicembre 14, 2013

Calculate space used by VHD on Azure Blob Storage

A piece of code to calculate space used by VHD files (Azure Virtual Machine) on Azure Blob Storage.
Code adapted (and fixed) from a previous post of Michel Chi http://nettecharticles.blogspot.tw/2012/12/azurevhdcharge.html


        public static void Exec()
        {
            string accountName = ".......";
            string keyValue = ".......";

            Console.WriteLine("UTC NOW : " + DateTime.UtcNow);

            float GB = (1024 * 1024 * 1024);
            float totalBytes = 0;

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

            foreach (var blob in blobs)
            {
                var pageBlob = vhds.GetPageBlobReference(blob.Uri.Segments.Last());
                pageBlob.FetchAttributes();
                var pageRanges = pageBlob.GetPageRanges();

                float vhdBytes = pageRanges.Sum(x => x.EndOffset - x.StartOffset);

                totalBytes += vhdBytes;

                Console.WriteLine(new string('-', 60));
                Console.WriteLine("ID         = " + blob.Uri.Segments.Last());
                Console.WriteLine("Used GB    = " + vhdBytes / GB);
                Console.WriteLine("DailyUsage = " + vhdBytes / GB / 31.0);
                Console.WriteLine("PageRanges = " + pageRanges.Count());                

            }

            Console.WriteLine(new string('-', 60));
            Console.WriteLine("Total GB    : " + totalBytes / GB);
            Console.WriteLine("Daily usage : " + totalBytes / GB / 31.0);

            Console.WriteLine(new string('-', 60));
        }

Nessun commento: