Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does AWS use CloudWatch Logs to collect logs

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

本篇文章为大家展示了AWS怎么使用CloudWatch Logs收集日志,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

很多时候,我们希望集中收集各服务器日志统一查看、报警。AWS 给我们提供了这种服务,叫 CloudWatch Logs。

我们需要在目标机器安装 CloudWatch Logs Agent 程序,以收集和报告日志。

角色

使用 AWS 服务第一件需要做的事情就是配置权限,这也不例外。有两种方式。

一是给目标实例的 IAM 角色允许这些权限:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents", "logs:DescribeLogStreams" ], "Resource": [ "arn:aws:logs:*:*:*" ] } ]}

二是在下文所述的 awscli.conf 文件里配置允许了这些权限的角色 access-key 和 secret-key。

[default]region = ${REGION}aws_access_key_id = ${ACCESS_KEY}aws_secret_access_key = ${SECRET_KEY}代理程序

awslogs 代理程序提供一种自动化的方法将日志数据发送到 CloudWatch Logs。该代理包括以下组件:

一个将日志数据推送到 CloudWatch Logs 的 AWS CLI 插件。

一个运行 CloudWatch Logs aws logs push 命令以将数据发送到 CloudWatch Logs 的脚本(守护程序)。

一个确保该守护程序始终运行的 cron 作业。

如果更喜欢官方文档,请参考 CloudWatch Logs 代理参考。

awscli.conf 配置[plugins]cwlogs = cwlogs[default]region = ${REGION}aws_access_key_id = ${ACCESS_KEY}aws_secret_access_key = ${SECRET_KEY}awslogs.conf 配置[general]# 状态文件路径# 该文件用于记录当前日志上传的状态state_file = # 自定义的 Python Logging Config 格式文件路径# 可选logging_config_file = # 启用 GZIP 压缩use_gzip_http_content_encoding = [true | false][logstream1]# 日志组名称log_group_name = # 日志流名称# 可用的预定义变量:{instance_id} {hostname} {ip_address}log_stream_name = # 时间格式,日志的记录时间以该时间为准# 如果无法匹配目标事件则以最近一次成功为准,没有最近一次成功则使用当前时间datetime_format = # 如果不能从时间格式获取时区,则指定时区time_zone = [LOCAL|UTC]# 待收集的日志源file = # 以前几行计算识别码file_fingerprint_lines = | # 日志行的匹配模式,匹配该模式则认为上一条日志已结束# 指定为 {datetime_format} 则以时间格式来匹配# 默认为 ^[^\s] 即行开头非空则认为是新的日志multi_line_start_pattern = | {datetime_format}# 当日志源没有被记录的收集状态时,从何处开始initial_position = [start_of_file|end_of_file]# 文件编码encoding = [ascii|utf_8|..]# 批量处理的时间段(单位:ms)buffer_duration = # 批量处理的最大条目batch_count = # 批量处理的最大大小(单位:字节)batch_size = [logstream2]...时间格式

下面列出了常见 datetime_format 代码。您也可以使用 Python datetime.strptime() 支持的所有 datetime_format 代码。时区偏移量 (%z) 也受支持,[+-]HHMM,不带冒号 (:)。

%y:年份,以零填充的十进制数字表示,不包括代表世纪的数字。00, 01, ..., 99

%Y:年份,以十进制数字形式表示且包括表示世纪的数字。如 1970、1988、2001、2013

%b:月份,使用区域设置的缩写名称形式。Jan、Feb...Dec (en_US);

%B:月份,使用区域设置的完整名称形式。January,February...December (en_US);

%m:月份,使用以零填充的十进制数字形式。01, 02, ..., 12

%d:月份中的日期,使用以零填充的十进制数字形式。01, 02, ..., 31

%H:小时(24 小时制),使用以零填充的十进制数字形式。00, 01, ..., 23

%I:小时(12 小时制),使用以零填充的十进制数字形式。01, 02, ..., 12

%p:区域设置中等效于 AM 或 PM 的表示形式。

%M:分钟,使用以零填充的十进制数字形式。00, 01, ..., 59

%S:秒,使用以零填充的十进制数字形式。00, 01, ..., 59

%f:微秒,在左边使用以零填充的十进制数字形式。000000, ..., 999999

%z:使用 +HHMM 或 -HHMM 形式的 UTC 偏移量。+0000, -0400, +1030

样例

比如可配置为:

[general]state_file = /var/lib/awslogs/agent-stateuse_gzip_http_content_encoding = true[app]log_group_name = /aws/ec2/applog_stream_name = staging-{hostname}datetime_format = %Y-%m-%d %H:%M:%Smulti_line_start_pattern = {datetime_format}time_zone = UTCencoding = utf_8buffer_duration = 5000file = /app/logs/app-*.loginitial_position = start_of_fileAmazon Linux

Amazon Linux 自带 awslogs 源。

function install_awslogs() { yum install -q -y awslogs && chkconfig awslogs on service awslogs start}service awslogs status || (service awslogs start || install_awslogs)

代理程序的配置默认在 /etc/awslogs/,可能有以下几个文件:

awscli.conf 基本配置

awslogs.conf 日志配置(必须)

proxy.conf 网络代理配置

注意,在其他 Linux 上 awslogs 的安装方式和配置路径与此不同。

Ubuntu Server、CentOS、Red HatREGION=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | awk -F\\\" '{print $4}')function install_awslogs() { curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O python ./awslogs-agent-setup.py --region ${REGION} -c s3://bucket/config-file-key}service awslogs status || (service awslogs start || install_awslogs)

获取地区值时我们使用了实例的动态数据。具体请参考 检索动态数据 和 检索实例元数据。

当然,在非 Amazon Linux 上通过"用户数据"做启动脚本,需要确认目标 AMI 装有 cloud-init,如果需要使用 aws-cli 也需要确认。

问题排查

通过 service awslogs status 检查代理程序是否在运行。如果出现异常,代理程序的日志保存在文件 /var/log/awslogs.log,检查该文件以获知是否存在错误。

你也可以通过 service awslogs start 启动、service awslogs restart 重启、service awslogs stop 停止。在修改配置文件后记得重启服务。

日志过期时间

可以在 AWS 控制台设置日志组的事件过期时间。

日志筛选指标

通过添加指标,可以监控符合给定模式的日志数,如提供筛选模式 "[WARN]" 得到警告级别的日志。具体请参考 筛选器和模式语法。

日志处理

使用订阅机制实时处理日志数据

批量导出日志数据到 S3

上述内容就是AWS怎么使用CloudWatch Logs收集日志,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report