通过Go SDK管理告警

本文介绍通过Go SDK使用告警的代码示例。

前提条件

  • 已创建RAM用户并完成授权。具体操作,请参见创建RAM用户并完成授权

  • 已配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具体操作,请参见在Linux、macOS和Windows系统配置环境变量

    重要
    • 阿里云账号的AccessKey拥有所有API的访问权限,建议您使用RAM用户的AccessKey进行API访问或日常运维。

    • 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • 已安装0.1.37及以上版本的Go SDK。具体操作,请参见安装Go SDK

管理告警监控规则

代码示例如下。具体的参数说明,请参见告警监控规则数据结构

package main

import (
    "fmt"
    "os"
    sls "github.com/aliyun/aliyun-log-go-sdk"
)

var (
    // 日志服务的服务接入点。
    endpoint        = "cn-huhehaote.log.aliyuncs.com"
    // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
    accessKeyId     = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") 
    // 创建日志服务Client。
    client          = sls.CreateNormalInterface(endpoint, accessKeyId, accessKeySecret, "")
    project         = "demo-alert"
    alertId         = "nginx-status-error"
)

func createAlert() {
    alert := &sls.Alert{
        Name:        alertId,
        DisplayName: "Nginx Status Error",
        State:       "Enabled",
        Schedule: &sls.Schedule{
            Type:     sls.ScheduleTypeFixedRate,
            Interval: "1m",
        },
        Configuration: &sls.AlertConfiguration{
            Version:   "2.0",
            Type:      "default",
            Dashboard: "internal-alert-analysis",
            QueryList: []*sls.AlertQuery{
                &sls.AlertQuery{
                    StoreType:    "log",
                    Region:       "cn-huhehaote",
                    Project:      project,
                    Store:        "nginx-access-log",
                    Query:        "status >= 400 | select count(*) as cnt",
                    TimeSpanType: "Truncated",
                    Start:        "-1m",
                    End:          "absolute",
                    PowerSqlMode: sls.PowerSqlModeAuto,
                },
            },
            GroupConfiguration: sls.GroupConfiguration{
                Type: sls.GroupTypeNoGroup,
            },
            JoinConfigurations: []*sls.JoinConfiguration{},
            SeverityConfigurations: []*sls.SeverityConfiguration{
                &sls.SeverityConfiguration{
                    Severity: sls.Medium,
                    EvalCondition: sls.ConditionConfiguration{
                        Condition:      "cnt > 0",
                        CountCondition: "",
                    },
                },
            },
            Labels: []*sls.Tag{
                &sls.Tag{
                    Key:   "service",
                    Value: "nginx",
                },
            },
            Annotations: []*sls.Tag{
                &sls.Tag{
                    Key:   "title",
                    Value: "Nginx Status Error",
                },
                &sls.Tag{
                    Key:   "desc",
                    Value: "Nginx Status Error, count: ${cnt}",
                },
            },
            AutoAnnotation: true,
            SendResolved:   false,
            Threshold:      1,
            NoDataFire:     false,
            NoDataSeverity: sls.Medium,
            PolicyConfiguration: sls.PolicyConfiguration{
                AlertPolicyId:  "sls.builtin.dynamic",
                ActionPolicyId: "test-action-policy",
                RepeatInterval: "1m",
                UseDefault:     false,
            },
        },
    }
    err := client.CreateAlert(project, alert)
    fmt.Println("[create alert]", err)
}

func getAndUpdateAlert() {
    alert, err := client.GetAlert(project, alertId)
    fmt.Println("[get alert]", sls.JsonMarshal(alert), err)

    alert.Configuration.QueryList[0].Query = "status >= 500 | select count(*) as cnt"
    err = client.UpdateAlert(project, alert)
    fmt.Println("[update alert]", err)
}

func disableAndEnableAlert() {
    err := client.DisableAlert(project, alertId)
    fmt.Println("[disable alert]", err)

    err = client.EnableAlert(project, alertId)
    fmt.Println("[enable alert]", err)
}

func listAlerts() {
    alerts, total, count, err := client.ListAlert(project, "", "", 0, 100)
    fmt.Println("[list alert]", sls.JsonMarshal(alerts), total, count, err)
}

func deleteAlert() {
    err := client.DeleteAlert(project, alertId)
    fmt.Println("[delete alert]", err)
}

func main() {
    createAlert()
    getAndUpdateAlert()
    disableAndEnableAlert()
    listAlerts()
    deleteAlert()
}

