使用CloudWatch Logs 收集应用程序日志

时间:2021-10-04 16:19:53

CloudWatch不光可以当做数据监控平台,还能收集日志,为后续自定义报警做准备

安装CloudWatch Logs Daemon

yum install -y awslogs


安装发送日志的的代理


wget https://s3.amazonaws.com/aws-cloudwatch/downloads/awslogs-agent-setup-v1.0.py
 
--2015-08-25 06:14:21--  https://s3.amazonaws.com/aws-cloudwatch/downloads/awslogs-agent-setup-v1.0.pyResolving s3.amazonaws.com (s3.amazonaws.com)... 54.231.10.112Connecting to s3.amazonaws.com (s3.amazonaws.com)|54.231.10.112|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 39445 (39K) [application/x-python]Saving to: ‘awslogs-agent-setup-v1.0.py’awslogs-agent-setup-v1.0.py          100%[========================================================================>]  38.52K  --.-KB/s   in 0.08s  2015-08-25 06:14:22 (498 KB/s) - ‘awslogs-agent-setup-v1.0.py’ saved [39445/39445]
 
<h1>运行脚本,配置监控内容</h1><pre>此处监控自己生成的/root/redis.log脚本

[root@ip-172-31-42-139 ~]# python ./awslogs-agent-setup-v1.0.py --region us-east-1Launching interactive setup of CloudWatch Logs agent ... Step 1 of 5: Installing pip ...DONEStep 2 of 5: Downloading the latest CloudWatch Logs agent bits ... DONEStep 3 of 5: Configuring AWS CLI ... AWS Access Key ID [None]: <你的Key>AWS Secret Access Key [None]: <你的Secret Key>Default region name [None]: us-east-1Default output format [None]: jsonStep 4 of 5: Configuring the CloudWatch Logs Agent ... Path of log file to upload [/var/log/messages]: /root/redis.logDestination Log Group name [/root/redis.log]: Choose Log Stream name: 1. Use EC2 instance id. 2. Use hostname. 3. Custom.Enter choice [1]: 1Choose Log Event timestamp format: 1. %b %d %H:%M:%S (Dec 31 23:59:59) 2. %d/%b/%Y:%H:%M:%S (10/Oct/2000:13:55:36) 3. %Y-%m-%d %H:%M:%S (2008-09-08 11:52:54) 4. CustomEnter choice [1]: 3Choose initial position of upload: 1. From start of file. 2. From end of file.Enter choice [1]: 2More log files to configure? [Y]: nStep 5 of 5: Setting up agent as a daemon ...DONE------------------------------------------------------- Configuration file successfully saved at: /var/awslogs/etc/awslogs.conf- You can begin accessing new log events after a few moments at https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logs:- You can use 'sudo service awslogs start|stop|status|restart' to control the daemon.- To see diagnostic information for the CloudWatch Logs Agent, see /var/log/awslogs.log- You can rerun interactive setup using 'sudo ./awslogs-agent-setup.py --region us-east-1 --only-generate-config'------------------------------------------------------
 


生成日志

把redis 的访问日志输出到/root/redis.log

redis-cli monitor >> /root/redis.log


在控制台查看日志记录

访问前面配置时的region的 “CloudWatch” -> 日志

使用CloudWatch Logs 收集应用程序日志


点击/root/redis.log,选择服务器节点名

使用CloudWatch Logs 收集应用程序日志


可以看到刚才打出的日志。

一开始只有“时间数据”列,可以通过点击右上方的齿轮图标,来选择显示其他两个和时间相关的列。

使用CloudWatch Logs 收集应用程序日志