透過使用 Logic Apps 自訂警示通知

本文說明如何建立邏輯應用程式,並將其與 Azure 監視器警示整合。

您可以使用 Azure Logic Apps 建置和自訂工作流程以進行整合。 使用 Logic Apps 自訂警示通知。 您可以:

  • 藉由使用您自己的電子郵件主旨和本文格式,自訂警示電子郵件。
  • 查閱受影響資源的標籤或擷取記錄查詢搜尋結果,以自訂警示中繼資料。 如需如何存取包含警示資料的搜尋結果資料列的詳細資訊,請參閱:
  • 藉由使用 Outlook、Microsoft Teams、Slack 和 PagerDuty 等現有連接器,以便與外部服務整合。 您也可以為自己的服務設定邏輯應用程式。

在此範例中,建立邏輯應用程式,以使用一般警示結構描述從警示傳送詳細資料。

建立邏輯應用程式

  1. Azure 入口網站中建立新的邏輯應用程式。 在頁面頂端的 [搜尋] 列中,輸入 邏輯應用程式

  2. 在 [邏輯應用程式] 頁面上,選取 [新增]

  3. 選取您邏輯應用程式的訂用帳戶資源群組

  4. 設定邏輯應用程式名稱。 針對方案類型,選取 [使用量]

  5. 選取 [檢閱 + 建立]>[建立]

  6. 部署完成後選取 [移至資源]

    Screenshot that shows the Create Logic App page.

  7. 在 [Logic Apps 設計工具] 頁面上,選取 [收到 HTTP 要求時]

    Screenshot that shows the Logic Apps Designer start page.

  8. 將一般警示結構描述從下列 JSON 貼到 [要求本文 JSON 結構描述] 欄位中:

     {
        "type": "object",
        "properties": {
            "schemaId": {
                "type": "string"
            },
            "data": {
                "type": "object",
                "properties": {
                    "essentials": {
                        "type": "object",
                        "properties": {
                            "alertId": {
                                "type": "string"
                            },
                            "alertRule": {
                                "type": "string"
                            },
                            "severity": {
                                "type": "string"
                            },
                            "signalType": {
                                "type": "string"
                            },
                            "monitorCondition": {
                                "type": "string"
                            },
                            "monitoringService": {
                                "type": "string"
                            },
                            "alertTargetIDs": {
                                "type": "array",
                                "items": {
                                    "type": "string"
                                }
                            },
                            "originAlertId": {
                                "type": "string"
                            },
                            "firedDateTime": {
                                "type": "string"
                            },
                            "resolvedDateTime": {
                                "type": "string"
                            },
                            "description": {
                                "type": "string"
                            },
                            "essentialsVersion": {
                                "type": "string"
                            },
                            "alertContextVersion": {
                                "type": "string"
                            }
                        }
                    },
                    "alertContext": {
                        "type": "object",
                        "properties": {}
                    }
                }
            }
        }
    }
    

    Screenshot that shows the Parameters tab for the When an HTTP request is received pane.

  9. (選擇性)。 您可以擷取警示引發時受影響資源的相關資訊,例如資源標籤,以自訂警示通知。 然後,您可以將這些資源標記包含在警示承載中,並使用邏輯運算式中的資訊來傳送通知。 若要執行此步驟,我們將:

    • 為受影響的資源識別碼建立變數。
    • 將資源識別碼分割成陣列,以便使用其各種元素 (例如訂用帳戶和資源群組)。
    • 使用 Azure Resource Manager 連接器來讀取資源的中繼資料。
    • 擷取資源的標籤,然後可在邏輯應用程式的後續步驟中使用。
    1. 選取 +>[新增動作] 圖示以插入新步驟。

    2. 在 [搜尋] 欄位中,搜尋並選取 [初始化變數]

    3. 在 [名稱] 字段中,輸入變數的名稱,例如 AffectedResource

    4. 類型欄位中,選取陣列

    5. 在 [值] 欄位中,選取 [新增動態內容]。 選擇 [運算式] 索引標籤,然後輸入字串 split(triggerBody()?['data']?['essentials']?['alertTargetIDs'][0], '/')

      Screenshot that shows the Parameters tab for the Initialize variable pane.

    6. 選取 +>[新增動作] 圖示以插入其他步驟。

    7. 在 [搜尋] 欄位中,搜尋並選取 [Azure Resource Manager]>[讀取資源]

    8. 使用來自 AffectedResource 變數的陣列值,填入 [讀取資源] 動作的欄位。 在每個欄位中,選取欄位並向下捲動至 [輸入自訂值]。 選取 [新增動態內容],然後選取 [運算式] 索引標籤。輸入此資料表中的字串:

      欄位 [字串值]
      訂用帳戶 variables('AffectedResource')[2]
      資源群組 variables('AffectedResource')[4]
      資源提供者 variables('AffectedResource')[6]
      短資源識別碼 concat(variables('AffectedResource')[7], '/', variables('AffectedResource')[8])
      用戶端 API 版本 資源類型的 API 版本

      若要尋找資源類型的 API 版本,請選取資源概觀頁面右上方的 [JSON 檢視] 連結。 [資源 JSON] 頁面會在頁面頂端搭配 ResourceIDAPI 版本顯示。

    動態內容現在包含受影響資源的標記。 您可以在設定通知時使用這些標記,如下列步驟所述。

  10. 傳送電子郵件或張貼 Teams 訊息。

  11. 選取 +>[新增動作] 圖示以插入新步驟。

    Screenshot that shows the parameters for When an HTTP request is received.

  1. 在搜尋欄位中,搜尋 Outlook

  2. 選取 [Office 365 Outlook]

    Screenshot that shows the Add an action page of the Logic Apps Designer with Office 365 Outlook selected.

  3. 從動作清單中,選取 [傳送電子郵件 (V2)]

  4. 系統提示您建立連線時,登入 Office 365。

  5. 從 [動態內容] 清單中選擇欄位,輸入靜態文字並包含從警示承載取得的內容,以建立電子郵件本文。 例如:

    • 警示有監視條件:從 [動態內容] 清單中選取 [monitorCondition]
    • 引發日期:從 [動態內容] 清單中選取 [firedDateTime]
    • 受影響的資源:從 [動態內容] 清單中選取 [alertTargetIDs]
  6. 在 [主旨] 欄位中,從 [動態內容] 清單中選擇欄位,輸入靜態文字並包含從警示承載取得的內容,以建立主旨文字。 例如:

    • 警示:從 [動態內容] 清單中選取 [alertRule]
    • 具有嚴重性:從 [動態內容] 清單中選取 [severity]
    • 具有條件:從 [動態內容] 清單中選取 [monitorCondition]
  7. 於 [收件者] 欄位中輸入警示要傳送至的電子郵件地址。

  8. 選取 [儲存]。

    Screenshot that shows the Parameters tab on the Send an email pane.