管理告警资源数据

代码示例如下。具体的参数说明,请参见告警资源数据结构

管理用户

package main

import (
    "fmt"
    "os"
    sls "github.com/aliyun/aliyun-log-go-sdk"
)

var (
    // 日志服务的服务接入点。资源数据的写操作,必须使用河源地域,读操作可以使用其它地域。
    endpoint        = "cn-heyuan.log.aliyuncs.com"
    // 本示例从环境变量中获取AccessKey ID和AccessKey Secret。
    accessKeyId     = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") 
    // 创建日志服务Client。
    client           = sls.CreateNormalInterface(endpoint, accessKeyId, accessKeySecret, "")
    userResourceName = "sls.common.user"
)

func createUser() {
    user := &sls.ResourceUser{
        UserId:       "alex",
        UserName:     "Alex",
        Enabled:      true,
        CountryCode:  "86",
        Phone:        "133****3333",
        Email:        []string{"****@example.com"},
        SmsEnabled:   true,
        VoiceEnabled: true,
    }
    record := &sls.ResourceRecord{
        Id:    user.UserId,
        Tag:   user.UserName,
        Value: sls.JsonMarshal(user),
    }
    err := client.CreateResourceRecord(userResourceName, record)
    fmt.Println("[create user]", err)
}

func getUser() {
    record, err := client.GetResourceRecord(userResourceName, "alex")
    fmt.Println("[list user]", sls.JsonMarshal(record), err)
}

func updateUser() {
    user := &sls.ResourceUser{
        UserId:       "alex",
        UserName:     "Alex",
        Enabled:      false,
        CountryCode:  "86",
        Phone:        "133****3333",
        Email:        []string{"****@example.com"},
        SmsEnabled:   true,
        VoiceEnabled: true,
    }
    record := &sls.ResourceRecord{
        Id:    user.UserId,
        Tag:   user.UserName,
        Value: sls.JsonMarshal(user),
    }
    err := client.UpdateResourceRecord(userResourceName, record)
    fmt.Println("[update user]", err)
}

func listUsers() {
    records, count, total, err := client.ListResourceRecord(userResourceName, 0, 100)
    fmt.Println("[list users]", sls.JsonMarshal(records), count, total, err)
}

func deleteUser() {
    err := client.DeleteResourceRecord(userResourceName, "alex")
    fmt.Println("[delete user]", err)
}

func main() {
    createUser()
    getUser()
    updateUser()
    listUsers()
    deleteUser()
}

管理用户组

userGroup := &sls.ResourceUserGroup{
    Id:      "devops",
    Name:    "DevOps Team",
    Enabled: true,
    Members: []string{"alex"},
}
record := &sls.ResourceRecord{
    Id:    userGroup.Id,
    Tag:   userGroup.Name,
    Value: sls.JsonMarshal(userGroup),
}
err := client.CreateResourceRecord("sls.common.user_group", record)
fmt.Println("[create user group]", err)

管理Webhook集成

webhooks := []*sls.WebhookIntegration{
    &sls.WebhookIntegration{
        Id:      "dingtalk",
        Name:    "Dingtalk Webhook",
        Method:  "POST",
        Url:     "https://oapi.dingtalk.com/robot/send?access_token=**********",
        Type:    "dingtalk",
        // 钉钉加签密钥。如果您在钉钉侧设置安全验证方式为签名校验,则需配置该字段。您可以在钉钉机器人管理界面获取加签密钥。
        // Secret:  "SEC**********",
        Headers: []*sls.ResourceWebhookHeader{},
    },
    &sls.WebhookIntegration{
        Id:      "wechat",
        Name:    "Wechat Webhook",
        Method:  "POST",
        Url:     "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=**********",
        Type:    "wechat",
        Headers: []*sls.ResourceWebhookHeader{},
    },
    &sls.WebhookIntegration{
        Id:      "feishu",
        Name:    "Feishu Webhook",
        Method:  "POST",
        Url:     "https://open.feishu.cn/open-apis/bot/v2/hook/**********",
        Type:    "lark",
        // 飞书加签密钥。如果您在飞书侧设置安全验证方式为签名校验,则需配置该字段。您可以在飞书机器人管理界面获取加签密钥。
        // Secret:  "**********",
        Headers: []*sls.ResourceWebhookHeader{},
    },
    &sls.WebhookIntegration{
        Id:      "slack",
        Name:    "Slack Webhook",
        Method:  "POST",
        Url:     "https://hooks.slack.com/services/**********",
        Type:    "slack",
        Headers: []*sls.ResourceWebhookHeader{},
    },
    &sls.WebhookIntegration{
        Id:     "webhook",
        Name:   "Common Webhook",
        Method: "POST",
        Url:    "https://example.com/***********",
        Type:   "custom",
        Headers: []*sls.ResourceWebhookHeader{
            &sls.ResourceWebhookHeader{
                Key:   "Authorization",
                Value: "Basic YWRtaW46Zm9vYmFy",
            },
        },
    },
}

