【メモ】CloudWatchAgentのConfig


## 環境
OS：AmazonLinux2
エージェント：amazon-cloudwatch-agent:1.247354.0b251981-1.amzn2.x86_64

## CloudWatchAgentをインストール

```
# 標準リポジトリからインストール
yum install amazon-cloudwatch-agent

# jsonファイルを作成
touch /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent.json

# jsonに追記する
vi /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent.json

# jsonを読み込ませる
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent.json
```
## 補足
<details>
<summary>
amazon-cloudwatch-agent.json()
</summary>

t2での稼働は確認してる
collectdがなくてもswapの収集可

```json
{
    "agent": {
            "metrics_collection_interval": 60,
            "run_as_user": "root",
            "region": "ap-northeast-1",
            "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
    },
    "logs": {
            "logs_collected": {
                    "files": {
                            "collect_list": [
                                    {
                                            "file_path": "var/log/messages",
                                            "log_group_name": "messages",
                                            "log_stream_name": "{instance_id}",
                                            "retention_in_days": 90,
                                            "timestamp_format": "%b %d %H:%M:%S"
                                    },
                                    {
                                            "file_path": "var/log/journal",
                                            "log_group_name": "journal",
                                            "log_stream_name": "{instance_id}",
                                            "retention_in_days": 90,
                                            "timestamp_format": "%b %d %H:%M:%S"
                                    },
                                    {
                                            "file_path": "var/log/secure",
                                            "log_group_name": "secure",
                                            "log_stream_name": "{instance_id}",
                                            "retention_in_days": 90,
                                            "timestamp_format": "%b %d %H:%M:%S"
                                    }
                            ]
                    }
            }
    },
    "metrics": {
            "namespace": "yournamespace",
            "aggregation_dimensions": [
                    [
                            "InstanceId"
                    ]
            ],
            "append_dimensions": {
                    "InstanceId": "${aws:InstanceId}"
            },
            "metrics_collected": {
                    "disk": {
                            "measurement": [
                                    "used_percent"
                            ],
                            "metrics_collection_interval": 300,
                            "resources": [
                                    "/"
                            ],
                            "ignore_file_system_types": [
                                "tmpfs",
                                "devtmpfs"
                            ]
                    },
                    "mem": {
                            "measurement": [
                                    "mem_used_percent"
                            ],
                            "metrics_collection_interval": 300
                    },
                    "swap": {
                            "measurement": [
                                    "swap_used_percent"
                            ],
                            "metrics_collection_interval": 300
                    }
            }
    }
}
```


</details>