add notify to different files

This commit is contained in:
Martin/Geno 2018-05-18 14:04:54 +02:00
parent 64dbedf0c8
commit ce46bad745
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
9 changed files with 96 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import (
const AlertMsg = "alert service from logmania, device did not send new message for a while" const AlertMsg = "alert service from logmania, device did not send new message for a while"
type DB struct { type DB struct {
// depraced Format -> transformation to new format by db.update()
Hostname map[string]string `json:"hostname,omitempty"` Hostname map[string]string `json:"hostname,omitempty"`
HostTo map[string]map[string]bool `json:"host_to,omitempty"` HostTo map[string]map[string]bool `json:"host_to,omitempty"`
MaxPrioIn map[string]log.Level `json:"maxLevel,omitempty"` MaxPrioIn map[string]log.Level `json:"maxLevel,omitempty"`

View File

@ -9,12 +9,12 @@ func ReadDBFile(path string) *DB {
var db DB var db DB
if err := file.ReadJSON(path, &db); err == nil { if err := file.ReadJSON(path, &db); err == nil {
log.Infof("loaded %d hosts", len(db.HostTo))
db.InitNotify() db.InitNotify()
db.InitHost() db.InitHost()
// import // import
db.update() db.update()
log.Infof("loaded %d hosts and %d notifies", len(db.Hosts), len(db.Notifies))
return &db return &db
} else { } else {
log.Error("failed to open db file: ", path, ":", err) log.Error("failed to open db file: ", path, ":", err)

View File

@ -19,6 +19,7 @@ type NotifyConfig struct {
Address string `toml:"address"` Address string `toml:"address"`
Webroot string `toml:"webroot"` Webroot string `toml:"webroot"`
} `toml:"websocket"` } `toml:"websocket"`
FileDirectory string `toml:"file_directory"`
} }
type ReceiveConfig struct { type ReceiveConfig struct {

View File

@ -10,6 +10,8 @@ address = ":10002"
state_file = "/tmp/logmania.state.json" state_file = "/tmp/logmania.state.json"
debug = true debug = true
file_directory = "/tmp/"
[notify.xmpp] [notify.xmpp]
jid = "user@example.org" jid = "user@example.org"
password = "password" password = "password"

View File

@ -50,7 +50,7 @@ func (n *Notifier) sender() {
} }
} }
if !send { if !send {
logger.Warn("notify not send to anybody: [%s] %s", c.Level.String(), c.Message) logger.Warnf("notify not send to anybody: [%s] %s", c.Level.String(), c.Message)
} }
} }
} }

View File

@ -1,6 +1,7 @@
package all package all
import ( import (
_ "dev.sum7.eu/genofire/logmania/notify/file"
_ "dev.sum7.eu/genofire/logmania/notify/websocket" _ "dev.sum7.eu/genofire/logmania/notify/websocket"
_ "dev.sum7.eu/genofire/logmania/notify/xmpp" _ "dev.sum7.eu/genofire/logmania/notify/xmpp"
) )

79
notify/file/main.go Normal file
View File

@ -0,0 +1,79 @@
package file
import (
"os"
"path"
"regexp"
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/logmania/bot"
"dev.sum7.eu/genofire/logmania/database"
"dev.sum7.eu/genofire/logmania/lib"
"dev.sum7.eu/genofire/logmania/notify"
)
const (
proto = "file"
)
var logger = log.WithField("notify", proto)
type Notifier struct {
notify.Notifier
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 == "" {
return nil
}
return &Notifier{
files: make(map[string]*os.File),
formatter: &log.JSONFormatter{},
path: config.FileDirectory,
}
}
func (n *Notifier) getFile(name string) *os.File {
if file, ok := n.files[name]; ok {
return file
}
if m, err := regexp.MatchString(`^[0-9A-Za-z_-]*$`, name); err != nil || !m {
logger.Errorf("not allowed to use '%s:%s'", proto, name)
return nil
}
filename := path.Join(n.path, name+".json")
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
logger.Errorf("could not open file: %s", err.Error())
return nil
}
n.files[name] = file
return file
}
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
if to.Protocol != proto {
return false
}
byteText, err := n.formatter.Format(e)
if err != nil {
return false
}
text := to.RunReplace(string(byteText))
file := n.getFile(to.To)
if file == nil {
return false
}
_, err = file.WriteString(text)
return err == nil
}
func init() {
notify.AddNotifier(Init)
}

View File

@ -66,9 +66,17 @@ func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
if to.Protocol != proto { if to.Protocol != proto {
return false return false
} }
n.ws.SendAll(&websocket.Message{ n.ws.SendAll(&websocket.Message{
Subject: to.Address(), Subject: to.Address(),
Body: e, Body: &log.Entry{
Buffer: e.Buffer,
Data: e.Data,
Level: e.Level,
Logger: e.Logger,
Message: to.RunReplace(e.Message),
Time: e.Time,
},
}) })
return true return true
} }

View File

@ -11,7 +11,7 @@ function levelToColor (lvl) {
function addItem (el, msg) { function addItem (el, msg) {
const div = domlib.newAt(el, 'div', { const div = domlib.newAt(el, 'div', {
'class': levelToColor(msg.Leve) 'class': levelToColor(msg.Level)
}); });
domlib.newAt(div, 'span', null, msg.Data.hostname); domlib.newAt(div, 'span', null, msg.Data.hostname);
domlib.newAt(div, 'span', null, msg.Message); domlib.newAt(div, 'span', null, msg.Message);
@ -24,13 +24,6 @@ class LogView extends View {
if (!this.init) { if (!this.init) {
this.init = true; this.init = true;
} }
/*
* Domlib.newAt(this.el, 'h2', {'class': 'ui header'}, 'Log');
* for (const msg in store.channel.ffhb) {
* domlib.newAt(this.el, 'div', null, msg.Data.hostname, msg);
*}
*/
} }