forked from CastawayLabs/cachet-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpp.go
79 lines (61 loc) · 1.72 KB
/
xmpp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cachet
import (
"strings"
"github.com/mattn/go-xmpp"
"github.com/Sirupsen/logrus"
)
// Investigating template
var defaultXMPPInvestigatingTpl = MessageTemplate{
Subject: `{{ .Monitor.Name }} - {{ .SystemName }}`,
Message: `{{ .Monitor.Name }} check **failed** (server time: {{ .now }})
{{ .FailReason }}`,
}
// Fixed template
var defaultXMPPFixedTpl = MessageTemplate{
Subject: `{{ .Monitor.Name }} - {{ .SystemName }}`,
Message: `**Resolved** - {{ .now }}
- - -
{{ .incident.Message }}`,
}
type XMPPMonitor struct {
AbstractMonitor `mapstructure:",squash"`
Username string
Password string
}
// TODO: test
// TODO: xmpp library doesn't support connection timeout
func (monitor *XMPPMonitor) test() bool {
var talk *xmpp.Client
var err error
options := xmpp.Options{Host: monitor.Target,
User: monitor.Username,
Password: monitor.Password,
NoTLS: true,
StartTLS: true,
}
talk, err = options.NewClient()
if err != nil && strings.Index(err.Error(), "PLAIN ") < 0 {
monitor.lastFailReason = err.Error()
logrus.Errorf("XMPP error: %s", monitor.lastFailReason)
return false
}
if talk != nil {
talk.Close()
}
return true
}
// TODO: test
func (mon *XMPPMonitor) Validate() []string {
mon.Template.Investigating.SetDefault(defaultXMPPInvestigatingTpl)
mon.Template.Fixed.SetDefault(defaultXMPPFixedTpl)
errs := mon.AbstractMonitor.Validate()
if len(mon.Username) == 0 || len(mon.Password) == 0 {
errs = append(errs, "Both 'username' and 'password' must be provided")
}
return errs
}
func (mon *XMPPMonitor) Describe() []string {
features := mon.AbstractMonitor.Describe()
features = append(features, "Username: "+mon.Username)
return features
}