安全云脑 SECMASTER-创建告警规则:Go

时间:2023-12-06 18:52:41

Go

创建一条告警规则,告警规则所属的管道ID为772fb35b-83bc-46c9-a0b1-ebe31070a889,告警规则名称为Alert rule,查询类型为SQL,状态为启用。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    secmaster "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/secmaster/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/secmaster/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/secmaster/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := secmaster.NewSecMasterClient(
        secmaster.SecMasterClientBuilder().
            WithRegion(region.ValueOf("cn-north-4")).
            WithCredential(auth).
            Build())

    request := &model.CreateAlertRuleRequest{}
	modeTriggers:= model.GetAlertRuleTriggerModeEnum().COUNT
	operatorTriggers:= model.GetAlertRuleTriggerOperatorEnum().GT
	severityTriggers:= model.GetAlertRuleTriggerSeverityEnum().TIPS
	var listTriggersbody = []model.AlertRuleTrigger{
        {
            Mode: &modeTriggers,
            Operator: &operatorTriggers,
            Expression: "10",
            Severity: &severityTriggers,
        },
    }
	delayIntervalSchedule:= int32(2)
	overtimeIntervalSchedule:= int32(10)
	schedulebody := &model.Schedule{
		FrequencyInterval: int32(5),
		FrequencyUnit: model.GetScheduleFrequencyUnitEnum().MINUTE,
		PeriodInterval: int32(5),
		PeriodUnit: model.GetSchedulePeriodUnitEnum().MINUTE,
		DelayInterval: &delayIntervalSchedule,
		OvertimeInterval: &overtimeIntervalSchedule,
	}
	var listCustomPropertiesbody = map[string]string{
        "references": "https://localhost/references",
        "maintainer": "isap",
    }
	suspressionCreateAlertRuleRequestBody:= true
	eventGroupingCreateAlertRuleRequestBody:= true
	severityCreateAlertRuleRequestBody:= model.GetCreateAlertRuleRequestBodySeverityEnum().TIPS
	statusCreateAlertRuleRequestBody:= model.GetCreateAlertRuleRequestBodyStatusEnum().ENABLED
	queryTypeCreateAlertRuleRequestBody:= model.GetCreateAlertRuleRequestBodyQueryTypeEnum().SQL
	descriptionCreateAlertRuleRequestBody:= "An alert rule"
	request.Body = &model.CreateAlertRuleRequestBody{
		Triggers: listTriggersbody,
		Schedule: schedulebody,
		Suspression: &suspressionCreateAlertRuleRequestBody,
		EventGrouping: &eventGroupingCreateAlertRuleRequestBody,
		CustomProperties: listCustomPropertiesbody,
		Severity: &severityCreateAlertRuleRequestBody,
		Status: &statusCreateAlertRuleRequestBody,
		QueryType: &queryTypeCreateAlertRuleRequestBody,
		Query: "* | select status, count(*) as count group by status",
		Description: &descriptionCreateAlertRuleRequestBody,
		RuleName: "Alert rule",
		PipeId: "772fb35b-83bc-46c9-a0b1-ebe31070a889",
	}
	response, err := client.CreateAlertRule(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}
support.huaweicloud.com/api-secmaster/CreateAlertRule.html