您已建立邏輯應用程式,可將電子郵件傳送至指定的位址,其中包含觸發警示的詳細資料。

下一個步驟是建立動作群組以觸發邏輯應用程式。

建立動作群組

若要觸發邏輯應用程式,請建立動作群組。 然後建立使用該動作群組的警示。

  1. 移至 [Azure 監視器] 頁面,然後從左側窗格選取 [警示]

  2. 選取 [動作群組]> [建立]

  3. 選取 [訂用帳戶] 和 [資源群組] 和 [區域] 的值。

  4. 輸入 [動作群組名稱] 和 [顯示名稱] 的名稱。

  5. 選取動作索引標籤。

    Screenshot that shows the Actions tab on the Create an action group page.

  6. 在 [ 動作類型] 的 [動作] 索引標籤中,選取 [邏輯應用程式]

  7. 在 [邏輯應用程式] 區段中,從下拉式清單中選取邏輯應用程式。

  8. 將 [啟用一般警示結構描述] 設定為 [是]。 如果您選取 [否],警示類型決定使用哪一個警示結構描述。 如需警示結構描述的詳細資訊,請參閱內容特定警示結構描述

  9. 選取 [確定]。

  10. 在 [名稱] 欄位中輸入名稱。

  11. 選取 [檢閱 + 建立]>[建立]

    Screenshot that shows the Actions tab on the Create an action group page and the Logic App pane.

測試您的動作群組

  1. 選取您的動作群組。

  2. 在 [邏輯應用程式] 區段中,選取 [測試動作群組] (預覽)。

    Screenshot that shows an action group details page with the Test action group option.

  3. 從 [選取樣本類型] 下拉式清單中選取 [範例警示類型]

  4. 選取 [測試]。

    Screenshot that shows an action group details Test page.

    下列電子郵件傳送至指定的帳戶:

    Screenshot that shows a sample email sent by the Test page.

藉由使用您的動作群組建立規則

  1. 為您的其中一個資源建立規則

  2. 在規則的 [動作] 索引標籤中,選取 [選取動作群組]

  3. 從清單中選取您的動作群組。

  4. 選擇選取

  5. 完成規則的建立。

    Screenshot that shows the Actions tab on the Create an alert rule pane and the Select action groups pane.

下一步