|
| 1 | +--- |
| 2 | +title: Azure Service Bus - Automatically update messaging units |
| 3 | +description: This article shows you how you can use an Azure Automation runbook to automatically update messaging units of a Service Bus namespace. |
| 4 | +services: service-bus-messaging |
| 5 | + |
| 6 | +ms.service: service-bus-messaging |
| 7 | +documentationcenter: '' |
| 8 | +author: spelluru |
| 9 | + |
| 10 | +ms.topic: how-to |
| 11 | +ms.date: 05/14/2020 |
| 12 | +ms.author: spelluru |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +# Automatically update messaging units of an Azure Service Bus namespace |
| 17 | +This article shows you how you can automatically update [messaging units](service-bus-premium-messaging.md) of a Service Bus namespace based on resource (CPU or memory) usage. |
| 18 | + |
| 19 | +The example in this article shows how to increase messaging units for a Service Bus namespace when the CPU usage of the namespace goes above 75%. The high-level steps are: |
| 20 | + |
| 21 | +1. Create an Azure Automation runbook with PowerShell script that scales up (increases) messaging units for a Service Bus namespace. |
| 22 | +2. Create a CPU usage alert on the Service Bus namespace, which invokes the PowerShell script when the namespace CPU usage goes above 75%. |
| 23 | + |
| 24 | +> [!IMPORTANT] |
| 25 | +> This article applies to only the **premium** tier of Azure Service Bus. |
| 26 | +
|
| 27 | + |
| 28 | +## Create a Service Bus namespace |
| 29 | +Create a premier tier Service Bus namespace. Follow steps from the [Create a namespace in the Azure portal](service-bus-quickstart-portal.md#create-a-namespace-in-the-azure-portal) article to create the namespace. |
| 30 | + |
| 31 | +## Create an Azure Automation account |
| 32 | +Create an Azure Automation account by following instructions from the [Create an Azure Automation account](../automation/automation-quickstart-create-account.md) article. |
| 33 | + |
| 34 | +## Import Az.Service module from gallery |
| 35 | +Import `Az.Accounts` and `Az.ServiceBus` modules from the gallery into the automation account. The `Az.ServiceBus` module depends on `Az.Accounts` module, so it must be installed first. |
| 36 | + |
| 37 | +For step-by-step instructions, see [Import a module from the module gallery](../automation/automation-runbook-gallery.md#import-a-module-from-the-module-gallery-with-the-azure-portal). |
| 38 | + |
| 39 | +## Create and publish a PowerShell runbook |
| 40 | + |
| 41 | +1. Create a PowerShell runbook by follow instructions in the [Create a PowerShell runbook](../automation/automation-quickstart-create-runbook.md) article. |
| 42 | + |
| 43 | + Here's a sample PowerShell script you can use to increase messaging units for a Service Bus namespace. This PowerShell script in an automation runbook increases MUs from 1 to 2, 2 to 4, or 4 to 8. The allowed values for this property are: 1, 2, 4, and 8. You can create another runbook to decrease the messaging units. |
| 44 | + |
| 45 | + The **namespaceName** and **resourceGroupName** parameters are used for testing the script separately from alerting scenario. |
| 46 | + |
| 47 | + The **WebHookData** parameter is for the alert to pass information such as resource group name, resource name, etc. at runtime. |
| 48 | + |
| 49 | + ```powershell |
| 50 | + [OutputType("PSAzureOperationResponse")] |
| 51 | + param |
| 52 | + ( |
| 53 | + [Parameter (Mandatory=$false)] |
| 54 | + [object] $WebhookData, |
| 55 | + |
| 56 | + [Parameter (Mandatory = $false)] |
| 57 | + [String] $namespaceName, |
| 58 | + |
| 59 | + [Parameter (Mandatory = $false)] |
| 60 | + [String] $resourceGroupName |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | + if ($WebhookData) |
| 65 | + { |
| 66 | + # Get the data object from WebhookData |
| 67 | + $WebhookBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody) |
| 68 | + |
| 69 | + # Get the alert schema ID |
| 70 | + $schemaId = $WebhookBody.schemaId |
| 71 | +
|
| 72 | + # If it's a metric alert |
| 73 | + if ($schemaId -eq "AzureMonitorMetricAlert") { |
| 74 | +
|
| 75 | + # Get the resource group name from the alert context |
| 76 | + $resourceGroupName = $AlertContext.resourceGroupName |
| 77 | + |
| 78 | + # Get the namespace name from the alert context |
| 79 | + $namespaceName = $AlertContext.resourceName |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + # Connect to Azure account |
| 84 | + $connection = Get-AutomationConnection -Name AzureRunAsConnection |
| 85 | + |
| 86 | + while(!($connectionResult) -And ($logonAttempt -le 10)) |
| 87 | + { |
| 88 | + $LogonAttempt++ |
| 89 | + # Logging in to Azure... |
| 90 | + $connectionResult = Connect-AzAccount ` |
| 91 | + -ServicePrincipal ` |
| 92 | + -Tenant $connection.TenantID ` |
| 93 | + -ApplicationId $connection.ApplicationID ` |
| 94 | + -CertificateThumbprint $connection.CertificateThumbprint |
| 95 | + |
| 96 | + Start-Sleep -Seconds 30 |
| 97 | + } |
| 98 | + |
| 99 | + # Get the current capacity (number of messaging units) of the namespace |
| 100 | + $sbusns=Get-AzServiceBusNamespace ` |
| 101 | + -Name $namespaceName ` |
| 102 | + -ResourceGroupName $resourceGroupName |
| 103 | + |
| 104 | + $currentCapacity = $sbusns.Sku.Capacity |
| 105 | + |
| 106 | + # Increase the capacity |
| 107 | + # Capacity can be one of these values: 1, 2, 4, 8 |
| 108 | + if ($currentCapacity -eq 1) { |
| 109 | + $newMU = 2 |
| 110 | + } |
| 111 | + elseif ($currentCapacity -eq 2) { |
| 112 | + $newMU = 4 |
| 113 | + } |
| 114 | + elseif ($currentCapacity -eq 4) { |
| 115 | + $newMU = 8 |
| 116 | + } |
| 117 | + else { |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + # Update the capacity of the namespace |
| 122 | + Set-AzServiceBusNamespace ` |
| 123 | + -Location eastus ` |
| 124 | + -SkuName Premium ` |
| 125 | + -Name $namespaceName ` |
| 126 | + -SkuCapacity $newMU ` |
| 127 | + -ResourceGroupName $resourceGroupName |
| 128 | +
|
| 129 | + ``` |
| 130 | +2. [Test the workbook](../automation/manage-runbooks.md#test-a-runbook) by specifying values for the **namespaceName** and **resourceGroupName** parameters. Confirm that the messaging units on the namespace are updated. |
| 131 | +3. After you test successfully, [publish the workbook](..//automation/manage-runbooks.md#publish-a-runbook) so that it's available to add as an action for an alert on the namespace later. |
| 132 | +
|
| 133 | +## Create an alert on the namespace to trigger the runbook |
| 134 | +See [Use an alert to trigger an Azure Automation runbook](../automation/automation-create-alert-triggered-runbook.md) article to configure an alert on your Service Bus namespace to trigger the automation runbook you created. For example, you can create an alert on **CPU usage per namespace** or **Memory size usage per namespace** metric, and add an action to trigger the automation runbook you created. For details about these metrics, see [Resource usage metrics](service-bus-metrics-azure-monitor.md#resource-usage-metrics). |
| 135 | +
|
| 136 | +The following procedure shows how to create an alert that triggers the automation runbook when the namespace **CPU usage** goes above **75%**. |
| 137 | +
|
| 138 | +1. On the **Service Bus Namespace** page for your namespace, select **Alerts** on the left menu, and then select **+ New alert rule** on the toolbar. |
| 139 | + |
| 140 | +  |
| 141 | +2. On the **Create alert rule** page, click **Select condition**. |
| 142 | +
|
| 143 | +  |
| 144 | +3. On the **Configure signal logic** page, select **CPU** for the signal. |
| 145 | +
|
| 146 | +  |
| 147 | +4. Enter a **threshold value** (in this example, it's **75%**), and select **Done**. |
| 148 | +
|
| 149 | +  |
| 150 | +5. Now, on the **Create alert page**, click **Select action group**. |
| 151 | + |
| 152 | +  |
| 153 | +6. Select **Create action group** button on the toolbar. |
| 154 | +
|
| 155 | +  |
| 156 | +7. On the **Add action group** page, do the following steps: |
| 157 | + 1. Enter a **name** for the action group. |
| 158 | + 2. Enter a **short name** for the action group. |
| 159 | + 3. Select the **subscription** in which you want to create this action group. |
| 160 | + 4. Select the **resource group**. |
| 161 | + 5. In the **Actions** section, enter a **name for the action**, and select **Automation Runbook** for **Action type**. |
| 162 | +
|
| 163 | +  |
| 164 | +8. On the **Configure Runbook** page, do the following steps: |
| 165 | + 1. For **Runbook source**, select **User**. |
| 166 | + 2. For **Subscription**, select your Azure **subscription** that contains the automation account. |
| 167 | + 3. For **Automation account**, select your **automation account**. |
| 168 | + 4. For **Runbook**, select your runbook. |
| 169 | + 5. Select **OK** on the **Configure Runbook** page. |
| 170 | +  |
| 171 | +9. Select **OK** on the **Add action group** page. |
| 172 | +5. Now, on the **Create alert rule** page, enter a **name for the rule**, and then select **Create alert rule**. |
| 173 | +  |
| 174 | +
|
| 175 | + > [!NOTE] |
| 176 | + > Now, when the namespace CPU usage goes above 75, the alert triggers the automation runbook, which increases messaging units of the Service Bus namespace. Similarly, you can create an alert for another automation runbook, which decreases the messaging units if the namespace CPU usage goes below 25. |
| 177 | +
|
| 178 | +## Next steps |
| 179 | +To learn about messaging units, see the [Premium messaging](service-bus-premium-messaging.md) |
0 commit comments