Skip to content

Commit

Permalink
Merge pull request #448 from terwey/issue_444
Browse files Browse the repository at this point in the history
Implement Hook for Logrus to send messages to Pushover
  • Loading branch information
svaroqui committed Aug 29, 2022
2 parents b893fc4 + 405d6e7 commit 5883f38
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions server/server_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
57 changes: 57 additions & 0 deletions utils/logrus/hooks/pushover/pushover.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 5883f38

Please sign in to comment.