Skip to content

Commit

Permalink
some sms sending tests
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Oct 23, 2023
1 parent 20a6eed commit c357341
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
7 changes: 6 additions & 1 deletion contrib/sample-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ notifier:
device: /dev/ttyUSB0
baudrate: 115200
timeout: 10s
pin: 1234
# pin: 1234
targets:
- name: hugo
backend: sms-bar
sms:
number: +1555123456789
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/enescakir/emoji v1.0.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog=
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
5 changes: 3 additions & 2 deletions notifier/backend_smsmodem.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func (smb *SMSModemBackend) Init() (err error) {

a := at.New(smb.modem, at.WithTimeout(smb.conf.Timeout))
if smb.conf.Pin != nil {
smb.dbgLog.Printf("SMSModem(%s): trying to enter PIN code: %d", smb.name, *smb.conf.Pin)
var resp []string
resp, err = a.Command(fmt.Sprintf("+CPIN=%d", smb.conf.Pin))
resp, err = a.Command(fmt.Sprintf("+CPIN=%d", *smb.conf.Pin))
if err != nil {
smb.modem.Close()
smb.modem = nil
Expand Down Expand Up @@ -110,7 +111,7 @@ func (smb *SMSModemBackend) Ready() bool {

func (smb *SMSModemBackend) Notify(ctx context.Context, target NotifierTarget, alert *store.Alert) error {
// TODO: improve alert formatting
message := fmt.Sprintf("%s / %s / %s", alert.State, alert.Severity, alert.Name)
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)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ func NewNotifier(conf *Config, st *store.Store, infoLog, dbgLog *log.Logger) (n
// TODO: start go-routine to re-initialize failed backends
// TODO: start go-routine to handle notfications

a := &store.Alert{}
a.State = store.StateClosed
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)
}
}

infoLog.Printf("notifier: started with %d backends and evaluation interval %s", len(n.backends), conf.Interval.String())
return
}
30 changes: 30 additions & 0 deletions store/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"errors"
"fmt"
"time"

"github.com/enescakir/emoji"
)

// Configuration
Expand Down Expand Up @@ -104,6 +106,22 @@ func (s *AlertState) FromString(str string) error {
return nil
}

func (s AlertState) Emoji() emoji.Emoji {
switch s {
case StateNew:
return emoji.GlowingStar
case StateOpen:
return emoji.Bell
case StateAcknowledged:
return emoji.BellWithSlash
case StateStale:
return emoji.QuestionMark
case StateClosed:
return emoji.CheckMarkButton
}
return emoji.WhiteQuestionMark
}

func (s AlertState) MarshalText() (data []byte, err error) {
data = []byte(s.String())
return
Expand Down Expand Up @@ -147,6 +165,18 @@ func (s *AlertSeverity) FromString(str string) error {
return nil
}

func (s AlertSeverity) Emoji() emoji.Emoji {
switch s {
case SeverityCritical:
return emoji.DoubleExclamationMark
case SeverityWarning:
return emoji.Warning
case SeverityInformational:
return emoji.Information
}
return emoji.WhiteQuestionMark
}

func (s AlertSeverity) MarshalText() (data []byte, err error) {
data = []byte(s.String())
return
Expand Down

0 comments on commit c357341

Please sign in to comment.