Skip to content

Commit de2be30

Browse files
committed
Incorporated review comments
1 parent 06f659d commit de2be30

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

articles/network-watcher/network-watcher-alert-triggered-packet-capture.md

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ ms.service: network-watcher
88
ms.topic: how-to
99
ms.tgt_pltfrm: na
1010
ms.workload: infrastructure-services
11-
ms.date: 12/28/2022
11+
ms.date: 01/09/2023
1212
ms.author: shijaiswal
1313
ms.custom: devx-track-azurepowershell, engagement-fy23
1414

1515
---
16-
# Use packet capture for proactive network monitoring with alerts and Azure Functions
16+
# Monitor networks proactively with alerts and Azure Functions using Packet Capture
1717

1818
Network Watcher packet capture creates capture sessions to track traffic in and out of virtual machines. The capture file can have a filter that is defined to track only the traffic that you want to monitor. This data is stored in a storage blob or locally on the guest machine.
1919

2020
This capability can be started remotely from other automation scenarios such as Azure Functions. Packet capture gives you the capability to run proactive captures based on defined network anomalies. Other uses include gathering network statistics, getting information about network intrusions, debugging client-server communications, and more.
2121

22-
Resources that are deployed in Azure run 24 * 7. You and your staff can't actively monitor the status of all resources 24 * 7. For example, what happens if an issue occurs at 2 AM?
22+
Resources that are deployed in Azure run 24 * 7. It is difficult to actively monitor the status of all resources 24 * 7. For example, what happens if an issue occurs at 2 AM?
2323

24-
By using Network Watcher, alerting and functions from within the Azure ecosystem, you can proactively respond with the data and tools to solve problems in your network.
24+
By using Network Watcher alerting and functions from within the Azure ecosystem, you can proactively respond with the data and tools to solve problems in your network.
2525

2626
[!INCLUDE [updated-for-az](../../includes/updated-for-az.md)]
2727

@@ -33,18 +33,18 @@ By using Network Watcher, alerting and functions from within the Azure ecosystem
3333

3434
## Scenario
3535

36-
In this example, your VM is utilizing more CPU percentage than usual and you want to be alerted. CPU percentage is used as an example here, but you can use any alert condition.
36+
In this example, your VM has more outgoing traffic than usual and you want to be alerted. Similarly, you can create alerts for any condition.
3737

38-
When you're alerted, the packet-level data helps to understand why utilization has increased. You can take steps to return the virtual machine to regular utilization.
38+
When an alert is triggered, the packet-level data helps to analyze why the outgoing traffic has increased. You can take steps to return the virtual machine to its original state.
3939

4040
This scenario assumes that you have an existing instance of Network Watcher and a resource group with a valid Virtual machine.
4141

42-
The following list is an overview of the workflow that takes place:
42+
The following workflow that takes place when an alert is triggered:
4343

4444
1. An alert is triggered on your VM.
4545
1. The alert calls your Azure function.
4646
1. Your Azure function processes the alert and starts a Network Watcher packet capture session.
47-
1. The packet capture runs on the VM and collects traffic.
47+
1. The packet capture runs on the VM and collects data.
4848
1. The packet capture file is uploaded to a storage account for review and diagnosis.
4949

5050
To automate this process, we create and connect an alert on our VM to trigger when the incident occurs. We also create a function to call Network Watcher.
@@ -56,7 +56,7 @@ This scenario does the following:
5656

5757
## Create an Azure function app
5858

59-
The first step is to create an Azure function to process the alert and create a packet capture.
59+
To create an Azure function to process the alert and create a packet capture, follow these steps:
6060

