CloudWatch Agentでオンプレミスサーバのメトリクスを収集

CloudWatch Agentでオンプレ環境のLinuxサーバーのメトリクスを取得します。

AWS Systems Managerでオンプレ環境のLinuxを管理できるようにする

以下の記事でAWS Systems Managerでオンプレ環境のLinuxを管理できるようにしました。

CloudWatch Agentのインストール

AWS Systems Manager経由でインストールします。ランコマンドでコマンドのドキュメントの[AWS-ConfigureAWSPackage]を選択します。

ターゲットに対象のオンプレミスサーバーを選択し、コマンドのパラメータは以下のように設定します。

Action: Install
Name: AmazonCloudWatchAgent
Version: latest

実行して、実行のステータスが「成功」で終了したら出力を確認します。

AWS CLIのインストール

オンプレミスサーバーにAWS CLIをインストールします。
python が入っていない場合はインストールします。(インストール手順は省略)
pip が入っていない場合、以下の手順でインストールします。

curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip --version

pipを使用して AWS CLIをインストールします。

pip install awscli --upgrade
aws --version

CloudWatch Agent用のIAMユーザの作成・設定

オンプレミスサーバーの為、IAMロールの代わりにIAMユーザーを使います。
オンプレミスサーバー用のIAMユーザーを作成し、「AmazonEC2RoleforSSM」と「CloudWatchAgentServerPolicy」のポリシーを割り当てます。

※ オンプレミスサーバーの設定1台目は、オンプレミスサーバー用のAgent設定をウィザードで作成し、パラメータストアに格納しますので、格納が終わるまで「CloudWatchAgentAdminPolicy」も付けておきます。終わったらこのポリシーは外しましょう。

作成したIAMユーザー情報のアクセスキーとシークレットアクセスキーで「AmazonCloudWatchAgent」プロファイルを設定します。

aws configure --profile AmazonCloudWatchAgent
AWS Access Key ID [None]: xxxxxxxxxxxxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]:ap-northeast-1
Default output format [None]:

オンプレミスサーバー用のCloudWatch Agent設定の作成

CloudWatch Agent の設定ファイル生成

オンプレミスサーバー用のCloudWatch Agentの設定を行い設定ファイルを生成します。以下のコマンドでウィザード形式で設定を進めます。

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
=============================================================
= Welcome to the AWS CloudWatch Agent Configuration Manager =
=============================================================

ウィザードでは以下を設定します。hosts の設定でデフォルトのEC2でなく「On-Premises」を選択することに注意しましょう。

メトリクス設定内容

Which OS are you planning to use the agent? :Linux(default)
Are you using EC2 or On-Premises hosts? :On-Premises
Do you want to monitor any host metrics? :yes(default)
Do you want to monitor cpu metrics per core? :yes(default)
Would you like to collect your metrics at high resolution ? :60s (ここは各自で決めてください)
Which default metrics config do you want? :Advanced (Basic/Standard/Advanced/None から選択可)

設定内容が json で表示された後で以下が表示ますので、確認したらエンターキーを押します。

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 configuration file to import for migration? :no(default)(移行元のLog Agent がない場合)
Do you want to monitor any log files? :yes(default)
Log file path: /var/log/messages
Log group name: messages
Do you want to specify any additional log files to monitor? :no(他に監視するログファイルがない場合)

設定内容と以下のメッセージが表示されます。修正する場合は手動で設定ファイルを修正します。

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.

CloudWatch Agent 設定を SSMのParameter store に保存

生成した設定ファイルをSSMのParameter storeに保存します。EC2のパラメータストア名とは異なる名前を付けることに注意しましょう。ここでは「AmazonCloudWatch-linux-OnPremise」と付けました。

Parameter Store 保存設定
Do you want to store the config in the SSM Parameter store? :yes(default)
What is the Parameter store name do you want to store your config? :AmazonCloudWatch-linux-OnPremise
Which region do you want to store the config in the Parameter store? :ap-northeast-1(default)

EC2の時の設定と異なり、パラメータストアに転送するときに使うクレデンシャルの選択肢に「From Profile」が追加されています。EC2の時と同じように「From SDK」を選択すると以下のように怒られます。

Which AWS credential should be used to send json config to parameter store?
1. ********************(From SDK)
2. ********************(From Profile: AmazonCloudWatchAgent)
3. Other
default choice: [1]:
1
Please make sure the creds you used have the right permissions configured for SSM access.

先にIAMユーザーのセキュリティーキーを設定した際に作成したプロファイル「AmazonCloudWatchAgent」を指定してパラメータストアへ正常に設定ファイルが格納されました。

Which AWS credential should be used to send json config to parameter store?
1. ********************(From SDK)
2. ********************(From Profile: AmazonCloudWatchAgent)
3. Other
default choice: [1]:
2
Successfully put config to parameter store AmazonCloudWatch-linux-OnPremise.
Program exits now.

CloudWatch Agent設定の配布

AWS Systems Manager から、SSMのParameter store に保存されているCloudWatch Agent 設定をオンプレミスサーバーへ配布します。ランコマンドでコマンドのドキュメントの [AmazonCloudWatch-ManageAgent]を選択し、ターゲットに対象のオンプレミスサーバーを選択します。

コマンドのパラメータは以下のように設定します。Optional Configuration Location に Configurationを保存したParameter storeの名称を記載します。
EC2の時と異なり、Modeを「onPremise」で、Optional Configuration Locationはオンプレミスサーバー用のパラメータ名で設定することに注意しましょう。

Action: configure
Mode: onPremise
Optional Configuration Source: ssm
Optional Configuration Location: AmazonCloudWatch-linux-OnPremise
Optional Restart: yes

実行して、実行のステータスが「成功」で終了したら出力を確認します。
CloudWatchメトリクスを開くと、当該オンプレミスサーバーのメトリクスが反映されています。

コメントを残す

コメントは当サイト運営者の承認後に反映されます。
お名前はニックネームでも結構です。

Comments are allowed only in Japanese. / 日本語が含まれない投稿は無視されますのでご注意ください。