LAB 11 ~75 phút Module 11 Monitoring 2026

Azure Monitor & Alerts — AMA + DCR

Tạo Log Analytics workspace, triển khai Azure Monitor Agent (AMA) thay thế MMA đã retire, cấu hình Data Collection Rule, tạo metric/log alert với action group email, truy vấn KQL cơ bản.

Cập nhật quan trọng 2026: MMA đã retire

Microsoft Monitoring Agent (MMA/OMS Agent) đã ngừng nhận data từ 02/03/2026 và chính thức retire 31/08/2024. Lab này sử dụng Azure Monitor Agent (AMA) + Data Collection Rules (DCR) — kiến trúc chuẩn hiện tại. Không dùng "Enable Guest-level monitoring" hay Log Analytics workspace connection kiểu cũ nữa.

🎯 Mục Tiêu Lab

Tạo Log Analytics Workspace và đăng ký resource providers cần thiết

Cài Azure Monitor Agent (AMA) lên VM bằng VM Extension

Tạo Data Collection Rule (DCR) thu thập Windows Event Logs và Performance Counters

Tạo Action Group gửi email khi alert trigger

Tạo Metric Alert (CPU >80%) và Log Alert (Event ID 4625 — failed login)

Viết và chạy KQL query cơ bản trong Log Analytics

📋 Chuẩn Bị

Yêu cầu:
  • Azure subscription với quyền Contributor
  • Azure CLI hoặc Cloud Shell
  • VM Windows Server 2022 (tạo trong lab)
  • Email để nhận alert notification
Resource Providers cần đăng ký:
  • Microsoft.Insights
  • Microsoft.AlertsManagement
  • Microsoft.Monitor
  • Đăng ký bằng CLI ở bước đầu tiên

🏗️ Kịch Bản & Tài Nguyên

HoaTranLab cần giám sát VM production: cảnh báo khi CPU cao, thu thập Windows Event Logs để phát hiện failed login, và có dashboard để theo dõi performance memory theo thời gian thực.

Resource Group
rg-lab11-monitor
Log Analytics WS
law-hoatranlab-lab11
VM được monitor
vm-lab11-win
Data Collection Rule
dcr-lab11-windows
Action Group
ag-lab11-email
Region
southeastasia

🧪 Các Bước Thực Hiện

1

Tạo Log Analytics Workspace & Đăng Ký Providers

Cách 1 — Azure Portal
  1. 1.1Subscriptions → Resource providers → tìm và Register: Microsoft.Insights, Microsoft.AlertsManagement, Microsoft.Monitor
  2. 1.2Search "Log Analytics workspaces" → Create → Name: law-hoatranlab-lab11 → Southeast Asia → Pay-as-you-go
  3. 1.3Review + Create → chờ deployment → vào workspace → ghi lại Workspace ID và Primary Key
Cách 2 — Azure CLI
Azure CLI
# Đăng ký resource providers
az provider register --namespace Microsoft.Insights
az provider register --namespace Microsoft.AlertsManagement
az provider register --namespace Microsoft.Monitor

# Tạo resource group
az group create \
  --name rg-lab11-monitor \
  --location southeastasia

# Tạo Log Analytics Workspace
az monitor log-analytics workspace create \
  --resource-group rg-lab11-monitor \
  --workspace-name law-hoatranlab-lab11 \
  --location southeastasia \
  --sku PerGB2018

# Lấy Workspace ID
az monitor log-analytics workspace show \
  --resource-group rg-lab11-monitor \
  --workspace-name law-hoatranlab-lab11 \
  --query customerId --output tsv
2

Tạo VM & Cài Azure Monitor Agent (AMA)

AMA được cài qua VM Extension — không cần tải agent thủ công. AMA thay thế hoàn toàn MMA (Log Analytics Agent cũ).

Cách 1 — Azure Portal
  1. 2.1Tạo VM: Windows Server 2022 → vm-lab11-win → Standard_B2s → Southeast Asia
  2. 2.2VM → Settings → Extensions + applications → Add → Azure Monitor Agent → Install
  3. 2.3Chờ extension state: Provisioning succeeded (1–3 phút)
  4. 2.4VM cần có System-assigned Managed Identity: VM → Identity → Status: On (AMA yêu cầu)
Cách 2 — Azure CLI
Azure CLI
# Tạo VM với System-assigned Identity (AMA yêu cầu)
az vm create \
  --resource-group rg-lab11-monitor \
  --name vm-lab11-win \
  --image Win2022AzureEditionCore \
  --size Standard_B2s \
  --admin-username azureadmin \
  --admin-password "P@ssw0rd2026!" \
  --public-ip-sku Standard \
  --assign-identity "[system]"

# Cài Azure Monitor Agent (AMA) qua extension
az vm extension set \
  --resource-group rg-lab11-monitor \
  --vm-name vm-lab11-win \
  --name AzureMonitorWindowsAgent \
  --publisher Microsoft.Azure.Monitor \
  --version 1.22 \
  --settings '{"workspaceId":""}' \
  --enable-auto-upgrade true

