Duration: 20 minutes
In this exercise, I will collect data from an Azure virtual machine using Azure Monitor. I will deploy a virtual machine, create a Log Analytics workspace, enable the Log Analytics virtual machine extension, configure data collection, and view/query the collected data.
Prerequisites:
- An Azure subscription with Owner or Contributor role access.
- Basic knowledge of Azure virtual machines and Azure portal.
Sign in to the Azure portal at https://portal.azure.com/ using an account with Owner or Contributor role access to the Azure subscription.
Open the Cloud Shell by clicking the first icon in the top right of the Azure Portal. If prompted, select PowerShell and Create storage.
Ensure PowerShell is selected in the drop-down menu in the upper-left corner of the Cloud Shell pane.
Run the following command in the PowerShell session within the Cloud Shell pane to create a resource group that will be used in this lab:
New-AzResourceGroup -Name AZ500LAB131415 -Location 'South Central US'
Wait for the resource group creation to complete.
Run the following command in the PowerShell session within the Cloud Shell pane to create a new Azure virtual machine:
New-AzVm -ResourceGroupName "AZ500LAB131415" -Name "myVM" -Location 'EastUS' -VirtualNetworkName "myVnet" -SubnetName "mySubnet" -SecurityGroupName "myNetworkSecurityGroup" -PublicIpAddressName "myPublicIpAddress" -PublicIpSku Standard -OpenPorts 80,3389 -Size Standard_DS1_v2
When prompted for credentials, use the following:
User: | localadmin |
---|---|
Password: | Use a password that can be remembered easily |
![Create Resource Group and Virtual Machine in PS1(2)](https://private-user-images.githubusercontent.com/23303634/243204358-fcd85178-a242-428e-840c-3db5e89ff5bd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDQzNTgtZmNkODUxNzgtYTI0Mi00MjhlLTg0MGMtM2RiNWU4OWZmNWJkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNhMmRiZGMxZjcwZWJlZTg5MGMzYjgyZmE4N2E5NGQ1MDIwOTVkMGI1YjZmNjg4ZjQzOTg2ZGIyNjU2OTA5YzMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.qE2UvHiE2mIWELo0sOsQs2Ud28ce-hkiEx3CliN_0cg)
Wait for the virtual machine deployment to complete.
Run the following command to confirm that the virtual machine named "myVM" was created and its ProvisioningState is "Succeeded":
Get-AzVM -Name 'myVM' -ResourceGroupName 'AZ500LAB131415' | Format-Table
![Successfully Deployed VM](https://private-user-images.githubusercontent.com/23303634/243204607-939cb4b4-595e-4e85-9142-fabfbfa313cc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDQ2MDctOTM5Y2I0YjQtNTk1ZS00ZTg1LTkxNDItZmFiZmJmYTMxM2NjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTczOWNmMjAxY2FkMDBkZmRjMzI0ODA0NTczMzA0N2VhZGRlYmUxOWRlNmFhZDIzZDM0NDM5ZWYyNTlhOTEyMDEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.jXf_wcvM7PLRSSmpfY4sKnLK9-SFjyfbFZyaPP-44j4)
Close the Cloud Shell pane.
In the Azure portal, use the search box at the top to search for "Log Analytics workspaces" and press Enter.
On the Log Analytics workspaces blade, click "+ Create."
On the Basics tab of the Create Log Analytics workspace blade, specify the following settings:
Subscription: | Select the name of the Azure subscription you are using in this lab. |
---|---|
Resource group: | AZ500LAB131415 |
Name: | Enter any valid, globally unique name for the Log Analytics workspace. |
Region: | South Central US |
![Create Log Analytics Workspace](https://private-user-images.githubusercontent.com/23303634/243205037-e2811af8-4f3f-414c-8343-446c7a03bad3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDUwMzctZTI4MTFhZjgtNGYzZi00MTRjLTgzNDMtNDQ2YzdhMDNiYWQzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTA3MTQyMjkyMzhiYTQxYjE1NTVkMTEzMmIyMTRkY2YyNjQ4YzJkZjc0MzI5ZDg5ZmY5ZjdlZDc3MDNlNjNlOWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ebRua-zYU3U9ktqr50aamY8t7ZJkk9R9cOj6G7aztLw)
Select "Review + create."
On the Review + create tab of the Create Log Analytics workspace blade, click "Create" to start the workspace creation.
Wait for the Log Analytics workspace creation to complete.
In the Azure portal, navigate back to the Log Analytics workspaces blade.
In the list of workspaces, click the entry representing the workspace you created in the previous task.
On the Log Analytics workspace blade, on the Overview page, in the "Connect a Data Source" section, click the "Azure Virtual machines (VMs)" entry.
![Connect a Data Source (VM)](https://private-user-images.githubusercontent.com/23303634/243206896-c98d3f03-922f-4754-915f-db477aaea4ae.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDY4OTYtYzk4ZDNmMDMtOTIyZi00NzU0LTkxNWYtZGI0NzdhYWVhNGFlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM1N2M3Y2NjNGYzNDAyOGEwMmQ2NWE1NmZiMGI3M2IzMjgzMzRiZjEyNGYwZGE1NjMxOTU3NWZmOTc5ZjcxZTgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.7K8NZmkF-Lrw5wql4AlP9Q-RIjBRFO8ZeEB-yWXyreU)
Note: For the agent to be successfully installed, the virtual machine must be running.
In the list of virtual machines, locate the entry representing the Azure VM "myVM" you deployed in Task 1 and note that it is listed as "Not connected."
Click the "myVM" entry and then, on the "myVM" blade, click "Connect."
![Connect VM to LAW](https://private-user-images.githubusercontent.com/23303634/243205528-5061f334-44b3-41fe-9821-02446b8eaff1.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDU1MjgtNTA2MWYzMzQtNDRiMy00MWZlLTk4MjEtMDI0NDZiOGVhZmYxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ2MTExYTkxN2FhYWQ4ZjZmMWRmMGI3NGFmZjY0MGY4NmY2ZDI4MDcwMDliZjU4Mjk4ZDAzYWUxZjAxM2Q4OTEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.zmWgbyJFCLqfBt9AOWIx7hX0D4kOMgFGe5uJUydAtV0)
Wait for the virtual machine to connect to the Log Analytics workspace. The status displayed on the "myVM" blade will change from "Connecting" to "This workspace."
![Connected VM to LAW](https://private-user-images.githubusercontent.com/23303634/243205763-be9c6b5e-3a4a-45cc-a2ad-37fe2a7c1d7b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDU3NjMtYmU5YzZiNWUtM2E0YS00NWNjLWEyYWQtMzdmZTJhN2MxZDdiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWM0NTE5YTY2ZDVkNTlhYmY2MThlNjBlZTc5YWE0YjhmZTA4YTNmNzJlNGRiNTdlMmM2ZjQ1NGRkZGRmZDViZDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.998JbcwEnRS54_CC317VI3dYtjDxjcp1M3LwSt_yo1U)
Note: This may take a few minutes. The Status displayed on the myVM blade, will change from Connecting to This workspace.
In the Azure portal, navigate back to the Log Analytics workspace you created earlier in this exercise.
On the Log Analytics workspace blade, in the "Classic" section, click "Legacy agents management."
On the Agents configuration blade, review the configurable settings including Windows Event Logs, Windows Performance Counters, Linux Performance Counters, IIS Logs, and Syslog.
Ensure that "Windows Event Logs" is selected, click "+ Add windows event log," and in the listing of event log types, select "System."
![Add Windows Event Logs](https://private-user-images.githubusercontent.com/23303634/243206843-7aa92297-42ad-4b16-8230-8f01a19647dc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDY4NDMtN2FhOTIyOTctNDJhZC00YjE2LTgyMzAtOGYwMWExOTY0N2RjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWIyNzEwYTBlZWMwZjM4MzdhMTY3OTk1NGM0N2QwZTM1MTFiOWE0MTIwMGEzYjdmODUwNjI0YzE4MTQzZTE2NjAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.-j987SPLqYrau9EBTi6jUaTJEqq_AJdJ2Wmg4f4pnH0)
Note: This is how you add event logs to the workspace. Other choices include, for example, Hardware events or Key Management Service.
Deselect the "Information" checkbox, leaving the "Error" and "Warning" checkboxes selected.
Click "Windows Performance Counters," click "+ Add performance counter," review the listing of available performance counters, and add the following ones:
-
Memory(*)\Available Memory Mbytes
-
Process(*)% Processor Time
-
Event Tracing for Windows\Total Memory Usage — Non-Paged Pool
-
Event Tracing for Windows\Total Memory Usage — Paged Pool
Note: The counters are added and configured with a 60-second collection sample interval.
On the Agents configuration blade, click "Apply."
![Add Performance Counter](https://private-user-images.githubusercontent.com/23303634/243206958-c5831563-6c27-4e0c-bc10-dc193afc18b1.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDY5NTgtYzU4MzE1NjMtNmMyNy00ZTBjLWJjMTAtZGMxOTNhZmMxOGIxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQ3OTIwYjJkNDEyM2FlYjhkMTEwMzliZGY5Mzk0MTdmZDBlMTkzMGY3NzE1MTNkMzFmNWQxY2Y2NmUxMThhOWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.PqhDWbBZDvOcfx-Ucm7O0Z91n2y-_bksi3qRrU9R5PY)
In the Azure portal, navigate back to the Log Analytics workspace you created earlier in this exercise.
On the Log Analytics workspace blade, in the "General" section, click "Logs."
If needed, close the "Welcome to Log Analysis" window.
On the Queries pane, in the "All Queries" column, scroll down to the bottom of the list of resource types, and click "Azure Monitor."
Review the list of predefined queries, select "Memory and CPU usage," and click the corresponding "Run" button.
![Run Memory and CPU Usage](https://private-user-images.githubusercontent.com/23303634/243208334-2bfb1374-ca95-408e-a9e4-d401896560b3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMDgzMzQtMmJmYjEzNzQtY2E5NS00MDhlLWE5ZTQtZDQwMTg5NjU2MGIzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTMzNjJiNmFhOWYyZmE1NDdmY2RlOTJjMTlkZTQ3MGZkYmUzNTJmOWE4MzQ3NTZlNGU5MThmYTRmMTVmNTA1MDMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.cbmDQqZa6v7zesXPRWndu_n7-2at9M-a5StUNj4leVE)
Note: You can start with the query "Virtual machine available memory." If you don't get any results, check that the scope is set to "virtual machine."
The query will automatically open in a new query tab. Note: Log Analytics uses the Kusto query language. You can customize the existing queries or create your own.
![Query Results for Memory and CPU Usage](https://private-user-images.githubusercontent.com/23303634/243214254-4381c4fe-72e8-4000-9c4a-daf9f5fae734.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMzk1MzgsIm5iZiI6MTcyNDEzOTIzOCwicGF0aCI6Ii8yMzMwMzYzNC8yNDMyMTQyNTQtNDM4MWM0ZmUtNzJlOC00MDAwLTljNGEtZGFmOWY1ZmFlNzM0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA3MzM1OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTBmYWQ1ZGE5MmFiNzBjZDA0ZTBmOGY0ZTRkY2U0NWYyYTRlZDdhOThiZWI4N2FjNGZlOTNlYmZlYTY2ZTRlMzAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.eyT19pI2FM603IMtpCp7XMCRrLQxkwrY7xcWqsDGQ9s)
Kusto Query Language (KQL) that was run for the Memory and CPU Usage:
// Memory and CPU usage
// Chart all computers' used memory and CPU, over the last hour.
Perf
| where TimeGenerated > ago(1h)
| where (CounterName == "% Processor Time" and InstanceName == "_Total") or CounterName == "% Used Memory"
| project TimeGenerated, CounterName, CounterValue
| summarize avg(CounterValue) by CounterName, bin(TimeGenerated, 1m)
| render timechart
Note: The results of the query you selected are automatically displayed below the query pane. To re-run the query, click "Run."
Note: Since this virtual machine was just created, there may not be any data yet.
Note: You have the option of displaying data in different formats. You also have the option of creating an alert rule based on the results of the query.
Optional: Generate additional load on the Azure VM to collect more data:
Navigate to the Azure VM blade. In the Operations section, select "Run command." On the "RunPowerShellScript" blade, run the following script:
cmd
:loop
dir c:\ /s > SWAP
goto loop
Switch back to the Log Analytics blade and re-run the query. You might need to wait a few minutes for data to be collected before re-running the query.
This exercise provided hands-on experience with Azure Monitor and Log Analytics for data collection and analysis. It familiarized me with deploying virtual machines, creating workspaces, connecting virtual machines to workspaces, configuring data collection settings, and running queries for analysis. It emphasized the significance of monitoring data to optimize performance and make informed decisions.
This exercise demonstrated the process of collecting data from an Azure virtual machine using Azure Monitor and Log Analytics. We deployed a virtual machine, created a Log Analytics workspace, enabled the Log Analytics virtual machine extension, configured data collection settings, and viewed/queried the collected data. It highlighted the importance of monitoring and analyzing data to gain insights into virtual machine performance and behavior.
Continue to Microsoft Defender for Cloud