title | titleSuffix | description | services | documentationcenter | author | ms.service | ms.topic | ms.tgt_pltfrm | ms.workload | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|
Manage NSG flow logs - Azure REST API |
Azure Network Watcher |
This page explains how to manage Network Security Group flow logs in Azure Network Watcher with REST API |
network-watcher |
na |
damendo |
network-watcher |
how-to |
na |
infrastructure-services |
01/07/2021 |
damendo |
[!div class="op_single_selector"]
Network Security Group flow logs are a feature of Network Watcher that allows you to view information about ingress and egress IP traffic through a Network Security Group. These flow logs are written in json format and show outbound and inbound flows on a per rule basis, the NIC the flow applies to, 5-tuple information about the flow (Source/Destination IP, Source/Destination Port, Protocol), and if the traffic was allowed or denied.
ARMclient is used to call the REST API using PowerShell. ARMClient is found on chocolatey at ARMClient on Chocolatey. The detailed specifications of NSG flow logs REST API can be found here
This scenario assumes you have already followed the steps in Create a Network Watcher to create a Network Watcher.
Important
For Network Watcher REST API calls the resource group name in the request URI is the resource group that contains the Network Watcher, not the resources you are performing the diagnostic actions on.
The scenario covered in this article shows you how to enable, disable, and query flow logs using the REST API. To learn more about Network Security Group flow loggings, visit Network Security Group flow logging - Overview.
In this scenario, you will:
- Enable flow logs (Version 2)
- Disable flow logs
- Query flow logs status
Log in to armclient with your Azure credentials.
armclient login
In order for flow logging to work successfully, the Microsoft.Insights provider must be registered. If you are not sure if the Microsoft.Insights provider is registered, run the following script.
$subscriptionId = "00000000-0000-0000-0000-000000000000"
armclient post "https://management.azure.com//subscriptions/${subscriptionId}/providers/Microsoft.Insights/register?api-version=2016-09-01"
The command to enable flow logs version 2 is shown in the following example. For version 1 replace the 'version' field with '1':
$subscriptionId = "00000000-0000-0000-0000-000000000000"
$targetUri = "" # example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName/providers/Microsoft.Network/networkSecurityGroups/{nsgName}"
$storageId = "/subscriptions/00000000-0000-0000-0000-000000000000/{resourceGroupName/providers/Microsoft.Storage/storageAccounts/{saName}"
$resourceGroupName = "NetworkWatcherRG"
$networkWatcherName = "NetworkWatcher_westcentralus"
$requestBody = @"
{
'targetResourceId': '${targetUri}',
'properties': {
'storageId': '${storageId}',
'enabled': 'true',
'retentionPolicy' : {
days: 5,
enabled: true
},
'format': {
'type': 'JSON',
'version': 2
}
}
}
"@
armclient post "https://management.azure.com/subscriptions/${subscriptionId}/ResourceGroups/${resourceGroupName}/providers/Microsoft.Network/networkWatchers/${networkWatcherName}/configureFlowLog?api-version=2016-12-01" $requestBody
The response returned from the preceding example is as follows:
{
"targetResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{nsgName}",
"properties": {
"storageId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{saName}",
"enabled": true,
"retentionPolicy": {
"days": 5,
"enabled": true
},
"format": {
"type": "JSON",
"version": 2
}
}
}
Note
- The api Network Watchers - Set Flow Log Configuration used above is old and may soon be deprecated.
- It is recommended to use the new Flow Logs - Create Or Update rest api instead.
Use the following example to disable flow logs. The call is the same as enabling flow logs, except false is set for the enabled property.
$subscriptionId = "00000000-0000-0000-0000-000000000000"
$targetUri = "" # example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName/providers/Microsoft.Network/networkSecurityGroups/{nsgName}"
$storageId = "/subscriptions/00000000-0000-0000-0000-000000000000/{resourceGroupName/providers/Microsoft.Storage/storageAccounts/{saName}"
$resourceGroupName = "NetworkWatcherRG"
$networkWatcherName = "NetworkWatcher_westcentralus"
$requestBody = @"
{
'targetResourceId': '${targetUri}',
'properties': {
'storageId': '${storageId}',
'enabled': 'false',
'retentionPolicy' : {
days: 5,
enabled: true
},
'format': {
'type': 'JSON',
'version': 2
}
}
}
"@
armclient post "https://management.azure.com/subscriptions/${subscriptionId}/ResourceGroups/${resourceGroupName}/providers/Microsoft.Network/networkWatchers/${networkWatcherName}/configureFlowLog?api-version=2016-12-01" $requestBody
The response returned from the preceding example is as follows:
{
"targetResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{nsgName}",
"properties": {
"storageId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{saName}",
"enabled": false,
"retentionPolicy": {
"days": 5,
"enabled": true
},
"format": {
"type": "JSON",
"version": 2
}
}
}
Note
- The api Network Watchers - Set Flow Log Configuration used above is old and may soon be deprecated.
- It is recommended to use the new Flow Logs - Create Or Update rest api to disable flow logs and the Flow Logs - Delete to delete flow logs resource.
The following REST call queries the status of flow logs on a Network Security Group.
$subscriptionId = "00000000-0000-0000-0000-000000000000"
$targetUri = "" # example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName/providers/Microsoft.Network/networkSecurityGroups/{nsgName}"
$resourceGroupName = "NetworkWatcherRG"
$networkWatcherName = "NetworkWatcher_westcentralus"
$requestBody = @"
{
'targetResourceId': '${targetUri}',
}
"@
armclient post "https://management.azure.com/subscriptions/${subscriptionId}/ResourceGroups/${resourceGroupName}/providers/Microsoft.Network/networkWatchers/${networkWatcherName}/queryFlowLogStatus?api-version=2016-12-01" $requestBody
The following is an example of the response returned:
{
"targetResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{nsgName}",
"properties": {
"storageId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{saName}",
"enabled": true,
"retentionPolicy": {
"days": 5,
"enabled": true
},
"format": {
"type": "JSON",
"version": 2
}
}
}
Note
- The api Network Watchers - Get Flow Log Status used above, requires an additional "reader" permission in the resource group of the network watcher. Also, this api is old and may soon be deprecated.
- It is recommended to use the new Flow Logs - Get rest api instead.
The storage location of a flow log is defined at creation. A convenient tool to access these flow logs saved to a storage account is Microsoft Azure Storage Explorer, which can be downloaded here: https://storageexplorer.com/
If a storage account is specified, packet capture files are saved to a storage account at the following location:
https://{storageAccountName}.blob.core.windows.net/insights-logs-networksecuritygroupflowevent/resourceId=/SUBSCRIPTIONS/{subscriptionID}/RESOURCEGROUPS/{resourceGroupName}/PROVIDERS/MICROSOFT.NETWORK/NETWORKSECURITYGROUPS/{nsgName}/y={year}/m={month}/d={day}/h={hour}/m=00/macAddress={macAddress}/PT1H.json
Learn how to Visualize your NSG flow logs with PowerBI
Learn how to Visualize your NSG flow logs with open source tools