Skip to content

Commit

Permalink
notifier: revamp target config
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Oct 23, 2023
1 parent c357341 commit da86920
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
5 changes: 2 additions & 3 deletions contrib/sample-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ notifier:
# pin: 1234
targets:
- name: hugo
backend: sms-bar
sms:
number: +1555123456789
sms: +1555123456789
email: [email protected]
29 changes: 25 additions & 4 deletions notifier/backend_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"context"
"fmt"
"log"
"sync"

"github.com/whawty/alerts/store"
)
Expand All @@ -44,26 +45,46 @@ type EMailBackend struct {
name string
conf *NotifierBackendConfigEMail
// TODO: add client config
mutex *sync.RWMutex
}

func NewEMailBackend(name string, conf *NotifierBackendConfigEMail, infoLog, dbgLog *log.Logger) *EMailBackend {
return &EMailBackend{name: name, conf: conf, infoLog: infoLog, dbgLog: dbgLog}
return &EMailBackend{name: name, conf: conf, infoLog: infoLog, dbgLog: dbgLog, mutex: &sync.RWMutex{}}
}

func (emb *EMailBackend) Init() (err error) {
emb.mutex.Lock()
defer emb.mutex.Unlock()

return fmt.Errorf("not yet implemented!")
}

func (smb *EMailBackend) Ready() bool {
func (emb *EMailBackend) ready() bool {
// TODO: implement this
return false
}

func (emb *EMailBackend) Notify(ctx context.Context, target NotifierTarget, alert *store.Alert) error {
return fmt.Errorf("not yet implemented!")
func (emb *EMailBackend) Ready() bool {
emb.mutex.RLock()
defer emb.mutex.RUnlock()

return emb.ready()
}

func (emb *EMailBackend) Notify(ctx context.Context, target NotifierTarget, alert *store.Alert) (bool, error) {
emb.mutex.RLock()
defer emb.mutex.RUnlock()

if target.EMail == nil || !emb.ready() {
return false, nil
}

return false, fmt.Errorf("not yet implemented!")
}

func (emb *EMailBackend) Close() error {
emb.mutex.Lock()
defer emb.mutex.Unlock()
// TODO: close client?
return nil
}
33 changes: 27 additions & 6 deletions notifier/backend_smsmodem.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"fmt"
"io"
"log"
"sync"
"time"

"github.com/warthog618/modem/at"
Expand All @@ -50,16 +51,20 @@ type SMSModemBackend struct {
conf *NotifierBackendConfigSMSModem
modem io.ReadWriteCloser
sms *gsm.GSM
mutex *sync.RWMutex
}

func NewSMSModemBackend(name string, conf *NotifierBackendConfigSMSModem, infoLog, dbgLog *log.Logger) *SMSModemBackend {
if conf.Timeout <= 0 {
conf.Timeout = 5 * time.Second
}
return &SMSModemBackend{name: name, conf: conf, infoLog: infoLog, dbgLog: dbgLog}
return &SMSModemBackend{name: name, conf: conf, infoLog: infoLog, dbgLog: dbgLog, mutex: &sync.RWMutex{}}
}

func (smb *SMSModemBackend) Init() (err error) {
smb.mutex.Lock()
defer smb.mutex.Unlock()

smb.modem, err = serial.New(serial.WithPort(smb.conf.Device), serial.WithBaud(smb.conf.Baudrate))
if err != nil {
smb.modem = nil
Expand Down Expand Up @@ -105,23 +110,39 @@ func (smb *SMSModemBackend) Init() (err error) {
return nil
}

func (smb *SMSModemBackend) Ready() bool {
func (smb *SMSModemBackend) ready() bool {
return smb.modem != nil && smb.sms != nil
}

func (smb *SMSModemBackend) Notify(ctx context.Context, target NotifierTarget, alert *store.Alert) error {
func (smb *SMSModemBackend) Ready() bool {
smb.mutex.RLock()
defer smb.mutex.RUnlock()
return smb.ready()
}

func (smb *SMSModemBackend) Notify(ctx context.Context, target NotifierTarget, alert *store.Alert) (bool, error) {
smb.mutex.RLock()
defer smb.mutex.RUnlock()

if target.SMS == nil || !smb.ready() {
return false, nil
}

// TODO: improve alert formatting
message := fmt.Sprintf("%v %s | %v %s | %s", alert.State.Emoji(), alert.State, alert.Severity.Emoji(), alert.Severity, alert.Name)

resp, err := smb.sms.SendLongMessage(target.SMS.Number, message)
resp, err := smb.sms.SendLongMessage(string(*target.SMS), message)
if err != nil {
return err
return false, err
}
smb.dbgLog.Printf("SMSModem(%s): send sms response: %v", smb.name, resp)
return nil
return true, nil
}

func (smb *SMSModemBackend) Close() error {
smb.mutex.Lock()
defer smb.mutex.Unlock()

smb.sms.StopMessageRx()
smb.modem.Close()
smb.modem = nil
Expand Down
19 changes: 9 additions & 10 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,15 @@ func NewNotifier(conf *Config, st *store.Store, infoLog, dbgLog *log.Logger) (n
a.Severity = store.SeverityCritical
a.Name = "This is just a drill!"
for _, t := range n.conf.Targets {
b := n.backends[t.Backend]
if b == nil {
infoLog.Printf("notifier: failed to notify '%+v': unknown backend '%s'", t, t.Backend)
continue
}

if err := b.Notify(context.TODO(), t, a); err != nil {
infoLog.Printf("notifier: failed to notify '%+v': %v", t, err)
} else {
infoLog.Printf("notifier: sent notification to '%+v'", t)
for bName, b := range n.backends {
sent, err := b.Notify(context.TODO(), t, a)
if err != nil {
infoLog.Printf("notifier: failed to notify '%s' via backend '%s': %v", t.Name, bName, err)
continue
}
if sent {
infoLog.Printf("notifier: sent notification to '%s' via backend '%s'", t.Name, bName)
}
}
}

Expand Down
20 changes: 6 additions & 14 deletions notifier/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,13 @@ type NotifierBackendConfig struct {
SMSModem *NotifierBackendConfigSMSModem `yaml:"smsModem"`
}

type NotifierTargetSMS struct {
Number string `yaml:"number"`
}

type NotifierTargetEMail struct {
TO []string `yaml:"to"`
CC []string `yaml:"cc"`
BCC []string `yaml:"bcc"`
}
type NotifierTargetSMS string
type NotifierTargetEMail string

type NotifierTarget struct {
Name string `yaml:"name"`
Backend string `yaml:"backend"`
EMail *NotifierTargetEMail `yaml:"email"`
SMS *NotifierTargetSMS `yaml:"sms"`
Name string `yaml:"name"`
EMail *NotifierTargetEMail `yaml:"email"`
SMS *NotifierTargetSMS `yaml:"sms"`
}

type Config struct {
Expand All @@ -84,6 +76,6 @@ type Config struct {
type NotifierBackend interface {
Init() error
Ready() bool
Notify(context.Context, NotifierTarget, *store.Alert) error
Notify(context.Context, NotifierTarget, *store.Alert) (bool, error)
Close() error
}

0 comments on commit da86920

Please sign in to comment.