brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Jun 29. 2019

122.CloudWatch Agent로 메모리 모니터링

실습

<1> 목표

EC2 서버의  메모리 사용량을  모니터링 하고자 한다.

CloudWatch에서 기본적으로 메모리 모니터링은 하지 못한다.

CloudWatch > Metrics 에서  메모리 사용량 확인



<2> 작업 순서

1.  IAM역할 추가하기

2.  서버에  IAM role 주기

3.  EC2 에 에이전트 다운로드와 위저드로 설정하기

4.  설정파일 확인과  에이전트 데몬 실행하기  

5.   에이전트 시작하기

6.   Cloudwatch에서 메모리 모니터링 되는지 확인하기

기타. AWS 서버에 대해  한국 시간으로 변경하기.



1.  IAM역할 추가하기

IAM  > Roles > Create role  > EC2 

cloudwatchAgentServerPolicy



2.  서버에  IAM role 주기

Ec2 > 오른 마우스  > 보안 (보안으로 변경됨) > 



3.  EC2 에 에이전트 다운로드와 위저드로 설정하기

cd  /var


에이전트 다운르드 하기 

# wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm


에이전트  설치하기 

# rpm -U ./amazon-cloudwatch-agent.rpm 


위저드로 설정하기
 

/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]:


Which user are you planning to run the agent?

1. root

2. cwagent

3. others

default choice: [1]:

2


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


Do you want to monitor cpu metrics per core? Additional CloudWatch charges may apply.

1. yes

2. no

default choice: [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]:


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


2

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": 10,

                "run_as_user": "cwagent"

        },

        "metrics": {

                "append_dimensions": {

                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",

                        "ImageId": "${aws:ImageId}",

                        "InstanceId": "${aws:InstanceId}",

                        "InstanceType": "${aws:InstanceType}"

                },

                "metrics_collected": {

                        "cpu": {

                                "measurement": [

                                        "cpu_usage_idle",

                                        "cpu_usage_iowait",

                                        "cpu_usage_user",

                                        "cpu_usage_system"

                                ],

                                "metrics_collection_interval": 10,

                                "resources": [

                                        "*"

                                ],

                                "totalcpu": false

                        },

                        "disk": {

                                "measurement": [

                                        "used_percent",

                                        "inodes_free"

                                ],

                                "metrics_collection_interval": 10,

                                "resources": [

                                        "*"

                                ]

                        },

                        "diskio": {

                                "measurement": [

                                        "io_time",

                                        "write_bytes",

                                        "read_bytes",

                                        "writes",

                                        "reads"

                                ],

                                "metrics_collection_interval": 10,

                                "resources": [

                                        "*"

                                ]

                        },

                        "mem": {

                                "measurement": [

                                        "mem_used_percent"

                                ],

                                "metrics_collection_interval": 10

                        },

                        "netstat": {

                                "measurement": [

                                        "tcp_established",

                                        "tcp_time_wait"

                                ],

                                "metrics_collection_interval": 10

                        },

                        "swap": {

                                "measurement": [

                                        "swap_used_percent"

                                ],

                                "metrics_collection_interval": 10

                        }

                }

        }

}


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


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


Do you want to monitor any log files?

1. yes

2. no  

default choice: [1]:

2   

(log 는 따로 모니터링 하지 안도록 한다. CPU , 메모리만 우선 나오는지 확인하자)


Saved config file to /opt/aws/amazon-cloudwatch-agent/bin/config.json successfully.

Current config as follows:

