Skip to content

Commit cb390b5

Browse files
committed
add pushover priority and cluster title
1 parent 10d0990 commit cb390b5

File tree

8 files changed

+61
-10
lines changed

8 files changed

+61
-10
lines changed

cluster/cluster.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type Cluster struct {
9797
DBIndexSize int64 `json:"dbIndexSize"`
9898
Connections int `json:"connections"`
9999
QPS int64 `json:"qps"`
100-
LogVault *log.Logger `json:"-"`
100+
LogPushover *log.Logger `json:"-"`
101101
Log s18log.HttpLog `json:"log"`
102102
LogSlack *log.Logger `json:"-"`
103103
JobResults map[string]*JobResult `json:"jobResults"`
@@ -311,13 +311,13 @@ func (cluster *Cluster) Init(conf config.Config, cfgGroup string, tlog *s18log.T
311311
os.MkdirAll(cluster.Conf.WorkingDir+"/"+cluster.Name, os.ModePerm)
312312
}
313313

314-
cluster.LogVault = log.New()
314+
cluster.LogPushover = log.New()
315315

316316
if cluster.Conf.PushoverAppToken != "" && cluster.Conf.PushoverUserToken != "" {
317-
cluster.LogVault.AddHook(
317+
cluster.LogPushover.AddHook(
318318
pushover.NewHook(cluster.Conf.PushoverAppToken, cluster.Conf.PushoverUserToken),
319319
)
320-
cluster.LogVault.SetLevel(log.WarnLevel)
320+
cluster.LogPushover.SetLevel(log.WarnLevel)
321321
}
322322

323323
cluster.LogSlack = log.New()

cluster/cluster_log.go

+40-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"io"
1616
"time"
1717

18+
teams "github.com/dasrick/go-teams-notify/v2"
1819
"github.com/nsf/termbox-go"
1920
"github.com/signal18/replication-manager/utils/s18log"
2021
log "github.com/sirupsen/logrus"
@@ -174,7 +175,9 @@ func (cluster *Cluster) LogPrintf(level string, format string, args ...interface
174175
if cluster.Conf.SlackURL != "" {
175176
cluster.LogSlack.WithFields(log.Fields{"cluster": cluster.Name, "type": "alert"}).Errorf(cliformat, args...)
176177
}
177-
178+
if cluster.Conf.TeamsUrl != "" {
179+
go cluster.sendMsTeams(level, format, args)
180+
}
178181
case "INFO":
179182
log.WithField("cluster", cluster.Name).Infof(cliformat, args...)
180183
case "DEBUG":
@@ -184,6 +187,9 @@ func (cluster *Cluster) LogPrintf(level string, format string, args ...interface
184187
if cluster.Conf.SlackURL != "" {
185188
cluster.LogSlack.WithFields(log.Fields{"cluster": cluster.Name, "type": "alert"}).Errorf(cliformat, args...)
186189
}
190+
if cluster.Conf.TeamsUrl != "" {
191+
go cluster.sendMsTeams(level, format, args)
192+
}
187193
case "TEST":
188194
log.WithFields(log.Fields{"cluster": cluster.Name, "type": "test"}).Infof(cliformat, args...)
189195
case "BENCH":
@@ -194,8 +200,10 @@ func (cluster *Cluster) LogPrintf(level string, format string, args ...interface
194200
cluster.LogSlack.WithFields(log.Fields{"cluster": cluster.Name, "type": "alert"}).Errorf(cliformat, args...)
195201
}
196202
if cluster.Conf.PushoverAppToken != "" && cluster.Conf.PushoverUserToken != "" {
197-
cluster.LogVault.WithFields(log.Fields{"cluster": cluster.Name, "type": "alert"}).Errorf(cliformat, args...)
198-
203+
cluster.LogPushover.WithFields(log.Fields{"cluster": cluster.Name, "type": "alert"}).Errorf(cliformat, args...)
204+
}
205+
if cluster.Conf.TeamsUrl != "" {
206+
go cluster.sendMsTeams(level, format, args)
199207
}
200208
case "STATE":
201209
status := cliformat[0:6]
@@ -213,3 +221,32 @@ func (cluster *Cluster) LogPrintf(level string, format string, args ...interface
213221
}
214222
return line
215223
}
224+
225+
func (cluster *Cluster) sendMsTeams(level string, format string, args ...interface{}) error {
226+
log.Printf("COUCOU sendMsTeams")
227+
// init the client
228+
mstClient := teams.NewClient()
229+
230+
// setup webhook url
231+
webhookUrl := cluster.Conf.TeamsUrl
232+
233+
// setup message card
234+
msgCard := teams.NewMessageCard()
235+
msgCard.Title = "Replication-Manager alert"
236+
switch level {
237+
case "ERROR":
238+
msgCard.ThemeColor = "#4169e1"
239+
case "ALERT":
240+
msgCard.ThemeColor = "#b22222"
241+
case "WARN":
242+
msgCard.ThemeColor = "#112233"
243+
}
244+
245+
msgCard.Text = fmt.Sprintf(format, args...)
246+
// send
247+
err := mstClient.Send(webhookUrl, msgCard)
248+
if err != nil {
249+
log.Printf("sendMSTeams error send alert : %s", err)
250+
}
251+
return nil
252+
}

config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ type Config struct {
211211
SlackUser string `mapstructure:"alert-slack-user" toml:"alert-slack-user" json:"alertSlackUser"`
212212
PushoverAppToken string `mapstructure:"alert-pushover-app-token" toml:"alert-pushover-app-token" json:"alertPushoverAppToken"`
213213
PushoverUserToken string `mapstructure:"alert-pushover-user-token" toml:"alert-pushover-user-token" json:"alertPushoverUserToken"`
214+
TeamsUrl string `mapstructure:"alert-teams-url" toml:"alert-teams-url" json:"alertTeamsUrl"`
214215
Heartbeat bool `mapstructure:"heartbeat-table" toml:"heartbeat-table" json:"heartbeatTable"`
215216
ExtProxyOn bool `mapstructure:"extproxy" toml:"extproxy" json:"extproxy"`
216217
ExtProxyVIP string `mapstructure:"extproxy-address" toml:"extproxy-address" json:"extproxyAddress"`

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ require (
9999
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
100100
github.com/aclements/go-moremath v0.0.0-20170210193428-033754ab1fee // indirect
101101
github.com/bluele/slack v0.0.0-20180528010058-b4b4d354a079 // indirect
102+
github.com/dasrick/go-teams-notify/v2 v2.1.0 // indirect
102103
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
103104
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 // indirect
104105
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
129129
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
130130
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
131131
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
132+
github.com/dasrick/go-teams-notify/v2 v2.1.0 h1:CSleKfkvrw2O9QmSY/LMHcg5hotuYnV+fftlHk8llRo=
133+
github.com/dasrick/go-teams-notify/v2 v2.1.0/go.mod h1:6TLarJg4hBXOybLxZpBvKIqeZiUZqUOM5SS2DLtUjTM=
132134
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
133135
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
134136
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -800,7 +802,6 @@ golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLL
800802
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
801803
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
802804
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
803-
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
804805
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
805806
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
806807
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
@@ -870,7 +871,6 @@ golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7w
870871
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
871872
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
872873
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
873-
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
874874
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
875875
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
876876
golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

server/server_monitor.go

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ func init() {
180180
monitorCmd.Flags().StringVar(&conf.PushoverAppToken, "alert-pushover-app-token", "", "Pushover App Token for alerts")
181181
monitorCmd.Flags().StringVar(&conf.PushoverUserToken, "alert-pushover-user-token", "", "Pushover User Token for alerts")
182182

183+
monitorCmd.Flags().StringVar(&conf.TeamsUrl, "alert-teams-url", "", "Teams channel url")
184+
183185
monitorCmd.Flags().BoolVar(&conf.RegistryConsul, "registry-consul", false, "Register write and read SRV DNS to consul")
184186
monitorCmd.Flags().StringVar(&conf.RegistryHosts, "registry-servers", "127.0.0.1", "Comma-separated list of registry addresses")
185187

utils/alert/alert.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (a *Alert) Email() error {
3636
e := email.NewEmail()
3737
e.From = a.From
3838
e.To = strings.Split(a.To, ",")
39-
subj := fmt.Sprintf("Repman alert - State change detected on host %s", a.Origin)
39+
subj := fmt.Sprintf("Replication-Manager alert - State change detected on host %s", a.Origin)
4040
e.Subject = subj
4141
text := fmt.Sprintf(`Replication Manager has detected a change of state for host %s.
4242
New server state change from %s is %s.`, a.Origin, a.PrevState, a.State)

utils/logrus/hooks/pushover/pushover.go

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
client "github.com/gregdel/pushover"
7+
"github.com/siddontang/go/log"
78
"github.com/sirupsen/logrus"
89
)
910

@@ -43,9 +44,18 @@ func NewHook(appToken, recipientToken string) *PushoverHook {
4344
}
4445

4546
func (p *PushoverHook) Fire(entry *logrus.Entry) error {
47+
pr := -1
48+
if entry.Level == log.LevelError {
49+
pr = 0
50+
}
51+
if entry.Data["type"].(string) == "alert" {
52+
pr = 1
53+
}
4654
message := &client.Message{
4755
Message: entry.Message,
4856
Timestamp: entry.Time.Unix(),
57+
Title: "Cluster: " + entry.Data["cluster"].(string),
58+
Priority: pr,
4959
}
5060

5161
_, err := p.app.SendMessage(message, p.recipient)

0 commit comments

Comments
 (0)