2017-11-10 18:57:36 +01:00
|
|
|
package database
|
2017-08-10 20:11:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
|
2019-06-20 09:25:43 +02:00
|
|
|
"github.com/bdlm/log"
|
|
|
|
logstd "github.com/bdlm/std/logger"
|
2017-08-10 20:11:35 +02:00
|
|
|
)
|
|
|
|
|
2017-08-13 12:12:46 +02:00
|
|
|
const AlertMsg = "alert service from logmania, device did not send new message for a while"
|
|
|
|
|
2017-11-10 18:57:36 +01:00
|
|
|
type DB struct {
|
2018-05-18 14:04:54 +02:00
|
|
|
// depraced Format -> transformation to new format by db.update()
|
2018-04-15 01:52:08 +02:00
|
|
|
Hostname map[string]string `json:"hostname,omitempty"`
|
|
|
|
HostTo map[string]map[string]bool `json:"host_to,omitempty"`
|
2019-06-20 09:25:43 +02:00
|
|
|
MaxPrioIn map[string]logstd.Level `json:"maxLevel,omitempty"`
|
2018-04-15 01:52:08 +02:00
|
|
|
RegexIn map[string]map[string]*regexp.Regexp `json:"regexIn,omitempty"`
|
2017-08-13 09:51:13 +02:00
|
|
|
Lastseen map[string]time.Time `json:"lastseen,omitempty"`
|
2017-08-13 12:12:46 +02:00
|
|
|
LastseenNotify map[string]time.Time `json:"-"`
|
2018-04-15 01:52:08 +02:00
|
|
|
// new format
|
|
|
|
Hosts []*Host `json:"hosts"`
|
|
|
|
HostsByAddress map[string]*Host `json:"-"`
|
|
|
|
HostsByName map[string]*Host `json:"-"`
|
|
|
|
Notifies []*Notify `json:"notifies"`
|
|
|
|
NotifiesByAddress map[string]*Notify `json:"-"`
|
2018-09-04 22:11:20 +02:00
|
|
|
DefaultNotify []*Notify `json:"-"`
|
2017-08-10 20:11:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-15 01:52:08 +02:00
|
|
|
func (db *DB) SendTo(e *log.Entry) (*log.Entry, *Host, []*Notify) {
|
|
|
|
addr, ok := e.Data["hostname"].(string)
|
2017-10-25 00:36:16 +02:00
|
|
|
if !ok {
|
2018-04-15 01:52:08 +02:00
|
|
|
return e, nil, nil
|
2017-10-25 00:36:16 +02:00
|
|
|
}
|
2018-04-15 01:52:08 +02:00
|
|
|
var host *Host
|
|
|
|
if host, ok := db.HostsByAddress[addr]; ok {
|
|
|
|
if e.Message != AlertMsg {
|
|
|
|
host.Lastseen = time.Now()
|
2017-08-13 12:12:46 +02:00
|
|
|
}
|
2018-04-15 01:52:08 +02:00
|
|
|
var toList []*Notify
|
2018-09-04 22:11:20 +02:00
|
|
|
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
|
2018-04-15 01:52:08 +02:00
|
|
|
for _, notify := range host.NotifiesByAddress {
|
|
|
|
if lvl := notify.MaxPrioIn; e.Level >= lvl {
|
2017-08-10 20:11:35 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-04-15 01:52:08 +02:00
|
|
|
stopForTo := false
|
|
|
|
for _, expr := range notify.RegexIn {
|
|
|
|
if expr.MatchString(e.Message) {
|
|
|
|
stopForTo = true
|
2017-08-13 09:51:13 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2018-04-15 01:52:08 +02:00
|
|
|
if stopForTo {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
toList = append(toList, notify)
|
2017-08-10 20:11:35 +02:00
|
|
|
}
|
2018-09-04 22:11:20 +02:00
|
|
|
return entry, host, toList
|
2017-08-10 20:11:35 +02:00
|
|
|
}
|
2018-09-04 22:11:20 +02:00
|
|
|
host = db.NewHost(addr)
|
|
|
|
return e, host, db.DefaultNotify
|
2017-08-10 20:11:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 21:05:27 +02:00
|
|
|
func (db *DB) Alert(expired time.Duration, send func(e *log.Entry, n *Notify) bool) {
|
2017-08-13 12:12:46 +02:00
|
|
|
c := time.Tick(time.Minute)
|
|
|
|
|
|
|
|
for range c {
|
|
|
|
now := time.Now()
|
2018-04-15 01:52:08 +02:00
|
|
|
for _, h := range db.Hosts {
|
2018-09-05 01:53:23 +02:00
|
|
|
if h.Lastseen.Before(now.Add(expired * -1)) {
|
2018-04-15 01:52:08 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-09-05 01:53:23 +02:00
|
|
|
if h.Lastseen.After(h.LastseenNotify) {
|
2018-04-15 01:52:08 +02:00
|
|
|
continue
|
2017-08-13 12:12:46 +02:00
|
|
|
}
|
2018-04-15 01:52:08 +02:00
|
|
|
h.LastseenNotify = now
|
|
|
|
entry := log.NewEntry(log.New())
|
|
|
|
entry.Level = log.ErrorLevel
|
|
|
|
entry.Message = AlertMsg
|
|
|
|
entry.WithField("hostname", h.Address)
|
2018-04-26 21:05:27 +02:00
|
|
|
send(entry, nil)
|
2017-08-13 12:12:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|