LoginSignup
2
2

More than 3 years have passed since last update.

[Azure] アラート設定をCLIで取得する

Last updated at Posted at 2021-02-09

アラートはAzureの監視機能の一つで、メトリック等の条件によって通知(メール、SMS等)を送信する機能です。障害発生時の通知が代表的な使い方になります。

Azureポータルでは、モニターのメニューからアラート設定を参照することができます。

image.png

「アラートルールの管理」を選択すると設定済みルール一覧が表示されますが、今回はこの設定をCLIで取得する方法を調査しました。

image.png

通知は様々なデータソースに対して設定できるため、その設定は一つのCLI/APIで取得することはできませんでした。以降、シグナルの種類ごとに取得する方法を順に記載します。

image.png

1) CLI: az monitor activity-log alert で取得(アクティビティログ, Advisor, Service Health, Resource Health)

シグナル種別がアクティビティログ, Advisor, Service Health, Resource Healthのものは CLI: az monitor activity-log alert で取得できます。

image.png

$ az monitor activity-log alert list

必要に応じて取得項目を絞り込みます。

$ az monitor activity-log alert list \
  | jq -r '.[] | ( .id , .name , .actions .actionGroups[] .actionGroupId , "\n")'
/subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/microsoft.insights/activityLogAlerts/Service-Health-Global
Service-Health-Global
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

/subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/microsoft.insights/activityLogAlerts/Service-Health-JapanEast
Service-Health-JapanEast
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

取得されたidを引数にaz monitor activity-log alert showコマンドを実行すれば詳細が取得できます。

$ az monitor activity-log alert show --ids <listコマンドで取得したID>

また、取得されたactionGroup(のid)を引数にaz monitor action-group showコマンドを実行すれば、通知先の詳細が取得できます(参照頻度の低そうな項目は一部割愛しています)。

$ az monitor action-group show --ids /subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general
{
  "armRoleReceivers": [],
  "automationRunbookReceivers": [],
  "azureAppPushReceivers": [],
  "azureFunctionReceivers": [],
  "emailReceivers": [
    {
      "emailAddress": "user_name@demo.com",
      "name": "Email alert to user_name",
      "status": "Enabled",
      "useCommonAlertSchema": false
    }
  ],
  "enabled": true,
  "identity": null,
  "kind": null,
  "location": "Global",
  "logicAppReceivers": [],
  "name": "action_group_general",
  "resourceGroup": "resource_group",
  "tags": {},
  "type": "Microsoft.Insights/ActionGroups",
  "voiceReceivers": [],
  "webhookReceivers": []
}

2) CLI: az monitor metrics alertで取得(メトリック)

シグナル種別がメトリックのものは CLI: az monitor metrics alert で取得できます。

image.png

$ az monitor metrics alert list

activity-logと同様に取得項目を絞り込みます。actionGroupのjson構成が少し違っているので注意が必要です。

$ az monitor metrics alert list \
  | jq -r '.[] | ( .id , .name , .actions[] .actionGroupId , "\n")'
/subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/microsoft.insights/metricAlerts/CPU Utilization
CPU Utilization
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

/subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/microsoft.insights/metricAlerts/Memory Utilization
Memory Utilization
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

取得されたidを使用してアラート詳細、通知の詳細が同様に取得できます。

アラート詳細:

$ az monitor metrics alert show --ids <listコマンドで取得したID>

通知の詳細:

$ az monitor action-group show --ids <listコマンドで取得した通知のID>

3) CLI: az monitor scheduled-queryで取得(ログ検索)

シグナル種別がログ検索のものは CLI: az monitor scheduled-query で取得できます。

image.png

$ az monitor scheduled-query list

絞り込みと詳細取得、通知の詳細取得はメトリックと同様に可能です。

$ az monitor metrics alert list \
  | jq -r '.[] | ( .id , .name , .actions[] .actionGroupId , "\n")'
/subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/microsoft.insights/scheduledqueryrules/Error EventLog
Error EventLog
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

/subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/microsoft.insights/scheduledqueryrules/Memory Utilization
Memory Utilization
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

アラート詳細:

$ az monitor scheduled-query show --ids <listコマンドで取得したID>

通知の詳細:

$ az monitor action-group show --ids <listコマンドで取得した通知のID>

一点注意点としては、正確にはこのコマンドのみシグナルの種類:ログ検索ではなく「ターゲットリソースの種類がLog Analyticsワークスペース」のものが出力対象となっています。そのため、下図のように設定されている場合は、2) と 3)で取得される設定に一部重複が発生します。アラート一覧を作成する場合は、その重複を取り除く必要があります。

image.png

4) REST APIで取得(以前のログ検索)

3) でログ検索の取得方法を確認しましたが、2019年6月1日より前に作成されたLog Analyticsワークスペースのアラート設定は対象外となっており、別の方法(REST API)で取得する必要があります。

その詳細は こちらのページ に記載されています。事前に armclient をインストールしておく必要があります。

検索条件の取得:

$ ./armclient get "<Log AnalyticsワークスペースのID>"/savedSearches?api-version=2015-03-20

