[TASK] add support for default (not configurated notify)
This commit is contained in:
parent
ce46bad745
commit
d3318177aa
|
@ -23,6 +23,7 @@ type DB struct {
|
|||
HostsByName map[string]*Host `json:"-"`
|
||||
Notifies []*Notify `json:"notifies"`
|
||||
NotifiesByAddress map[string]*Notify `json:"-"`
|
||||
DefaultNotify []*Notify `json:"-"`
|
||||
}
|
||||
|
||||
func (db *DB) SendTo(e *log.Entry) (*log.Entry, *Host, []*Notify) {
|
||||
|
@ -36,6 +37,17 @@ func (db *DB) SendTo(e *log.Entry) (*log.Entry, *Host, []*Notify) {
|
|||
host.Lastseen = time.Now()
|
||||
}
|
||||
var toList []*Notify
|
||||
entry := e
|
||||
if host.Name != "" {
|
||||
entry = entry.WithField("hostname", host.Name)
|
||||
entry.Level = e.Level
|
||||
entry.Message = e.Message
|
||||
}
|
||||
// return default notify list
|
||||
if host.Notifies == nil || len(host.Notifies) == 0 {
|
||||
return entry, host, db.DefaultNotify
|
||||
}
|
||||
// return with host specify list
|
||||
for _, notify := range host.NotifiesByAddress {
|
||||
if lvl := notify.MaxPrioIn; e.Level >= lvl {
|
||||
continue
|
||||
|
@ -52,17 +64,10 @@ func (db *DB) SendTo(e *log.Entry) (*log.Entry, *Host, []*Notify) {
|
|||
}
|
||||
toList = append(toList, notify)
|
||||
}
|
||||
if host.Name != "" {
|
||||
entry := e.WithField("hostname", host.Name)
|
||||
entry.Level = e.Level
|
||||
entry.Message = e.Message
|
||||
return entry, host, toList
|
||||
}
|
||||
return e, host, toList
|
||||
} else {
|
||||
host = db.NewHost(addr)
|
||||
return entry, host, toList
|
||||
}
|
||||
return e, host, nil
|
||||
host = db.NewHost(addr)
|
||||
return e, host, db.DefaultNotify
|
||||
}
|
||||
|
||||
func (db *DB) Alert(expired time.Duration, send func(e *log.Entry, n *Notify) bool) {
|
||||
|
|
|
@ -12,14 +12,19 @@ type NotifyConfig struct {
|
|||
AlertCheck Duration `toml:"alert_check"`
|
||||
Console bool `toml:"debug"`
|
||||
XMPP struct {
|
||||
JID string `toml:"jid"`
|
||||
Password string `toml:"password"`
|
||||
JID string `toml:"jid"`
|
||||
Password string `toml:"password"`
|
||||
Defaults map[string]bool `toml:"default"`
|
||||
} `toml:"xmpp"`
|
||||
Websocket struct {
|
||||
Address string `toml:"address"`
|
||||
Webroot string `toml:"webroot"`
|
||||
Default string `toml:"default"`
|
||||
} `toml:"websocket"`
|
||||
FileDirectory string `toml:"file_directory"`
|
||||
File struct {
|
||||
Directory string `toml:"directory"`
|
||||
Default string `toml:"default"`
|
||||
} `toml:"file"`
|
||||
}
|
||||
|
||||
type ReceiveConfig struct {
|
||||
|
|
|
@ -10,12 +10,17 @@ address = ":10002"
|
|||
state_file = "/tmp/logmania.state.json"
|
||||
debug = true
|
||||
|
||||
file_directory = "/tmp/"
|
||||
[file]
|
||||
directory = "/tmp/"
|
||||
default = "raw"
|
||||
|
||||
[notify.xmpp]
|
||||
jid = "user@example.org"
|
||||
password = "password"
|
||||
# if boolean is true for muc either user chat
|
||||
default = { "log-raw@conference.example.org" = true, "person@example.org" = false }
|
||||
|
||||
[notify.websocket]
|
||||
address = ":8080"
|
||||
webroot = "./webroot/"
|
||||
default = "raw"
|
||||
|
|
|
@ -20,6 +20,7 @@ type Notifier struct {
|
|||
|
||||
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
||||
var list []notify.Notifier
|
||||
var defaults []*database.Notify
|
||||
for _, init := range notify.NotifyRegister {
|
||||
notify := init(config, db, bot)
|
||||
|
||||
|
@ -27,8 +28,15 @@ func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifi
|
|||
continue
|
||||
}
|
||||
list = append(list, notify)
|
||||
def := notify.Default()
|
||||
if def != nil {
|
||||
continue
|
||||
}
|
||||
defaults = append(defaults, def...)
|
||||
}
|
||||
|
||||
db.DefaultNotify = defaults
|
||||
|
||||
n := &Notifier{
|
||||
db: db,
|
||||
list: list,
|
||||
|
@ -50,7 +58,7 @@ func (n *Notifier) sender() {
|
|||
}
|
||||
}
|
||||
if !send {
|
||||
logger.Warnf("notify not send to anybody: [%s] %s", c.Level.String(), c.Message)
|
||||
logger.Warnf("notify not send to %s: [%s] %s", to.Address(), c.Level.String(), c.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,24 +21,38 @@ var logger = log.WithField("notify", proto)
|
|||
|
||||
type Notifier struct {
|
||||
notify.Notifier
|
||||
defaults []*database.Notify
|
||||
files map[string]*os.File
|
||||
formatter log.Formatter
|
||||
path string
|
||||
}
|
||||
|
||||
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
||||
logger.Info("startup")
|
||||
if config.FileDirectory == "" {
|
||||
if config.File.Directory == "" {
|
||||
return nil
|
||||
}
|
||||
logger.WithField("directory", config.File.Directory).Info("startup")
|
||||
|
||||
var defaults []*database.Notify
|
||||
if config.File.Default != "" {
|
||||
defaults = append(defaults, &database.Notify{
|
||||
Protocol: proto,
|
||||
To: config.File.Default,
|
||||
})
|
||||
}
|
||||
|
||||
return &Notifier{
|
||||
defaults: defaults,
|
||||
files: make(map[string]*os.File),
|
||||
formatter: &log.JSONFormatter{},
|
||||
path: config.FileDirectory,
|
||||
path: config.File.Directory,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) Default() []*database.Notify {
|
||||
return n.defaults
|
||||
}
|
||||
|
||||
func (n *Notifier) getFile(name string) *os.File {
|
||||
if file, ok := n.files[name]; ok {
|
||||
return file
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
var NotifyRegister []NotifyInit
|
||||
|
||||
type Notifier interface {
|
||||
Default() []*database.Notify
|
||||
Send(entry *log.Entry, to *database.Notify) bool
|
||||
Close()
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ var logger = log.WithField("notify", proto)
|
|||
|
||||
type Notifier struct {
|
||||
notify.Notifier
|
||||
defaults []*database.Notify
|
||||
ws *websocket.Server
|
||||
formatter log.Formatter
|
||||
}
|
||||
|
@ -53,15 +54,28 @@ func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifi
|
|||
}
|
||||
}()
|
||||
|
||||
logger.Info("startup")
|
||||
logger.WithField("http-socket", config.Websocket.Address).Info("startup")
|
||||
|
||||
var defaults []*database.Notify
|
||||
if config.Websocket.Default != "" {
|
||||
defaults = append(defaults, &database.Notify{
|
||||
Protocol: proto,
|
||||
To: config.Websocket.Default,
|
||||
})
|
||||
}
|
||||
return &Notifier{
|
||||
ws: ws,
|
||||
defaults: defaults,
|
||||
ws: ws,
|
||||
formatter: &log.TextFormatter{
|
||||
DisableTimestamp: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) Default() []*database.Notify {
|
||||
return n.defaults
|
||||
}
|
||||
|
||||
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
|
||||
if to.Protocol != proto {
|
||||
return false
|
||||
|
|
|
@ -24,6 +24,7 @@ var logger = log.WithField("notify", proto)
|
|||
|
||||
type Notifier struct {
|
||||
notify.Notifier
|
||||
defaults []*database.Notify
|
||||
client *xmpp_client.Client
|
||||
channels map[string]bool
|
||||
formatter log.Formatter
|
||||
|
@ -128,9 +129,23 @@ func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifi
|
|||
}
|
||||
}
|
||||
}
|
||||
logger.Info("startup")
|
||||
|
||||
logger.WithField("jid", config.XMPP.JID).Info("startup")
|
||||
|
||||
var defaults []*database.Notify
|
||||
for to, muc := range config.XMPP.Defaults {
|
||||
def := &database.Notify{
|
||||
Protocol: proto,
|
||||
To: to,
|
||||
}
|
||||
if muc {
|
||||
def.Protocol = protoGroup
|
||||
}
|
||||
defaults = append(defaults, def)
|
||||
}
|
||||
return &Notifier{
|
||||
channels: channels,
|
||||
defaults: defaults,
|
||||
client: client,
|
||||
formatter: &log.TextFormatter{
|
||||
DisableTimestamp: true,
|
||||
|
@ -138,6 +153,10 @@ func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifi
|
|||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) Default() []*database.Notify {
|
||||
return n.defaults
|
||||
}
|
||||
|
||||
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
|
||||
textByte, err := n.formatter.Format(e)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue