EC2のメトリクスにメモリ使用率とディスク使用量を追加(モニタリングスクリプト使用)

参考

https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/mon-scripts.html

手順

1.必要なパッケージをインストール

$ sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64

2.モニタリング用スクリプトをダウンロード

# スクリプト保存用ディレクトリを作成
$ cd
$ mkdir custom-metrics
$ cd custom-metrics
# モニタリング用スクリプトをダウンロード
$ sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64
# ダウンロードファイルを解凍
$ unzip CloudWatchMonitoringScripts-1.2.2.zip && rm CloudWatchMonitoringScripts-1.2.2.zip

3.EC2のロールに以下の内容のポリシーをアタッチする

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData",
                "cloudwatch:GetMetricStatistics",
                "cloudwatch:ListMetrics",
                "ec2:DescribeTags"
            ],
            "Resource": "*"
        }
    ]
}

4.cronの設定

# crontabエディタを開く
$ crontab -e

以下の内容を追加する

*/5 * * * * ~/custom-metrics/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-space-util --disk-path=/ --from-cron