[AWS]CloudWatch AgentのWindows版を試す

2017.12.18

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

コンニチハ、千葉です。

CloudWatch Agentが発表され、メトリクスやログの収集も1つのエージェントで行えるようになりました。詳細については以下のエントリを参照してください。

新しいCloudWatch Agentでメトリクスとログの収集が行なえます

今回はWindows版について試してみます。

試してみた

環境

  • リージョン:東京
  • AMI:ami-fc259e9a (Windows_Server-2016-Japanese-Full-Base-2017.11.29)
  • インスタンスタイプ:t2.small

IAMポリシーのアタッチ

起動したEC2にAmazonSSMFullAccessAmazonEC2ReadOnlyAccessをアタッチします。

SSMのバージョンアップ

SSMエージェントのバージョンが2.2.93.0以上である必要があります。Systems Manager経由でアップデートしてみましょう。

現在のバージョンは2.2.64.0だったので、アップデートします。Run Commandから実行します。

バージョンが2.2.103.0にアップデートできました。

CloudWatch Agentのインストール

Run Commandでインストールします。Windowsの場合も、ログインせずにインストールすることができます。AmazonCloudWatchAgentを指定します。

ログを確認したところSuccessfully installed arn:aws:ssm:::package/AmazonCloudWatchAgent 1.73.9と表示されました。問題なくインストールできてます。

設定の出力

CloudWatch Agentの設定をパラメータストアに格納できます。これにより複数のインスタンスがあったとしても、SSMを利用して一括で設定することができます。設定情報を出力するためにWindowsにログインしコマンドを実行します。同じ設定を他のインスタンスにも適用する場合、この作業は1度のみ実施します。

windowsにログインしコンマンドプロンプトを起動し、以下を入力します。

cd "C:\Program Files\Amazon\AmazonCloudWatchAgent"
amazon-cloudwatch-agent-config-wizard.exe

各設定項目を設定して行きます。OSはwindowsを選択します。

Which OS are you planning to use the agent?
1. linux
2. windows
default choice: [2]:

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]:


既存のCloudWatch Logの設定を移行するかですが、今回は新規なので移行しないを選択します。


Do you have any existing CloudWatch Log Agent configuration file to import for migration?
1. yes
2. no
default choice: [2]:

CPU、memoryなどメトリクスを取得を選択します。

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? Additional CloudWatch charges may apply.
1. yes
2. no
default choice: [1]:

利用可能なディメンジョンを取得を選択します。

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]:

60秒間隔でメトリクスを取得します。

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を選択します。

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 want to monitor any customized log files?
1. yes
2. no
default choice: [1]:
2

イベントログの監視を選択します。

Do you want to monitor any Windows event log?
1. yes
2. no
default choice: [1]:

イベントログ名を指定します。

Windows event log name:
default choice: [System]

取得するイベントログのレベルを指定します。

Do you want to monitor VERBOSE level events for Windows event log System ?
1. yes
2. no
default choice: [1]:

Do you want to monitor INFORMATION level events for Windows event log System ?
1. yes
2. no
default choice: [1]:

Do you want to monitor WARNING level events for Windows event log System ?
1. yes
2. no
default choice: [1]:

Do you want to monitor ERROR level events for Windows event log System ?
1. yes
2. no
default choice: [1]:

Do you want to monitor CRITICAL level events for Windows event log System ?
1. yes
2. no
default choice: [1]:

CloudWatchへ出力するロググループ名を指定します。

Log group name:
default choice: [System]

CloudWatchログへ出力するときのフォーマットを指定します。

In which format do you want to store windows event to CloudWatch Logs?
1. XML: XML format in Windows Event Viewer
2. Plain Text: Legacy CloudWatch Windows Agent (SSM Plugin) Format
default choice: [1]:

追加のログ設定は実施しないので2を選択します。

Do you want to specify any additional Windows event log to monitor?
1. yes
2. no
default choice: [1]:
2


設定内容が記載されたjsonが表示されます。

[code:json]
{
        "logs": {
                "logs_collected": {
                        "windows_events": {
                                "collect_list": [
                                        {
                                                "event_format": "xml",
                                                "event_levels": [
                                                        "VERBOSE",
                                                        "INFORMATION",
                                                        "WARNING",
                                                        "ERROR",
                                                        "CRITICAL"
                                                ],
                                                "event_name": "System",
                                                "log_group_name": "System"
                                        }
                                ]
                        }
                }
        },
        "metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
                "metrics_collected": {
                        "Memory": {
                                "measurement": [
                                        "% Committed Bytes In Use"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "Paging File": {
                                "measurement": [
                                        "% Usage"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        }
                }
        }
}
[/code]

設定をパラメータストアに保管を選択します。


Do you want to store the config in the SSM parameter store?
1. yes
2. no
default choice: [1]:

パラメータストア名を指定します。

What is the parameter store name do you want to store your config?
default choice: [agent-config-windows]

データを格納するパラメータストアのリージョンを指定します。

Trying to fetch the default region based on ec2 metadata...
Which region do you want to store the config in the parameter store?
default choice: [ap-northeast-1]

パラメータストアに格納するためのクレデンシャルを指定します。これで設定が完了です。

Which AWS credential should be used to send json config to parameter store?
1. XXXXXXXX(From SDK)
2. Other
default choice: [1]:

パラメータストアを確認してみます。CloudWatch Agent用の設定が格納されてました。

設定を反映

パラメータストアに出力した設定を利用し、CloudWatch Agentの設定をします。これはSSMを利用して実施します。

AmazonCloudWatch-ManageAgentを選択し、設定を更新します。オプションに設定が格納されているパラメータストア名agent-config-windowsを指定します。

CloudWatchへの出力確認

CloudWatchからメトリクスが取得できているか確認します。

イベントログが取得できています!

ディスク使用率やメモリも取得できてました!

最後に

今回は、Windowsに対してCloudWatch Agentの設定を行いました。多少のパラメータに差はありますが、手順としてはLinuxと同じ手順で実施できました。OSの差をあまり意識せずに設定できるのは便利だなぁと思いました。また、複数サーバに対して一括で設定できるのも効率的でかなり素敵です。

参考