2016-02-09 96 views

回答

2

解決方法是定義兩個指標,其名稱相同但帶有逆濾波器。與錯誤級別日誌消息相匹配的過濾器必須返回度量值1,而第二個過濾器應匹配所有消息或時間段內的至少一條消息,並返回度量值00值的存在可避免數據錯誤不足。

當從度量標準創建警報時,兩個過濾器合併在一起。如果應用總數統計並應用了>0的警報規則,則僅當錯誤消息到達並且沒有遇到不足的數據時纔會觸發警報。

下面是使用boto3客戶機的示例:

import boto3 
client = boto3.client('logs') 
logGroupName = 'myLogGroup' 
# create this SNS topic with your email subscription... 
env['aws_sns_arn_error_email'] = 'arn:aws:sns:eu-west-1:1234567:log_error' 

env['sys_type'] = 'production' 

metricsNamespace = 'LogMetrics' 
metricName = 'ErrorCount' + "_%(sys_type)s" % env 

print colors.cyan('Put metric $(metricName)s' % env) 

cloudwatch_client = boto3.client('cloudwatch') 
response = cloudwatch_client.put_metric_data(
    Namespace=metricsNamespace, 
    MetricData=[ 
     { 
      'MetricName': metricName, 
      'Unit': 'Count', 
      'Value': 1 
     }, 
    ] 
) 

logs_client = boto3.client('logs') 
print colors.cyan('Put metric filter $.levelname-ERROR') 
logs_client.put_metric_filter(
    logGroupName=env.log_group_name_ea, 
    filterName='levelname-ERROR', 
    filterPattern='{ $.levelname = "ERROR" }', 
    metricTransformations=[ 
     { 
      'metricNamespace': metricsNamespace, 
      'metricValue': '0', 
      'metricName': metricName, 
     }] 

) 
print colors.cyan('Put metric filter catchAll') 
logs_client.put_metric_filter(
    logGroupName=env.log_group_name_ea, 
    filterName="catchAll", 
    filterPattern='', 
    metricTransformations=[ 
     { 
      'metricNamespace': metricsNamespace, 
      'metricValue': '1', 
      'metricName': metricName, 
     }] 
) 

print colors.cyan('Put metric alarm, email on error') 

response = cloudwatch_client.put_metric_alarm(
    AlarmName='email on error', 
    AlarmDescription='email on error', 
    ActionsEnabled=True, 

    AlarmActions=[ 
     env.aws_sns_arn_error_email, 
    ], 

    MetricName=metricName, 
    Namespace=metricsNamespace, 
    Statistic='Sum', 
    Period=300, 
    Unit='Count', 
    EvaluationPeriods=1, 
    Threshold=0, 
    ComparisonOperator='GreaterThanThreshold' 
) 
相關問題