Enabling Azure VM memory metrics using Azure CLI for Linux VMs

Document created by richard.stinton on Jan 21, 2020
Version 1Show Document
  • View in full screen mode

This document explained how to enable basic memory metrics in Azure VMs manually.  When you have more than a few this can be quite tedious. For Linux admins, Azure CLI enables command line access to the Azure API to automate things.  This article in the Azure documentation provides a method to automate this process on a per-VM basis. I've taken the opportunity to take this a step further by allowing the script to read a list of VMs that can be generated using another AZ CLI command.


Azure CLI is available for Windows, Linux and MacOS. I've installed it successfully on MacOS using Homebrew.


The simple command # az vm list will produce a JSON-formatted output of all the VMs running in your subscription. This can be refined using various filters or switches. To get a nice list of just VM names:


# az vm list --query "[?storageProfile.osDisk.osType=='Linux'].{Name:name}" -o tsv


Additional filters can be added to reduce the scope as required, for example -g resource_group


In the archive.zip attached there is in a small script called GetListofLinuxVMs.sh that will write to a file called Linux_VMs.txt


The main script, EnableLinuxDiags.sh will read this file of VM names.  At the top of the script are some variables that need to be set for the following:

  • my_resource_group
  • my_diagnostic_storage_account
  • my-subscription


Depending on the environment, the diagnostic storage accounts may be distributed, so you'll need to check.


The script can be executed simply by typing # ./EnableLinuxDiags.sh


The script will initially download a JSON file of basic metrics to enable, called portal_public_settings.json


There is an example in the ZIP file of the script running (linux_example.txt)


Usual disclaimer: While I've tested this in my Azure subscription, I can take no responsibility for running in other accounts/subscriptions.