Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:1.verify notify template 2.heartbeat add remote_addr 3. gid auto busi group #1498

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion center/center.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Initialize(configDir string, cryptoKey string) (func(), error) {
writers := writer.NewWriters(config.Pushgw)

alertrtRouter := alertrt.New(config.HTTP, config.Alert, alertMuteCache, targetCache, busiGroupCache, alertStats, ctx, externalProcessors)
centerRouter := centerrt.New(config.HTTP, config.Center, cconf.Operations, dsCache, notifyConfigCache, promClients, redis, sso, ctx, metas)
centerRouter := centerrt.New(config.HTTP, config.Center, cconf.Operations, dsCache, notifyConfigCache, promClients, redis, sso, ctx, metas, targetCache)
pushgwRouter := pushgwrt.New(config.HTTP, config.Pushgw, targetCache, busiGroupCache, idents, writers, ctx)

r := httpx.GinEngine(config.Global.RunMode, config.HTTP)
Expand Down
4 changes: 3 additions & 1 deletion center/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ type Router struct {
PromClients *prom.PromClientMap
Redis storage.Redis
MetaSet *metas.Set
TargetCache *memsto.TargetCacheType
Sso *sso.SsoClient
Ctx *ctx.Context
}

func New(httpConfig httpx.Config, center cconf.Center, operations cconf.Operation, ds *memsto.DatasourceCacheType, ncc *memsto.NotifyConfigCacheType,
pc *prom.PromClientMap, redis storage.Redis, sso *sso.SsoClient, ctx *ctx.Context, metaSet *metas.Set) *Router {
pc *prom.PromClientMap, redis storage.Redis, sso *sso.SsoClient, ctx *ctx.Context, metaSet *metas.Set, tc *memsto.TargetCacheType) *Router {
return &Router{
HTTP: httpConfig,
Center: center,
Expand All @@ -46,6 +47,7 @@ func New(httpConfig httpx.Config, center cconf.Center, operations cconf.Operatio
PromClients: pc,
Redis: redis,
MetaSet: metaSet,
TargetCache: tc,
Sso: sso,
Ctx: ctx,
}
Expand Down
13 changes: 12 additions & 1 deletion center/router/router_heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ func (rt *Router) heartbeat(c *gin.Context) {
ginx.Dangerous(err)

req.Offset = (time.Now().UnixMilli() - req.UnixTime)
req.RemoteAddr = c.Request.RemoteAddr
rt.MetaSet.Set(req.Hostname, req)
ginx.NewRender(c).Message(nil)

gid := ginx.QueryInt64(c, "gid", 0)

if gid != 0 {
target, has := rt.TargetCache.Get(req.Hostname)
if has && target.GroupId != gid {
err = models.TargetUpdateBgid(rt.Ctx, []string{req.Hostname}, gid, false)
}
}

ginx.NewRender(c).Message(err)
}
22 changes: 22 additions & 0 deletions center/router/router_notify_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package router
import (
"bytes"
"encoding/json"
"fmt"
"html/template"

"github.com/ccfos/nightingale/v6/center/cconf"
Expand All @@ -22,16 +23,37 @@ func (rt *Router) notifyTplUpdateContent(c *gin.Context) {
var f models.NotifyTpl
ginx.BindJSON(c, &f)

if err := templateValidate(f); err != nil {
ginx.NewRender(c).Message(err.Error())
return
}

ginx.NewRender(c).Message(f.UpdateContent(rt.Ctx))
}

func (rt *Router) notifyTplUpdate(c *gin.Context) {
var f models.NotifyTpl
ginx.BindJSON(c, &f)

if err := templateValidate(f); err != nil {
ginx.NewRender(c).Message(err.Error())
return
}

ginx.NewRender(c).Message(f.Update(rt.Ctx))
}

func templateValidate(f models.NotifyTpl) error {
if f.Content == "" {
return nil
}
if _, err := template.New(f.Channel).Funcs(tplx.TemplateFuncMap).Parse(f.Content); err != nil {
return fmt.Errorf("notify template verify illegal:%s", err.Error())
}

return nil
}

func (rt *Router) notifyTplPreview(c *gin.Context) {
var event models.AlertCurEvent
err := json.Unmarshal([]byte(cconf.EVENT_EXAMPLE), &event)
Expand Down
1 change: 1 addition & 0 deletions models/host_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type HostMeta struct {
MemUtil float64 `json:"mem_util"`
Offset int64 `json:"offset"`
UnixTime int64 `json:"unixtime"`
RemoteAddr string `json:"remote_addr"`
}

func (h HostMeta) MarshalBinary() ([]byte, error) {
Expand Down
18 changes: 10 additions & 8 deletions models/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ type Target struct {
TagsMap map[string]string `json:"-" gorm:"-"` // internal use, append tags to series
UpdateAt int64 `json:"update_at"`

UnixTime int64 `json:"unixtime" gorm:"-"`
Offset int64 `json:"offset" gorm:"-"`
TargetUp float64 `json:"target_up" gorm:"-"`
MemUtil float64 `json:"mem_util" gorm:"-"`
CpuNum int `json:"cpu_num" gorm:"-"`
CpuUtil float64 `json:"cpu_util" gorm:"-"`
OS string `json:"os" gorm:"-"`
Arch string `json:"arch" gorm:"-"`
UnixTime int64 `json:"unixtime" gorm:"-"`
Offset int64 `json:"offset" gorm:"-"`
TargetUp float64 `json:"target_up" gorm:"-"`
MemUtil float64 `json:"mem_util" gorm:"-"`
CpuNum int `json:"cpu_num" gorm:"-"`
CpuUtil float64 `json:"cpu_util" gorm:"-"`
OS string `json:"os" gorm:"-"`
Arch string `json:"arch" gorm:"-"`
RemoteAddr string `json:"remote_addr" gorm:"-"`
}

func (t *Target) TableName() string {
Expand Down Expand Up @@ -296,6 +297,7 @@ func (t *Target) FillMeta(meta *HostMeta) {
t.Offset = meta.Offset
t.OS = meta.OS
t.Arch = meta.Arch
t.RemoteAddr = meta.RemoteAddr
}

func TargetIdents(ctx *ctx.Context, ids []int64) ([]string, error) {
Expand Down