If you know the command, the help messages are generally quite useful and detailed:
az monitor scheduled-query create --help
Here's one example based on a query that worked for me. It assumes you've already created a resource group, workspace and action group:
RESOURCE_GROUP="ResourceGroupName"
WORKSPACE_NAME="LogAnalyticsWorkspaceName"
ACTION_GROUP_NAME="ActionGroupName"
QUERY='AzureDiagnostics
| where Message contains "Connection successful"
| where TimeGenerated > ago(5m)
| order by TimeGenerated desc'
WORKSPACE_ID=$(az monitor log-analytics workspace show \
--resource-group $RESOURCE_GROUP \
--workspace-name $WORKSPACE_NAME \
--query id --out tsv)
az monitor scheduled-query create \
--name "TestScheduledQuery" \
--resource-group $RESOURCE_GROUP \
--scopes $WORKSPACE_ID \
--description "Test rule" \
--action $ACTION_GROUP_NAME \
--evaluation-frequency 5m \
--mute-actions-duration PT30M \
--severity 3 \
--condition "count 'QRY1' > 0" \
--condition-query QRY1="$QUERY"
This example will:
- run every 5 minutes (
--evaluation-frequency
)
- look for new diagnostics fitting the constraints that were generated in the last 5 minutes (
QUERY
)
- if the match count is greater than zero (
--condition
):
- it'll activate an alert (send a mail, etc. depending on the Action Group in
--action
)
- and once an alert has fired it will be muted for 30 minutes so that repeated alerts won't spam anyone (
--mute-actions-duration
)
Most of these settings are the defaults anyway, I've just defined them for clarity.