Skip to content

Commit

Permalink
smtp ibex config to web
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo committed Mar 15, 2023
1 parent 3383ca1 commit 6514891
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 154 deletions.
2 changes: 0 additions & 2 deletions alert/aconf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ type Alert struct {
EngineDelay int64
Heartbeat HeartbeatConfig
Alerting Alerting
SMTP SMTPConfig
Ibex Ibex
}

type SMTPConfig struct {
Expand Down
7 changes: 3 additions & 4 deletions alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func Start(alertc aconf.Alert, pushgwc pconf.Pushgw, syncStats *memsto.Stats, al
userGroupCache := memsto.NewUserGroupCache(ctx, syncStats)
alertSubscribeCache := memsto.NewAlertSubscribeCache(ctx, syncStats)
recordingRuleCache := memsto.NewRecordingRuleCache(ctx, syncStats)
webhookCache := memsto.NewWebhookCache(ctx)
notifyScript := memsto.NewNotifyScript(ctx)
notifyConfigCache := memsto.NewNotifyConfigCache(ctx)

go models.InitNotifyConfig(ctx, alertc.Alerting.TemplatesDir)

Expand All @@ -93,12 +92,12 @@ func Start(alertc aconf.Alert, pushgwc pconf.Pushgw, syncStats *memsto.Stats, al

eval.NewScheduler(isCenter, alertc, externalProcessors, alertRuleCache, targetCache, busiGroupCache, alertMuteCache, promClients, naming, ctx, alertStats)

dp := dispatch.NewDispatch(alertRuleCache, userCache, userGroupCache, alertSubscribeCache, targetCache, webhookCache, notifyScript, alertc.Alerting, alertc.Ibex, ctx)
dp := dispatch.NewDispatch(alertRuleCache, userCache, userGroupCache, alertSubscribeCache, targetCache, notifyConfigCache, alertc.Alerting, ctx)
consumer := dispatch.NewConsumer(alertc.Alerting, ctx, dp)

go dp.ReloadTpls()
go consumer.LoopConsume()

go queue.ReportQueueSize(alertStats)
go sender.StartEmailSender(alertc.SMTP)
go sender.StartEmailSender(notifyConfigCache.GetSMTP()) // todo
}
16 changes: 6 additions & 10 deletions alert/dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ type Dispatch struct {
userGroupCache *memsto.UserGroupCacheType
alertSubscribeCache *memsto.AlertSubscribeCacheType
targetCache *memsto.TargetCacheType
webhookCache *memsto.WebhookCacheType
notifyScriptCache *memsto.NotifyScriptCacheType
notifyConfigCache *memsto.NotifyConfigCacheType

alerting aconf.Alerting
ibex aconf.Ibex

senders map[string]sender.Sender
tpls map[string]*template.Template
Expand All @@ -40,19 +38,17 @@ type Dispatch struct {

// 创建一个 Notify 实例
func NewDispatch(alertRuleCache *memsto.AlertRuleCacheType, userCache *memsto.UserCacheType, userGroupCache *memsto.UserGroupCacheType,
alertSubscribeCache *memsto.AlertSubscribeCacheType, targetCache *memsto.TargetCacheType, webhookCache *memsto.WebhookCacheType, notifyScriptCache *memsto.NotifyScriptCacheType,
alerting aconf.Alerting, ibex aconf.Ibex, ctx *ctx.Context) *Dispatch {
alertSubscribeCache *memsto.AlertSubscribeCacheType, targetCache *memsto.TargetCacheType, notifyConfigCache *memsto.NotifyConfigCacheType,
alerting aconf.Alerting, ctx *ctx.Context) *Dispatch {
notify := &Dispatch{
alertRuleCache: alertRuleCache,
userCache: userCache,
userGroupCache: userGroupCache,
alertSubscribeCache: alertSubscribeCache,
targetCache: targetCache,
webhookCache: webhookCache,
notifyScriptCache: notifyScriptCache,
notifyConfigCache: notifyConfigCache,

alerting: alerting,
ibex: ibex,

senders: make(map[string]sender.Sender),
tpls: make(map[string]*template.Template),
Expand Down Expand Up @@ -190,13 +186,13 @@ func (e *Dispatch) Send(rule *models.AlertRule, event *models.AlertCurEvent, not
}

// handle event callbacks
sender.SendCallbacks(e.ctx, notifyTarget.ToCallbackList(), event, e.targetCache, e.ibex)
sender.SendCallbacks(e.ctx, notifyTarget.ToCallbackList(), event, e.targetCache, e.notifyConfigCache.GetIbex())

// handle global webhooks
sender.SendWebhooks(notifyTarget.ToWebhookList(), event)

// handle plugin call
go sender.MayPluginNotify(e.genNoticeBytes(event), e.notifyScriptCache)
go sender.MayPluginNotify(e.genNoticeBytes(event), e.notifyConfigCache.GetNotifyScript())
}

type Notice struct {
Expand Down
2 changes: 1 addition & 1 deletion alert/dispatch/notify_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NotifyGroupDispatch(rule *models.AlertRule, event *models.AlertCurEvent, pr
}

func GlobalWebhookDispatch(rule *models.AlertRule, event *models.AlertCurEvent, prev *NotifyTarget, dispatch *Dispatch) *NotifyTarget {
webhooks := dispatch.webhookCache.GetWebhooks()
webhooks := dispatch.notifyConfigCache.GetWebhooks()
NotifyTarget := NewNotifyTarget()
for _, webhook := range webhooks {
if !webhook.Enable {
Expand Down
8 changes: 4 additions & 4 deletions alert/sender/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"os/exec"
"time"

"github.com/ccfos/nightingale/v6/memsto"
"github.com/ccfos/nightingale/v6/models"

"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/sys"
)

func MayPluginNotify(noticeBytes []byte, notifyScript *memsto.NotifyScriptCacheType) {
func MayPluginNotify(noticeBytes []byte, notifyScript models.NotifyScript) {
if len(noticeBytes) == 0 {
return
}
alertingCallScript(noticeBytes, notifyScript)
}

func alertingCallScript(stdinBytes []byte, notifyScript *memsto.NotifyScriptCacheType) {
func alertingCallScript(stdinBytes []byte, notifyScript models.NotifyScript) {
// not enable or no notify.py? do nothing
config := notifyScript.GetNotifyScript()
config := notifyScript
if !config.Enable || config.Content == "" {
return
}
Expand Down
3 changes: 3 additions & 0 deletions center/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ func (rt *Router) Config(r *gin.Engine) {

pages.GET("/notify-contact", rt.auth(), rt.admin(), rt.notifyContactGets)
pages.PUT("/notify-contact", rt.auth(), rt.admin(), rt.notifyContactPuts)

pages.GET("/notify-config", rt.auth(), rt.admin(), rt.notifyConfigGet)
pages.PUT("/notify-config", rt.auth(), rt.admin(), rt.notifyConfigPut)
}

if rt.HTTP.Service.Enable {
Expand Down
27 changes: 27 additions & 0 deletions center/router/router_notify_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package router
import (
"encoding/json"

"github.com/ccfos/nightingale/v6/alert/aconf"
"github.com/ccfos/nightingale/v6/models"
"github.com/pelletier/go-toml/v2"

"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/ginx"
Expand Down Expand Up @@ -133,3 +135,28 @@ func (rt *Router) notifyContactPuts(c *gin.Context) {

ginx.NewRender(c).Message(models.ConfigsSet(rt.Ctx, models.NOTIFYCONTACT, string(data)))
}

func (rt *Router) notifyConfigGet(c *gin.Context) {
key := ginx.QueryStr(c, "key")
cval, err := models.ConfigsGet(rt.Ctx, key)
ginx.NewRender(c).Data(cval, err)
}

func (rt *Router) notifyConfigPut(c *gin.Context) {
var f models.Configs
ginx.BindJSON(c, &f)
switch f.Ckey {
case models.SMTP:
var smtp aconf.SMTPConfig
err := toml.Unmarshal([]byte(f.Cval), &smtp)
ginx.Dangerous(err)
case models.IBEX:
var ibex aconf.Ibex
err := toml.Unmarshal([]byte(f.Cval), &ibex)
ginx.Dangerous(err)
default:
ginx.Bomb(200, "key %s can not modify", f.Ckey)
}

ginx.NewRender(c).Message(models.ConfigsSet(rt.Ctx, f.Ckey, f.Cval))
}
120 changes: 120 additions & 0 deletions memsto/notify_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package memsto

import (
"encoding/json"
"sync"
"time"

"github.com/BurntSushi/toml"
"github.com/ccfos/nightingale/v6/alert/aconf"
"github.com/ccfos/nightingale/v6/models"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/toolkits/pkg/logger"
)

type NotifyConfigCacheType struct {
ctx *ctx.Context
webhooks []*models.Webhook
smtp aconf.SMTPConfig
script models.NotifyScript
ibex aconf.Ibex

sync.RWMutex
}

func NewNotifyConfigCache(ctx *ctx.Context) *NotifyConfigCacheType {
w := &NotifyConfigCacheType{
ctx: ctx,
}
w.SyncNotifyConfigs()
return w
}

func (w *NotifyConfigCacheType) SyncNotifyConfigs() {
err := w.syncNotifyConfigs()
if err != nil {
logger.Errorf("failed to sync webhooks:", err)
}

go w.loopSyncNotifyConfigs()
}

func (w *NotifyConfigCacheType) loopSyncNotifyConfigs() {
duration := time.Duration(9000) * time.Millisecond
for {
time.Sleep(duration)
if err := w.syncNotifyConfigs(); err != nil {
logger.Warning("failed to sync webhooks:", err)
}
}
}

func (w *NotifyConfigCacheType) syncNotifyConfigs() error {
w.RWMutex.Lock()
defer w.RWMutex.Unlock()

cval, err := models.ConfigsGet(w.ctx, models.WEBHOOKKEY)
if err != nil {
return err
}
json.Unmarshal([]byte(cval), &w.webhooks)
logger.Infof("timer: sync wbhooks done number: %d", len(w.webhooks))

cval, err = models.ConfigsGet(w.ctx, models.SMTP)
if err != nil {
return err
}

err = toml.Unmarshal([]byte(cval), &w.smtp)
if err != nil {
logger.Errorf("failed to unmarshal smtp:%s config:", cval, err)
}

logger.Infof("timer: sync smtp done")

cval, err = models.ConfigsGet(w.ctx, models.NOTIFYSCRIPT)
if err != nil {
return err
}
err = json.Unmarshal([]byte(cval), &w.script)
if err != nil {
logger.Errorf("failed to unmarshal notify script:%s config:", cval, err)
}
logger.Infof("timer: sync notify script done")

cval, err = models.ConfigsGet(w.ctx, models.IBEX)
if err != nil {
return err
}
err = toml.Unmarshal([]byte(cval), &w.ibex)
if err != nil {
logger.Errorf("failed to unmarshal ibex:%s config:", cval, err)
}
logger.Infof("timer: sync ibex done")

return nil
}

func (w *NotifyConfigCacheType) GetWebhooks() []*models.Webhook {
w.RWMutex.RLock()
defer w.RWMutex.RUnlock()
return w.webhooks
}

func (w *NotifyConfigCacheType) GetSMTP() aconf.SMTPConfig {
w.RWMutex.RLock()
defer w.RWMutex.RUnlock()
return w.smtp
}

func (w *NotifyConfigCacheType) GetNotifyScript() models.NotifyScript {
w.RWMutex.RLock()
defer w.RWMutex.RUnlock()
return w.script
}

func (w *NotifyConfigCacheType) GetIbex() aconf.Ibex {
w.RWMutex.RLock()
defer w.RWMutex.RUnlock()
return w.ibex
}
62 changes: 0 additions & 62 deletions memsto/notify_script.go

This file was deleted.

Loading

0 comments on commit 6514891

Please sign in to comment.