Windows Server 2012 Hyper-V: VM Resource Metering via PowerShell
With Windows Server 2012 Hyper-V Microsoft introduced a new feature in Hyper-V called Resource Metering which allows you to measure the usage of a virtual machine. This allows you to track CPU, Memory, Disk and network usage. This is a great feature especially if you need to do charge back or maybe even for trouble shooting.
Last week I had the chance to test and implement this feature for a customer.
First you can check the available PowerShell cmdlets for Hyper-V or for the the commands which include VMResourceMetering.
Get-Command -Module Hyper-V Get-Command *VMResourceMetering*
The resource metering has to be enabled per Virtual Machine. This is great, so even if you move the virtual machine from one Hyper-V host to another you still have the usage data.
To enable the resource metering you can use the following cmdlet. In my case I enable VM Resource Metering for my VM called SQL2012.
Get-VM SQL2012 | Enable-VMResourceMetering
With the cmdlet Measure-VM you can get the statistic for the VM.
Measure-VM -VMName SQL2012 Get-VM SQL2012 | Measure-VM | select *
To get the network traffic use the properties of the NetworkMeteredTrafficReport.
(Measure-VM -VMName SQL2012).NetworkMeteredTrafficReport
Here is another great thing, if you want to measure Network from or to a specific network you can use VM Network Adapter ACLs to do so. With ACLs you can not just allow or deny network traffic, you can also meter network traffic for a special subnet or IP address.
Add-VMNetworkAdapterAcl -VMName SQL2012 -Action Meter -RemoteIPAddress 10.10.0.0/16 -Direction Outbound
Of course you can reset the statistics for the VM.
Get-VM SQL2012 | Reset-VMResourceMetering
And to disable resource metering for the VM use:
Get-VM SQL2012 | Disable-VMResourceMetering
I think this is one of the great new features of Windows Server 2012 Hyper-V which gets not a lot of attention but is really important.
If you want to know more about Resource Metering in Hyper-V, checkout the blog post from Lalithra Fernando on the TechNet Virtualization Blog.