tcp from Soontao/cachet-monitor
# Conflicts: # .gitignore # cli/main.go
This commit is contained in:
committed by
Christoph Eck
parent
7e5187764b
commit
c3d22a40db
@@ -184,6 +184,10 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) {
|
|||||||
var s cachet.DNSMonitor
|
var s cachet.DNSMonitor
|
||||||
err = mapstructure.Decode(rawMonitor, &s)
|
err = mapstructure.Decode(rawMonitor, &s)
|
||||||
t = &s
|
t = &s
|
||||||
|
case "tcp":
|
||||||
|
var s cachet.TCPMonitor
|
||||||
|
err = mapstructure.Decode(rawMonitor, &s)
|
||||||
|
t = &s
|
||||||
default:
|
default:
|
||||||
logrus.Errorf("Invalid monitor type (index: %d) %v", index, monType)
|
logrus.Errorf("Invalid monitor type (index: %d) %v", index, monType)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -8,53 +8,11 @@
|
|||||||
"slack_webhook": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
|
"slack_webhook": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
|
||||||
"monitors": [
|
"monitors": [
|
||||||
{
|
{
|
||||||
"name": "google",
|
"name": "IN-Ulm IRC",
|
||||||
"target": "https://google.com",
|
"target": "irc.in-ulm.de",
|
||||||
"strict": true,
|
"type": "tcp",
|
||||||
"method": "POST",
|
"port": "6660",
|
||||||
"component_id": 1,
|
"component_id": 4
|
||||||
"metric_id": 4,
|
|
||||||
"template": {
|
|
||||||
"investigating": {
|
|
||||||
"subject": "{{ .Monitor.Name }} - {{ .SystemName }}",
|
|
||||||
"message": "{{ .Monitor.Name }} check **failed** (server time: {{ .now }})\n\n{{ .FailReason }}"
|
|
||||||
},
|
|
||||||
"fixed": {
|
|
||||||
"subject": "I HAVE BEEN FIXED"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"interval": 1,
|
|
||||||
"timeout": 1,
|
|
||||||
"threshold": 80,
|
|
||||||
"headers": {
|
|
||||||
"Authorization": "Basic <hash>"
|
|
||||||
},
|
|
||||||
"expected_status_code": 200,
|
|
||||||
"expected_body": "P.*NG"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "dns",
|
|
||||||
"target": "matej.me.",
|
|
||||||
"question": "mx",
|
|
||||||
"type": "dns",
|
|
||||||
"component_id": 2,
|
|
||||||
"interval": 1,
|
|
||||||
"timeout": 1,
|
|
||||||
"dns": "8.8.4.4:53",
|
|
||||||
"answers": [
|
|
||||||
{
|
|
||||||
"regex": "[1-9] alt[1-9].aspmx.l.google.com."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"exact": "10 aspmx2.googlemail.com."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"exact": "1 aspmx.l.google.com."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"exact": "10 aspmx3.googlemail.com."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
76
tcp.go
Normal file
76
tcp.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
// Thanks go to https://github.com/Soontao/cachet-monitor/blob/master/tcp.go
|
||||||
|
package cachet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Investigating template
|
||||||
|
var defaultTCPInvestigatingTpl = MessageTemplate{
|
||||||
|
Subject: `{{ .Monitor.Name }} - {{ .SystemName }}`,
|
||||||
|
Message: `{{ .Monitor.Name }} check **failed** (server time: {{ .now }})
|
||||||
|
|
||||||
|
{{ .FailReason }}`,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fixed template
|
||||||
|
var defaultTCPFixedTpl = MessageTemplate{
|
||||||
|
Subject: `{{ .Monitor.Name }} - {{ .SystemName }}`,
|
||||||
|
Message: `**Resolved** - {{ .now }}
|
||||||
|
|
||||||
|
Down seconds: {{ .downSeconds }}s`,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TCPMonitor struct
|
||||||
|
type TCPMonitor struct {
|
||||||
|
AbstractMonitor `mapstructure:",squash"`
|
||||||
|
Port string
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckTCPPortAlive func
|
||||||
|
func CheckTCPPortAlive(ip, port string, timeout int64) (bool, error) {
|
||||||
|
|
||||||
|
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), time.Duration(timeout)*time.Second)
|
||||||
|
if conn != nil {
|
||||||
|
defer conn.Close()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
} else {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// test if it available
|
||||||
|
func (m *TCPMonitor) test() bool {
|
||||||
|
if alive, e := CheckTCPPortAlive(m.Target, m.Port, int64(m.Timeout)); alive {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
m.lastFailReason = fmt.Sprintf("TCP check failed: %v", e)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate configuration
|
||||||
|
func (m *TCPMonitor) Validate() []string {
|
||||||
|
|
||||||
|
// set incident temp
|
||||||
|
m.Template.Investigating.SetDefault(defaultTCPInvestigatingTpl)
|
||||||
|
m.Template.Fixed.SetDefault(defaultTCPFixedTpl)
|
||||||
|
|
||||||
|
// super.Validate()
|
||||||
|
errs := m.AbstractMonitor.Validate()
|
||||||
|
|
||||||
|
if m.Target == "" {
|
||||||
|
errs = append(errs, "Target is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Port == "" {
|
||||||
|
errs = append(errs, "Port is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
return errs
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user