Lesson 3 of 5 Monitoring Resources with Azure Monitor

Querying Logs with Kusto Query Language

This lesson explains the structure of KQL queries, common tables and operators, and how queries are used.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the structure of a KQL query.
  • Explain the purpose of common tables and operators.
  • Interpret simple KQL queries.

Query structure

A KQL query starts with a table name and passes rows through a series of operators separated by the pipe character. Each operator takes the output of the previous one. Queries run on the Logs page of a workspace (all data in it) or of a resource (only data about that resource), within the time range picker unless the query filters TimeGenerated itself. Common tables include those below.

TableContents
AzureActivityActivity log: control plane operations and who performed them
HeartbeatOne row per minute from each machine with the Azure Monitor Agent
Perf / InsightsMetricsPerformance counters from VMs (Perf from DCRs, InsightsMetrics from VM insights)
Event / SyslogWindows event logs and Linux syslog
AzureDiagnostics and resource-specific tablesResource logs, such as StorageBlobLogs or AzureFirewallNetworkRule
SigninLogs / AuditLogsMicrosoft Entra sign-ins and directory changes
Match the pairs

Which table?

Exam skills 5.1.3

Match each question with the table that holds the answer.

Log in to try this activity Interactive activities are free with an account.

Essential operators

where filters rows by a condition; project chooses and renames columns; extend adds calculated columns; summarize aggregates with functions such as count(), avg(), max(), dcount() and arg_max(), grouped by columns or time bins (bin(TimeGenerated, 1h)); sort by and top order results; take returns sample rows; join and union combine tables; and render draws a chart. ago(1d) and now() express relative times. The following queries are typical administration examples.

// Who deleted resources in the last 7 days?
AzureActivity
| where TimeGenerated > ago(7d)
| where OperationNameValue endswith "DELETE" and ActivityStatusValue == "Success"
| project TimeGenerated, Caller, ResourceGroup, _ResourceId

// Average CPU per computer in 15-minute bins, as a chart
Perf
| where ObjectName == "Processor" and CounterName == "% Processor Time"
| summarize AvgCPU = avg(CounterValue) by Computer, bin(TimeGenerated, 15m)
| render timechart

// Machines that have not reported for 15 minutes
Heartbeat
| summarize LastSeen = max(TimeGenerated) by Computer
| where LastSeen < ago(15m)

// Failed storage requests by operation and status
StorageBlobLogs
| where StatusCode >= 400
| summarize Failures = count() by OperationName, StatusText
| sort by Failures desc

The following diagram shows a KQL query that counts Windows error events per computer, and the shape of its result.

Reading a KQL query. It returns one row per computer that logged more than 50 Windows error events in the last 24 hours, with the count, sorted from most to fewest. The Event table holds data only if the VMs run the Azure Monitor Agent with a data collection rule collecting Windows event logs at the Error level into the workspace.
The result rows are illustrative.
Put in order

Building a query

Exam skills 5.1.3

Arrange the lines of a query that charts the average CPU per computer for the last day.

Log in to try this activity Interactive activities are free with an account.

Using query results

Queries can be saved in a workspace or in query packs, pinned to dashboards, added to workbooks with parameters and visualizations, exported to Excel or Power BI, and used as the condition of log search alert rules, which run a query on a schedule and fire when the number of results or a measured value crosses a threshold. The portal's Queries hub offers examples for each resource type, and simple mode builds queries without writing KQL.

True or false

Using queries

Exam skills 5.1.3

Check what can be done with KQL query results.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reading a query

An administrator runs the query Event | where EventLevelName == "Error" | summarize count() by Computer | where count_ > 50 | sort by count_ desc over the last 24 hours. What does it return, and what must be in place for the Event table to contain data?

It returns one row per computer that logged more than 50 Windows error events in the period, with the count, sorted from most to fewest. The Event table is populated only if the VMs have the Azure Monitor Agent and a data collection rule that collects Windows event logs at the Error level and sends them to this workspace.

Check yourself

Which KQL operator aggregates rows, for example counting events per computer?

Show answer

summarize.

Exam Tips

Remember for the exam

  • A query starts with a table and pipes rows through operators.
  • summarize with bin(TimeGenerated, interval) creates time series.
  • AzureActivity answers who changed or deleted a resource.
  • Heartbeat shows whether machines are reporting.