본문 바로가기
Cloud/AWS

AWS#12 CloudWatch를 활용한 모니터링

by 짜깡이 2021. 5. 14.

1) CloudWatch?

-CloudWatch는 AWS 사용자들을 위해서 애플리케이션이나 시스템 성능에 대해서 전반적으로 확인하여 변경 사항에 대응할 수 있는 모니터링 시스템 

-AWS뿐만이 아닌 온프레미스 서버에서 관리되는 애플리케이션이나 시스템 성능 또한 수집 가능함

-문제나 장애 발생시 경보를 설정 하여 알림을 받읈 수 있고 지표를 시각화 하여 한 눈에 파악할 수 있으며 자동화된 작업 수행이 가능함

 

2) EC2 인스턴스 모니터링(CPU)

CloudWatch 검색 후 대시보드로 이동

 

새 대시보드 생성

 

빈 대시보드 화면, 위젯을 추가해서 모니터링을 진행

 

위젯을 추가

 

원하는 지표로 생성

 

지표나 로그를 바탕으로 위젯을 추가할 수 있음, 기본은 지표로 선택

 

위젯 추가시 그래프가 비어있기 때문에 아래의 지표에서 선택해서 추가함

 

EC2- 인스턴스별 지표에서 원하는 지표를 선택 , CPUUtilization(CPU 사용률)

 

생성된 CPU 사용률 지표

 

이렇게 지표를 생성하고 CPU 사용률에 대한 평균값, 최대값, 최소값 등등을 얻을 수 있음

하지만 서버의 기본적인 메모리, 디스크 사용률에 대한 지표를 제공하지 않기 때문에

서버에 접속해 CloudWatch Agent를 설치하는 과정을 거쳐야 함

 

3) EC2 인스턴스 모니터링(메모리, 디스크)

 

-IAM 역할 생성
 인스턴스가 CloudWatch Agent의 데이터를 수집할 수 있게 역할을 생성

IAM - 역할

 

역할 만들기
EC2 클릭 후 다음
정책 연결
이름 설정 후 역할 만들기

 

EC2 우클릭 - 보안 - IAM 역할 수정

 

CloudWatch Agent 허용 역할 추가

 

EC2 접속

 

 

명령줄을 사용하여 CloudWatch 에이전트 다운로드 및 구성 - Amazon CloudWatch

이 페이지에 작업이 필요하다는 점을 알려 주셔서 감사합니다. 실망시켜 드려 죄송합니다. 잠깐 시간을 내어 설명서를 향상시킬 수 있는 방법에 대해 말씀해 주십시오.

docs.aws.amazon.com

위 링크에서 CloudWatch Agent를 각 OS에 맞게 다운받음

Amazon Linux, CentOS 그 밖에 다른 OS들도 지원

 

wget을 통해 다운로드 (Amazon Linux)

[root@linux_test ~]# wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm  

 

패키지 설치(내려 받은 파일)

[root@linux_test ~]# rpm -ivh amazon-cloud-watch-agent.rpm

 

Agent 구성 마법사 실행

[root@linux_test ~]# /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
3. darwin
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 (subminute 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

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

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

 

CloudWatch Agent 실행

[root@linux_test ~]# /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

 

 

대시보드에서 위젯 추가 시 새로 만든 지표가 생성되어있음

 

왼쪽 디스크, 오른쪽 메모리 사용률 지표

 

CPU는 약1% Memory는 약11%, 디스크는 약 54%의 사용률을 보임

 

/data 디스크는 55% 사용률 보임 거의 동일

 

메모리도 1GB중 112MB 사용

 

 

 

4) EC2 프로세스 모니터링

 

테스트할 프로세스 다운 및 실행

[root@linux_test ~]# yum -y install httpd
[root@linux_test ~]# systemctl start httpd

 

 

설정값 수정
(metrics, metrics_collected 아래 procstat으로 작성)
-cpu_usage: 프로세스의 cpu 사용률
-pid_count: 해당 프로세스의 실행 수

[root@linux_test ~]# vim /opt/aws/amazon-cloudwatch-agent/bin/config.json

{
    "metrics": {
        "metrics_collected": {
            "procstat": [
                {
                    "exe": "httpd",
                    "measurement": [
                        "cpu_usage",
                        "pid_count"
                    ]
                }       
            ]
        }
    }
}

 

 

CloudWatch Agent 설정 값 저장 및 재실행

[root@linux_test ~]# /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

 

 

httpd 프로세스의 pid_count와 cpu 사용률

 

 

종료 시 pid_count는 0, cpu_usage는 사용하지 않는 것으로 확인

 

 

 

5) URL 모니터링

-원하는 url의 상태를 체크할 수있음

 

canary 생성

 

이름 및 url 입력 후 디폴트 설정으로 생성

 

URL 정상 작동 확인

 

URL 다운 시 화면

댓글