Using API to Create Alarm Policy

Last updated: 2021-04-22 15:47:23

    This document describes how to use APICreateAlarmPolicy and APIBinding Policy Object to create alarm policies and bind alarm objects.

    Preparations

    Before calling the APICreate Alarm Policy , you need to prepare the following information.

    Preparing personal key

    1. Go to the API Key Management page in the CAM console.
    2. Click Show to get the SecretKey.

    Note:

    If no key has been created, please click Create Key to create one.

    Preparing alarm policy type

    You can query all policy types through the APIDescribeAllNamespaces in the following steps:

    1. Log in to the API Explorer console and enter the input parameters as shown below:

      Parameter Description
      SecretId, SecretKey Enter the prepared `SecretId` and `SecretKey`
      Region Select the corresponding region
      SceneType Enter `ST_ALARM`
      Module Enter `monitor`
      MonitorTypes.N Optional
    2. Click Online Call > Send Request to get the response, where Response.QceNamespacesNew.N.Id is the Namespace required by alarm policy creation.

    Note:

    Here, Namespace is the alarm policy type, which is different from the Tencent Cloud service namespace used to pull monitoring data.

    Preparing metric list

    You can query all alarm metrics under the policy type through the APIDescribeAlarmMetrics.

    1. Log in to the API Explorer console and enter the input parameters as shown below:
      Parameter Description
      SecretId, SecretKey Enter the prepared `SecretId` and `SecretKey`
      Region Select the corresponding region
      Module Enter `monitor`
      MonitorType Enter `MT_QCE`
      Namespace Enter the alarm policy type obtained in the "Preparing alarm policy type" step, i.e., `Response.QceNamespacesNew.N.Id` in the returned result
    2. Click Online Call > Send Request on the right to get the response, where Response.Metrics.N lists all the alarm metrics under the policy type.

    Preparing event list

    You can query all the alarm events under the policy type through the APIDescribeAlarmEvents.

    1. Log in to the API Explorer console and enter the input parameters as shown below:
    Parameter Description
    SecretId, SecretKey Enter the prepared `SecretId` and `SecretKey`
    Region Select the corresponding region
    Module Enter `monitor`
    Namespace Enter the alarm policy type obtained in the "Preparing alarm policy type" step, i.e., `Response.QceNamespacesNew.N.Id` in the returned result
    2. Click Online Call > Send Request to get the response, where `Response.Events.N.EventName` is the `EventName` required by alarm policy creation.

    Directions

    This document describes how to use APIs such as APICreateAlarmPolicy to create an alarm policy for CVM - basic monitoring with the following example.

    Creating alarm policy

    1. Log in to the API Explorer console.

    2. Copy the prepared personal key into the corresponding SecretId and SecretKey text boxes.

    3. Find Region in the Input Parameters section and select the relevant region.

    4. Enter monitor in Module, a custom policy name in PolicyName, and MT_QCE in MonitorType.

    5. Enter the alarm policy type obtained in the Preparing alarm policy type step above in Namespace. For example, the alarm policy type of CVM - basic monitoring is cvm_device.

    6. In the CVM - basic monitoring use case, Remark and Enable are optional, while ProjectId is required.

      • Remark: remark, which is optional.
      • Enable: whether to enable the alarm policy. 0 indicates to disable, 1 indicates to enable, and the default value is 1. This parameter is optional.
      • ProjectId: project ID, which should be 0 for CVM - basic monitoring.

        Note:

        ProjectId is the project ID. -1 indicates no project, 0 indicates the default project, and the default value is -1. This parameter is optional depending on the policy type. For example, some alarm policy types don't have the concept of project (such as VPC), and the default value -1 can be used. If the alarm policy type has the concept of project (such as CVM - basic monitoring), an error will be reported if the default value -1 is passed in, and the input parameter should be changed to "0".

    7. Configure Condition as shown below:

      Parameter Required Description
      IsUnionRule Yes Metric trigger condition operator. Valid values: 0 (OR), 1 (AND). OR means that the alarm will be sent when any condition is triggered, while AND means that the alarm will be sent when all conditions are triggered
      Rules.N Yes List of alarm trigger conditions, which can be configured by referring to the `AlarmPolicyRule` parameter
      • MetricName: enter the `MetricName` (Metrics.N.MetricName) returned in the Preparing metric list step
      • Period: enter the `Period` (Metrics.N.MetricConfig.Period) returned in the Preparing metric list step
      • Operator: enter the `Operator` (Metrics.N.MetricConfig.Operator) returned in the Preparing metric list step
      • Value: enter a threshold without the unit, such as 80
      • ContinuePeriod: enter the `ContinuePeriod` (Metrics.N.MetricConfig.ContinuePeriod) returned in the Preparing metric list step
      • NoticeFrequency: set the alarm frequency in seconds. Parameter description: alarm interval in seconds. Valid values: 0 (do not repeat), 300 (alarm once every 5 minutes), 600 (alarm once every 10 minutes), 900 (alarm once every 15 minutes), 1800 (alarm once every 30 minutes), 3600 (alarm once every hour), 7200 (alarm once every 2 hours), 10800 (alarm once every 3 hours), 21600 (alarm once every 6 hours), 43200 (alarm once every 12 hours), 86400 (alarm once every day)
      • IsPowerNotice: set whether the alarm frequency grows exponentially. Valid values: 0 (no), 1 (yes)
      • Other parameters can be left empty
    8. If you want to trigger an event alarm, you need to configure the EventCondition parameter. Under EventCondition, you only need to enter the EventName obtained in the Preparing event list step in Rules.N.MetricName, and you can leave other parameters empty.

    9. Enter the alarm notification template ID in NoticeIds.N, such as notice-qvq836vc, which can be obtained through the APIDescribeAlarmNotices.

    10. After entering the above parameters, click Online Call > Send Request. The following figure shows the successful creation of the alarm policy for CVM - basic monitoring.

    11. After the creation is successful, you can view the alarm policy on the Alarm Policy page in the Cloud Monitor console.

    Binding alarm object

    1. Log in to the API Explorer console.

    2. Copy the prepared personal key into the corresponding SecretId and SecretKey text boxes.

    3. Find Region in the Input Parameters section and select the relevant region.

    4. Enter monitor in Module.

    5. Enter 0 in GroupId.

    6. Enter either the InstanceGroupId or Dimensions as detailed below:

      • InstanceGroupId: instance group ID. If you want to bind alarm objects by instance group, you need to pass in the instance group ID (such as 1234), which can be found on the instance group page in the Cloud Monitor console by clicking the corresponding instance name as shown below:

      • Dimensions.N: if you wany to bind an alarm policy by instance ID, you need to enter Dimensions as detailed below:

        Parameter Description
        RegionId, Region Please see the instance region description; for example, for the Guangzhou region, `RegionId` is `1`, and `Region` is `gz`
        Dimensions Enter the CVM instance ID, which can be obtained through the DescribeInstances API. The input parameter format is {"unInstanceId":"ins-xxxxxxxx'"}
        EventDimensions Enter the globally unique instance ID, which can be obtained through the DescribeInstances API. The input parameter format is {"uuid":"9d51a69e-0e4a-4120-ae58-9c073c851e24"}
    7. Enter the PolicyId (Response.PolicyId) returned in the Creating alarm policy step in PolicyId, such as policy-zg2sk27j.

    8. After entering the above parameters, click Online Call > Send Request. The following figure shows that the alarm policy is successfully bound.

    9. After the creation is successful, you can view the number of instances associated with the corresponding alarm policy on the Alarm Policy page in the Cloud Monitor console.