시스템 메트릭 로그를 수집하는 데 사용할 수 있는 CloudWatch Agent 설치

소개



이 절차는 CloudWatch Agent를 수동으로 설치하는 절차를 소개합니다.
이전에는 로그 수집용으로 CloudWatch Logs Agent가 존재했지만, 지금은 낡은 방법이 되고 있다.
CloudWatch Agent 설치 및 구성 파일 배포에는 AWS Systems Manager(SSM)를 사용하는 것이 좋습니다.
SSM은 수동 설치를 대체하기 때문에
CloudWatch Agent를 설치할 때
무슨 일이 일어나고 있는지 이해하기 위해 소개합니다.

EC2 인스턴스 준비


CloudWatchAgentServerPolicy 정책이 있는 EC2 인스턴스를 생성합니다.
이 절차에서는 OS에 CentOS Linux release 7.6.1810을 사용합니다.

CloudWatch Agent 설치



아래 페이지를 참조하여 CloudWatch Agent 설치 프로그램 다운로드 URL 확인

명령
curl -L https://s3.amazonaws.com/amazoncloudwatch-agent/centos/amd64/latest/amazon-cloudwatch-agent.rpm -o amazon-cloudwatch-agent.rpm
sudo rpm -U ./amazon-cloudwatch-agent.rpm

실행 결과
[centos@ip-172-31-18-148 ~]$ curl -L https://s3.amazonaws.com/amazoncloudwatch-a
gent/centos/amd64/latest/amazon-cloudwatch-agent.rpm -o amazon-cloudwatch-agent.
rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 35.8M  100 35.8M    0     0  4660k      0  0:00:07  0:00:07 --:--:-- 5715k
[centos@ip-172-31-18-148 ~]$ sudo rpm -U ./amazon-cloudwatch-agent.rpm
create group cwagent, result: 0
create user cwagent, result: 0
[centos@ip-172-31-18-148 ~]$

CloudWatch Agent 설정



설정 마법사로 설정 파일을 작성할 수 있습니다.

구성 마법사 실행 명령
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

이 절차에서는 시스템 메트릭 수집(현재 사용 중인 메모리의 비율, 현재 사용 중인 스왑 공간의 비율),/var/log/messages 의 로그 수집을 하고 있다.
[centos@ip-172-31-18-148 ~]$ sudo /opt/aws/amazon-cloudwatch-agent/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]:
2
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]:
1
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]:
1
Current config as follows:
{
        "agent": {
                "metrics_collection_interval": 60,
                "run_as_user": "root"
        },
        "metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
                "metrics_collected": {
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "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]:
1
Log file path:
/var/log/messages
Log group name:
default choice: [messages]
/var/log/messages
Log stream name:
default choice: [{instance_id}]

Do you want to specify any additional log files to monitor?
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"
        },
        "logs": {
                "logs_collected": {
                        "files": {
                                "collect_list": [
                                        {
                                                "file_path": "/var/log/messages",
                                                "log_group_name": "/var/log/messages",
                                                "log_stream_name": "{instance_id}"
                                        }
                                ]
                        }
                }
        },
        "metrics": {
                "append_dimensions": {
                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",
                        "ImageId": "${aws:ImageId}",
                        "InstanceId": "${aws:InstanceId}",
                        "InstanceType": "${aws:InstanceType}"
                },
                "metrics_collected": {
                        "mem": {
                                "measurement": [
                                        "mem_used_percent"
                                ],
                                "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.
[centos@ip-172-31-18-148 ~]$

에이전트가 만든 파일은/opt/aws/amazon-cloudwatch-agent/bin/config.json에 창조된다.
[centos@ip-172-31-18-148 ~]$ ls -l /opt/aws/amazon-cloudwatch-agent/bin/config.json
-rwxr-xr-x. 1 root root 270 Jun 16 09:45 /opt/aws/amazon-cloudwatch-agent/bin/config.json
[centos@ip-172-31-18-148 ~]$

하기 URL을 참고로 에이전트의 설정 파일을 수동으로 작성·편집도 가능.https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.htmlhttps://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/metrics-collected-by-CloudWatch-agent.html

CloudWatch Agent 서비스 시작



구성 파일을 읽고 서비스를 작성하고 시작하는 명령
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:<設定ファイルのパス> -s
[centos@ip-172-31-18-148 ~]$ 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
/opt/aws/amazon-cloudwatch-agent/bin/config-downloader --output-dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d --download-source file:/opt/aws/amazon-cloudwatch-agent/bin/config.json --mode ec2 --config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml --multi-config default
Successfully fetched the config and saved in /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp
Start configuration validation...
/opt/aws/amazon-cloudwatch-agent/bin/config-translator --input /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json --input-dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d --output /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml --mode ec2 --config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml --multi-config default
Valid Json input schema.
I! Detecting runasuser...
No csm configuration found.
No metric configuration found.
Configuration validation first phase succeeded
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -schematest -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
Configuration validation second phase succeeded
Configuration validation succeeded
Created symlink from /etc/systemd/system/multi-user.target.wants/amazon-cloudwatch-agent.service to /etc/systemd/system/amazon-cloudwatch-agent.service.
Redirecting to /bin/systemctl restart amazon-cloudwatch-agent.service
[centos@ip-172-31-18-148 ~]$

서비스가 시작되었는지 확인합니다.
``
[centos@ip-172-31-18-148 ~]$ sudo systemctl status amazon-cloudwatch-agent.service
● amazon-cloudwatch-agent.service - Amazon CloudWatch Agent
Loaded: loaded (/etc/systemd/system/amazon-cloudwatch-agent.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-06-16 10:00:57 UTC; 23s ago
Main PID: 3180 (amazon-cloudwat)
CGroup:/system.slice/amazon-cloudwatch-agent.service
mq3180/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -config/opt/aws/amazon-cloudwatch-...

Jun 16 10:00:57 ip-172-31-18-148.ap-northeast-1.compute.internal systemd[1]: Started Amazon CloudWatch Agent.
Jun 16 10:00:57 ip-172-31-18-148.ap-northeast-1.compute.internal start-amazon-cloudwatch-agent[3180]:/opt/aws/ama...
Jun 16 10:00:57 ip-172-31-18-148.ap-northeast-1.compute.internal start-amazon-cloudwatch-agent[3180]: Valid Json i...
Jun 16 10:00:57 ip-172-31-18-148.ap-northeast-1.compute.internal start-amazon-cloudwatch-agent[3180]: I! Detecting...
Jun 16 10:00:57 ip-172-31-18-148.ap-northeast-1.compute.internal start-amazon-cloudwatch-agent[3180]: 2019/06/16 1...
Hint: Some lines were ellipsized, use -l to show in full.
[centos@ip-172-31-18-148 ~]$
``

서비스 로그 파일은/var/log/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log에 저장됩니다.
[centos@ip-172-31-18-148 ~]$ ls -l  /var/log/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log
-rw-r--r--. 1 root root 15347 Jun 16 10:13 /var/log/amazon/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log
[centos@ip-172-31-18-148 ~]$

수집 결과 확인



시스템 메트릭





로그



좋은 웹페이지 즐겨찾기