Home Microsoft AzureAzure Log Analytics for Linux VMs: Query Guide

Azure Log Analytics for Linux VMs: Query Guide

by Knowledge Base Linux
Azure Log Analytics dashboards for Linux virtual machines

Azure Log Analytics Linux monitoring turns the syslog files and performance counters scattered across your virtual machines into a single queryable workspace, and learning its query language is what makes the difference between having logs and actually using them. A Linux VM without central logging is a black box after an incident: you SSH in, grep through rotated files, and hope the evidence is still there. With the Azure Monitor agent forwarding syslog, metrics, and heartbeat data to a Log Analytics workspace, you can ask questions across your whole fleet in seconds: which VMs stopped reporting, which ones are running out of disk, and which services failed overnight. This guide sets up the agent, explains the core tables, teaches the Kusto Query Language patterns you will use daily, and builds alerts that notify you before users do.

The architecture is straightforward once the names are clear. The Log Analytics workspace is the storage and query engine. The Azure Monitor agent, installed via a VM extension or Azure Arc for machines outside Azure, collects data according to a data collection rule, which says what to collect and where to send it. Syslog goes to the Syslog table, performance counters to Perf, heartbeat signals to Heartbeat, and custom logs to tables you define. The agent does not replace local logs; it copies selected data centrally, so the VM’s own journal and files remain your fallback. For related Azure monitoring, see our guide to Azure Monitor alerts for Linux VMs, which builds on the workspace created here.

Azure Log Analytics KQL query filtering Linux server logs

Prerequisites

You need an Azure subscription, a resource group, and at least one Linux VM running Ubuntu or another supported distribution, with the Azure Monitor agent extension installed or installable. You also need permission to create a Log Analytics workspace and data collection rules, which the Log Analytics Contributor and Monitoring Contributor roles provide. The examples use the Azure CLI, but every step is also available in the portal. You should be comfortable with basic Linux administration and with reading log output, because KQL queries are only useful if you know what a normal syslog line looks like for your services.

Azure Log Analytics Linux: Create a Workspace and Connect VMs

Create the workspace first, choosing a region close to your VMs to reduce latency and data transfer costs, and a retention period that matches your compliance needs. The default retention is 90 days for the pay-as-you-go tier, and you can set per-table retention for tables that grow fast, such as verbose syslog data. Creating the workspace alone collects nothing: you still need a data collection rule and an association that links the rule to your VMs.

az monitor log-analytics workspace create   --resource-group rg-monitoring   --workspace-name law-production   --location southeastasia   --retention-time 90

az monitor data-collection rule create   --resource-group rg-monitoring   --name dcr-linux   --location southeastasia   --rule-file dcr.json

The data collection rule file declares the data sources: syslog facilities and severities, performance counters, and the destination workspace. A practical starting rule collects syslog from auth, authpriv, daemon, and kern at warning level and above, plus CPU, memory, and disk counters every minute. Collecting everything at debug level is a cost and noise mistake: start narrow, prove the pipeline works, and widen only for services you are actively troubleshooting. Associate the rule with each VM, then wait a few minutes and confirm heartbeats arrive.

az monitor data-collection rule association create   --resource-group rg-monitoring   --rule dcr-linux   --name vm-association   --resource "/subscriptions/SUB/resourceGroups/rg-vms/providers/Microsoft.Compute/virtualMachines/web01"

Verify in the workspace that the Heartbeat table shows your VM reporting. A VM that never appears usually has an agent problem: check that the extension installed successfully, that the VM can reach the Azure Monitor endpoints, and that the data collection rule association exists. The official Microsoft documentation on collecting logs with Azure Monitor agent lists the supported syslog facilities and the data collection rule schema in full.

Azure Log Analytics Linux: Essential KQL Queries

KQL reads like a pipeline: start with a table, filter with where, summarise with summarize, and sort with sort by. The five queries below cover the daily operational questions for a Linux fleet, and they are worth saving as workspace queries or functions so the whole team uses the same definitions. Time filtering is built into every query through TimeGenerated, and you should always bound it: an unbounded query over 90 days of data is slow and expensive, while a query over the last hour is fast and focused.

