Monitoring Azure VMs (Virtual Machines) Performance Metrics using Azure Log Analytics

Akash Patel
6 min readDec 28, 2021

Azure Insights & Log Analytics for Azure Services (Virtual Machines) — Write Analytics queries and Generate Dashboard using Log Analytics WS

Azure Monitor + Azure Log Analytics Workspace (https://www.cloudsma.com/2020/04/overview-azure-monitor-sentinel-security-center/)

Let’s say we are running VM Lab in the multiple Environment — Dev, UAT, and PROD, etc. We have multiple VMs up and running and need to monitor the activities and processes which are running on the VM, memory, and disk available or not, set up and alert when memory reaches its 90% capacity, VM performance metrics, and many more… Azure has centralized Log — Azure Monitor & Log Analytics Workspace services which collects log and run analytical queries on top of that. So, without waiting more let's roll up our sleeves and explore these services.

Here are the high-level steps & topics we are going to cover in this article.

  • Create a Resource Group
  • Create a Virtual Machine
  • Create a Log Analytics Workspace
  • Write VMInsights Queries and Save
  • Generate Dashboard and Publish it.

Go to https://portal.azure.com/

Azure Services -> Click on “Create a Resource” and then find the Windows Server, and click Create

Basic tab

Resource Group Name: vm-lab

Instance details
Virtual machine name: devvm1

Administrator account
Username : abc1578
Password: enter your own password
Confirm password: Re-enter the same password

Keep default values as it is. Click on Next.

Disks

Disk options
OS disk type: Standard HDD

Keep default values as it is. Click on Next.

Networking

Network interface
Let’s create or select existing Virtual Network, Subnet, and Public IP
Virtual Network: vm-lab-vnet
Subnet:default
Public IP:devvm1-ip

Management

Keep default values as it is. Click on Next.

Advanced

Keep default values as it is. Click on Next.

Tags

Add Key: product
Add Value : vmlab

Click on Next: Review + Create. After the validation completes we can click on create and it will create the Azure VM resources.

Now let’s go to the Resource Group — vm-lab we have just created. We can see the following resources.

Now let’s create Log Analytics Workspace :

Search: Log Analytics Workspace, a click on Create

once the following windows is available, we can fill the values in the Basics and Tags :

Basics

Name : dev-log-analytics-ws

Tags

Tags

Add Key: product
Add Value : vmlab

Click on Next: Review + Create. After the validation completes we can click on create and it will create the Azure VM resources.

In the Azure portal search bar: type Monitor, and then click on it.
Once you reached the Azure Monitor Home page, click on the Virtual Machine.

Select the Other onboarding options, and then Configure a workspace. Select a subscription and a workspace and then click Configure. After the successful agent configuration installation, it will look like the following image.

Note: Here are three ways to configure VMinsights workspace
https://docs.microsoft.com/en-us/azure/azure-monitor/vm/vminsights-configure-workspace?tabs=CLI

Now, Let’s go back to the VM: devvm1.

On the left panel — Go to the Monitoring section and then click on the insights.
Here we can see the basic/standard metrics are available. (If you don’t see these charts/matrics then it could be some misconfiguration, VM is not running, etc. It takes 5-10 mins to start publishing metrics after the VM started and logs flowing.. )

We are seeing CPU utilization, Disks space, and available memory metrics.

Now let’s go further deeper and check how many processes are running in the Virtual Machine :

Go to the Log Analytics workspace — dev-log-analytics-ws and then on the left panel go to Settings. Then click on the Agents configuration.

Click on the Recommended Process Counters to add the standard Performance counters. Also, we can add special Performance counters and set their frequency.

Performance counter name : Process(*)\% Processor Time
Sample rate : 60

Click Apply. After 5–10 min, This will start flowing VM Process details to the Azure monitor.

Let’s write a query and check how many processes are running on Windows Virtual Machine.

Go to Log Analytics workspace — dev-log-analytics-ws, and click on the Overview, then click on the View logs.

Here, we can write queries: The highlighted tables can be used to get insights.

Perf
| where ObjectName == “Process” and CounterName == “% Processor Time”
| summarize count() by TimeGenerated
| render columnchart

Here is the output of the query: As we have selected frequency 60 sec, it generates records per min.

Here are some bonus queries you can try :

// Chart CPU usage trends by computer

// Calculate CPU usage patterns over the last hour, chart by percentiles.
InsightsMetrics
| where TimeGenerated > ago(1h)
| where Origin == “vm.azm.ms”
| where Namespace == “Processor”
| where Name == “UtilizationPercentage”
| summarize avg(Val) by bin(TimeGenerated, 5m), Computer //split up by computer
| render timechart

// Virtual Machine free disk space
// Show the latest report of free disk space, per instance.
InsightsMetrics
| where TimeGenerated > ago(1h)
| where Origin == “vm.azm.ms”
| where Namespace == “LogicalDisk”
| where Name == “FreeSpaceMB”
| extend t=parse_json(Tags)
| summarize arg_max(TimeGenerated, *) by tostring(t[“vm.azm.ms/mountId”]), Computer // arg_max over TimeGenerated returns the latest record
| project Computer, TimeGenerated, t[“vm.azm.ms/mountId”], Val

// Virtual Machine available memory.
InsightsMetrics
| where TimeGenerated > ago(1h)
| where Origin == “vm.azm.ms”
| where Namespace == “Memory”
| where Name == “AvailableMB”
| summarize avg(Val) by bin(TimeGenerated, 5m), Computer
| render timechart

Once you are satisfied with the queries we can save them. Also “Pin to Dashboard” feature generates a Dashboard for automated monitoring.

Congratulation!! 🥂 Now, you can use these learnings as a seed for your next project. Automate monitoring process for other Azure resources and customize based on your requirement. Good Luck!

Reference :
https://docs.microsoft.com/en-us/azure/azure-monitor/agents/data-sources-performance-counters
https://docs.microsoft.com/en-us/azure/azure-monitor/vm/vminsights-configure-workspace?tabs=CLI
https://www.cloudsma.com/2020/04/overview-azure-monitor-sentinel-security-center/

--

--