知ったことまとめ
1. アラームの有効/無効
マネジメントコンソールからは有効/無効の状態は確認できるものの、設定はできないという仕様。
1.1. 各アラームの有効/無効の確認
AWSマネジメントコンソールからできる。
アラームの一覧ページで右上歯車アイコンから。
「アクションが有効になっています」スイッチをONにすると一覧で確認できるようになる。
1.2. 各アラームの有効/無効設定
aws-cliからしかできない・・・
# aws-cliのインストール(Mac)
brew install awscli
# awscliの設定(アクセスキーID、シークレットアクセスキー、リージョン、出力形式を設定)
aws configure
# アラームの状態確認
aws cloudwatch describe-alarms --alarm-name-prefix <アラーム名の接頭辞文字列(表示を絞り込みたいときに)> --query "MetricAlarms[].[AlarmName,AlarmDescription,ActionsEnabled]" --output table
# アラーム無効化
aws cloudwatch disable-alarm-actions --alarm-name <AlarmName>
# アラーム有効化
aws cloudwatch enable-alarm-actions --alarm-name <AlarmName>
2. アラームの追加(aws-cliから行う場合)
<>内にセットすべき値は、コンソールで確認できる項目名の値に相当
# アラーム追加(アラーム無効の状態)
# パラメータ詳細はhttps://docs.aws.amazon.com/cli/latest/reference/cloudwatch/put-metric-alarm.html
aws cloudwatch put-metric-alarm --alarm-name <アラーム名> --alarm-description "<アラームの説明>" --namespace <名前空間> --metric-name <メトリクス名> --dimensions Name=InstanceId,Value=<InstanceId> --statistic <統計(※)> --threshold <...よりも> --comparison-operator <XXXが次の時...(※)> --period <期間> --evaluation-periods <アラームを実行するデータポイント(分母)> --datapoints-to-alarm <アラームを実行するデータポイント(分子)> --treat-missing-data <欠落データの処理(※)> --alarm-actions <アラーム状態になったときのアクション(※)> --no-actions-enabled
# アラームを有効化
aws cloudwatch enable-alarm-actions --alarm-name <アラーム名>
※統計:以下の値から設定
- SampleCount
- Average
- Sum
- Minimum
- Maximum
※XXXが次の時...:以下の値から選択
- GreaterThanOrEqualToThreshold : >=
- GreaterThanThreshold : >
- LessThanThreshold : <
- LessThanOrEqualToThreshold : <=
※期間:秒単位で指定
(例) 1時間→3600
※欠落データの処理
- breaching : 欠落データを不正(しきい値を超えている)として処理
- notBreaching : 欠落データを適正(しきい値を超えていない)として処理
- ignore : 欠落データを無視(アラーム状態を維持する)として処理
- missing : 欠落データを見つかりませんとして処理
※アラーム状態になったときのアクション:
以下から選択
# EC2停止
arn:aws:automate:*region* :ec2:stop
# EC2終了
arn:aws:automate:*region* :ec2:terminate
# EC2回復(多分停止 + 起動。つまりインスタンスの物理ホストが変わる)
arn:aws:automate:*region* :ec2:recover
# EC2再起動(インスタンスの物理ホストが変わらない)
arn:aws:automate:*region* :ec2:reboot
# メール配信
arn:aws:sns:region :account-id :sns-topic-name
# ?
arn:aws:autoscaling:region :account-id :scalingPolicy:policy-id :autoScalingGroupName/group-friendly-name :policyName/policy-friendly-name
# ロールで実行するとき
# EC2停止
arn:aws:swf:*region* :*account-id* :action/actions/AWS_EC2.InstanceId.Stop/1.0
# EC2終了
arn:aws:swf:*region* :*account-id*
:action/actions/AWS_EC2.InstanceId.Terminate/1.0
# EC2再起動
arn:aws:swf:*region* :*account-id* :action/actions/AWS_EC2.InstanceId.Reboot/1.0
3. アラートメールの通知テスト
アラートメールの通知テストをしたいアラームについて、
その状態を無理やり手動で「アラーム」や「OK」の状態にして飛ばす。
参考https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/US_AlarmAtThresholdEC2.html
# 通知テスト対象アラームの状態確認
aws cloudwatch describe-alarms --alarm-name-prefix <アラーム名> --query "MetricAlarms[].[AlarmName,AlarmDescription,StateValue]" --output table
# 強制的に「OK」にする
aws cloudwatch set-alarm-state --alarm-name <アラーム名> --state-reason "alert mail test" --state-value OK
# 強制的に「アラーム」にする
aws cloudwatch set-alarm-state --alarm-name <アラーム名> --state-reason "alert mail test" --state-value ALARM