上篇文章我们主要是针对处理告警自助创建群进行了说明设计。增加该功能的初衷主要是为了方便处理问题,当我们遇到比较严重的问题、或者说需要开发、主管一起进行排查时我们就可以使用该功能,点击后会自动将对应开发、主管拉入群聊中,当问题解决后该群会移除除了群主(建议新增一个账号来作为群主账号)之外所有人。
使用gin
、gorm
实现对钉钉日程进行crud
,以便我们可以灵活的排版、调班等操作。当然也可以直接在钉钉进行创建日程。创建后日程后每天会提醒当值人员。
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master-50.57 Ready control-plane,master 96d v1.20.5
k8s-node-50.58 Ready <none> 96d v1.20.5
k8s-node-50.59 Ready <none> 96d v1.20.5
# kubectl get pod -n monitoring
NAME READY STATUS RESTARTS AGE
alertmanager-main-0 2/2 Running 0 8d
alertmanager-main-1 2/2 Running 0 8d
alertmanager-main-2 2/2 Running 0 8d
blackbox-exporter-55c457d5fb-5m7ql 3/3 Running 0 8d
grafana-9df57cdc4-gpzsq 1/1 Running 0 8d
kube-state-metrics-56dbb74497-gpkn9 3/3 Running 0 8d
node-exporter-4wl6d 2/2 Running 0 8d
node-exporter-b4595 2/2 Running 0 8d
node-exporter-g4l99 2/2 Running 0 8d
prometheus-adapter-59df95d9f5-tnt4w 1/1 Running 0 8d
prometheus-adapter-59df95d9f5-xhz5v 1/1 Running 0 8d
prometheus-k8s-0 2/2 Running 1 8d
prometheus-k8s-1 2/2 Running 1 10m
prometheus-operator-c46b8b7c9-mg9cv 2/2 Running 0 8d
APP
进行创建这里的需要填写的内容就不做过多的解释了,按照需求填写即可
API
进行创建具体代码下边有简要说明
type UserListOfCalendar struct {
ID uint `gorm:"primarykey" json:"id" form:"id"`
CreatedAt time.Time `gorm:"column:creation_time" json:"created_at" form:"created_at"`
UpdatedAt time.Time `gorm:"column:update_time" json:"updated_at" form:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"column:delete_time;index" json:"deleted_at" form:"deleted_at"`
Name string `gorm:"comment: '值班人员名称';size:255;not null" json:"name" form:"name"`
UserID string `gorm:"comment: '值班人员userID';size:64;not null;unique" json:"userid" form:"userid"`
UserPhone string `gorm:"comment: '值班人员登录钉钉手机号';size:11;not null;unique" json:"user_phone" form:"user_phone"`
UnionID string `gorm:"comment: '值班人员登录钉钉手机号';size:64;not null;unique" json:"unionID" form:"unionID"`
}
func InitCreateCalendarBody() {
ccb := calendar.RequestCalendar{}
ccb.Recurrence.Pattern.Type = "daily"
ccb.Recurrence.Range.Type = "numbered"
ccb.Recurrence.Range.NumberOfOccurrences = 30
reminder := struct {
Method string `json:"method"`
Minutes int `json:"minutes"`
}{Method: "dingtalk", Minutes: 10}
ccb.Reminders = append(ccb.Reminders, reminder)
ccb.IsAllDay = false
uiConfig := struct {
UIName string `json:"uiName"`
}{UIName: "transferEventButton"}
ccb.UIConfigs = append(ccb.UIConfigs, uiConfig)
ccb.Start.DateTime = "2023-09-01T09:00:00+08:00"
ccb.Start.TimeZone = "Asia/Shanghai"
ccb.End.DateTime = "2023-09-01T23:59:59+08:00"
ccb.End.TimeZone = "Asia/Shanghai"
CreateCalendarBody = ccb
}
unionid
func (gu *getUnionid) GetUserUnionid(userid string) (err error, resUnionid *calendar.ResponseUnionID) {
var (
ctx context.Context
cancel context.CancelFunc
reqUnionid calendar.RequestUnionID
)
reqUnionid.UserID = userid
ctx, cancel = context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
header := map[string]string{
"Content-type": "application/json",
}
_, token := utils.GetToken()
url := viper.GetString("calendar.getunionidURL") + "?" + "access_token=" + token
err, data := utils.DoRequest(ctx, "POST", url, header, reqUnionid)
if err != nil {
return err, nil
}
_ = json.Unmarshal([]byte(data), &resUnionid)
return nil, resUnionid
}
func (gc *getCalendar) GetCalendarOfUser() (err error, summaryName string) {
var (
ctx context.Context
cancel context.CancelFunc
responseGetCalendarOfUser *calendar.ResponseGetCalendarOfUser
)
ctx, cancel = context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
_, token := utils.GetToken()
header := map[string]string{
"x-acs-dingtalk-access-token": token,
"Content-Type": "application/json",
}
nowTime := time.Now()
startTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, nowTime.Location())
endTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 23, 59, 59, 0, nowTime.Location())
urlAdd := fmt.Sprintf("%s%s%s%s%s%s", viper.GetString("calendar.getCalendarOfUser"),
viper.GetString("calendar.adminUnion"),
"/calendars/primary/events?timeMin=", url.QueryEscape(startTime.Format("2006-01-02T15:04:05-07:00")), "&timeMax=",
url.QueryEscape(endTime.Format("2006-01-02T15:04:05-07:00")))
// 这里你可以重试请求
err, data := utils.DoRequest(ctx, "GET", urlAdd, header, nil)
if err != nil {
common.TPLogger.Error("获取值班人员信息失败")
return err, ""
}
_ = json.Unmarshal([]byte(data), &responseGetCalendarOfUser)
summaryName = responseGetCalendarOfUser.Events[0].Summary
return nil, summaryName
}
看下图,可以看出已告警已触发了
告警排班功能到此已经添加完成了,主要是为了解决有些小伙伴公司没有内部的排班系统,所以这次利用钉钉的日程功能来实现一个灵活的排班机制。下期内容:告警聚合