forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
123 lines (114 loc) · 2.7 KB
/
checker.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package core
import (
"fmt"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"io/ioutil"
"net"
"net/http"
"net/url"
"regexp"
"time"
)
type FailureData types.FailureData
func CheckServices() {
CoreApp.Services, _ = SelectAllServices()
utils.Log(1, fmt.Sprintf("Loaded %v Services", len(CoreApp.Services)))
for _, v := range CoreApp.Services {
obj := v
go obj.StartCheckins()
go obj.CheckQueue()
}
}
func (s *Service) CheckQueue() {
defer s.CheckQueue()
s.Check()
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)
}
func (s *Service) DNSCheck() (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 (s *Service) Check() *Service {
dnsLookup, err := s.DNSCheck()
if err != nil {
s.Failure(fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
return s
}
s.dnsLookup = dnsLookup
t1 := time.Now()
client := http.Client{
Timeout: 30 * time.Second,
}
response, err := client.Get(s.Domain)
if err != nil {
s.Failure(fmt.Sprintf("HTTP Error %v", err))
return s
}
response.Header.Set("User-Agent", "StatupMonitor")
t2 := time.Now()
s.Latency = t2.Sub(t1).Seconds()
if err != nil {
s.Failure(fmt.Sprintf("HTTP Error %v", err))
return s
}
defer response.Body.Close()
contents, _ := ioutil.ReadAll(response.Body)
if s.Expected != "" {
match, _ := regexp.MatchString(s.Expected, string(contents))
if !match {
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
s.Failure(fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected))
return s
}
}
if s.ExpectedStatus != response.StatusCode {
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
s.Failure(fmt.Sprintf("HTTP Status Code %v did not match %v", response.StatusCode, s.ExpectedStatus))
return s
}
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
s.Online = true
s.Record(response)
return s
}
type HitData struct {
Latency float64
}
func (s *Service) Record(response *http.Response) {
s.Online = true
s.LastOnline = time.Now()
data := HitData{
Latency: s.Latency,
}
s.CreateHit(data)
OnSuccess(s)
}
func (s *Service) Failure(issue string) {
s.Online = false
data := FailureData{
Issue: issue,
}
utils.Log(2, fmt.Sprintf("Service %v Failing: %v", s.Name, issue))
s.CreateFailure(data)
//SendFailureEmail(s)
OnFailure(s)
}