简体   繁体   English

基于来自 AWS Cloudwatch 日志组的应用程序错误日志的警报

[英]Alerts based on Application error logs from AWS Cloudwatch log groups

I have an Ruby on Rails application running inside AWS EC2, which pushes all the application logs to cloudwatchlog groups.我有一个 Ruby on Rails 应用程序在 AWS EC2 内运行,它将所有应用程序日志推送到 cloudwatchlog 组。 the task now is to get an alerts daily from the cloudwatch logs reporting how many error codes are produced in a day.现在的任务是每天从 cloudwatch 日志中获取警报,报告一天产生了多少错误代码。

example:例子:

if my application is giving 10 "500 internal server errors", 5 "403 forbidden errors".如果我的应用程序给出 10 个“500 内部服务器错误”、5 个“403 禁止错误”。 I should get an email from AWS services that, "your application generated 10 - 500 error codes 5 - 403 error codes."我应该从 AWS 服务获得一个 email,“您的应用程序生成了 10 - 500 个错误代码 5 - 403 个错误代码。”

I think we can achieve this with a lambda function and AWS SNS service, but dont know how to code my lambda function to work in this manner. I think we can achieve this with a lambda function and AWS SNS service, but dont know how to code my lambda function to work in this manner.

I need a lambda function code to store the error code counts , After capturing the data i can run lambda function daily at a specific time to send an email. I need a lambda function code to store the error code counts , After capturing the data i can run lambda function daily at a specific time to send an email.

kindly help.请帮助。

Thanks in Advance:)提前致谢:)

What kind of load balancer do you have in front of the EC2; EC2前面有什么样的负载均衡器; if any?如果有的话?

Cloudwatch already contains the below metrics for a load balancer: Cloudwatch 已经包含负载均衡器的以下指标:

HTTPCode_Target_2XX_Count, HTTPCode_Target_3XX_Count, HTTPCode_Target_4XX_Count, HTTPCode_Target_5XX_Count See - https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-cloudwatch-metrics.html HTTPCode_Target_2XX_Count、HTTPCode_Target_3XX_Count、HTTPCode_Target_4XX_Count、HTTPCode_Target_5XX_Count 请参阅 - https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-cloudwatch-metrics.ZFC35FDC70D5C796

Requesting those metrics might be easier than parsing your application log files and counting the results.请求这些指标可能比解析应用程序日志文件和计算结果更容易。

To request those stats using the aws-sdk for ruby the below code should get you most of the way.要使用 ruby 的 aws-sdk 请求这些统计信息,下面的代码应该可以帮助您。 You will need to adapt it to your needs and setup, so see the docs I've linked at the bottom of the answer:您需要根据您的需要和设置对其进行调整,因此请参阅我在答案底部链接的文档:

metric = Aws::CloudWatch::Metric.new 'AWS/ApplicationELB', 'HTTPCode_ELB_4XX_Count'

stats = metric.get_statistics({
  dimensions: [
    {
      name: "LoadBalancerName",
      value: "'YOUR_ALB_NAME'",
    },
  ],
  start_time: Time.now - 3600 * 24 * 30,
  end_time: Time.now,
  period: 3600 * 24 * 30,
  unit: "Seconds", # accepts Seconds, Microseconds, Milliseconds, Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, Bits, Kilobits, Megabits, Gigabits, Terabits, Percent, Count, Bytes/Second, Kilobytes/Second, Megabytes/Second, Gigabytes/Second, Terabytes/Second, Bits/Second, Kilobits/Second, Megabits/Second, Gigabits/Second, Terabits/Second, Count/Second, None
  statistics: ["Average"], # accepts SampleCount, Average, Sum, Minimum, Maximum
})

See https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-cloudwatch-metrics.html for details on the stats you can request有关您可以请求的统计信息的详细信息,请参阅https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-cloudwatch-metrics.html

and https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CloudWatch/Metric.html for use of the ruby SDK and https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/CloudWatch/Metric.html for use of the ruby SDK

You can also dashboard these metrics if you go to the AWS dashboard, and you should probably start with this to get a feel for some of the params you use in your SDK call.如果您将 go 连接到 AWS 控制面板,您还可以控制这些指标,您可能应该从这个开始,以了解您在 SDK 调用中使用的一些参数。

  1. Go to the Cloudwatch service in your AWS dashboard Go 到 AWS 控制面板中的 Cloudwatch 服务
  2. Select Metrics from the left Select 左侧的指标
  3. Select ELB from the AWS namespaces section来自 AWS 命名空间部分的 Select ELB
  4. Select by namespace to get all load balancer info at the same time, or select by load balancer to narrow your metrics display. Select 通过命名空间同时获取所有负载均衡器信息,或 select 通过负载均衡器来缩小您的指标显示。
  5. Select the status code you want to visualise. Select 您想要可视化的状态代码。

This is how to get the count of status codes, and could form the basis of a ruby lambda function which can run every 30 days.这是获取状态码计数的方法,并且可以构成每 30 天运行一次的 ruby lambda function 的基础。 How to then email that to yourself could be a whole question unto itself but there are plenty of tutorials online for how to send email with Ruby - start there.然后如何 email 这对你自己来说可能是一个完整的问题,但是网上有很多关于如何发送 email 和 Ruby 的教程 - 从那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM