All Products
Search
Document Center

CloudMonitor:Write alerts to MNS

Last Updated:Mar 28, 2024

This topic describes how to write alerts from CloudMonitor to Message Service (MNS).

Procedure

  1. Authorize CloudMonitor to write alerts to MNS.

    1. Go to the Cloud Resource Access Authorization page to allow CloudMonitor to assume the AliyunCloudMonitorDefaultRole role.

    2. Click Confirm Authorization Policy.

  2. Call the PutResourceMetricRule operation to create an alert rule.

    For more information, see PutResourceMetricRule.

  3. Call the PutMetricRuleTargets operation to create alert messages for the alert rule and write them to the specified MNS queue.

    For more information, see PutMetricRuleTargets.

    The Alibaba Cloud Resource Name (ARN) of an MNS queue is in the "acs:mns:{$RegionId}:{$UserId}:/queues/{$queueName}/messages" format.

    The following code shows an example of the PutMetricRuleTargets operation:

    RuleId:"putNewAlarm_group_778af9ba-a291-46ab-ac53-3983bcee****",
    Targets:[{
        Id: 1,
        Arn:"acs:mns:{$RegionId}:{$UserId}:/queues/{$queueName}/messages",
        Level: ["INFO", "WARN", "CRITICAL"],
    }]

Message body written to MNS

CloudMonitor writes a message body to MNS in the JSON format. The following code shows an example of a message body in the JSON format:

{
  "ruleId": "putNewAlarm_group_778af9ba-a291-46ab-ac53-3983bcee****",
  "ruleName": "test123",
  // The current alert level. 
  "curLevel": "WARN",
  // The previous alert level. 
  "preLevel": "OK",
  // The instance that triggers the alert. 
  "resources": "{\"instanceId\": \"i-uf61rfofjd2iku7e****\"}",
  // The condition that triggers the alert. 
  "escalation": {
    "comparisonOperator": "GreaterThanYesterday",
    "level": 3,
    "statistics": "Average",
    "tag": "WARN",
    "threshold": "0",
    "times": 1
  },
  "metricData": {
    "timestamp": 1534736160000,
    "userId": "127067667954****",
    "instanceId": "i-uf61rfofjd2iku7e****",
    "Average": 470687744,
    "Maximum": 470794240,
    "Minimum": 470556672,
    // The historical value of the AliyunCmsPrevValues parameter. For example, you can compare the value in the current period between today and yesterday. 
    "AliyunCmsPrevValues": { 
      "timestamp": 1534649760000,
      "userId": "127067667954****",
      "instanceId": "i-uf61rfofjd2iku7e****",
      "Average": 468463616,
      "Maximum": 468549632,
      "Minimum": 468258816
    },
    // The comparison formula. 
    "AliyunCmsComplexExpression": "100.0 * ($Average-$$prevAverage)/$$prevAverage",
    // The conversion formula. 
    "AliyunCmsComplexMath": "100.0 * (470687744-468463616)/468463616",
    // The calculation result. 
    "AliyunCmsComplexValue": 0.47477070236336133
  },
  // The information about the metric. 
  "metricName": "memory_actualusedspace#60",
  "namespace": "acs_ecs_dashboard",
  "period": "60",

  // The information about the application group. 
  "groupBy": "group",
  "productGroupName": "ECS instance",
  "groupId":"7301****",

  // The alert time. 
  "lastTime": 327362743, // The duration of the alert. Unit: milliseconds. 
  "time": 1534736160000, // The time when the alert was generated. Unit: milliseconds. 

  "userId": "173651113438****",
  "eventName": "AlertOk",
  "eventType": "Alert",
  // The trace information. 
  "batchId": "4272653-152082****",
  "version": "1.0"
}