From 405d6e721a2383cd8bbffca97002e890008711df Mon Sep 17 00:00:00 2001 From: Yorick Terweijden Date: Mon, 1 Aug 2022 21:53:52 +0200 Subject: [PATCH] Implement Hook for Logrus to send messages to Pushover --- config/config.go | 2 + go.mod | 1 + go.sum | 2 + server/server.go | 6 +++ server/server_monitor.go | 3 ++ utils/logrus/hooks/pushover/pushover.go | 57 +++++++++++++++++++++++++ 6 files changed, 71 insertions(+) create mode 100644 utils/logrus/hooks/pushover/pushover.go diff --git a/config/config.go b/config/config.go index 27175f777..6b25910a5 100644 --- a/config/config.go +++ b/config/config.go @@ -207,6 +207,8 @@ type Config struct { SlackURL string `mapstructure:"alert-slack-url" toml:"alert-slack-url" json:"alertSlackUrl"` SlackChannel string `mapstructure:"alert-slack-channel" toml:"alert-slack-channel" json:"alertSlackChannel"` SlackUser string `mapstructure:"alert-slack-user" toml:"alert-slack-user" json:"alertSlackUser"` + PushoverAppToken string `mapstructure:"alert-pushover-app-token" toml:"alert-pushover-app-token" json:"alertPushoverAppToken"` + PushoverUserToken string `mapstructure:"alert-pushover-user-token" toml:"alert-pushover-user-token" json:"alertPushoverUserToken"` Heartbeat bool `mapstructure:"heartbeat-table" toml:"heartbeat-table" json:"heartbeatTable"` ExtProxyOn bool `mapstructure:"extproxy" toml:"extproxy" json:"extproxy"` ExtProxyVIP string `mapstructure:"extproxy-address" toml:"extproxy-address" json:"extproxyAddress"` diff --git a/go.mod b/go.mod index e02d0aa8b..c34470bbf 100644 --- a/go.mod +++ b/go.mod @@ -119,6 +119,7 @@ require ( github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/sessions v0.0.0-20180209192218-6ba88b7f1c1e // indirect + github.com/gregdel/pushover v1.1.0 // indirect github.com/imdario/mergo v0.3.11 // indirect github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494 // indirect diff --git a/go.sum b/go.sum index 6e1fcb328..d122ee5df 100644 --- a/go.sum +++ b/go.sum @@ -332,6 +332,8 @@ github.com/gorilla/sessions v0.0.0-20180209192218-6ba88b7f1c1e/go.mod h1:+WVp8kd github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gregdel/pushover v1.1.0 h1:dwHyvrcpZCOS9V1fAnKPaGRRI5OC55cVaKhMybqNsKQ= +github.com/gregdel/pushover v1.1.0/go.mod h1:EcaO66Nn1StkpEm1iKtBTV3d2A16SoMsVER1PthX7to= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw= diff --git a/server/server.go b/server/server.go index 16fe775c0..4f76c252c 100644 --- a/server/server.go +++ b/server/server.go @@ -41,6 +41,7 @@ import ( "github.com/signal18/replication-manager/regtest" "github.com/signal18/replication-manager/repmanv3" "github.com/signal18/replication-manager/utils/crypto" + "github.com/signal18/replication-manager/utils/logrus/hooks/pushover" "github.com/signal18/replication-manager/utils/misc" "github.com/signal18/replication-manager/utils/s18log" ) @@ -500,6 +501,11 @@ func (repman *ReplicationManager) Run() error { Timeout: 5 * time.Second, // request timeout for calling slack api }) } + if repman.Conf.PushoverAppToken != "" && repman.Conf.PushoverUserToken != "" { + log.AddHook( + pushover.NewHook(repman.Conf.PushoverAppToken, repman.Conf.PushoverUserToken), + ) + } if repman.Conf.LogLevel > 1 { log.SetLevel(log.DebugLevel) } diff --git a/server/server_monitor.go b/server/server_monitor.go index 586626897..aaf02199f 100644 --- a/server/server_monitor.go +++ b/server/server_monitor.go @@ -175,6 +175,9 @@ func init() { monitorCmd.Flags().StringVar(&conf.SlackChannel, "alert-slack-channel", "#support", "Slack channel to alert") monitorCmd.Flags().StringVar(&conf.SlackUser, "alert-slack-user", "", "Slack user for alert") + monitorCmd.Flags().StringVar(&conf.PushoverAppToken, "alert-pushover-app-token", "", "Pushover App Token for alerts") + monitorCmd.Flags().StringVar(&conf.PushoverUserToken, "alert-pushover-user-token", "", "Pushover User Token for alerts") + monitorCmd.Flags().BoolVar(&conf.RegistryConsul, "registry-consul", false, "Register write and read SRV DNS to consul") monitorCmd.Flags().StringVar(&conf.RegistryHosts, "registry-servers", "127.0.0.1", "Comma-separated list of registry addresses") diff --git a/utils/logrus/hooks/pushover/pushover.go b/utils/logrus/hooks/pushover/pushover.go new file mode 100644 index 000000000..795ec73f4 --- /dev/null +++ b/utils/logrus/hooks/pushover/pushover.go @@ -0,0 +1,57 @@ +package pushover + +import ( + "fmt" + + client "github.com/gregdel/pushover" + "github.com/sirupsen/logrus" +) + +type PushoverHook struct { + app *client.Pushover + recipient *client.Recipient + AcceptedLevels []logrus.Level +} + +/* + TODO: We need to define if we want to match specific Logrus levels + to specific Pushover priorities. They range from -2 to 2 +*/ + +var defaultLevels []logrus.Level = []logrus.Level{ + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel, +} + +func (p *PushoverHook) Levels() []logrus.Level { + if p.AcceptedLevels == nil { + return defaultLevels + } + + return p.AcceptedLevels +} + +// NewHook returns a Logrus.Hook for pushing messages to Pushover. +// Implements the gregdel/pushover package +func NewHook(appToken, recipientToken string) *PushoverHook { + p := &PushoverHook{} + p.app = client.New(appToken) + p.recipient = client.NewRecipient(recipientToken) + + return p +} + +func (p *PushoverHook) Fire(entry *logrus.Entry) error { + message := &client.Message{ + Message: entry.Message, + Timestamp: entry.Time.Unix(), + } + + _, err := p.app.SendMessage(message, p.recipient) + if err != nil { + return fmt.Errorf("Could not send message to Pushover API: %s", err) + } + + return nil +}