From edc2d9b7d04b48d72d30869fe4ebabbc098cc441 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Tue, 17 Jul 2018 22:24:52 -0700 Subject: [PATCH] @Southclaws TCP checker - email notifier update --- core/checker.go | 43 +++++++++++++++++++++++++++++++++++-------- notifiers/email.go | 12 ++++++++---- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/core/checker.go b/core/checker.go index 68ad41c6..d14520c8 100644 --- a/core/checker.go +++ b/core/checker.go @@ -33,13 +33,8 @@ func CheckQueue(s *types.Service) { return default: ServiceCheck(s) - if s.Interval < 1 { - s.Interval = 1 - } - msg := fmt.Sprintf("Service: %v | Online: %v | Latency: %0.0fms", s.Name, s.Online, (s.Latency * 1000)) - utils.Log(1, msg) - time.Sleep(time.Duration(s.Interval) * time.Second) } + time.Sleep(time.Duration(s.Interval) * time.Second) } } @@ -58,7 +53,39 @@ func DNSCheck(s *types.Service) (float64, error) { return subTime, err } +func ServiceTCPCheck(s *types.Service) *types.Service { + t1 := time.Now() + domain := fmt.Sprintf("%v", s.Domain) + if s.Port != 0 { + domain = fmt.Sprintf("%v:%v", s.Domain, s.Port) + } + conn, err := net.Dial("tcp", domain) + if err != nil { + RecordFailure(s, fmt.Sprintf("TCP Dial Error %v", err)) + return s + } + if err := conn.Close(); err != nil { + RecordFailure(s, fmt.Sprintf("TCP Socket Close Error %v", err)) + return s + } + t2 := time.Now() + s.Latency = t2.Sub(t1).Seconds() + s.LastResponse = "" + RecordSuccess(s) + return s +} + func ServiceCheck(s *types.Service) *types.Service { + switch s.Type { + case "http": + ServiceHTTPCheck(s) + case "tcp": + ServiceTCPCheck(s) + } + return s +} + +func ServiceHTTPCheck(s *types.Service) *types.Service { dnsLookup, err := DNSCheck(s) if err != nil { RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err)) @@ -113,7 +140,7 @@ func ServiceCheck(s *types.Service) *types.Service { s.LastResponse = string(contents) s.LastStatusCode = response.StatusCode s.Online = true - RecordSuccess(s, response) + RecordSuccess(s) return s } @@ -121,7 +148,7 @@ type HitData struct { Latency float64 } -func RecordSuccess(s *types.Service, response *http.Response) { +func RecordSuccess(s *types.Service) { s.Online = true s.LastOnline = time.Now() data := HitData{ diff --git a/notifiers/email.go b/notifiers/email.go index ad9715e9..827439a9 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -66,6 +66,12 @@ func init() { Title: "Outgoing Email Address", Placeholder: "Insert your Outgoing Email Address", DbField: "Var1", + }, { + Id: 1, + Type: "email", + Title: "Send Alerts To", + Placeholder: "Email Address", + DbField: "Var2", }}, }} @@ -103,7 +109,7 @@ func (u *Email) Init() error { func (u *Email) Test() error { if u.Enabled { email := &EmailOutgoing{ - To: "info@socialeck.com", + To: emailer.Var2, Subject: "Test Email", Template: "message.html", Data: nil, @@ -159,13 +165,11 @@ func (u *Email) Run() error { // ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS func (u *Email) OnFailure(s *types.Service) error { if u.Enabled { - msg := emailMessage{ Service: s, } - email := &EmailOutgoing{ - To: "info@socialeck.com", + To: emailer.Var2, Subject: fmt.Sprintf("Service %v is Failing", s.Name), Template: "failure.html", Data: msg,