Skip to content

Commit 5883f38

Browse files
authored
Merge pull request #448 from terwey/issue_444
Implement Hook for Logrus to send messages to Pushover
2 parents b893fc4 + 405d6e7 commit 5883f38

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ type Config struct {
207207
SlackURL string `mapstructure:"alert-slack-url" toml:"alert-slack-url" json:"alertSlackUrl"`
208208
SlackChannel string `mapstructure:"alert-slack-channel" toml:"alert-slack-channel" json:"alertSlackChannel"`
209209
SlackUser string `mapstructure:"alert-slack-user" toml:"alert-slack-user" json:"alertSlackUser"`
210+
PushoverAppToken string `mapstructure:"alert-pushover-app-token" toml:"alert-pushover-app-token" json:"alertPushoverAppToken"`
211+
PushoverUserToken string `mapstructure:"alert-pushover-user-token" toml:"alert-pushover-user-token" json:"alertPushoverUserToken"`
210212
Heartbeat bool `mapstructure:"heartbeat-table" toml:"heartbeat-table" json:"heartbeatTable"`
211213
ExtProxyOn bool `mapstructure:"extproxy" toml:"extproxy" json:"extproxy"`
212214
ExtProxyVIP string `mapstructure:"extproxy-address" toml:"extproxy-address" json:"extproxyAddress"`

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ require (
119119
github.com/gorilla/context v1.1.1 // indirect
120120
github.com/gorilla/securecookie v1.1.1 // indirect
121121
github.com/gorilla/sessions v0.0.0-20180209192218-6ba88b7f1c1e // indirect
122+
github.com/gregdel/pushover v1.1.0 // indirect
122123
github.com/imdario/mergo v0.3.11 // indirect
123124
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
124125
github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ github.com/gorilla/sessions v0.0.0-20180209192218-6ba88b7f1c1e/go.mod h1:+WVp8kd
332332
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
333333
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
334334
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
335+
github.com/gregdel/pushover v1.1.0 h1:dwHyvrcpZCOS9V1fAnKPaGRRI5OC55cVaKhMybqNsKQ=
336+
github.com/gregdel/pushover v1.1.0/go.mod h1:EcaO66Nn1StkpEm1iKtBTV3d2A16SoMsVER1PthX7to=
335337
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
336338
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
337339
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=

server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/signal18/replication-manager/regtest"
4242
"github.com/signal18/replication-manager/repmanv3"
4343
"github.com/signal18/replication-manager/utils/crypto"
44+
"github.com/signal18/replication-manager/utils/logrus/hooks/pushover"
4445
"github.com/signal18/replication-manager/utils/misc"
4546
"github.com/signal18/replication-manager/utils/s18log"
4647
)
@@ -500,6 +501,11 @@ func (repman *ReplicationManager) Run() error {
500501
Timeout: 5 * time.Second, // request timeout for calling slack api
501502
})
502503
}
504+
if repman.Conf.PushoverAppToken != "" && repman.Conf.PushoverUserToken != "" {
505+
log.AddHook(
506+
pushover.NewHook(repman.Conf.PushoverAppToken, repman.Conf.PushoverUserToken),
507+
)
508+
}
503509
if repman.Conf.LogLevel > 1 {
504510
log.SetLevel(log.DebugLevel)
505511
}

server/server_monitor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ func init() {
175175
monitorCmd.Flags().StringVar(&conf.SlackChannel, "alert-slack-channel", "#support", "Slack channel to alert")
176176
monitorCmd.Flags().StringVar(&conf.SlackUser, "alert-slack-user", "", "Slack user for alert")
177177

178+
monitorCmd.Flags().StringVar(&conf.PushoverAppToken, "alert-pushover-app-token", "", "Pushover App Token for alerts")
179+
monitorCmd.Flags().StringVar(&conf.PushoverUserToken, "alert-pushover-user-token", "", "Pushover User Token for alerts")
180+
178181
monitorCmd.Flags().BoolVar(&conf.RegistryConsul, "registry-consul", false, "Register write and read SRV DNS to consul")
179182
monitorCmd.Flags().StringVar(&conf.RegistryHosts, "registry-servers", "127.0.0.1", "Comma-separated list of registry addresses")
180183

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package pushover
2+
3+
import (
4+
"fmt"
5+
6+
client "github.com/gregdel/pushover"
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
type PushoverHook struct {
11+
app *client.Pushover
12+
recipient *client.Recipient
13+
AcceptedLevels []logrus.Level
14+
}
15+
16+
/*
17+
TODO: We need to define if we want to match specific Logrus levels
18+
to specific Pushover priorities. They range from -2 to 2
19+
*/
20+
21+
var defaultLevels []logrus.Level = []logrus.Level{
22+
logrus.PanicLevel,
23+
logrus.FatalLevel,
24+
logrus.ErrorLevel,
25+
}
26+
27+
func (p *PushoverHook) Levels() []logrus.Level {
28+
if p.AcceptedLevels == nil {
29+
return defaultLevels
30+
}
31+
32+
return p.AcceptedLevels
33+
}
34+
35+
// NewHook returns a Logrus.Hook for pushing messages to Pushover.
36+
// Implements the gregdel/pushover package
37+
func NewHook(appToken, recipientToken string) *PushoverHook {
38+
p := &PushoverHook{}
39+
p.app = client.New(appToken)
40+
p.recipient = client.NewRecipient(recipientToken)
41+
42+
return p
43+
}
44+
45+
func (p *PushoverHook) Fire(entry *logrus.Entry) error {
46+
message := &client.Message{
47+
Message: entry.Message,
48+
Timestamp: entry.Time.Unix(),
49+
}
50+
51+
_, err := p.app.SendMessage(message, p.recipient)
52+
if err != nil {
53+
return fmt.Errorf("Could not send message to Pushover API: %s", err)
54+
}
55+
56+
return nil
57+
}

0 commit comments

Comments
 (0)