for _, webhook := range webhooks {
    record := &sls.ResourceRecord{
        Id:    webhook.Id,
        Tag:   webhook.Name,
        Value: sls.JsonMarshal(webhook),
    }
    err := client.CreateResourceRecord("sls.alert.action_webhook", record)
    fmt.Println("[create webhook integration]", record.Id, err)
}

管理行动策略

actionPolicy := &sls.ResourceActionPolicy{
    ActionPolicyId:              "test-action-policy",
    ActionPolicyName:            "Test Action Policy",
    PrimaryPolicyScript:         `fire(type="sms", users=["alex"], groups=[], oncall_groups=[], receiver_type="static", external_url="", external_headers={}, template_id="sls.builtin.cn", period="any")`,
    SecondaryPolicyScript:       `fire(type="voice", users=["alex"], groups=[], oncall_groups=[], receiver_type="static", external_url="", external_headers={}, template_id="sls.builtin.cn", period="any")`,
    EscalationStartEnabled:      false,
    EscalationStartTimeout:      "10m",
    EscalationInprogressEnabled: false,
    EscalationInprogressTimeout: "30m",
    EscalationEnabled:           true,
    EscalationTimeout:           "1h",
}
record := &sls.ResourceRecord{
    Id:    actionPolicy.ActionPolicyId,
    Tag:   actionPolicy.ActionPolicyName,
    Value: sls.JsonMarshal(actionPolicy),
}
err := client.CreateResourceRecord("sls.alert.action_policy", record)
fmt.Println("[create action policy]", err)

管理告警策略

alertPolicy := &sls.ResourceAlertPolicy{
    PolicyId:      "test-alert-policy",
    PolicyName:    "Test Alert Policy",
    Parent:        "",
    GroupPolicy:   `fire(action_policy="test-action-policy", group={"alert.alert_id": alert.alert_id}, group_by_all_labels=true, group_wait="15s", group_interval="5m", repeat_interval="1h")`,
    InhibitPolicy: "",
    SilencePolicy: "",
}
record := &sls.ResourceRecord{
    Id:    alertPolicy.PolicyId,
    Tag:   alertPolicy.PolicyName,
    Value: sls.JsonMarshal(alertPolicy),
}
err := client.CreateResourceRecord("sls.alert.alert_policy", record)
fmt.Println("[create alert policy]", err)

管理内容模板

template := &sls.ResourceContentTemplate{
    TemplateId:   "test-template",
    TemplateName: "Test Template",
    Templates: sls.ResourceTemplates{
        Sms: sls.ResourceTemplate{
            Locale:  "zh-CN",
            Content: "",
        },
        Voice: sls.ResourceTemplate{
            Locale:  "zh-CN",
            Content: "",
        },
        Email: sls.ResourceTemplate{
            Locale:  "en-US",
            Subject: "SLS Alert",
            Content: "",
        },
        Dingtalk: sls.ResourceTemplate{
            Locale:  "zh-CN",
            Title:   "SLS Alert",
            Content: "",
        },
        Wechat: sls.ResourceTemplate{
            Locale:  "zh-CN",
            Title:   "SLS Alert",
            Content: "",
        },
        Lark: sls.ResourceTemplate{
            Locale:  "zh-CN",
            Title:   "SLS Alert",
            Content: "",
        },
        Slack: sls.ResourceTemplate{
            Locale:  "zh-CN",
            Title:   "SLS Alert",
            Content: "",
        },
        Webhook: sls.ResourceTemplate{
            Locale:   "zh-CN",
            SendType: "single",
            Limit:    0,
            Content:  "",
        },
    },
}
record := &sls.ResourceRecord{
    Id:    template.TemplateId,
    Tag:   template.TemplateName,
    Value: sls.JsonMarshal(template),
}
err := client.CreateResourceRecord("sls.alert.content_template", record)
fmt.Println("[create content template]", err)