2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Azure] Azure Monitor アラートルールをCSVで一覧取得する方法

Posted at

VMのリソース監視等を、Azure Monitorのアラートルールを作成して監視するぜ!ってときありますよね。
各サーバ×監視種別(CPU使用率、メモリ使用率etc.)作成すると、すごい数になるので、設定誤りないかな?と不安になります。

今回は、設定したアラートルールをCSV形式で一覧取得する方法をご紹介します。
これで横串確認が楽になるはず。

前提

・対象:ログアラートのみのようです。
 メトリックアラートやアクティビティログは別コマンドになるようですが、試せていません。
 https://docs.microsoft.com/ja-jp/azure/azure-monitor/alerts/alerts-unified-log

・利用ツール:Azure Powershellを使用します。

コマンド

アラートルール名で検索し、定義情報を取得させています。
検索文字列は正規表現可能で、たとえば「Testで始まる」を条件とするなら "^Test.*" のように記載可能です。

$ResourceGroup = "<リソースグループ名>"
$searchStr = "<アラートルール名の検索文字列>"
$CsvPath = ".\AlertRulesDefinition.csv"

Get-AzScheduledQueryRule -ResourceGroupName $ResourceGroup | Where-Object {$_.Name -Match $searchStr} | Select-Object -Property `
@{Label="アラート ルール名";Expression={($_.Name)}}, `
@{Label="ターゲット リソース";Expression={($_.Source.dataSourceId)}}, `
@{Label="検索クエリ";Expression={($_.Source.Query)}}, `
@{Label="基準";Expression={($_.Source.queryType)}}, `
@{Label="演算子";Expression={($_.Action.Trigger.ThresholdOperator)}}, `
@{Label="しきい値";Expression={($_.Action.Trigger.Threshold)}}, `
@{Label="期間 (分単位)";Expression={($_.Schedule.TimeWindowInMinutes)}}, `
@{Label="頻度 (分単位)";Expression={($_.Schedule.frequencyInMinutes)}}, `
@{Label="アクション グループ";Expression={($_.Action.AznsAction.ActionGroup)} }, `
@{Label="メールの件名";Expression={($_.Action.AznsAction.EmailSubject)} }, `
@{Label="説明";Expression={($_.Description)} }, `
@{Label="重要度";Expression={($_.Action.Severity)}} `
 | Export-Csv -path $CsvPath -Encoding UTF8 -NoTypeInformation

実行結果

出力されたCSVファイルの中身は、下記のようになります。

アラート ルール名 ターゲット リソース 検索クエリ 基準 演算子 しきい値 期間
(分単位)
頻度
(分単位)
アクション グループ メールの件名 説明 重要度
TestVM_VMHealth  /subscriptions/xxx/resourceGroups/yyy
/providers/Microsoft.OperationalInsights
/workspaces/zzz
Heartbeat
where Computer =="test.test.co.jp"         
where TimeGenerated > ago(5m)
ResultCount LessThan 1     5     5     /subscriptions/xxx/resourceGroups/yyy
/providers/microsoft.insights/actionGroups/zzz
[ERROR]TestVM_VMHealth メール本文      1   
2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?