statping/checker.go

95 lines
1.9 KiB
Go
Raw Normal View History

2018-06-10 01:31:13 +00:00
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"
)
func CheckServices() {
2018-06-15 04:30:10 +00:00
services, _ = SelectAllServices()
2018-06-10 01:31:13 +00:00
for _, v := range services {
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-10 04:16:04 +00:00
fmt.Printf(" Service: %v | Online: %v | Latency: %v\n", s.Name, s.Online, s.Latency)
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-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-15 04:30:10 +00:00
type FailureData struct {
Issue string
}
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,
}
s.CreateFailure(data)
2018-06-23 08:42:50 +00:00
SendFailureEmail(s)
2018-06-14 01:19:00 +00:00
OnFailure(s)
2018-06-10 01:31:13 +00:00
}