본문 바로가기

AWS/CloudWatch

Amazon Linux 2에서 CloudWatch Agent 설치하기

안녕하세요. 오늘은 AWS EC2에서 기본적으로 지원되지 않는 지표(Ex. Memory ..)를 수집하기 위해서
Amazon Linux 2에 CloudWatch Agent를 설치하고 구성해보도록 하겠습니다.

Agent 구성방식은 AWS 에서 제공하는 Wizard를 사용하는 방법과 구성파일을 수동으로 생성하는 방법이 있는데,
포스팅에서는 Wizard를 사용하여 구성해보도록 하겠습니다.

어떤 방법으로 구성하시더라도, 이후 사용자가 커스터마이징 할 수 있으니 걱정하지 마세요.


EC2 인스턴스가 사용할 IAM Role에 CloudWatchAgentServerPolicy를 부착하도록 하겠습니다.

 

  • AWS Web Console - IAM - Roles - Create role - Choose a use case == EC2 - Next: Permissions

EC2 선택

 

  • CloudWatchAgentServerPolicy - Next: Tags - 적절한 Tag와 역할이름, 설명 입력하여 생성

CloudWatchAgentServerPolicy 선택.


생성한 Role을 EC2 인스턴스에 부착하겠습니다.

 

  • AWS Web Console - EC2 - Instances - Select - Actions - Instance Settings - Attach/Replace IAM Role

위에서 생성한 역할을 연결


다음은 EC2 인스턴스 내부에 접속하여서 CloudWatch-Agent 파일을 Install 하겠습니다.

[root@localhost ~]# sudo yum -y install amazon-cloudwatch-agent

 

다운이 완료되면 Wizard 파일은 /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard 에
있을건데요, 그렇지 않다면 아래 명령어를 쳐서 Wizard 파일의 위치를 찾고 해당 디렉터리로 이동합니다.

[root@localhost ~]# find / -name amazon-cloudwatch-agent-config-wizard

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

[root@localhost ~]# cd /opt/aws/amazon-cloudwatch-agent/bin

 

이후 아래의 amazon-cloudwatch-agent-config-wizard 파일을 실행하여 아래의 물음에 답합니다.
저는 단순하게 Memory와 Disk 수집을 위한 용도이기에 별도의 Log 파일 수집과 SSM 마이그레이션,

파라미터 스토어에 동기화 등은 설정하지 않았습니다.

 

[root@localhost bin]# ./amazon-cloudwatch-agent-config-wizard
=============================================================
= Welcome to the AWS CloudWatch Agent Configuration Manager =
=============================================================
On which OS are you planning to use the agent?
1. linux
2. windows
default choice: [1]:
1
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]:
1
Which user are you planning to run the agent?
1. root
2. cwagent
3. others
default choice: [1]:
1
Do you want to turn on StatsD daemon?
1. yes
2. no
default choice: [1]:
2
Do you want to monitor metrics from CollectD?
1. yes
2. no
default choice: [1]:
2
Do you want to monitor any host metrics? e.g. CPU, memory, etc.
1. yes
2. no
default choice: [1]:
1
Do you want to monitor cpu metrics per core? Additional CloudWatch charges may apply.
1. yes
2. no
default choice: [1]:
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]:
2
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]:
4
Which default metrics config do you want?
1. Basic
2. Standard
3. Advanced
4. None
default choice: [1]:
3
Current config as follows:
{
        "agent": {
                "metrics_collection_interval": 60,
                "run_as_user": "root"
        },
        "metrics": {
                "metrics_collected": {
                        "cpu": {
                                "measurement": [
                                        "cpu_usage_idle",
                                        "cpu_usage_iowait",
                                        "cpu_usage_user",
                                        "cpu_usage_system"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ],
                                "totalcpu": false
                        },
                        "disk": {
                                "measurement": [
                                        "used_percent",
                                        "inodes_free"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "diskio": {
                                "measurement": [
                                        "io_time",
                                        "write_bytes",
                                        "read_bytes",
                                        "writes",
                                        "reads"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "netstat": {
                                "measurement": [
                                        "tcp_established",
                                        "tcp_time_wait"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "swap": {
                                "measurement": [
                                        "swap_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        }
                }
        }
}
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]:
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]:
2
Do you want to monitor any log files?
1. yes
2. no
default choice: [1]:
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"
        },
        "metrics": {
                "metrics_collected": {
                        "cpu": {
                                "measurement": [
                                        "cpu_usage_idle",
                                        "cpu_usage_iowait",
                                        "cpu_usage_user",
                                        "cpu_usage_system"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ],
                                "totalcpu": false
                        },
                        "disk": {
                                "measurement": [
                                        "used_percent",
                                        "inodes_free"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "diskio": {
                                "measurement": [
                                        "io_time",
                                        "write_bytes",
                                        "read_bytes",
                                        "writes",
                                        "reads"
                                ],
                                "metrics_collection_interval": 60,
                                "resources": [
                                        "*"
                                ]
                        },
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "netstat": {
                                "measurement": [
                                        "tcp_established",
                                        "tcp_time_wait"
                                ],
                                "metrics_collection_interval": 60
                        },
                        "swap": {
                                "measurement": [
                                        "swap_used_percent"
                                ],
                                "metrics_collection_interval": 60
                        }
                }
        }
}
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.
Do you want to store the config in the SSM parameter store?
1. yes
2. no
default choice: [1]:
2
Program exits now.

 

 

구성파일을 생성하였으면 이제 마지막으로 amazon-cloudwatch-agent를 실행하도록 하겠습니다.

 

[root@localhost bin]# sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

 


그 후, AWS Web Console에서 지표를 확인해보면..

  • AWS Web Console - CloudWatch - Metrics - CWAgent

CloudWatch Agent의 지표

 

위와 같이 지표를 땡겨오는 것을 확인하실 수 있습니다.

 

다음 포스팅은 이렇게 땡겨낸 Memory 지표와, CPU 지표 기준으로 AWS Compute Optimizer를 사용하여,
비용 및 성능 최적화에 대해서 포스팅 하도록 하겠습니다.

 

오늘도 긴 글 읽어주셔서 감사드리며 다음 포스팅으로 인사드리겠습니다.

 

 

질문은 언제나 환영합니다!