CloudWatch Agent ãèµ·åããªããã©ãã«ã®å¯¾å¿ã¡ã¢
å
æ¥ããä»äºã§ä¹
ãã¶ãã«ãAWSã®EC2ã« CloudWatch Agent
ãå
¥ããä½æ¥ããã¦ããã¨ãããã¡ãã£ã¨ããã£ããã¤ã³ãããã£ãã®ã§ããã«ã¡ã¢ãã¦ããã¾ãã
ç®æãç¶æ
- CloudWatch Agent ãèµ·åãã¦ãã¡ããªã¯ã¹ã CloudWatch ã«éä¿¡ãã¦ããç¶æ
- CloudWatch Agent ãèµ·åãã¦ãWebã¢ããªã±ã¼ã·ã§ã³ã®ãã°ã CloudWatch Logs ã«éä¿¡ãã¦ããç¶æ
- æ¬å½ã¯Nginxã¨ãOSã®ä¸é¨æ©è½ã®ãã°ã¨ããéããã©ãæ¬ã¡ã¢ã®ä¸»é¡ã¨ã¯é¢ãã¦ãã®ã§ããã®è¦³ç¹ã§ã®æ å ±ã¯å²æ
IAM Role ã®è¨å®
対象㮠EC2 Instance ã«è¨å®ãã IAM Role ã«ã¯ã以ä¸ã®2ã¤ã® AWS managed policy ãä»ä¸ãã¾ããã
AmazonSSMManagedEC2InstanceDefaultPolicy
: Session Manager ã§æ¥ç¶ããããã«å¿ è¦ã§ããAmazonSSMManagedInstanceCore
ã§ãè¯ãã¨æãã¾ããããã2ã¤ã®éã㯠SSM ã®ãã©ã¡ã¼ã¿ã¹ãã¢ã使ããã©ããã§ããAmazonSSMManagedInstanceCore
ã ã¨ssm:GetParameter
権éãä»ãã¦ã¾ããCloudWatchAgentServerPolicy
: CloudWatch ã«ã¡ããªã¯ã¹ãéã£ãããCloudWatch Logs ã«ãã°ãéã£ãããã権éã§ãã
CloudWatch Agent ã®ã¤ã³ã¹ãã¼ã«
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html
ã¤ã³ã¹ãã¼ã«æé ã®ãå
¬å¼ã®ããã¥ã¢ã«ã¯âã®éãã§ããcommand line ããå
¥ããããSystems Manager ããå
¥ããããCloudFormation ã§ï½ºï¾ï¾ï½®ï½ºï¾ï¾ï½®ããããã®3æã§ãããæ¬ã¡ã¢ã§ã¯command line ããå®æ½ãã¾ãããã¤ã¾ããããã¥ã¢ã«ã¨ãã¦ã¯ https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/installing-cloudwatch-agent-commandline.html ãåç
§ããã¨ãããã¨ã«ãªãã¾ãã
ãªãããã®ããã¥ã¢ã«ã«ã¯ã1ã¤ã ããã¨ã¦ãä¸è¦ªåãªç¹ãããã¾ããããã¯ãã¤ã³ã¹ãã¼ã«æé ã®æå¾ã«Agentãèµ·åããéãè¨å®ãã¡ã¤ã«ã®ãã¡ã¤ã«ãã¹ã®å
¥åãæ±ãã¦ããã®ã§ãããããã¥ã¢ã«ãããã¾ã§ *é çªéãã«* èªãã§ããã¨ãè¨å®ãã¡ã¤ã«ãæ¸ãå·¥ç¨ã«å°éããªãã¨ããç¹ã§ãï¼ã¾ãã¯ç§ãè¦è½ã¨ãããï¼ã
è¨å®ãã¡ã¤ã«ã®æ¸ãæ¹ãããã©ã«ãã®ãã¡ã¤ã«ãã¹ã¯ https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file.html ã«ããã¾ãã
è¨å®ãã¡ã¤ã«ã®ä½æ
åè¿°ã®ããã¥ã¢ã«ã«ããéããè¨å®ãã¡ã¤ã«ã¯ããã©ã«ãã§ã¯JSONå½¢å¼ã§ããããããä»ã®ãã©ã¼ããããèªãã§ãããã®ã§ããããã©ããè¨å®ãã¡ã¤ã«ãä½ã£ã¦ãããwizardãããJSONãåãã®ã§ãJSONã使ãã®ãä¸è¬çãªã®ã ããã¨æãã¾ããç§ãJSONæ´¾ã§ãã
wizardããã¯ãããã¥ã¢ã«ã«ããéã sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
ã§èµ·åãã¾ããããã§å¤ãã®è³ªåã«çããã¨ãè¨å®ãã¡ã¤ã«ãã§ããããã¾ãã
ãªãã質åäºé
ã«ã¯ StatsD
ã CollectD
ã使ããã©ããã®è³ªåãããã¾ããã使ãåçãããã¨ã¤ã³ã¹ãã¼ã«ã¨ããã¡ãã£ã¨é¢åãªã®ã§ãããã§ã¯ä½¿ãã¾ããð
Do you want to turn on StatsD daemon? 1. yes 2. no default choice: [1]: 2 Do you want to monitor metrics from CollectD? WARNING: CollectD must be installed or the Agent will fail to start 1. yes 2. no default choice: [1]: 2
å
¨é¨ã®è³ªåã«çãã㨠The config file is also located at /opt/aws/amazon-cloudwatch-agent/bin/config.json.
ã¿ãããªã¡ãã»ã¼ã¸ãåºåãããã®ã§ãããã§è¨å®ãã¡ã¤ã«ã®ãã¡ã¤ã«ãã¹ãç¥ããã¨ã«ãªãã¾ãã
CloudWatch Agent ã®ã³ãã³ããããã
EC2ä¸ã§åãUbuntuã®å ´åãã§ãããªã³ãã¬ãWindowsãªã©ãç°å¢ãå¤ããã°å½ç¶ã«ã³ãã³ãã¯å¤ããã¾ãã
ãªããæ¬æ¥ã¯ sudo
ã§ä½æ¥ããã¹ãã ããã¨ã¯æãã¾ãããããã§ã¯éã«root権éã§ä½æ¥ããåæã«ãªã£ã¦ãã¾ãð
- è¨å®ãã¡ã¤ã«ãé£ããã¦èµ·å :
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
- ããã»ã¹ã®ç¶æ³ãç¢ºèª :
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
ãã©ãã«ã®å 容
è¨å®ãã¡ã¤ã«ãé£ããã¦èµ·åãããã¨ããã¨ãããAgentãããä¸æãèµ·åãã¦ããã¾ããã§ãããæç³»åã§è©±ãæ¸ãã¦ãèªã¿ã«ããã¦ä»æ¹ãªãã®ã§ããããããééããããããããããã¨ã©ã¼ã«ãªã£ããã¨ããæ å ±ãããã¤ãåæããã¨ããæ¸ãæ¹ã§ãã©ãã«ãç´¹ä»ãããã¨æãã¾ãã
ãã©ãã«ï¼ï¼è¨å®ãã¡ã¤ã«ãJSONã¨ãã¦æ£ãããªãã¨ã
è¨å®ãã¡ã¤ã«ãé£ããã¦èµ·åãããã¨ããã¨ãã³ãã³ãã¯æ£å¸¸ã£ã½ãçµäºãã¾ããããConfiguration validation succeededãã£ã¦è¨ã£ã¦ã¾ãããçµäºã¹ãã¼ã¿ã¹ã¯ 0
ã§ããããè¦ãã¨ãunable to parse jsonãã¨è¨ããã¦ãã¾ãããããã¯å®¹æã«è¦è½ã¨ãã¾ãã
ãã®å¾ã«ç¨¼åç¶æ³ãè¦ã¦ã status: stopped
ã®ã¾ã¾ã§ãã
root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json ****** processing amazon-cloudwatch-agent ****** I! Trying to detect region from ec2 D! [EC2] Found active network interface I! imds retry client will retry 1 timesSuccessfully fetched the config and saved in /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp Start configuration validation... 2023/11/23 11:18:27 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp ... 2023/11/23 11:18:27 unable to scan config dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d with error: unable to parse json, error: invalid character 'a' looking for beginning of value 2023/11/23 11:18:27 No json config files found, use the default one 2023/11/23 11:18:27 I! Valid Json input schema. 2023/11/23 11:18:27 D! ec2tagger processor required because append_dimensions is set 2023/11/23 11:18:27 D! pipeline hostDeltaMetrics has no receivers 2023/11/23 11:18:27 Configuration validation first phase succeeded I! Detecting run_as_user... I! Trying to detect region from ec2 D! [EC2] Found active network interface I! imds retry client will retry 1 times /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 amazon-cloudwatch-agent has already been stopped root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# echo $? 0 root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status { "status": "stopped", "starttime": "", "configstatus": "configured", "version": "1.300031.0b313" }
â»è¦ãããã®ããã«ãã³ãã³ãéã«ç©ºè¡ãå ¥ãã¦ãã¾ãã
ãªãããã°ã /opt/aws/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log
ã«åããã¾ããããã®å
容ã¯ä»¥ä¸ã®ãããªãã®ã§ããã
2023/11/23 11:18:28 I! Return exit error: exit code=99 2023/11/23 11:18:28 I! No json config files found, please provide config, exit now
config file ãç¡ãã¿ãããªãã¨ãè¨ã£ã¦ãããã¨ãããç§ã¯èª¤ã£ã調æ»ãé²ãã¦ãã¾ããæéã浪費ãã¾ããã
ãã©ãã«ï¼ï¼multi_line_start_pattern ã«ä¸ããæ£è¦è¡¨ç¾ããæ£è¦è¡¨ç¾ã¨ãã¦æ£ãããªãã¨ã
CloudWatch Logs ã«ãã°ãéä¿¡ããéãããã©ã«ãã§ã¯ãããã¹ããã¡ã¤ã«ã¨ãã¦ã®1è¡ï¼ã¤ã¾ãæ¹è¡æåãã¨ã«1è¡ï¼ã¨ãã¦å¦çããã¾ããå¤ãã®å ´åã¯ããã§è¯ãã®ã§ãããã¢ããªã±ã¼ã·ã§ã³ã®ãã°ã1ã¤ã®ã¨ã³ããªã®éä¸ã§æ¹è¡æåãåºããã¨ãããå ´åï¼ä¾ï¼ã¹ã¿ãã¯ãã¬ã¼ã¹ï¼ãCloudWatch Logs ã§ãã°ãåç
§ããéã®è¦æããææªã®ç¶æ
ã«ãªãã¾ããfilterãä¸æãæ©è½ããªããªãã®ã§ãããªã®ã§ããã®ãããªãã°ãå¦çããé㯠multi_line_start_pattern
ã®è¨å®ãéè¦ã§ãã
ã¢ããªã±ã¼ã·ã§ã³ã®ãã°ããè«ççãª1è¡ï¼1ã¤ã®ã¨ã³ããªï¼ã®æåã«å¿
ãã¿ã¤ã ã¹ã¿ã³ããåãããã§ããã° "multi_line_start_pattern": "{datetime_format}",
ã¨æ¸ããã¨ãã§ãï¼å¥é datetime_format
ã®è¨å®ãå¿
è¦ï¼ã便å©ã§ããããã§ã¯ãªãå ´åã¯æ£è¦è¡¨ç¾ãæ¸ããã¨ã«ãªãã¾ããããã®æ¸ãæ¹ãééããã¨ããã©ãã«ã«ãªãã¾ãã
- ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹
\
ã®ä½¿ãæ¹ã誤ã£ãå ´å- ä¾ãã°
"multi_line_start_pattern": "^\["
ã¨æ¸ãã¨ãèµ·åæã®ã¡ãã»ã¼ã¸ã«ã¯unable to parse json, error: invalid character '[' in string escape code
ã¨åºã¾ããèµ·åã¯ãã¾ããã - ããæ¸ãããå ´åã¯ãæ£ããã¯
"multi_line_start_pattern": "^\\["
ã®ããã«ãã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã2ã¤éãããã¨ã«ãªãã¾ããJSON Parse ã®æ®µéã§escapeã¨ãã¦å¦çããã¦ãã¾ããããã§ããã - ãã®èª¤ãã¯ãåè¿°ã®ãJSONã¨ãã¦æ£ãããªãã¨ããã¨ãæ¬è³ªçã«ã¯åãã¨ãããã¨ã«ãªãã¾ãã
- ä¾ãã°
- JSON parseã¯æåããããã©ããæ£è¦è¡¨ç¾ã¨ãã¦æ£ãããªãæååãå
¥ããå ´å
- ä¾ãã°
"multi_line_start_pattern": "*"
ã¨æ¸ãã¨ãèµ·åã«ã¯æåãã¾ããããã°ã¯ CloudWatch Logs ã«ã¯éä¿¡ããã¾ãããAgentã®ãã°/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log
ã«ã¯2023-11-23T11:39:36Z W! [inputs.logfile] not started with file state folder %s/opt/aws/amazon-cloudwatch-agent/logs/state
ã¨ãã£ããã°ã1ç§ã«1è¡ãåºåããã¾ãããã®ã¨ã©ã¼ã¡ãã»ã¼ã¸ãããåå ãæ£è¦è¡¨ç¾ã®èª¤ãã§ãããã¨ã«æ°ä»ãã®ã¯ããªããªãé£ããã¨æãã¾ãã
- ä¾ãã°
- 空æååãä¸ããå ´å
"multi_line_start_pattern": ""
ã¨ããã¨ã以ä¸ã®ãããªã¡ãã»ã¼ã¸ãåºã¦ãããããAgentã®èµ·åã«ãæ示çã«å¤±æãã¾ããããã ãã¨ã¦ã親åã§ãï½
root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json ****** processing amazon-cloudwatch-agent ****** I! Trying to detect region from ec2 D! [EC2] Found active network interface I! imds retry client will retry 1 timesSuccessfully fetched the config and saved in /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp Start configuration validation... 2023/11/23 11:47:30 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp ... 2023/11/23 11:47:30 E! Invalid Json input schema. 2023/11/23 11:47:30 E! Invalid Json input schema. 2023/11/23 11:47:30 Under path : /logs/logs_collected/files/collect_list/0/multi_line_start_pattern | Error : String length must be greater than or equal to 1 2023/11/23 11:47:30 Configuration validation first phase failed. Agent version: 1.0. Verify the JSON input is only using features supported by this version. root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# echo $? 1
ãããã«
æ¥æ¬èªã§ãããªãã¨ãåã£ã¦ãã¦ãä¸çã¯æããªãã®ã§ãissueãreportãã¾ããã
ãã®ã¡ã¢ã§è¨åããå 容ã¯ããã°ç®¡çã¨ããæå³ã§ã¯2ã¤ã«åããã®ã妥å½ã ã¨æã£ãã®ã§ãåãã¦å ±åãã¾ããã
ç°å¢æ å ±
- Ubuntu 22.04 ã®å ¬å¼ã®AMIããèµ·ããã EC2 instance
- t4g.nano
- CloudWatch Agent version
1.300031.0b313
- ãã¼ã¸ã§ã³çªå·ã¯
/opt/aws/amazon-cloudwatch-agent/bin/CWAGENT_VERSION
ãã¡ã¤ã«ã«æ¸ãã¦ããã¾ãã
- ãã¼ã¸ã§ã³çªå·ã¯
- ä½æ¥ã¯ Session Manager ã®æ¥ç¶ã§å®æ½