logmania/database/main.go

128 lines
3.2 KiB
Go
Raw Normal View History

package database
import (
"regexp"
"time"
"dev.sum7.eu/genofire/golang-lib/file"
2017-10-25 00:36:16 +02:00
log "github.com/sirupsen/logrus"
)
2017-08-13 12:12:46 +02:00
const AlertMsg = "alert service from logmania, device did not send new message for a while"
type DB struct {
Hostname map[string]string `json:"hostname"`
HostTo map[string]map[string]bool `json:"host_to"`
2017-10-25 00:36:16 +02:00
MaxPrioIn map[string]log.Level `json:"maxLevel"`
RegexIn map[string]map[string]*regexp.Regexp `json:"regexIn"`
Lastseen map[string]time.Time `json:"lastseen,omitempty"`
2017-08-13 12:12:46 +02:00
LastseenNotify map[string]time.Time `json:"-"`
}
func (db *DB) SendTo(e *log.Entry) (*log.Entry, []string) {
2017-10-25 00:36:16 +02:00
hostname, ok := e.Data["hostname"].(string)
if !ok {
2017-11-05 17:23:10 +01:00
return e, nil
2017-10-25 00:36:16 +02:00
}
if to, ok := db.HostTo[hostname]; ok {
2017-10-25 00:36:16 +02:00
if e.Message != AlertMsg && hostname != "" {
db.Lastseen[hostname] = time.Now()
2017-08-13 12:12:46 +02:00
}
var toList []string
2017-08-11 19:59:19 +02:00
for toEntry, _ := range to {
if lvl := db.MaxPrioIn[toEntry]; e.Level >= lvl {
continue
}
if regex, ok := db.RegexIn[toEntry]; ok {
stopForTo := false
for _, expr := range regex {
2017-10-25 00:36:16 +02:00
if expr.MatchString(e.Message) {
stopForTo = true
continue
}
}
if stopForTo {
continue
}
}
toList = append(toList, toEntry)
}
if replaceHostname, ok := db.Hostname[hostname]; ok {
2017-11-05 17:23:10 +01:00
entry := e.WithField("hostname", replaceHostname)
entry.Level = e.Level
entry.Message = e.Message
return entry, toList
2017-08-11 19:59:19 +02:00
}
2017-11-05 17:23:10 +01:00
return e, toList
2017-08-11 19:59:19 +02:00
} else {
db.HostTo[hostname] = make(map[string]bool)
}
2017-11-05 17:23:10 +01:00
return e, nil
}
func (db *DB) AddRegex(to, expression string) error {
regex, err := regexp.Compile(expression)
if err == nil {
if _, ok := db.RegexIn[to]; !ok {
db.RegexIn[to] = make(map[string]*regexp.Regexp)
}
db.RegexIn[to][expression] = regex
return nil
}
return err
}
func ReadDBFile(path string) *DB {
var db DB
2017-10-27 13:07:12 +02:00
if err := file.ReadJSON(path, &db); err == nil {
log.Infof("loaded %d hosts", len(db.HostTo))
if db.Lastseen == nil {
db.Lastseen = make(map[string]time.Time)
2017-10-27 13:07:12 +02:00
}
if db.LastseenNotify == nil {
db.LastseenNotify = make(map[string]time.Time)
2017-10-27 13:07:12 +02:00
}
if db.RegexIn == nil {
db.RegexIn = make(map[string]map[string]*regexp.Regexp)
2017-10-27 13:07:12 +02:00
} else {
for to, regexs := range db.RegexIn {
2017-10-27 13:07:12 +02:00
for exp, _ := range regexs {
db.AddRegex(to, exp)
}
}
}
return &db
} else {
log.Error("failed to open db file: ", path, ":", err)
}
return &DB{
Hostname: make(map[string]string),
HostTo: make(map[string]map[string]bool),
2017-10-25 00:36:16 +02:00
MaxPrioIn: make(map[string]log.Level),
RegexIn: make(map[string]map[string]*regexp.Regexp),
Lastseen: make(map[string]time.Time),
LastseenNotify: make(map[string]time.Time),
}
}
func (db *DB) Alert(expired time.Duration, send func(e *log.Entry) error) {
2017-08-13 12:12:46 +02:00
c := time.Tick(time.Minute)
for range c {
now := time.Now()
for host, time := range db.Lastseen {
2017-08-13 12:12:46 +02:00
if time.Before(now.Add(expired * -2)) {
if timeNotify, ok := db.LastseenNotify[host]; !ok || !time.Before(timeNotify) {
db.LastseenNotify[host] = now
2017-10-25 00:36:16 +02:00
entry := log.NewEntry(log.New())
entry.Level = log.ErrorLevel
entry.Message = AlertMsg
entry.WithField("hostname", host)
send(entry)
2017-08-13 12:12:46 +02:00
}
}
}
}
}