[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:"-"`
|
HostsByName map[string]*Host `json:"-"`
|
||||||
Notifies []*Notify `json:"notifies"`
|
Notifies []*Notify `json:"notifies"`
|
||||||
NotifiesByAddress map[string]*Notify `json:"-"`
|
NotifiesByAddress map[string]*Notify `json:"-"`
|
||||||
|
DefaultNotify []*Notify `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) SendTo(e *log.Entry) (*log.Entry, *Host, []*Notify) {
|
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()
|
host.Lastseen = time.Now()
|
||||||
}
|
}
|
||||||
var toList []*Notify
|
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 {
|
for _, notify := range host.NotifiesByAddress {
|
||||||
if lvl := notify.MaxPrioIn; e.Level >= lvl {
|
if lvl := notify.MaxPrioIn; e.Level >= lvl {
|
||||||
continue
|
continue
|
||||||
|
@ -52,17 +64,10 @@ func (db *DB) SendTo(e *log.Entry) (*log.Entry, *Host, []*Notify) {
|
||||||
}
|
}
|
||||||
toList = append(toList, 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 entry, host, toList
|
||||||
}
|
}
|
||||||
return e, host, toList
|
|
||||||
} else {
|
|
||||||
host = db.NewHost(addr)
|
host = db.NewHost(addr)
|
||||||
}
|
return e, host, db.DefaultNotify
|
||||||
return e, host, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Alert(expired time.Duration, send func(e *log.Entry, n *Notify) bool) {
|
func (db *DB) Alert(expired time.Duration, send func(e *log.Entry, n *Notify) bool) {
|
||||||
|
|
|
@ -14,12 +14,17 @@ type NotifyConfig struct {
|
||||||
XMPP struct {
|
XMPP struct {
|
||||||
JID string `toml:"jid"`
|
JID string `toml:"jid"`
|
||||||
Password string `toml:"password"`
|
Password string `toml:"password"`
|
||||||
|
Defaults map[string]bool `toml:"default"`
|
||||||
} `toml:"xmpp"`
|
} `toml:"xmpp"`
|
||||||
Websocket struct {
|
Websocket struct {
|
||||||
Address string `toml:"address"`
|
Address string `toml:"address"`
|
||||||
Webroot string `toml:"webroot"`
|
Webroot string `toml:"webroot"`
|
||||||
|
Default string `toml:"default"`
|
||||||
} `toml:"websocket"`
|
} `toml:"websocket"`
|
||||||
FileDirectory string `toml:"file_directory"`
|
File struct {
|
||||||
|
Directory string `toml:"directory"`
|
||||||
|
Default string `toml:"default"`
|
||||||
|
} `toml:"file"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReceiveConfig struct {
|
type ReceiveConfig struct {
|
||||||
|
|
|
@ -10,12 +10,17 @@ address = ":10002"
|
||||||
state_file = "/tmp/logmania.state.json"
|
state_file = "/tmp/logmania.state.json"
|
||||||
debug = true
|
debug = true
|
||||||
|
|
||||||
file_directory = "/tmp/"
|
[file]
|
||||||
|
directory = "/tmp/"
|
||||||
|
default = "raw"
|
||||||
|
|
||||||
[notify.xmpp]
|
[notify.xmpp]
|
||||||
jid = "user@example.org"
|
jid = "user@example.org"
|
||||||
password = "password"
|
password = "password"
|
||||||
|
# if boolean is true for muc either user chat
|
||||||
|
default = { "log-raw@conference.example.org" = true, "person@example.org" = false }
|
||||||
|
|
||||||
[notify.websocket]
|
[notify.websocket]
|
||||||
address = ":8080"
|
address = ":8080"
|
||||||
webroot = "./webroot/"
|
webroot = "./webroot/"
|
||||||
|
default = "raw"
|
||||||
|
|
|
@ -20,6 +20,7 @@ type Notifier struct {
|
||||||
|
|
||||||
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
||||||
var list []notify.Notifier
|
var list []notify.Notifier
|
||||||
|
var defaults []*database.Notify
|
||||||
for _, init := range notify.NotifyRegister {
|
for _, init := range notify.NotifyRegister {
|
||||||
notify := init(config, db, bot)
|
notify := init(config, db, bot)
|
||||||
|
|
||||||
|
@ -27,7 +28,14 @@ func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifi
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
list = append(list, notify)
|
list = append(list, notify)
|
||||||
|
def := notify.Default()
|
||||||
|
if def != nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
defaults = append(defaults, def...)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.DefaultNotify = defaults
|
||||||
|
|
||||||
n := &Notifier{
|
n := &Notifier{
|
||||||
db: db,
|
db: db,
|
||||||
|
@ -50,7 +58,7 @@ func (n *Notifier) sender() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !send {
|
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 {
|
type Notifier struct {
|
||||||
notify.Notifier
|
notify.Notifier
|
||||||
|
defaults []*database.Notify
|
||||||
files map[string]*os.File
|
files map[string]*os.File
|
||||||
formatter log.Formatter
|
formatter log.Formatter
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
|
||||||
logger.Info("startup")
|
if config.File.Directory == "" {
|
||||||
if config.FileDirectory == "" {
|
|
||||||
return nil
|
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{
|
return &Notifier{
|
||||||
|
defaults: defaults,
|
||||||
files: make(map[string]*os.File),
|
files: make(map[string]*os.File),
|
||||||
formatter: &log.JSONFormatter{},
|
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 {
|
func (n *Notifier) getFile(name string) *os.File {
|
||||||
if file, ok := n.files[name]; ok {
|
if file, ok := n.files[name]; ok {
|
||||||
return file
|
return file
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
var NotifyRegister []NotifyInit
|
var NotifyRegister []NotifyInit
|
||||||
|
|
||||||
type Notifier interface {
|
type Notifier interface {
|
||||||
|
Default() []*database.Notify
|
||||||
Send(entry *log.Entry, to *database.Notify) bool
|
Send(entry *log.Entry, to *database.Notify) bool
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ var logger = log.WithField("notify", proto)
|
||||||
|
|
||||||
type Notifier struct {
|
type Notifier struct {
|
||||||
notify.Notifier
|
notify.Notifier
|
||||||
|
defaults []*database.Notify
|
||||||
ws *websocket.Server
|
ws *websocket.Server
|
||||||
formatter log.Formatter
|
formatter log.Formatter
|
||||||
}
|
}
|
||||||
|
@ -53,8 +54,17 @@ 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{
|
return &Notifier{
|
||||||
|
defaults: defaults,
|
||||||
ws: ws,
|
ws: ws,
|
||||||
formatter: &log.TextFormatter{
|
formatter: &log.TextFormatter{
|
||||||
DisableTimestamp: true,
|
DisableTimestamp: true,
|
||||||
|
@ -62,6 +72,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 {
|
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
|
||||||
if to.Protocol != proto {
|
if to.Protocol != proto {
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -24,6 +24,7 @@ var logger = log.WithField("notify", proto)
|
||||||
|
|
||||||
type Notifier struct {
|
type Notifier struct {
|
||||||
notify.Notifier
|
notify.Notifier
|
||||||
|
defaults []*database.Notify
|
||||||
client *xmpp_client.Client
|
client *xmpp_client.Client
|
||||||
channels map[string]bool
|
channels map[string]bool
|
||||||
formatter log.Formatter
|
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{
|
return &Notifier{
|
||||||
channels: channels,
|
channels: channels,
|
||||||
|
defaults: defaults,
|
||||||
client: client,
|
client: client,
|
||||||
formatter: &log.TextFormatter{
|
formatter: &log.TextFormatter{
|
||||||
DisableTimestamp: true,
|
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 {
|
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
|
||||||
textByte, err := n.formatter.Format(e)
|
textByte, err := n.formatter.Format(e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue