AWS RDS 不提供主机文件访问权限,因此无法直接访问数据库日志。但是,AWS RDS提供将数据库日志导出到CloudWatch Logs的功能,您可以访问该功能进行监控报警。
解决方案概述
操作步骤:
- RDS开通慢日志查询
- 导入到CloudWatch Log
- 启用订阅SNS
- Lambda代码开发
import sys
import re
import boto3
import os
# Reading the cloudwatch log data
def lambda_handler(event, context):
print("start")
cloud_log_time = event['logStream']
cloud_log_data = event['message']
# Resding Query_time
result = re.findall(r'Query_time: \d+\.\d+', cloud_log_data)
#print(result[0])
data_list = result[0].split(":")[1]
#print(data_list)
data_time = float(data_list)
# # Sends an SNS notification with the error information
if data_time > 10:
SNSClient = boto3.client('sns')
response = SNSClient.publish(TopicArn='arn:aws:sns:us-east-2:1234148497:Default_CloudWatch_Alarms_Topic',Message=cloud_log_data,Subject='rds_slow_log')
print(response)
权限帮助链接: > https://aws.amazon.com/cn/premiumsupport/knowledge-center/sns-authorization-error-lambda-function/
标签:log,rds,aws,amazon,time,日志,data From: https://www.cnblogs.com/flyhgx/p/17098169.html