# Kiểm tra extension đã cài
az vm extension list \
  --resource-group rg-lab11-monitor \
  --vm-name vm-lab11-win \
  --output table
3

Tạo Data Collection Rule (DCR)

DCR định nghĩa dữ liệu nào được thu thập, từ nguồn nào (VM), và gửi đến đâu (Log Analytics Workspace). Đây là kiến trúc chuẩn thay thế workspace-based collection cũ.

Cách 1 — Azure Portal
  1. 3.1Monitor → Data Collection Rules → Create → Name: dcr-lab11-windows → Southeast Asia → Platform: Windows
  2. 3.2Resources tab → Add resources → chọn vm-lab11-win (AMA sẽ được associate)
  3. 3.3Collect and deliver tab → Add data source → Data source type: Windows Event Logs → Basic → chọn: Application (Critical, Error), Security (Audit failure), System (Critical, Error)
  4. 3.4Destination: Azure Monitor Logs → Destination type: Log Analytics workspace → chọn law-hoatranlab-lab11 → Save
  5. 3.5Add data source nữa → Performance Counters → Basic → chọn CPU, Memory, Disk → Sample rate: 60 seconds → Destination: cùng workspace → Save → Review + Create
Cách 2 — Azure CLI
Azure CLI
# Lấy workspace resource ID
WS_ID=$(az monitor log-analytics workspace show \
  --resource-group rg-lab11-monitor \
  --workspace-name law-hoatranlab-lab11 \
  --query id --output tsv)

# Tạo DCR (Data Collection Rule) thu thập Windows Events + Perf
az monitor data-collection rule create \
  --resource-group rg-lab11-monitor \
  --name dcr-lab11-windows \
  --location southeastasia \
  --data-flows '[{
    "streams": ["Microsoft-Event","Microsoft-Perf"],
    "destinations": ["law-hoatranlab-lab11"]
  }]' \
  --destinations '{
    "logAnalytics": [{
      "workspaceResourceId": "'$WS_ID'",
      "name": "law-hoatranlab-lab11"
    }]
  }' \
  --data-sources '{
    "windowsEventLogs": [{
      "name": "eventLogsDataSource",
      "streams": ["Microsoft-Event"],
      "xPathQueries": [
        "Application!*[System[(Level=1 or Level=2)]]",
        "Security!*[System[(band(Keywords,13510798882111488))]]",
        "System!*[System[(Level=1 or Level=2)]]"
      ]
    }],
    "performanceCounters": [{
      "name": "perfCounters",
      "streams": ["Microsoft-Perf"],
      "samplingFrequencyInSeconds": 60,
      "counterSpecifiers": [
        "\\Processor Information(_Total)\\% Processor Time",
        "\\Memory\\Available Bytes",
        "\\LogicalDisk(_Total)\\% Free Space"
      ]
    }]
  }'
4

Tạo Action Group & Alert Rules

Cách 1 — Azure Portal

Action Group:

  1. 4.1Monitor → Alerts → Action groups → Create → Group name: ag-lab11-email → Display name: EmailAlert
  2. 4.2Notifications tab → Notification type: Email/SMS/Push/Voice → Email: nhập địa chỉ của bạn → OK → Review + Create

Metric Alert (CPU >80%):

  1. 4.3Monitor → Alerts → Create alert rule → Scope: vm-lab11-win
  2. 4.4Condition: Signal = Percentage CPU → Threshold: Static → Operator: Greater than → 80 → Aggregation: Average → Period: 5 minutes
  3. 4.5Actions: Select action group ag-lab11-email → Severity: 2 – Warning → Alert rule name: alert-cpu-high → Create

Log Alert (Failed Login — Event 4625):

  1. 4.6Create alert rule → Scope: law-hoatranlab-lab11 → Condition: Custom log search
  2. 4.7Query: xem bên phải → Threshold: count > 5 trong 5 phút → Actions: ag-lab11-email → Severity: 1 – Error
Cách 2 — Azure CLI
Azure CLI
# Tạo Action Group với email notification
az monitor action-group create \
  --resource-group rg-lab11-monitor \
  --name ag-lab11-email \
  --short-name EmailAlert \
  --action email admin [email protected]

# Lấy VM resource ID
VM_ID=$(az vm show \
  --resource-group rg-lab11-monitor \
  --name vm-lab11-win \
  --query id --output tsv)

AG_ID=$(az monitor action-group show \
  --resource-group rg-lab11-monitor \
  --name ag-lab11-email \
  --query id --output tsv)

# Tạo Metric Alert: CPU > 80%
az monitor metrics alert create \
  --resource-group rg-lab11-monitor \
  --name alert-cpu-high \
  --scopes $VM_ID \
  --condition "avg Percentage CPU > 80" \
  --window-size 5m \
  --evaluation-frequency 1m \
  --severity 2 \
  --action $AG_ID \
  --description "Alert khi CPU vuot 80%"
