1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AnsibleでAmazon Linux2にamazon-cloudwatch-agentを入れる

Last updated at Posted at 2021-06-16

AnsibleでAmazon Linux2にamazon-cloudwatch-agentを入れてデータを取得する基本設定です。

設定ファイル

ここではウィザードで作ったBasicなEC2 Linuxの監視設定を利用します。

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

ファイルは以下の通りです。

ansible/roles/amazon-cloudwatch-agent/files/amazon-cloudwatch-agent.json
{
    "agent": {
        "metrics_collection_interval": 60,
        "run_as_user": "cwagent"
    },
    "metrics": {
        "append_dimensions": {
            "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
            "ImageId": "${aws:ImageId}",
            "InstanceId": "${aws:InstanceId}",
            "InstanceType": "${aws:InstanceType}"
        },
        "metrics_collected": {
            "collectd": {
                "metrics_aggregation_interval": 60
            },
            "disk": {
                "measurement": [
                    "used_percent"
                ],
                "metrics_collection_interval": 60,
                "resources": [
                    "*"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_used_percent"
                ],
                "metrics_collection_interval": 60
            },
            "statsd": {
                "metrics_aggregation_interval": 60,
                "metrics_collection_interval": 10,
                "service_address": ":8125"
            }
        }
    }
}

Ansibleタスク

collectdを使うためにepelを最初に有効化しています。

ansible/roles/amazon-cloudwatch-agent/tasks/main.yaml
---
collectd.html
- name: Enable epel repo
  shell: amazon-linux-extras install epel -y

- name: Install collectd
  yum:
    name: collectd
    state: latest

- name: Install amazon-cloudwatch-agent
  yum:
    name: amazon-cloudwatch-agent
    state: latest

- name: Copy amazon-cloudwatch-agent config
  copy:
    src: amazon-cloudwatch-agent.json
    dest: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
    mode: '0644'

- name: Fetch amazon-cloudwatch-agent config
  shell: |
    /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
      -a fetch-config \
      -m ec2 \
      -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \
      -s

- name: Restart amazon-cloudwatch-agent 
  systemd:
    name: amazon-cloudwatch-agent.service
    state: restarted

IAM権限あるAmazon Linux2のEC2サーバーならこれを適用するだけで、CloudWatch AgentによるOS上からの情報取得が動くはずです。

image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?