Collect data from virtual machines with Azure Monitor Agent
This article describes how to collect events and performance counters from virtual machines by using Azure Monitor Agent.
To collect data from virtual machines by using Azure Monitor Agent, you'll:
Create data collection rules (DCRs) that define which data Azure Monitor Agent sends to which destinations.
Associate the data collection rule to specific virtual machines.
You can associate virtual machines to multiple data collection rules. Define each data collection rule to address a particular requirement. Associate one or more data collection rules to a virtual machine based on the specific data you want the machine to collect.
Create data collection rule and association
To send data to Log Analytics, create the data collection rule in the same region as your Log Analytics workspace. You can still associate the rule to machines in other supported regions.
On the Monitor menu, select Data Collection Rules.
Select Create to create a new data collection rule and associations.
Enter a Rule name and specify a Subscription, Resource Group, Region, and Platform Type:
- Region specifies where the DCR will be created. The virtual machines and their associations can be in any subscription or resource group in the tenant.
- Platform Type specifies the type of resources this rule can apply to. The Custom option allows for both Windows and Linux types.
On the Resources tab, add the resources to which to associate the data collection rule. Resources can be virtual machines, virtual machine scale sets, and Azure Arc for servers. The Azure portal installs Azure Monitor Agent on resources that don't already have it installed. The portal also enables Azure Managed Identity.
The portal enables system-assigned managed identity on the target resources, along with existing user-assigned identities, if there are any. For existing applications, unless you specify the user-assigned identity in the request, the machine defaults to using system-assigned identity instead.
If you need network isolation using private links, select existing endpoints from the same region for the respective resources or create a new endpoint.
On the Collect and deliver tab, select Add data source to add a data source and set a destination.
Select a Data source type.
Select which data you want to collect. For performance counters, you can select from a predefined set of objects and their sampling rate. For events, you can select from a set of logs and severity levels.
Select Custom to collect logs and performance counters that aren't currently supported data sources or to filter events by using XPath queries. You can then specify an XPath to collect any specific values. For an example, see Sample DCR.
On the Destination tab, add one or more destinations for the data source. You can select multiple destinations of the same or different types. For instance, you can select multiple Log Analytics workspaces, which is also known as multihoming.
You can send Windows event and Syslog data sources to Azure Monitor Logs only. You can send performance counters to both Azure Monitor Metrics and Azure Monitor Logs.
Select Add data source and then select Review + create to review the details of the data collection rule and association with the set of virtual machines.
Select Create to create the data collection rule.
It might take up to 5 minutes for data to be sent to the destinations after you create the data collection rule and associations.
Filter events using XPath queries
Since you're charged for any data you collect in a Log Analytics workspace, you should limit data collection from your agent to only the event data that you need. The basic configuration in the Azure portal provides you with a limited ability to filter out events.
For strategies to reduce your Azure Monitor costs, see Cost optimization and Azure Monitor.
To specify more filters, use custom configuration and specify an XPath that filters out the events you don't need. XPath entries are written in the form
LogName!XPathQuery. For example, you might want to return only events from the Application event log with an event ID of 1035. The
XPathQuery for these events would be
*[System[EventID=1035]]. Because you want to retrieve the events from the Application event log, the XPath is
Extract XPath queries from Windows Event Viewer
In Windows, you can use Event Viewer to extract XPath queries as shown in the screenshots.
When you paste the XPath query into the field on the Add data source screen, as shown in step 5, you must append the log type category followed by an exclamation point (!).
For a list of limitations in the XPath supported by Windows event log, see XPath 1.0 limitations.
You can use the PowerShell cmdlet
Get-WinEvent with the
FilterXPath parameter to test the validity of an XPath query locally on your machine first. The following script shows an example:
$XPath = '*[System[EventID=1035]]' Get-WinEvent -LogName 'Application' -FilterXPath $XPath
- In the preceding cmdlet, the value of the
-LogNameparameter is the initial part of the XPath query until the exclamation point (!). The rest of the XPath query goes into the
- If the script returns events, the query is valid.
- If you receive the message "No events were found that match the specified selection criteria," the query might be valid but there are no matching events on the local machine.
- If you receive the message "The specified query is invalid," the query syntax is invalid.
Examples of using a custom XPath to filter events:
|Collect only System events with Event ID = 4648||
|Collect Security Log events with Event ID = 4648 and a process name of consent.exe||
|Collect all Critical, Error, Warning, and Information events from the System event log except for Event ID = 6 (Driver loaded)||
|Collect all success and failure Security events except for Event ID 4624 (Successful logon)||
Submit and view feedback for