1

I am trying to run a query in Azure to show the free disk space or used disk space of each VM in Azure under a specific subscription and I wanted to show in the result the actual disk size of the VM. I need the data to identify the over-allocated resource and to downscale it to minimize the cost. Can someone help me to modify the query inorder to show the actual disk size of the VM in another column aside from the free disk space? or is there other ways to get the data of the disks that I need?

Here is the query that I run from Azure under Monitor>Logs>Query

InsightsMetrics
| where Origin == "vm.azm.ms"
 and Namespace == "LogicalDisk" and Name == "FreeSpacePercentage"
| extend Disk=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
| summarize Disk_Free_Space = avg(Val) by Computer, Disk, _ResourceId
| project Computer, Disk, Disk_Free_Space

and only shows 3 columns Computer Name, Disk, Disk_Free_Space

Shane Tan
  • 13
  • 1
  • 3

2 Answers2

0

Please check if below points can give an idea.

  1. According to Expand virtual hard disks - Azure VM's | Microsoft Docs

When you create a new virtual machine (VM) in a resource group by deploying an image from Azure Marketplace, the default operating system (OS) drive is often 127 GB (some images have smaller OS disk sizes by default). Even though it's possible to add data disks to the VM (the number depends on the SKU you chose)

  1. Try to summarize used memory and free space to get total memory and try to get a column named total disk space or memory out of it.

  2. Hoping this reference Calculating total memory might give an idea.

  3. Also see Azure Resource Graph sample queries in which query looks for virtual machine scale set resources and gets various details including the virtual machine size and the capacity of the scale set.

  4. Azure VM RAM and CPU size depend on the hardware profile chosen for the VM. In this example, we will retrieve VM (TestMachine2k16) hardware profile and then we can find how much RAM or CPU is allocated to it.

  5. To get the Size of the Azure VM,try to use powershell.

     $azvm = Get-AzVM -VMName 'VMname'
    $azvm.HardwareProfile.VmSize
    

We can check the above output size on the Microsoft Azure website to know how much RAM and CPU are associated with it and another way using the PowerShell by using the Get-AZVmSize command.

$vmsize = $azvm.HardwareProfile.VmSize Get-AzVMSize -VMName $azvm.Name -ResourceGroupName $azvm.ResourceGroupName | where{$_.Name -eq $vmsize}

  1. We can monitor VM memory usage performance :Go to your VM -> Monitoring -> Insight enter image description here
  2. Setup alerts and get notified when a threshold is met - then you can just accomplish by creating a log alert rule. For more information w.r.t it, please refer this document.

References:

  1. Disk query in Log Analytics on Azure - Stack Overflow
  2. Is there any API to query an Azure VM for free disk/memory space? - (starwindsoftware.com)
  3. Resizing the Disk for Azure VM | StarWind Blog (starwindsoftware.com)
  4. help to set up azure alert for disk space alert when 10gb or less - Microsoft Q&A
kavyaS
  • 8,026
  • 1
  • 7
  • 19
0

I do highly recommend you to use VM's terminal or some command free or df or even fdisk. e.g: A.sudo fdisk -l:

Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VBOX HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x50c3fc86

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 23164927 23162880   11G 83 Linux
/dev/sda2       23166974 25163775  1996802  975M  5 Extended
/dev/sda5       23166976 25163775  1996800  975M 82 Linux swap / Solaris

B.df -h:

Filesystem      Size  Used Avail Use% Mounted on
udev            465M     0  465M   0% /dev
tmpfs            98M  2.6M   96M   3% /run
/dev/sda1        36G   11G   24G  31% /
tmpfs           489M     0  489M   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs            98M   88K   98M   1% /run/user/116
tmpfs            98M   60K   98M   1% /run/user/1000

C:free -l

               total        used        free      shared  buff/cache   available
Mem:         1000120      433260       88684       12420      478176      405884
Low:         1000120      911436       88684
High:              0           0           0
Swap:         997372         256      997116
Vittore Marcas
  • 1,007
  • 8
  • 11