Powershell – Get Azure VM Overview / Monitoring data via powershell or azure cli

azurecommand-line-interfacepowershell

When we login to azure portal and have a look at any resource like VM, we will see some monitoring data in Overview section of each resource which shows us charts for 30 days. (CPU average, network in/out etc)

My requirement is to not make any changes to azure resources via portal or cli. But i am rather looking for a powershell / azure cli command which will pull all this data from azure to my local machine for some analysis.

I am looking for VM, Web apps and Azure SQL to start with.

Thanks

Best Answer

You are right, we can use CLI 2.0 to get the metric data:

az monitor metrics list --resource /subscriptions/xxxxxxx/resourceGroups/xxxxx/providers/Micros‌​oft.Compute/virtualM‌​achines/xxxx --metric-names "Percentage CPU" --time-grain "PT1M" >> PercentageCpuData.txt

Also we can use Azure PowerShell command Get-AzureRmMetricDefinition to get metrics.

Here are the metrics for Azure VM:

PS D:\testdata> (Get-AzureRmMetricDefinition -ResourceId $id).name

Value                     LocalizedValue
-----                     --------------
Percentage CPU            Percentage CPU
Network In                Network In
Network Out               Network Out
Disk Read Bytes           Disk Read Bytes
Disk Write Bytes          Disk Write Bytes
Disk Read Operations/Sec  Disk Read Operations/Sec
Disk Write Operations/Sec Disk Write Operations/Sec
CPU Credits Remaining     CPU Credits Remaining
CPU Credits Consumed      CPU Credits Consumed

Then we can use the value to get other metrics:

Get-AzureRmMetric -ResourceId $id -TimeGrain 00:01:00 -DetailedOutput -MetricNames "Network in"

Here a similar case about you, please refer to it.

By the way, difference about Azure Host metrics and Guest metrics, please refer to this link.

Related Topic