CloudWatchAgentとは
EC2にインストールすることで、デフォルトでは収集できないメモリ、ディスク、プロセスなどの情報がメトリクスで収集可能となる。
参考:ユーザーガイド-CloudWatch エージェントを使用してメトリクス、ログ、トレースを収集する
参考:CloudWatch エージェントにより収集されるメトリクス
インストール手順
参考:コマンドラインを使用した CloudWatch エージェントのインストール
- CloudWatch エージェントパッケージをダウンロード/インストールする
sudo dnf install -y https://s3.ap-northeast-1.amazonaws.com/amazoncloudwatch-agent-ap-northeast-1/redhat/amd64/latest/amazon-cloudwatch-agent.rpm
- 設定ウィザードの起動
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
設定ウィザードを起動すると以下の質問が表示されるので、キーボードで数字を入力後Enterキーを押下する。
数字を入力せずにEnterキーを押下することでdefault choiceが選択される。
- インストールを行うインスタンスのOS
On which OS are you planning to use the agent?
1. linux
2. windows
3. darwin
default choice: [1]:
- インストール先のサーバがEC2なのかオンプレなのか
Trying to fetch the default region based on ec2 metadata...
Are you using EC2 or On-Premises hosts?
1. EC2
2. On-Premises
default choice: [1]:
-
CloudWatchAgentを実行するユーザ
Which user are you planning to run the agent?
1. root
2. cwagent
3. others
default choice: [1]:
-
StatsDの使用有無
Do you want to turn on StatsD daemon?
1. yes
2. no
default choice: [1]:
-
CollectDの使用有無
※ yesを選択した場合、CollectDのインストールが行われていないとCloudWatchAgentが起動しないので注意
Do you want to monitor metrics from CollectD? WARNING: CollectD must be installed or the Agent will fail to start
1. yes
2. no
default choice: [1]:
- ホストのメトリクスを収集するか否か
Do you want to monitor any host metrics? e.g. CPU, memory, etc.
1. yes
2. no
default choice: [1]:
- CPUをコア毎にモニタリングするか否か
Do you want to monitor cpu metrics per core?
1. yes
2. no
default choice: [1]:
- ImageId, InstanceId, InstanceType, AutoScalingGroupNameを取得するか否か
Do you want to add ec2 dimensions (ImageId, InstanceId, InstanceType, AutoScalingGroupName) into all of your metrics if the info is available?
1. yes
2. no
default choice: [1]:
- メトリクスをInstanceIDで集約するか否か
Do you want to aggregate ec2 dimensions (InstanceId)?
1. yes
2. no
default choice: [1]:
- メトリクスの収集頻度
Would you like to collect your metrics at high resolution (sub-minute resolution)? This enables sub-minute resolution for all metrics, but you can customize for specific metrics in the output json file.
1. 1s
2. 10s
3. 30s
4. 60s
default choice: [4]:
-
取得するメトリクスセットの選択
Which default metrics config do you want?
1. Basic
2. Standard
3. Advanced
4. None
default choice: [1]:
- これまでの設定値をJsonが表示されます。問題が無ければ1を選択
Current config as follows:
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
~~省略~~
}
}
Are you satisfied with the above config? Note: it can be manually customized after the wizard completes to add additional items.
1. yes
2. no
default choice: [1]:
- 他の設定ファイルをインポートするか否か
Do you have any existing CloudWatch Log Agent (http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AgentReference.html) configuration file to import for migration?
1. yes
2. no
default choice: [2]:
- ログファイルの収集を行うか否か
Do you want to monitor any log files?
1. yes
2. no
default choice: [1]:
yesにした場合[Log file path:][Log group name:][Log stream name:][Log Group Retention in days]をログに合わせて入力する。
- X-rayの使用有無
Do you want the CloudWatch agent to also retrieve X-ray traces?
1. yes
2. no
default choice: [1]:
- これまでの設定値をJsonが表示されます(2回目)
Saved config file to /opt/aws/amazon-cloudwatch-agent/bin/config.json successfully.
Current config as follows:
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
~~省略~~
}
}
Please check the above content of the config.
The config file is also located at /opt/aws/amazon-cloudwatch-agent/bin/config.json.
Edit it manually if needed.
- 設定をSSMパラメータストアに保存するか否か
Do you want to store the config in the SSM parameter store?
1. yes
2. no
default choice: [1]:
yesにした場合[パラメータストア名]][保存するリージョン][AWSクレデンシャル情報]を保存するパラメータストアに合わせて入力する。
CloudWatchAgent操作コマンド
起動
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
起動確認
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
起動ログの確認
sudo systemctl status amazon-cloudwatch-agent
停止
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop