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