diff --git a/database/main.go b/database/main.go index 47a7679..4bb49b6 100644 --- a/database/main.go +++ b/database/main.go @@ -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) { diff --git a/lib/config.go b/lib/config.go index 6e8872c..0f80b3a 100644 --- a/lib/config.go +++ b/lib/config.go @@ -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 { diff --git a/logmania_example.conf b/logmania_example.conf index 23ee178..9ceed3b 100644 --- a/logmania_example.conf +++ b/logmania_example.conf @@ -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" diff --git a/notify/all/internal.go b/notify/all/internal.go index 0ecc0f3..ddb29be 100644 --- a/notify/all/internal.go +++ b/notify/all/internal.go @@ -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) } } } diff --git a/notify/file/main.go b/notify/file/main.go index 20c0c2d..0868f31 100644 --- a/notify/file/main.go +++ b/notify/file/main.go @@ -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 diff --git a/notify/main.go b/notify/main.go index 79cd368..9013071 100644 --- a/notify/main.go +++ b/notify/main.go @@ -11,6 +11,7 @@ import ( var NotifyRegister []NotifyInit type Notifier interface { + Default() []*database.Notify Send(entry *log.Entry, to *database.Notify) bool Close() } diff --git a/notify/websocket/main.go b/notify/websocket/main.go index 6d917d1..7a75bca 100644 --- a/notify/websocket/main.go +++ b/notify/websocket/main.go @@ -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 diff --git a/notify/xmpp/main.go b/notify/xmpp/main.go index f473d3c..eaed47d 100644 --- a/notify/xmpp/main.go +++ b/notify/xmpp/main.go @@ -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 {