5

Truy Vấn KQL trong Log Analytics

Sau khi DCR thu thập đủ data (~15 phút), vào Log Analytics Workspace → Logs để chạy KQL queries. Monitor → Logs hoặc trực tiếp từ workspace.

KQL — Kusto Query Language— chạy trong Log Analytics → Logs
// 1. Xem memory available trong 1 giờ qua (từ AMA + DCR)
InsightsMetrics
| where TimeGenerated > ago(1h)
| where Name == "AvailableMB"
| project TimeGenerated, Computer, Name, Val
| render timechart

// 2. Xem CPU utilization trung bình theo máy
Perf
| where TimeGenerated > ago(1h)
| where CounterName == "% Processor Time"
| summarize avg(CounterValue) by Computer, bin(TimeGenerated, 5m)
| render timechart

// 3. Phát hiện failed login (Event ID 4625)
SecurityEvent
| where TimeGenerated > ago(24h)
| where EventID == 4625
| project TimeGenerated, Computer, Account, IpAddress, Activity
| order by TimeGenerated desc

// 4. Top 10 events theo Computer
Event
| where TimeGenerated > ago(1h)
| summarize count() by Computer, EventLevelName
| order by count_ desc
| take 10

// 5. Alert log query — failed logins > 5 trong 5 phút
SecurityEvent
| where TimeGenerated > ago(5m)
| where EventID == 4625
| summarize FailedLogins = count() by Computer
| where FailedLogins > 5
Kết quả (Output)— Query Perf CPU
TimeGenerated           Computer        avg_CounterValue
--------------          --------        ----------------
2026-05-21T10:00:00Z    vm-lab11-win    12.45
2026-05-21T10:05:00Z    vm-lab11-win    8.72
2026-05-21T10:10:00Z    vm-lab11-win    15.30
...
// Timechart hiển thị trong Log Analytics UI
// Nếu không có data: chờ thêm 15-30 phút sau khi AMA cài xong

📊 Kết Quả Đầu Ra Lab 11

Log Analytics Workspace hoạt động

law-hoatranlab-lab11 → Logs → có data từ AMA sau ~15 phút

AMA Extension cài thành công

VM Extensions → AzureMonitorWindowsAgent → Status: Provisioning succeeded

DCR liên kết VM

Monitor → Data Collection Rules → dcr-lab11-windows → Resources tab → vm-lab11-win

Alert Rules active

Monitor → Alerts → Alert rules → alert-cpu-high → Condition: Enabled

Email alert nhận được

Khi stress test CPU, email từ [email protected] gửi trong vòng 5 phút

KQL query trả về data

Perf table, InsightsMetrics, SecurityEvent có data từ VM sau khi AMA thu thập

🧹 Dọn Dẹp Tài Nguyên

Azure CLI
# Xóa toàn bộ resource group (VM, Workspace, DCR, Alert rules)
az group delete \
  --name rg-lab11-monitor \
  --yes \
  --no-wait

# Lưu ý: Alert rules và Action Group cũng cần xóa
# nếu tạo trong subscription scope thay vì resource group
az monitor action-group delete \
  --resource-group rg-lab11-monitor \
  --name ag-lab11-email

❓ Câu Hỏi Ôn Tập

1. Azure Monitor Agent (AMA) khác Microsoft Monitoring Agent (MMA) như thế nào? Tại sao MMA bị retire?

Gợi ý: AMA dùng DCR (granular control), Managed Identity thay workspace key, multi-homing, hiệu năng tốt hơn; MMA retire 31/8/2024 do kiến trúc cũ, không hỗ trợ Arc, khó quản lý multi-workspace

2. Data Collection Rule (DCR) là gì? Một DCR có thể liên kết với bao nhiêu VM?

Gợi ý: DCR là cấu hình định nghĩa data sources + destinations; có thể associate với nhiều VMs; ngược lại 1 VM có thể có nhiều DCRs (multi-homing)

3. Metric Alert và Log Alert khác nhau thế nào? Khi nào nên dùng loại nào?

Gợi ý: Metric = near-realtime từ platform metrics (CPU, Memory), nhanh hơn, rẻ hơn; Log = query KQL trên Log Analytics, linh hoạt hơn (custom logic), latency cao hơn 5-15 phút

4. Viết KQL query để tính số lần restart VM trong 7 ngày qua.

Gợi ý: AzureActivity | where TimeGenerated > ago(7d) | where OperationNameValue == "Microsoft.Compute/virtualMachines/restart/action" | summarize count() by ResourceGroup

5. Action Group có những notification channel nào? SMS alert có giới hạn gì?

Gợi ý: Email, SMS, Push (Azure App), Voice, Webhook, ITSM, Logic App, Automation Runbook, Azure Function; SMS giới hạn 1 tin/5 phút/subscription để tránh flood

Lab 10: Azure File Sync Thư viện Labs Lab 12: Migrate Servers
Zalo