statping/core/checker.go

97 lines
2.2 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 (
"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/http"
"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()
utils.Log(1, fmt.Sprintf("Loaded %v Services", len(CoreApp.Services)))
for _, v := range CoreApp.Services {
2018-06-10 01:31:13 +00:00
obj := v
2018-06-22 06:56:44 +00:00
go obj.StartCheckins()
2018-06-10 01:31:13 +00:00
go obj.CheckQueue()
}
}
func (s *Service) CheckQueue() {
2018-06-23 23:09:35 +00:00
defer s.CheckQueue()
2018-06-10 01:31:13 +00:00
s.Check()
2018-06-15 04:30:10 +00:00
if s.Interval < 1 {
s.Interval = 1
}
2018-06-29 02:24:31 +00:00
msg := fmt.Sprintf("Service: %v | Online: %v | Latency: %0.0fms", s.Name, s.Online, (s.Latency * 1000))
2018-06-30 00:57:05 +00:00
utils.Log(1, msg)
2018-06-10 04:16:04 +00:00
time.Sleep(time.Duration(s.Interval) * time.Second)
2018-06-10 01:31:13 +00:00
}
2018-06-15 04:30:10 +00:00
func (s *Service) Check() *Service {
2018-06-10 01:31:13 +00:00
t1 := time.Now()
2018-06-10 05:05:08 +00:00
client := http.Client{
Timeout: 30 * time.Second,
}
response, err := client.Get(s.Domain)
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-06-10 05:05:08 +00:00
s.Failure(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()
2018-06-23 08:42:50 +00:00
contents, _ := ioutil.ReadAll(response.Body)
2018-06-10 01:31:13 +00:00
if s.Expected != "" {
match, _ := regexp.MatchString(s.Expected, string(contents))
if !match {
2018-06-23 08:42:50 +00:00
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
2018-06-10 05:05:08 +00:00
s.Failure(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-06-10 05:05:08 +00:00
s.Failure(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
2018-06-10 01:31:13 +00:00
s.Record(response)
2018-06-15 04:30:10 +00:00
return s
}
type HitData struct {
Latency float64
2018-06-10 01:31:13 +00:00
}
func (s *Service) Record(response *http.Response) {
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,
}
s.CreateHit(data)
2018-06-14 06:38:15 +00:00
OnSuccess(s)
2018-06-10 01:31:13 +00:00
}
2018-06-10 05:05:08 +00:00
func (s *Service) Failure(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-06-15 04:30:10 +00:00
s.CreateFailure(data)
2018-06-30 00:57:05 +00:00
//SendFailureEmail(s)
2018-06-14 01:19:00 +00:00
OnFailure(s)
2018-06-10 01:31:13 +00:00
}