6161
1. In the [Azure portal](https://portal.azure.com), search for *function app* in **All services** and select it.
6262

@@ -66,18 +66,19 @@ The first step is to create an Azure function to process the alert and create a
6666

6767
:::image type="content" source="./media/network-watcher-alert-triggered-packet-capture/create-function-app.png" alt-text="Screenshot of the Create function app screen.":::
6868

69-
2. In the **Basics** tab, enter the following values and select **OK** to create the app:
69+
2. In the **Basics** tab, enter the following values:
7070
1. Under **Project Details**, select the **Subscription** for which you want to create the Function app and the **Resource Group** to contain the app.
7171
2. Under **Instance details**, do the following:
7272
1. Enter the name of the Function app. This name will be appended by *.azurewebsites.net*.
7373
2. In **Publish**, select the mode of publishing, either *Code* or *Docker Container*.
7474
3. Select a **Runtime stack**.
7575
4. Select the version of the Runtime stack in **Version**.
7676
5. Select the **Region** in which you want to create the function app.
77+
3. Select **OK** to create the app.
7778
3. Under **Operating System**, select the type of Operating system that you're currently using. Azure recommends the type of Operating system based on your runtime stack selection.
7879
4. Under **Plan**, select the type of plan that you want to use for the function app. Choose from the following options:
79-
- Consumption (Serverless) - For event-driven scaling for the lowest minimum cost
80-
- Functions Premium - For enterprise-level, serverless applications with event-based scaling and network isolation
80+
- Consumption (Serverless) - For event-driven scaling for the most minimum cost.
81+
- Functions Premium - For enterprise-level, serverless applications with event-based scaling and network isolation.
8182
- App Service Plan - For reusing compute from an existing app service plan.
8283
3. Select **Review + create** to create the app.
8384

@@ -100,10 +101,10 @@ The first step is to create an Azure function to process the alert and create a
100101

101102
### Authentication
102103

103-
To use the PowerShell cmdlets, you must authenticate. You configure authentication in the function app. To configure authentication, you must configure environment variables and upload an encrypted key file to the function app.
104+
To use the PowerShell cmdlets, you must configure authentication in the function app. To configure authentication, you must configure environment variables and upload an encrypted key file to the function app.
104105

105106
> [!NOTE]
106-
> This scenario provides just one example of how to implement authentication with Azure Functions. There are other ways to do this.
107+
> This scenario provides only one example of how to implement authentication with Azure Functions. There are other ways to do the same action.
107108
108109
#### Encrypted credentials
109110

@@ -126,28 +127,26 @@ $Encryptedpassword = $secPw | ConvertFrom-SecureString -Key $AESKey
126127
$Encryptedpassword
127128
```
128129

129-
In the App Service Editor of the function app, create a folder called **keys** under **AlertPacketCapturePowerShell**. Upload the **PassEncryptKey.key** file that you created in the previous PowerShell sample.
130-
131-
:::image type="content" source="./media/network-watcher-alert-triggered-packet-capture/key-function.png" alt-text="Screenshot of the Encryption key.":::
132-
133130
### Retrieve values for environment variables
134131

135-
The final requirement is to set up the environment variables that are necessary to access the values for authentication. The following list shows the environment variables that are created:
132+
Set up the environment variables that are necessary to access the values for authentication. The following list shows the environment variables that are created:
136133

137134
* AzureClientID
138135
* AzureTenant
139136
* AzureCredPassword
140137

138+
If you already have an Application ID, use the AzureClientID, AzureTenant, and AzureCredPassword of that application. If you don't have one, proceed to [store the environment variables](#store-the-environment-variables).
139+
141140
#### AzureClientID
142141

143142
The client ID is the Application ID of an application in the Azure Active Directory.
144143

145-
1. If you don't already have an application to use, run the following example to create an application.
144+
1. If you don't already have an application to use, run the following cmdlet to create an application.
146145

147146
```powershell
148147
$app = New-AzADApplication -DisplayName "ExampleAutomationAccount_MF" -HomePage "https://exampleapp.com" -IdentifierUris "https://exampleapp1.com/ExampleFunctionsAccount" -Password "<same password as defined earlier>"
149148
New-AzADServicePrincipal -ApplicationId $app.ApplicationId
150-
Start-Sleep 15
149+
Start-Sleep 15]
151150
New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $app.ApplicationId
152151
```
153152
@@ -161,7 +160,7 @@ The client ID is the Application ID of an application in the Azure Active Direct
161160
162161
#### AzureTenant
163162
164-
Obtain the tenant ID by running the following PowerShell sample:
163+
Obtain the tenant ID by running the following PowerShell cmdlet:
165164
166165
```powershell
167166
(Get-AzSubscription -SubscriptionName "<subscriptionName>").TenantId
@@ -190,6 +189,8 @@ $Encryptedpassword
190189

191190
### Store the environment variables
192191

192+
To store the environment variables, follow these steps:
193+
193194
1. Go to the function app. Select **Configurations** > **Application settings**.
194195

195196
:::image type="content" source="./media/network-watcher-alert-triggered-packet-capture/application-insights.png" alt-text="Screenshot of the Application settings screen.":::
@@ -198,7 +199,7 @@ $Encryptedpassword
198199

199200
### Add PowerShell to the function
200201

