夜莺Nightingale告警推送Jira

夜莺Nightingale告警推送Jira

nightingale

最近收到个需求,在处理告警事件中,需要将一级告警推送Jira工单

夜莺版本:V5.15.0

开发环境:Golang1.18

1、server.conf添加相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Jira]
# Enable
Enable = true
# Jira 地址
Url = "https://xxx.com/"
# Jira 账号
BasicAuthUser = "xxx"
# Jira 密码
BasicAuthPass = "xxx"
# Jira 项目名
ProjectName = "xxx"
# Jira 项目ID
ProjectId = "xxx"
# Issue类型
IssueType = "xxx"

2、 代码修改

修改src/server/config/config.go

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
#添加jira 结构体
type Jira struct {
Enable bool
Url string
BasicAuthUser string
BasicAuthPass string
ProjectName string
ProjectId string
IssueType string
}
#在原有config结构体添加jira
type Config struct {
RunMode string
ClusterName string // 监控对象上报时,指定的集群名称
BusiGroupLabelKey string
EngineDelay int64
DisableUsageReport bool
ReaderFrom string
LabelRewrite bool
ForceUseServerTS bool
Log logx.Config
HTTP httpx.Config
BasicAuth gin.Accounts
SMTP SMTPConfig
Heartbeat HeartbeatConfig
Alerting Alerting
NoData NoData
Redis storage.RedisConfig
DB ormx.DBConfig
WriterOpt WriterGlobalOpt
Writers []WriterOptions
Reader PromOption
Readers []PromOption
Ibex Ibex
Jira Jira
}

在src/server/common/sender/ 新建jira.go

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
package sender

import (
"github.com/andygrunwald/go-jira"
"github.com/didi/nightingale/v5/src/models"
"github.com/didi/nightingale/v5/src/pkg/tplx"
"github.com/didi/nightingale/v5/src/server/config"
"github.com/toolkits/pkg/logger"
"html/template"
"io/ioutil"
"path"
)

func JiraSender(ctx *models.AlertCurEvent) {
title := ctx.RuleName
tplpath := path.Join(config.C.Alerting.TemplatesDir, "jira.tpl")
tpl, err := template.New("jira.tpl").Funcs(tplx.TemplateFuncMap).ParseFiles(tplpath)
if err != nil {
logger.Errorf("failed to parse tpl: " + tplpath)
return
}
message := BuildTplMessage(tpl, ctx)
auth := jira.BasicAuthTransport{
Username: config.C.Jira.BasicAuthUser,
Password: config.C.Jira.BasicAuthPass,
}
jiraClient, err := jira.NewClient(auth.Client(), config.C.Jira.Url)
if err != nil {
logger.Errorf("jira_sender: result=fail" + err.Error())
return
}
owner := ctx.GetTagValue("owner")
if owner == "" {
return
}
issue := jira.Issue{
Fields: &jira.IssueFields{
Assignee: &jira.User{
Name: owner,
},
Description: message,
Type: jira.IssueType{
Name: config.C.Jira.IssueType,
},
Project: jira.Project{
Name: config.C.Jira.ProjectName,
ID: config.C.Jira.ProjectId,
},
Summary: title,
},
}
_, res, err := jiraClient.Issue.Create(&issue)
if err != nil {
response, err := ioutil.ReadAll(res.Body)
logger.Errorf("jira_sender: result=fail", err, string(response))
return
} else {
logger.Infof("jira_sender: result=succ %s: %+v")
}
}

修改src/server/engine/notify.go

在send中添加触发推送jira逻辑

1
2
3
4
	if config.C.Jira.Enable && event.Severity == 1 && !event.IsRecovered {
sender.JiraSender(event)
}
#是否启用,告警级别是否为1,告警状态为触发状态

3、添加jira工单模板

创建 etc/template/jira.tpl

1
2
3
4
5
6
7
策略备注:{{.RuleNote}}
告警区域:{{.TagsMap.Region}}
{{if not .TargetIdent}}告警对象:{{.TargetNote}}{{.TagsMap.container}}{{.TagsMap.topic}}{{.TagsMap.description}}{{end}}
{{if .TargetIdent}}告警机器:{{.TargetIdent}} - {{.TargetNote}}{{end}}
触发时值:{{.TriggerValue}}
触发时间:{{timeformat .TriggerTime}}
事件ID:{{.Id}}

4、编译部署

1
2
3
go mod tidy
make
./n9e server

5、告警规则修改

需要推送jira的告警规则需要添加附加标签,附加标签对应jira处理人,告警等级为一级告警

标签为:owner=”xxx”

image-20230808134357465

6、工单效果

image-20230808134832784


夜莺Nightingale告警推送Jira
https://www.starsfox.com/posts/a503d6f5.html
作者
Flycat
发布于
2023年8月8日
许可协议