statping/types/services/routine.go

283 lines
6.9 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package services
2018-06-10 01:31:13 +00:00
import (
"bytes"
2018-06-10 01:31:13 +00:00
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/hits"
"github.com/statping/statping/utils"
2019-04-20 03:41:09 +00:00
"github.com/tatsushid/go-fastping"
"net"
2018-06-10 01:31:13 +00:00
"net/http"
"net/url"
2018-06-10 01:31:13 +00:00
"regexp"
"strings"
2018-06-10 01:31:13 +00:00
"time"
)
// checkServices will start the checking go routine for each service
2020-03-04 10:29:00 +00:00
func CheckServices() {
log.Infoln(fmt.Sprintf("Starting monitoring process for %v Services", len(allServices)))
for _, s := range allServices {
2020-02-25 07:41:28 +00:00
//go CheckinRoutine()
2020-03-04 14:20:47 +00:00
time.Sleep(250 * time.Millisecond)
2020-02-26 05:38:03 +00:00
go ServiceCheckQueue(s, true)
2018-06-10 01:31:13 +00:00
}
}
// CheckQueue is the main go routine for checking a service
2020-02-26 05:38:03 +00:00
func ServiceCheckQueue(s *Service, record bool) {
2020-03-04 10:29:00 +00:00
s.Start()
2018-08-20 07:59:17 +00:00
s.Checkpoint = time.Now()
2020-02-25 07:41:28 +00:00
s.SleepDuration = (time.Duration(s.Id) * 100) * time.Millisecond
2020-03-04 14:20:47 +00:00
2018-08-19 00:37:00 +00:00
CheckLoop:
for {
select {
2018-08-19 00:37:00 +00:00
case <-s.Running:
log.Infoln(fmt.Sprintf("Stopping service: %v", s.Name))
2018-08-19 00:37:00 +00:00
break CheckLoop
case <-time.After(s.SleepDuration):
2020-03-04 14:20:47 +00:00
s.CheckService(record)
2020-02-26 05:38:03 +00:00
s.Checkpoint = s.Checkpoint.Add(s.Duration())
sleep := s.Checkpoint.Sub(time.Now())
if !s.Online {
2020-02-26 05:38:03 +00:00
s.SleepDuration = s.Duration()
} else {
s.SleepDuration = sleep
}
}
continue
2018-06-15 04:30:10 +00:00
}
2018-06-10 01:31:13 +00:00
}
2020-02-26 05:38:03 +00:00
func parseHost(s *Service) string {
2018-10-07 02:44:31 +00:00
if s.Type == "tcp" || s.Type == "udp" {
return s.Domain
} else {
2019-05-27 17:35:51 +00:00
u, err := url.Parse(s.Domain)
if err != nil {
return s.Domain
}
2019-05-27 17:35:51 +00:00
return strings.Split(u.Host, ":")[0]
}
}
// dnsCheck will check the domain name and return a float64 for the amount of time the DNS check took
2020-02-26 05:38:03 +00:00
func dnsCheck(s *Service) (float64, error) {
var err error
t1 := time.Now()
2020-02-26 05:38:03 +00:00
host := parseHost(s)
if s.Type == "tcp" {
_, err = net.LookupHost(host)
} else {
_, err = net.LookupIP(host)
}
if err != nil {
return 0, err
}
t2 := time.Now()
subTime := t2.Sub(t1).Seconds()
return subTime, err
}
2019-06-06 19:10:52 +00:00
func isIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
2019-04-20 03:41:09 +00:00
// checkIcmp will send a ICMP ping packet to the service
2020-03-04 10:29:00 +00:00
func CheckIcmp(s *Service, record bool) *Service {
2020-03-05 08:27:51 +00:00
defer s.updateLastCheck()
2019-04-20 03:41:09 +00:00
p := fastping.NewPinger()
2019-06-06 19:10:52 +00:00
resolveIP := "ip4:icmp"
if isIPv6(s.Domain) {
resolveIP = "ip6:icmp"
}
ra, err := net.ResolveIPAddr(resolveIP, s.Domain)
2019-04-20 03:41:09 +00:00
if err != nil {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("Could not send ICMP to service %v, %v", s.Domain, err))
2020-03-04 10:29:00 +00:00
return s
2019-04-20 03:41:09 +00:00
}
p.AddIPAddr(ra)
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
s.Latency = rtt.Seconds()
recordSuccess(s)
}
err = p.Run()
if err != nil {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("Issue running ICMP to service %v, %v", s.Domain, err))
2020-03-04 10:29:00 +00:00
return s
2019-04-20 03:41:09 +00:00
}
s.LastResponse = ""
2020-03-04 10:29:00 +00:00
return s
2019-04-20 03:41:09 +00:00
}
// checkTcp will check a TCP service
2020-03-04 10:29:00 +00:00
func CheckTcp(s *Service, record bool) *Service {
2020-03-05 08:27:51 +00:00
defer s.updateLastCheck()
2020-02-26 05:38:03 +00:00
dnsLookup, err := dnsCheck(s)
if err != nil {
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("Could not get IP address for TCP service %v, %v", s.Domain, err))
}
2020-03-04 10:29:00 +00:00
return s
}
s.PingTime = dnsLookup
t1 := time.Now()
domain := fmt.Sprintf("%v", s.Domain)
if s.Port != 0 {
domain = fmt.Sprintf("%v:%v", s.Domain, s.Port)
2019-06-06 19:10:52 +00:00
if isIPv6(s.Domain) {
domain = fmt.Sprintf("[%v]:%v", s.Domain, s.Port)
}
}
2018-10-07 02:44:31 +00:00
conn, err := net.DialTimeout(s.Type, domain, time.Duration(s.Timeout)*time.Second)
if err != nil {
2018-08-19 00:37:00 +00:00
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("Dial Error %v", err))
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
}
if err := conn.Close(); err != nil {
2018-08-19 00:37:00 +00:00
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("%v Socket Close Error %v", strings.ToUpper(s.Type), err))
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
}
t2 := time.Now()
s.Latency = t2.Sub(t1).Seconds()
s.LastResponse = ""
2018-08-19 00:37:00 +00:00
if record {
2018-09-25 07:03:49 +00:00
recordSuccess(s)
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
}
2020-03-05 08:27:51 +00:00
func (s *Service) updateLastCheck() {
s.LastCheck = time.Now()
}
// checkHttp will check a HTTP service
2020-03-04 10:29:00 +00:00
func CheckHttp(s *Service, record bool) *Service {
2020-03-05 08:27:51 +00:00
defer s.updateLastCheck()
2020-02-26 05:38:03 +00:00
dnsLookup, err := dnsCheck(s)
if err != nil {
2018-08-19 00:37:00 +00:00
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
}
s.PingTime = dnsLookup
2018-06-10 01:31:13 +00:00
t1 := time.Now()
2018-11-25 10:18:21 +00:00
timeout := time.Duration(s.Timeout) * time.Second
var content []byte
var res *http.Response
2019-03-16 22:55:45 +00:00
var headers []string
if s.Headers.Valid {
headers = strings.Split(s.Headers.String, ",")
} else {
headers = nil
}
if s.Method == "POST" {
content, res, err = utils.HttpRequest(s.Domain, s.Method, "application/json", headers, bytes.NewBuffer([]byte(s.PostData.String)), timeout, s.VerifySSL.Bool)
} else {
content, res, err = utils.HttpRequest(s.Domain, s.Method, nil, headers, nil, timeout, s.VerifySSL.Bool)
}
2018-06-30 22:37:01 +00:00
if err != nil {
2018-08-19 00:37:00 +00:00
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("HTTP Error %v", err))
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
2018-06-30 22:37:01 +00:00
}
2018-06-10 01:31:13 +00:00
t2 := time.Now()
s.Latency = t2.Sub(t1).Seconds()
2018-11-25 10:18:21 +00:00
s.LastResponse = string(content)
s.LastStatusCode = res.StatusCode
if s.Expected.String != "" {
2018-11-25 10:18:21 +00:00
match, err := regexp.MatchString(s.Expected.String, string(content))
if err != nil {
log.Warnln(fmt.Sprintf("Service %v expected: %v to match %v", s.Name, string(content), s.Expected.String))
}
2018-06-10 01:31:13 +00:00
if !match {
2018-08-19 00:37:00 +00:00
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected))
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
2018-06-10 01:31:13 +00:00
}
}
2018-11-25 10:18:21 +00:00
if s.ExpectedStatus != res.StatusCode {
2018-08-19 00:37:00 +00:00
if record {
2020-02-26 05:38:03 +00:00
recordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", res.StatusCode, s.ExpectedStatus))
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
2018-06-10 01:31:13 +00:00
}
2018-08-19 00:37:00 +00:00
if record {
2018-09-25 07:03:49 +00:00
recordSuccess(s)
2018-08-19 00:37:00 +00:00
}
2020-03-04 10:29:00 +00:00
return s
2018-06-15 04:30:10 +00:00
}
2018-09-25 07:03:49 +00:00
// recordSuccess will create a new 'hit' record in the database for a successful/online service
2020-02-26 05:38:03 +00:00
func recordSuccess(s *Service) {
2020-02-04 03:49:17 +00:00
s.LastOnline = time.Now().UTC()
2020-03-04 14:20:47 +00:00
s.Online = true
2020-03-04 10:29:00 +00:00
hit := &hits.Hit{
Service: s.Id,
Latency: s.Latency,
PingTime: s.PingTime,
2020-01-04 02:03:59 +00:00
CreatedAt: time.Now().UTC(),
2018-06-15 04:30:10 +00:00
}
2020-03-04 14:20:47 +00:00
if err := hit.Create(); err != nil {
log.Error(err)
}
log.WithFields(utils.ToFields(hit, s)).Infoln(
fmt.Sprintf("Service #%d '%v' Successful Response: %0.2f ms | Lookup in: %0.2f ms | Online: %v | Interval: %d seconds", s.Id, s.Name, hit.Latency*1000, hit.PingTime*1000, s.Online, s.Interval))
2020-03-05 08:27:51 +00:00
s.LastLookupTime = int64(hit.PingTime * 1000)
s.LastLatency = int64(hit.Latency * 1000)
2020-03-04 10:29:00 +00:00
//notifier.OnSuccess(s)
s.SuccessNotified = true
2018-06-10 01:31:13 +00:00
}
2018-12-06 19:03:55 +00:00
// recordFailure will create a new 'Failure' record in the database for a offline service
2020-02-26 05:38:03 +00:00
func recordFailure(s *Service, issue string) {
2020-03-05 08:27:51 +00:00
s.LastOffline = time.Now().UTC()
2020-03-04 10:29:00 +00:00
fail := &failures.Failure{
Service: s.Id,
Issue: issue,
PingTime: s.PingTime,
2020-01-04 02:03:59 +00:00
CreatedAt: time.Now().UTC(),
2019-01-29 12:02:13 +00:00
ErrorCode: s.LastStatusCode,
}
2020-02-25 07:41:28 +00:00
log.WithFields(utils.ToFields(fail, s)).
2019-12-30 04:51:28 +00:00
Warnln(fmt.Sprintf("Service %v Failing: %v | Lookup in: %0.2f ms", s.Name, issue, fail.PingTime*1000))
2020-03-04 10:29:00 +00:00
2020-03-04 14:20:47 +00:00
if err := fail.Create(); err != nil {
log.Error(err)
}
s.Online = false
s.SuccessNotified = false
2020-02-26 05:38:03 +00:00
s.DownText = s.DowntimeText()
2020-03-04 10:29:00 +00:00
//notifier.OnFailure(s, fail)
}
// Check will run checkHttp for HTTP services and checkTcp for TCP services
// if record param is set to true, it will add a record into the database.
2020-03-04 14:20:47 +00:00
func (s *Service) CheckService(record bool) {
switch s.Type {
2020-03-04 10:29:00 +00:00
case "http":
2020-03-04 14:20:47 +00:00
CheckHttp(s, record)
2020-03-04 10:29:00 +00:00
case "tcp", "udp":
2020-03-04 14:20:47 +00:00
CheckTcp(s, record)
2020-03-04 10:29:00 +00:00
case "icmp":
2020-03-04 14:20:47 +00:00
CheckIcmp(s, record)
2020-03-04 10:29:00 +00:00
}
2018-06-10 01:31:13 +00:00
}