201-
It's now time to make calls into Network Watcher from within the Azure function. Depending on the requirements, the implementation of this function can vary. However, the general flow of the code is as follows:
202+
Now, make calls into Network Watcher from within the Azure function. Depending on the requirements, the implementation of this function can vary. However, the general flow of the code is as follows:
202203

203204
1. Process input parameters.
204205
2. Query existing packet captures to verify limits and resolve name conflicts.
@@ -288,12 +289,6 @@ $clientid = $env:AzureClientId
288289
$password = ConvertTo-SecureString $pw -AsPlainText -Force
289290
$credential = New-Object System.Management.Automation.PSCredential ($clientid, $password)
290291
291-
# Credentials can also be provided as encrypted key file as mentioned below
292-
# $keypath = "D:\home\site\wwwroot\AlertPacketCapturePowerShell\keys\PassEncryptKey.key"
293-
# $secpassword = $pw | ConvertTo-SecureString -Key (Get-Content $keypath)
294-
# $credential = New-Object System.Management.Automation.PSCredential ($clientid, $secpassword)
295-
296-
297292
Connect-AzAccount -ServicePrincipal -Tenant $tenant -Credential $credential #-WarningAction SilentlyContinue | out-null
298293
299294
if ($requestBody.context.resourceType -eq "Microsoft.Compute/virtualMachines") {
@@ -305,7 +300,7 @@ if ($requestBody.context.resourceType -eq "Microsoft.Compute/virtualMachines") {
305300
$networkWatcher = Get-AzNetworkWatcher -Location $vm.Location
306301
307302
# Get existing packetCaptures
308-
# $packetCaptures = Get-AzNetworkWatcherPacketCapture -NetworkWatcher $networkWatcher
303+
packetCaptures = Get-AzNetworkWatcherPacketCapture -NetworkWatcher $networkWatcher
309304
310305
# Remove existing packet capture created by the function (if it exists)
311306
$packetCaptures | ForEach-Object { if ($_.Name -eq $packetCaptureName)
@@ -319,24 +314,18 @@ if ($requestBody.context.resourceType -eq "Microsoft.Compute/virtualMachines") {
319314
Write-Output "Initiating Packet Capture"
320315
New-AzNetworkWatcherPacketCapture -NetworkWatcher $networkWatcher -TargetVirtualMachineId $requestBody.context.resourceId -PacketCaptureName $packetCaptureName -StorageAccountId $storageaccountid -TimeLimitInSeconds $packetCaptureDuration
321316
}
322-
}
323-
324-
325-
317+
}
326318
```
327319

328-
329320
## Configure an alert on a VM
330321

331-
Alerts can be configured to notify individuals when a specific metric crosses a threshold that's assigned to it. In this example, the alert is on the CPU Percentage that is sent, but the alert can be triggered for many other metrics.
322+
You can configure alerts to notify individuals when a specific metric crosses a threshold that's assigned to it. In this example, the alert is on the Network Out Total that is sent, but the alert can be triggered for many other metrics.
332323

333324
### Create the alert rule
334325

335326
Go to an existing virtual machine and [add an alert rule](../azure-monitor/alerts/alerts-classic-portal.md). Do the following in the **Create an Alert rule** screen.
336327

337-
1. In the **Select a signal** pane, search for the name of the signal and select it. In this example, Percentage CPU is the selected signal. It denotes the percentage of allocated compute units that are in use by the VM.
338-
339-
:::image type="content" source="./media/network-watcher-alert-triggered-packet-capture/action-group.png" alt-text="Screenshot of the Create action group screen.":::
328+
1. In the **Select a signal** pane, search for the name of the signal and select it. In this example, Network Out Total is the selected signal. It denotes the The number of bytes out on all network interfaces by the Virtual Machine.
340329

341330
2. In the **Conditions** tab, set the following values and select **Next: Actions >**.
342331

@@ -353,6 +342,7 @@ Go to an existing virtual machine and [add an alert rule](../azure-monitor/alert
353342
4. In the **Create action group** screen, select the **Subscription**, **Resource group**, and **Region**. Also enter the Action group name and the display name and select **Next: Notifications >**.
354343
5. In the screen that appears, select **Action type** as **Azure Function**.
355344
6. In the Azure Function pane, select the **Subscription**, **Resource group**, **Function app**, and **Azure Function**.
345+
:::image type="content" source="./media/network-watcher-alert-triggered-packet-capture/action-group.png" alt-text="Screenshot of the Create action group screen.":::
356346
7. Select **No** in **Enable the common alert schema** slider and select **OK**.
357347

358348

0 commit comments

Comments
 (0)