// VMs that stopped reporting in the last 15 minutes
Heartbeat
| where TimeGenerated > ago(15m)
| summarize LastHeartbeat = max(TimeGenerated) by Computer
| where LastHeartbeat < ago(5m)

// Failed SSH logins in the last day
Syslog
| where TimeGenerated > ago(1d)
| where Facility == "auth"
| where SyslogMessage contains "Failed password"
| summarize Failures = count() by Computer, HostIP
| sort by Failures desc

// Disk usage above 85 percent
Perf
| where TimeGenerated > ago(1h)
| where ObjectName == "Logical Disk"
| where CounterName == "% Used Space"
| summarize MaxUsed = max(CounterValue) by Computer, InstanceName
| where MaxUsed > 85

Two habits make KQL queries reliable. First, use contains rather than == for syslog messages, because message formats vary by distribution and version, and an exact match that worked on Ubuntu 22.04 may miss on 24.04. Second, aggregate before you sort: summarize reduces millions of rows to a handful, and sorting the reduced set is fast, while sorting raw rows before aggregating is the most common cause of a slow query. For a deeper look at Azure storage patterns that complement this monitoring, see our guide to Azure Private Endpoints for Blob Storage.

Azure Log Analytics Linux: Alerts That Catch Real Problems

A query becomes an alert when you schedule it and define a threshold: run the failed-login query every 15 minutes, and alert when failures exceed a count you chose from your baseline, not from a guess. Start with alerts for the failures that are unambiguous: a VM that stops sending heartbeats, a disk that crosses 90 percent, and a service that logs repeated failures. Each alert should name the VM, the value, and the suggested first action in its description, because an alert that requires the reader to guess what to do next will be ignored at 3 a.m. Route alerts to an action group that notifies the on-call channel, and set a suppression window so a flapping condition does not flood the channel with duplicates.

az monitor scheduled-query create   --resource-group rg-monitoring   --name alert-disk-full   --scopes "/subscriptions/SUB/resourceGroups/rg-monitoring/providers/Microsoft.OperationalInsights/workspaces/law-production"   --condition "count > 0"   --evaluation-frequency 15m   --window-size 15m   --description "Disk usage above 85 percent on a Linux VM"

Review alert history monthly and retire alerts that never fire or that always fire: both are noise, and noise trains a team to ignore the channel. A healthy alert set is small, specific, and trusted, which is worth more than a large set that everyone mutes. Also consider cost controls at the workspace level: set a daily ingestion cap as a safety net against a runaway verbose service, but treat hitting the cap as an incident, not a saving, because a capped workspace stops collecting the data you need during the incident you are trying to diagnose.

Troubleshooting

When data does not appear, work through the pipeline in order: agent, association, rule, workspace. Check the agent extension status on the VM, confirm the data collection rule association exists for that VM’s resource ID, and confirm the rule’s data sources include the facility you are looking for. If heartbeats arrive but syslog does not, the facility or severity filter is almost always the cause: a rule collecting only error severity will never show informational messages, and a rule collecting only daemon will never show auth events. If queries return data but slowly, narrow the time range, add a filter on Computer or Facility before aggregating, and check whether a table’s retention or a daily cap is truncating the data you expect.

If an alert fires for the wrong VM or at the wrong time, inspect the alert query’s time window and aggregation: a query that summarises by hour but evaluates every 15 minutes can fire repeatedly for the same event, while a query without a Computer grouping can fire for the fleet when only one VM is affected. And if costs spike, use the Usage table to find which table grew and which computer sent the most data; the culprit is usually a verbose service logging at debug level or a performance counter collected every few seconds instead of every minute. Fix the collection rule, not the workspace, because deleting data after ingestion does not refund the ingestion cost.

Conclusion

Azure Log Analytics for Linux VMs is valuable precisely because it centralises the questions you already ask during incidents: which machines are alive, which are filling up, and what failed. Set up the workspace and agent with a narrow, deliberate collection rule, learn the handful of KQL patterns that answer those questions, save them as shared queries, and turn only the unambiguous ones into alerts. With that foundation, your next incident starts with a query across the fleet instead of an SSH session into one machine and a hope that the logs are still there.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.