{

        "agent": {

                "metrics_collection_interval": 10,

                "run_as_user": "cwagent"

        },

        "metrics": {

                "append_dimensions": {

                        "AutoScalingGroupName": "${aws:AutoScalingGroupName}",

                        "ImageId": "${aws:ImageId}",

                        "InstanceId": "${aws:InstanceId}",

                        "InstanceType": "${aws:InstanceType}"

                },

                "metrics_collected": {

                        "cpu": {

                                "measurement": [

                                        "cpu_usage_idle",

                                        "cpu_usage_iowait",

                                        "cpu_usage_user",

                                        "cpu_usage_system"

                                ],

                                "metrics_collection_interval": 10,

                                "resources": [

                                        "*"

                                ],

                                "totalcpu": false

                        },

                        "disk": {

                                "measurement": [

                                        "used_percent",

                                        "inodes_free"

                                ],

                                "metrics_collection_interval": 10,

                                "resources": [

                                        "*"

                                ]

                        },

                        "diskio": {

                                "measurement": [

                                        "io_time",

                                        "write_bytes",

                                        "read_bytes",


----------------------------


참고 :https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html


Do you want to store the config in the SSM parameter store?

1. yes

2. no

default choice: [1]:

2

Program exits now.




4.  설정파일 확인과  에이전트 데몬 실행하기 


cd  /opt/aws/amazon-cloudwatch-agent/bin/

[root@ip-172-31-19-87 bin]# more config.json 


5

위저드로 설정한  json  파일을 읽어 들임.

bin]# 
./amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 file://opt/aws/amazon-cloudwatch-agent/bin/config.json -s


6

에이전트 상태 확인하기
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

{

  "status": "stopped",

  "starttime": "",

  "version": "1.219020.0"

}



7

 에이전트 시작하기

[root@ip-172-31-24-96 bin]# 

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start



8

에이전트 데몬 확인하기 

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status



[root@ip-172-31-24-96 ~]# ps -ef |grep amazon-cloudwatch-agent
root       2245     1  0 11:13   ?        00:00:00  /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -config  /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml  -pidfile  /opt/aws/amazon-cloudwatch-agent/var/amazon-cloudwatch-agent.pid


9

에러가 없는지 확인 한다.

[root@ip-172-31-19-87 logs]# 

cd /opt/aws/amazon-cloudwatch-agent/logs


[root@ip-172-31-19-87 logs]# 

more amazon-cloudwatch-agent.log 


2019/07/05 09:30:02 I! I! Detected the host is EC2

/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json does not exist or cannot read. Skipping it.

Valid Json input schema.

I! Detecting runasuser...

No csm configuration found.

No log configuration found.

Configuration validation first phase succeeded 

2019/07/05 09:30:02 I! Config has been translated into TOML /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml 

2019/07/05 09:30:02 I! Detected runAsUser: root

2019-07-05T09:30:02Z I! cloudwatch: get unique roll up list []

2019-07-05T09:30:02Z I! Starting AmazonCloudWatchAgent (version 1.219020.0)

2019-07-05T09:30:02Z I! Loaded outputs: cloudwatch

2019-07-05T09:30:02Z I! Loaded inputs: mem swap

2019-07-05T09:30:02Z I! Tags enabled: host=ip-172-31-19-87.ap-northeast-2.compute.internal

2019-07-05T09:30:02Z I! Agent Config: Interval:1m0s, Quiet:false, Hostname:"ip-172-31-19-87.ap-northeast-2.compute.internal", Flush 

Interval:1s 

2019-07-05T09:30:02Z I! cloudwatch: publish with ForceFlushInterval: 1m0s, Publish Jitter: 37s

[root@ip-172-31-19-87 logs]# 



10

 CloudWatch에서 메모리 모니터링 되는지 확인하기


 CloudWatch >  Metrics  > CWAgent 





11

 AWS 서버에 대해  한국 시간으로 변경하기.


[root@ip-172-31-24-96 bin]# date

Sat Jun 29 11:45:10 UTC 2019


[root@ip-172-31-24-96 bin]# more /etc/localtime 

TZif2

UTC0


[root@ip-172-31-24-96 bin]# rm /etc/localtime 

rm: remove regular file ??etc/localtime?? y


[root@ip-172-31-24-96 bin]# ln -s /usr/share/zoneinfo/Asia/Seoul  /etc/localtime


[root@ip-172-31-24-96 bin]# date

Sat Jun 29 20:46:10 KST 2019








https://brunch.co.kr/@topasvga/1581



감사합니다.


브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari