Skip to content

Commit

Permalink
fix(mongodb): 发送告警时添加appid #8298
Browse files Browse the repository at this point in the history
  • Loading branch information
cycker committed Nov 29, 2024
1 parent ad39cba commit ff6f3d1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 279 deletions.
23 changes: 4 additions & 19 deletions dbm-services/mongodb/db-tools/dbmon/cmd/dbmonheartbeat/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
package dbmonheartbeat

import (
"dbm-services/mongodb/db-tools/dbmon/cmd/mongojob"
"fmt"
"strconv"
"sync"

"dbm-services/mongodb/db-tools/dbmon/config"
"dbm-services/mongodb/db-tools/dbmon/mylog"
"dbm-services/mongodb/db-tools/dbmon/pkg/sendwarning"
)

const MongoDbmonHeartBeatMetricName = "mongo_dbmon_heart_beat"
Expand Down Expand Up @@ -53,25 +52,11 @@ func (job *Job) Run() {

// SendHeartBeat 发送心跳
func SendHeartBeat(conf *config.BkMonitorBeatConfig, serverConf *config.ConfServerItem) error {
msgH, err := sendwarning.NewBkMonitorEventSender(
conf.BeatPath,
conf.AgentAddress,
)
msgH, err := mongojob.GetBkMonitorBeatSender(conf, serverConf)
if err != nil {
return err
}
return msgH.SetBkBizID(strconv.Itoa(serverConf.BkBizID)).
SetBkCloudID(serverConf.BkCloudID).
SetApp(serverConf.App).
SetAppName(serverConf.AppName).
SetClusterDomain(serverConf.ClusterDomain).
SetClusterName(serverConf.ClusterName).
SetClusterType(serverConf.ClusterType).
SetInstanceRole(serverConf.MetaRole).
SendTimeSeriesMsg(conf.MetricConfig.DataID,
conf.MetricConfig.Token,
serverConf.IP,
MongoDbmonHeartBeatMetricName,
1)
return msgH.SendTimeSeriesMsg(conf.MetricConfig.DataID, conf.MetricConfig.Token,
serverConf.IP, MongoDbmonHeartBeatMetricName, 1)

}
16 changes: 6 additions & 10 deletions dbm-services/mongodb/db-tools/dbmon/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,18 @@ func sendmsgCmdMain() {
msgTargetIp = server.IP
}
beatConfig := &config.GlobalConf.BkMonitorBeat
msgH, err := mongojob.GetBkMonitorBeatSender(beatConfig, &server)
if err != nil {
fmt.Printf("fatal err %s", err)
os.Exit(1)
}
if msgType == "event" {
msgH, err := mongojob.GetBkMonitorEventSender(beatConfig, &server)
if err != nil {
fmt.Printf("fatal err %s", err)
os.Exit(1)
}
msgH.SendEventMsg(
beatConfig.EventConfig.DataID,
beatConfig.EventConfig.Token,
msgEventName, msgEventMsg, msgEventLevel, msgTargetIp)
} else if msgType == "ts" {
msgH, err := mongojob.GetBkMonitorMetricSender(beatConfig, &server)
if err != nil {
fmt.Printf("fatal err %s", err)
os.Exit(1)
}

msgH.SendTimeSeriesMsg(
beatConfig.MetricConfig.DataID,
beatConfig.MetricConfig.Token,
Expand Down
56 changes: 15 additions & 41 deletions dbm-services/mongodb/db-tools/dbmon/cmd/mongojob/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package mongojob

import (
"fmt"
"github.com/pkg/errors"
"strconv"

"dbm-services/mongodb/db-tools/dbmon/config"
"dbm-services/mongodb/db-tools/dbmon/mylog"
"dbm-services/mongodb/db-tools/dbmon/pkg/sendwarning"
)

// GetBkMonitorEventSender Retrun a BkMonitorEventSender instance
func GetBkMonitorEventSender(beatConf *config.BkMonitorBeatConfig, serverConf *config.ConfServerItem) (msgH *sendwarning.BkMonitorEventSender, err error) {
// GetBkMonitorBeatSender Retrun a BkMonitorEventSender instance
func GetBkMonitorBeatSender(beatConf *config.BkMonitorBeatConfig, serverConf *config.ConfServerItem) (
msgH *sendwarning.BkMonitorEventSender, err error) {
msgH, err = sendwarning.NewBkMonitorEventSender(
beatConf.BeatPath,
beatConf.AgentAddress,
Expand All @@ -20,59 +22,31 @@ func GetBkMonitorEventSender(beatConf *config.BkMonitorBeatConfig, serverConf *c
}
msgH.SetBkBizID(strconv.Itoa(serverConf.BkBizID)).
SetBkCloudID(serverConf.BkCloudID).
SetBkTargetIp(serverConf.IP).
SetApp(serverConf.App).
SetAppName(serverConf.AppName).
SetClusterDomain(serverConf.ClusterDomain).
SetClusterName(serverConf.ClusterName).
SetClusterType(serverConf.ClusterType).
SetInstanceRole(serverConf.MetaRole)
return
}

// GetBkMonitorMetricSender Retrun a BkMonitorMetricSender instance
func GetBkMonitorMetricSender(conf *config.BkMonitorBeatConfig, serverConf *config.ConfServerItem) (
msgH *sendwarning.BkMonitorEventSender, err error) {
msgH, err = sendwarning.NewBkMonitorEventSender(
conf.BeatPath,
conf.AgentAddress,
)
if err != nil {
return
}
msgH.SetBkBizID(strconv.Itoa(serverConf.BkBizID)).
SetBkCloudID(serverConf.BkCloudID).
SetApp(serverConf.App).
SetAppName(serverConf.AppName).
SetClusterDomain(serverConf.ClusterDomain).
SetClusterName(serverConf.ClusterName).
SetClusterType(serverConf.ClusterType).
SetInstanceRole(serverConf.MetaRole)
SetInstanceRole(serverConf.MetaRole).
SetInstance(serverConf.Addr())
return
}

// SendEvent 发送告警消息
func SendEvent(conf *config.BkMonitorBeatConfig, serverConf *config.ConfServerItem,
eventName, warnLevel, warnMsg string) error {

msgH, err := sendwarning.NewBkMonitorEventSender(
conf.BeatPath,
conf.AgentAddress,
)

if msgH != nil && err == nil {
err = msgH.SetBkBizID(strconv.Itoa(serverConf.BkBizID)).
SetBkCloudID(serverConf.BkCloudID).
SetApp(serverConf.App).
SetAppName(serverConf.AppName).
SetClusterDomain(serverConf.ClusterDomain).
SetClusterName(serverConf.ClusterName).
SetClusterType(serverConf.ClusterType).
SetInstanceRole(serverConf.MetaRole).SendEventMsg(
conf.EventConfig.DataID,
conf.EventConfig.Token,
eventName, warnMsg, warnLevel, serverConf.IP)
msgH, err := GetBkMonitorBeatSender(conf, serverConf)
if err != nil {
return errors.Wrap(err, "NewBkMonitorEventSender failed")
}

err = msgH.SendEventMsg(
conf.EventConfig.DataID,
conf.EventConfig.Token,
eventName, warnMsg, warnLevel, serverConf.IP)

if err != nil {
mylog.Logger.Warn(
fmt.Sprintf("SendEvent failed,name:%s level:%s warnMsg:%q err: %+v",
Expand Down
46 changes: 0 additions & 46 deletions dbm-services/mongodb/db-tools/dbmon/models/mymongo/mymongo.go

This file was deleted.

2 changes: 1 addition & 1 deletion dbm-services/mongodb/db-tools/dbmon/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

repoVersion=0.0.1
respGitHash=$(git rev-parse --short HEAD)
respGitDate=$(date +"%Y%m%d%H%M%S")
respGitDate=$(date +"%Y%m%d%H%M.%S")

# 解析传入的 --version=xxx 和 --git-hash=xxx --git-date="yyyymmdd hhmmss"参数
while [ $# -gt 0 ]; do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func NewBkMonitorEventSender(beatPath, agentAddress string) (ret *BkMonitorEvent
}

// SendEventMsg 发送告警消息
func (bm *BkMonitorEventSender) SendEventMsg(dataId int64, token string, eventName, warnmsg, warnLevel, targetIP string) (err error) {
func (bm *BkMonitorEventSender) SendEventMsg(dataId int64, token string, eventName,
warnmsg, warnLevel, targetIP string) (err error) {
bm.newDimenSion()
bm.DataID = dataId
bm.AccessToken = token
Expand All @@ -82,7 +83,8 @@ func (bm *BkMonitorEventSender) SendEventMsg(dataId int64, token string, eventNa
}

// SendTimeSeriesMsg dbmon心跳上报. "mongo_dbmon_heart_beat"
func (bm *BkMonitorEventSender) SendTimeSeriesMsg(dataId int64, token string, targetIP string, metricName string, val float64) (err error) {
func (bm *BkMonitorEventSender) SendTimeSeriesMsg(dataId int64, token string, targetIP string,
metricName string, val float64) (err error) {
bm.newDimenSion()
bm.DataID = dataId
bm.AccessToken = token
Expand Down Expand Up @@ -145,7 +147,7 @@ func (bm *BkMonitorEventSender) SetBkBizID(bkBizID string) *BkMonitorEventSender

// SetBkCloudID set bk_cloud_id
func (bm *BkMonitorEventSender) SetBkCloudID(bkCloudID int64) *BkMonitorEventSender {
return bm.set("bk_cloud_id", bkCloudID)
return bm.set("bk_cloud_id", bkCloudID).set("bk_target_cloud_id", bkCloudID)
}

// set key value
Expand All @@ -160,8 +162,12 @@ func (bm *BkMonitorEventSender) SetApp(app string) *BkMonitorEventSender {
return bm.set("app", app)
}

// SetBkTargetIp TODO
func (bm *BkMonitorEventSender) SetBkTargetIp(bkTargetIp string) *BkMonitorEventSender {
return bm.set("bk_target_ip", bkTargetIp)
}

// SetAppName TODO
// SetApp set app
func (bm *BkMonitorEventSender) SetAppName(appname string) *BkMonitorEventSender {
return bm.set("app_name", appname)
}
Expand Down
Loading

0 comments on commit ff6f3d1

Please sign in to comment.