AWS Cloudwatch日志过滤报警可在报警通知中使用

时间:2017-06-29 17:37:14

标签: aws-lambda amazon-sns amazon-cloudwatch

我使用Cloudwatch和Lambda来监控日志。 我已经设置了一些在日志中出现特定类型的错误时触发的过滤器。 是否可以在Cloudwatch Alarm SNS上发送在Log上过滤的参数?

例如:

收到错误:

  

[2017-06-29 17:58:52] prod.ERROR:ErrorException:未定义的变量:   消息X

指标过滤器:

  

[日期,信息=" * ERROR:",错误]

警报:

  

< = 0

时可以

向SNS和Lambda发送通知以触发不同的通知代理。

SNS事件向我提供了此消息,但我想访问已过滤的变量:

{
    "AlarmName": "PHP_ERROR",
    "AlarmDescription": null,
    "AWSAccountId": "xxxxxxxxx",
    "NewStateValue": "OK",
    "NewStateReason": "Threshold Crossed: no datapoints were received for 1 period and 1 missing datapoint was treated as [NonBreaching]",
    "StateChangeTime": "2017-06-29T17:09:12.336+0000",
    "Region": "EU - Ireland",
    "OldStateValue": "ALARM",
    "Trigger": {
        "MetricName": "PHP_ERROR",
        "Namespace": "Logs",
        "StatisticType": "Statistic",
        "Statistic": "SUM",
        "Unit": null,
        "Dimensions": [],
        "Period": 60,
        "EvaluationPeriods": 1,
        "ComparisonOperator": "GreaterThanOrEqualToThreshold",
        "Threshold": 0,
        "TreatMissingData": "- TreatMissingData:                    NonBreaching",
        "EvaluateLowSampleCountPercentile": ""
    }
}

谢谢,

1 个答案:

答案 0 :(得分:0)

不幸的是,alarm只看metric来评估阈值。所以简短的答案是:(

然后,您有了另一个抽象级别,因为已经从metric的特定值设置了filter

enter image description here是唯一可以找到所提取值的地方,但是只会将匹配项转换为度量值(即:数字)的值或增量,因此无法用作已解析的日志存储。

AWS最近发布了filter,它可以帮助您查找错误消息。

否则,您可以尝试使用AWS Cloudwatch Log Insights,它可以让您浏览日志。

Ubuntu中的示例

export YOUR_LOG_GROUP_NAME=SomeLogGroup

# The 1000 multiplication is to convert from seconds to milliseconds
# If you already have a specific timestamp, just replace it on the start-time argument
aws logs filter-log-events --log-group-name $YOUR_LOG_GROUP_NAME \
--start-time $(($(date +%s --date="1 minute ago") * 1000)) \
--interleaved --filter-pattern ".ERROR" \
--output=text --query events[*].[message]

OSx中的示例

export YOUR_LOG_GROUP_NAME=SomeLogGroup

# The 1000 multiplication is to convert from seconds to milliseconds
# If you already have a specific timestamp, just replace it on the start-time argument
aws logs filter-log-events --log-group-name $YOUR_LOG_GROUP_NAME \
--start-time $(($(date -v-1M +%s) * 1000)) \
--interleaved --filter-pattern ".ERROR" \
--output=text --query events[*].[message]

如果要使其自动化,可以替换触发操作,以调用lambda(使用某些AWS开发工具包而不是CLI),从而可以使用此信息为SNS生成所需的消息。

即:

  • 发件人:Metric -> Alarm -> SNS
  • 收件人:Metric -> Alarm -> Lambda -> SNS