実行例:

$ ./armclient get /subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches?api-version=2015-03-20 | jq -r '.value[] .id'
subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches/02dfde9f-xxxxxxxxxxxxx-ac839e9da8b4
subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches/0566bc87-xxxxxxxxxxxxx-b31c0be0a993
subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches/10f8f280-xxxxxxxxxxxxx-a3550550358b

続いて、取得したidからスケジュールを取得します。idは実行例にあるように先頭の"/"が無くなっているので補完してください。

$ ./armclient get /"<取得した検索条件のID>"/schedules?api-version=2015-03-20

実行例:

$ ./armclient get /subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches/02dfde9f-xxxxxxxxxxxxx-ac839e9da8b4/schedules?api-version=2015-03-20 | jq -r '.value[] .id'
subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches/02dfde9f-xxxxxxxxxxxxx-ac839e9da8b4/schedules/3fb7d1a2-xxxxxxxxxxxxx-813b37fded3c

最後に、取得したスケジュールのidからactionGroupのidが取得できます。id先頭の"/"は同様に補完します。

$ ./armclient get /"<取得したスケジュールのID>"/actions?api-version=2015-03-20

実行例:

$ ./armclient get /subscriptions/ccexxxxxxxxxxe23/resourceGroups/resource_group/providers/Microsoft.OperationalInsights/workspaces/log_analytics_workspace_name/savedSearches/02dfde9f-xxxxxxxxxxxxx-ac839e9da8b4/schedules/3fb7d1a2-xxxxxxxxxxxxx-813b37fded3c/actions?api-version=2015-03-20 | jq -r '.value[] .properties .AzNsNotification .GroupIds[]'
/subscriptions/ccexxxxxxxxxxe23/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

この後のアクショングループの取得はこれまで同様にaz monitor action-group showコマンドで可能です。

$ az monitor action-group show --ids <APIで取得したactionGroupのid>

5) CLI: az rest --method で取得(Smart Detector)

最後に、シグナル種別がSmart Detectorの設定についてです。これは現在専用のAPIは無いようでしたので、次のように CLI: az rest --method で取得します。

image.png

種別がSmart Detectorのものは多くの場合Application Insightで設定したものが該当するかと思います。

image.png

$ az rest --method GET --uri https://management.azure.com/subscriptions/<サブスクリプションID>/providers/microsoft.alertsmanagement/smartDetectorAlertRules?api-version=2019-06-01

実行例:

$ az rest --method GET --uri https://management.azure.com/subscriptions/633xxxxxxxxxxa81/providers/microsoft.alertsmanagement/smartDetectorAlertRules?api-version=2019-06-01 \
  | jq -r '.value[] | ( .id , .resourceGroup , .name , .properties .actionGroups .groupIds[] , "\n")'
/subscriptions/633xxxxxxxxxxa81/resourcegroups/resource_group/providers/microsoft.alertsmanagement/smartdetectoralertrules/failure anomalies
resource_group
Failure Anomalies
/subscriptions/633xxxxxxxxxxa81/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general

取得された情報を使用してアラート詳細が取得できます。

アラート詳細の取得:

$ az rest --method GET --uri https://management.azure.com/subscriptions/<サブスクリプションID>/resourcegroups/<リソースグループID>/providers/microsoft.alertsmanagement/smartDetectorAlertRules/<アラートルール名>?api-version=2019-06-01

ここでは一覧の取得と

  • パスにリソースグループが入っている
  • アラートルール名はIDではなく名前なので、スペース等が入っているとエスケープの必要がある

の2点違いがあるので注意が必要です。

実行例(参照頻度の低そうな項目は一部割愛しています):

$ az rest --method GET --uri https://management.azure.com/subscriptions/633xxxxxxxxxxa81/resourcegroups/resource_group/providers/microsoft.alertsmanagement/smartDetectorAlertRules/failure%20anomalies?api-version=2019-06-01
{
  "id": "/subscriptions/633xxxxxxxxxxa81/resourcegroups/resource_group/providers/microsoft.alertsmanagement/smartdetectoralertrules/failure anomalies",
  "location": "global",
  "name": "Failure Anomalies",
  "properties": {
    "actionGroups": {
      "groupIds": [
        "/subscriptions/633xxxxxxxxxxa81/resourcegroups/resource_group/providers/microsoft.insights/actiongroups/action_group_general"
      ]
    },
    "description": "Failure Anomalies notifies",
    "detector": {
      "description": "Failure Anomalies notifies",
      "id": "FailureAnomaliesDetector",
      "name": "Failure Anomalies"
    },
    "severity": "Sev3",
    "state": "Enabled"
  },
  "resourceGroup": "resource_group",
  "tags": {},
  "type": "Microsoft.AlertsManagement/smartDetectorAlertRules"
}

通知の詳細はこれまで同様にaz monitor action-group showコマンドで可能です。

$ az monitor action-group show --ids <listコマンドで取得した通知のID>

以上で一通りのアラート設定が取得できました。

参考資料

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2