Resource Manager template samples for log queries in Azure Monitor
This article includes sample Azure Resource Manager templates to create and configure log queries in Azure Monitor. Each sample includes a template file and a parameters file with sample values to provide to the template.
Note
See Azure Resource Manager samples for Azure Monitor for a list of samples that are available and guidance on deploying them in your Azure subscription.
Template references
Simple log query
The following sample adds a log query to a Log Analytics workspace.
Template file
@description('The name of the workspace.')
param workspaceName string
@description('The location of the resources.')
param location string = resourceGroup().location
resource workspace 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: workspaceName
location: location
}
resource savedSearch 'Microsoft.OperationalInsights/workspaces/savedSearches@2020-08-01' = {
parent: workspace
name: 'VMSS query'
properties: {
etag: '*'
displayName: 'VMSS Instance Count'
category: 'VMSS'
query: 'Event | where Source == "ServiceFabricNodeBootstrapAgent" | summarize AggregatedValue = count() by Computer'
version: 1
}
}
Parameter file
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"workspaceName": {
"value": "MyWorkspace"
},
"location": {
"value": "eastus"
}
}
}
Log query as a function
The following sample adds a log query as a function to a Log Analytics workspace.
Template file
@description('The name of the workspace.')
param workspaceName string
@description('The location of the resources.')
param location string = resourceGroup().location
resource workspace 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: workspaceName
location: location
}
resource savedSearch 'Microsoft.OperationalInsights/workspaces/savedSearches@2020-08-01' = {
parent: workspace
name: 'VMSS query'
properties: {
etag: '*'
displayName: 'VMSS Instance Count'
category: 'VMSS'
query: 'Event | where Source == "ServiceFabricNodeBootstrapAgent" | summarize AggregatedValue = count() by Computer'
version: 1
}
}
Parameter file
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"workspaceName": {
"value": "MyWorkspace"
},
"location": {
"value": "eastus"
}
}
}
Parameterized function
The following sample adds a log query as a function that uses a parameter to a Log Analytics workspace. A second log query is included that uses the parameterized function.
Note
Resource template is currently the only method that can be used to parameterized functions. Any log query can use the function once it's installed in the workspace.
Template file
param workspaceName string
param location string
resource workspace 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' = {
name: workspaceName
location: location
}
resource parameterizedFunctionSavedSearch 'Microsoft.OperationalInsights/workspaces/savedSearches@2020-08-01' = {
parent: workspace
name: 'Parameterized function'
properties: {
etag: '*'
displayName: 'Unavailable computers function'
category: 'Samples'
functionAlias: 'UnavailableComputers'
functionParameters: 'argSpan: timespan'
query: ' Heartbeat | summarize LastHeartbeat=max(TimeGenerated) by Computer| where LastHeartbeat < ago(argSpan)'
}
}
resource queryUsingFunctionSavedSearch 'Microsoft.OperationalInsights/workspaces/savedSearches@2020-08-01' = {
parent: workspace
name: 'Query using function'
properties: {
etag: '*'
displayName: 'Unavailable computers'
category: 'Samples'
query: 'UnavailableComputers(7days)'
}
}
Parameter file
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"workspaceName": {
"value": "MyWorkspace"
},
"location": {
"value": "eastus"
}
}
}