statping/core/checker.go

175 lines
3.9 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package core
2018-06-10 01:31:13 +00:00
import (
"bytes"
2018-06-10 01:31:13 +00:00
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
2018-06-10 01:31:13 +00:00
"io/ioutil"
"net"
2018-06-10 01:31:13 +00:00
"net/http"
"net/url"
2018-06-10 01:31:13 +00:00
"regexp"
"time"
)
2018-06-30 00:57:05 +00:00
type FailureData types.FailureData
2018-06-10 01:31:13 +00:00
func CheckServices() {
2018-06-30 00:57:05 +00:00
CoreApp.Services, _ = SelectAllServices()
2018-07-02 06:21:41 +00:00
utils.Log(1, fmt.Sprintf("Starting monitoring process for %v Services", len(CoreApp.Services)))
2018-07-14 02:37:39 +00:00
for _, ser := range CoreApp.Services {
s := ser.ToService()
2018-07-02 06:21:41 +00:00
//go obj.StartCheckins()
2018-08-12 19:23:04 +00:00
s.Start()
2018-07-17 09:18:20 +00:00
go CheckQueue(s)
2018-06-10 01:31:13 +00:00
}
}
2018-07-14 02:37:39 +00:00
func CheckQueue(s *types.Service) {
for {
select {
2018-07-14 02:37:39 +00:00
case <-s.StopRoutine:
return
default:
2018-08-12 19:23:04 +00:00
s = SelectService(s.Id).ToService()
2018-07-14 02:37:39 +00:00
ServiceCheck(s)
}
time.Sleep(time.Duration(s.Interval) * time.Second)
2018-06-15 04:30:10 +00:00
}
2018-06-10 01:31:13 +00:00
}
2018-07-14 02:37:39 +00:00
func DNSCheck(s *types.Service) (float64, error) {
t1 := time.Now()
url, err := url.Parse(s.Domain)
if err != nil {
return 0, err
}
_, err = net.LookupIP(url.Host)
if err != nil {
return 0, err
}
t2 := time.Now()
subTime := t2.Sub(t1).Seconds()
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.DialTimeout("tcp", domain, time.Duration(s.Timeout)*time.Second)
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
}
2018-07-14 02:37:39 +00:00
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 {
2018-07-14 02:37:39 +00:00
dnsLookup, err := DNSCheck(s)
if err != nil {
2018-07-14 02:37:39 +00:00
RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
return s
}
2018-07-14 02:37:39 +00:00
s.DnsLookup = dnsLookup
2018-06-10 01:31:13 +00:00
t1 := time.Now()
timeout := time.Duration(s.Timeout)
2018-06-10 05:05:08 +00:00
client := http.Client{
Timeout: timeout * time.Second,
2018-06-10 05:05:08 +00:00
}
var response *http.Response
if s.Method == "POST" {
response, err = client.Post(s.Domain, "application/json", bytes.NewBuffer([]byte(s.PostData)))
} else {
response, err = client.Get(s.Domain)
}
2018-06-30 22:37:01 +00:00
if err != nil {
2018-07-14 02:37:39 +00:00
RecordFailure(s, fmt.Sprintf("HTTP Error %v", err))
2018-06-30 22:37:01 +00:00
return s
}
2018-08-15 06:28:29 +00:00
response.Header.Set("Connection", "close")
2018-06-30 05:17:51 +00:00
response.Header.Set("User-Agent", "StatupMonitor")
2018-06-10 01:31:13 +00:00
t2 := time.Now()
s.Latency = t2.Sub(t1).Seconds()
if err != nil {
2018-07-14 02:37:39 +00:00
RecordFailure(s, fmt.Sprintf("HTTP Error %v", err))
2018-06-15 04:30:10 +00:00
return s
2018-06-10 01:31:13 +00:00
}
2018-06-10 05:05:08 +00:00
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
utils.Log(2, err)
}
2018-06-10 01:31:13 +00:00
if s.Expected != "" {
match, err := regexp.MatchString(s.Expected, string(contents))
if err != nil {
utils.Log(2, err)
}
2018-06-10 01:31:13 +00:00
if !match {
2018-06-23 08:42:50 +00:00
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
2018-07-14 02:37:39 +00:00
RecordFailure(s, fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected))
2018-06-15 04:30:10 +00:00
return s
2018-06-10 01:31:13 +00:00
}
}
if s.ExpectedStatus != response.StatusCode {
2018-06-23 08:42:50 +00:00
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
2018-07-14 02:37:39 +00:00
RecordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", response.StatusCode, s.ExpectedStatus))
2018-06-15 04:30:10 +00:00
return s
2018-06-10 01:31:13 +00:00
}
2018-06-23 08:42:50 +00:00
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
2018-06-10 03:44:47 +00:00
s.Online = true
RecordSuccess(s)
2018-06-15 04:30:10 +00:00
return s
}
type HitData struct {
Latency float64
2018-06-10 01:31:13 +00:00
}
func RecordSuccess(s *types.Service) {
2018-06-15 04:30:10 +00:00
s.Online = true
2018-06-23 08:42:50 +00:00
s.LastOnline = time.Now()
2018-06-15 04:30:10 +00:00
data := HitData{
Latency: s.Latency,
}
utils.Log(1, fmt.Sprintf("Service %v Successful: %0.2f ms", s.Name, data.Latency*1000))
2018-07-14 02:37:39 +00:00
CreateServiceHit(s, data)
2018-06-14 06:38:15 +00:00
OnSuccess(s)
2018-06-10 01:31:13 +00:00
}
2018-07-14 02:37:39 +00:00
func RecordFailure(s *types.Service, issue string) {
2018-06-15 04:30:10 +00:00
s.Online = false
data := FailureData{
Issue: issue,
}
2018-06-30 00:57:05 +00:00
utils.Log(2, fmt.Sprintf("Service %v Failing: %v", s.Name, issue))
2018-07-14 02:37:39 +00:00
CreateServiceFailure(s, data)
2018-06-30 00:57:05 +00:00
//SendFailureEmail(s)
OnFailure(s, data)
2018-06-10 01:31:13 +00:00
}