使用 Azure CLI 管理 NSG 流日志

网络安全组流日志记录是 Azure 网络观察程序的一项功能,可用于记录有关流经网络安全组的 IP 流量的信息。 有关网络安全组流日志记录的详细信息,请参阅 NSG 流日志概述

本文介绍如何使用 Azure CLI 创建、更改、禁用或删除 NSG 流日志。 你可以了解如何使用 Azure 门户PowerShellREST APIARM 模板管理 NSG 流日志。

先决条件

注册 Insights 提供程序

必须注册 Microsoft.Insights 提供程序才能成功记录流经网络安全组的流量。 如果不确定是否已注册 Microsoft.Insights 提供程序,请使用 az provider register 进行注册。

# Register Microsoft.Insights provider.
az provider register --namespace 'Microsoft.Insights'

创建流日志

使用 az network watcher flow-log create 创建流日志。 流日志是在网络观察程序默认资源组 NetworkWatcherRG 中创建的。

# Create a version 1 NSG flow log.
az network watcher flow-log create --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account 'myStorageAccount'

注意

  • 如果存储帐户位于不同的订阅中,则网络安全组和存储帐户必须与同一 Azure Active Directory 租户相关联。 用于每个订阅的帐户必须有必要的权限
  • 如果存储帐户位于不同的资源组或订阅中,则必须指定存储帐户的完整 ID,而非其名称。 例如,如果 myStorageAccount 存储帐户位于名为 StorageRG 的资源组中,而网络安全组位于资源组 myResourceGroup 中,则必须使用 --storage-account 参数的 /subscriptions/{SubscriptionID}/resourceGroups/RG-Storage/providers/Microsoft.Storage/storageAccounts/myStorageAccount 代替 myStorageAccount
# Place the storage account resource ID into a variable.
sa=$(az storage account show --name 'myStorageAccount' --query 'id' --output 'tsv')

# Create a version 1 NSG flow log (the storage account is in a different resource group).
az network watcher flow-log create --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account $sa

创建流日志和流量分析工作区

  1. 使用 az monitor log-analytics workspace create 创建 Log Analytics 工作区。

    # Create a Log Analytics workspace.
    az monitor log-analytics workspace create --name 'myWorkspace' --resource-group 'myResourceGroup'
    
  2. 使用 az network watcher flow-log create 创建流日志。 流日志是在网络观察程序默认资源组 NetworkWatcherRG 中创建的。

    # Create a version 1 NSG flow log and enable traffic analytics for it.
    az network watcher flow-log create --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account 'myStorageAccount' --traffic-analytics 'true' --workspace 'myWorkspace'
    

注意

  • 存储帐户不能有任何网络规则仅限 Azure 服务或特定虚拟网络进行网络访问。
  • 如果存储帐户位于不同的订阅中,则网络安全组和存储帐户必须与同一 Azure Active Directory 租户相关联。 用于每个订阅的帐户必须有必要的权限
  • 如果存储帐户位于其他资源组或订阅中,则必须使用存储帐户的完整 ID。 例如,如果 myStorageAccount 存储帐户位于名为 StorageRG 的资源组中,而网络安全组位于资源组 myResourceGroup 中,则必须使用 --storage-account 参数的 /subscriptions/{SubscriptionID}/resourceGroups/RG-Storage/providers/Microsoft.Storage/storageAccounts/myStorageAccount 代替 myStorageAccount
# Place the storage account resource ID into a variable.
sa=$(az storage account show --name 'myStorageAccount' --query 'id' --output 'tsv')

# Create a Log Analytics workspace.
az monitor log-analytics workspace create --name 'myWorkspace' --resource-group 'myResourceGroup'

# Create a version 1 NSG flow log and enable traffic analytics for it (the storage account is in a different resource group).
az network watcher flow-log create --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account $sa --traffic-analytics 'true' --workspace 'myWorkspace'

更改流日志

可以使用 az network watcher flow-log update 更改流日志的属性。 例如,可以更改流日志版本或禁用流量分析。

# Update the flow log.
az network watcher flow-log update --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account 'myStorageAccount' --traffic-analytics 'false' --log-version '2'

列出区域中的所有流日志

使用 az network watcher flow-log list 列出订阅中特定区域内的所有 NSG 流日志资源。

# Get all NSG flow logs in China East region.
az network watcher flow-log list --location 'chinaeast' --out table

查看流日志资源的详细信息

使用 az network watcher flow-log show 查看流日志资源的详细信息。

# Get the details of a flow log.
az network watcher flow-log show --name 'myFlowLog' --resource-group 'NetworkWatcherRG' --location 'chinaeast'

下载流日志

流日志的存储位置是在创建时定义的。 若要从存储帐户访问和下载流日志,可以使用 Azure 存储资源管理器。 有关详细信息,请参阅存储资源管理器入门

NSG 流日志文件保存到存储账户的路径为:

https://{storageAccountName}.blob.core.chinacloudapi.cn/insights-logs-networksecuritygroupflowevent/resourceId=/SUBSCRIPTIONS/{subscriptionID}/RESOURCEGROUPS/{resourceGroupName}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{NetworkSecurityGroupName}/y={year}/m={month}/d={day}/h={hour}/m=00/macAddress={macAddress}/PT1H.json

有关流日志结构的信息,请参阅 NSG 流日志的日志格式

禁用流日志

若要暂时禁用流日志而不将其删除,请使用 az network watcher flow-log update 命令。 禁用流日志会停止关联网络安全组的流日志记录。 但是,流日志资源会保留其所有设置和关联。 可以随时重新启用它,以恢复对配置的网络安全组进行流日志记录。

备注

如果为流日志启用了流量分析,则必须先禁用流量分析,然后才能禁用流日志。

# Disable traffic analytics log if it's enabled.
az network watcher flow-log update --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account 'myStorageAccount' --traffic-analytics 'false' --workspace 'myWorkspace'

# Disable the flow log.
az network watcher flow-log update --name 'myFlowLog' --nsg 'myNSG' --resource-group 'myResourceGroup' --storage-account 'myStorageAccount' --enabled 'false'

删除流日志

若要永久删除流日志,请使用 az network watcher flow-log delete 命令。 删除流日志会删除其所有设置和关联。 若要再次为同一网络安全组开始流日志记录,必须为其创建新的流日志。

# Delete the flow log.
az network watcher flow-log delete --name 'myFlowLog' --location 'chinaeast' --no-wait 'true'

备注

删除流日志不会从存储帐户中删除流日志数据。 存储帐户中存储的流日志数据遵循配置的保留策略。

后续步骤