statping/core/checker.go

233 lines
5.4 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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"
"github.com/hunterlong/statup/notifiers"
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"
"strings"
2018-06-10 01:31:13 +00:00
"time"
)
func CheckServices() {
utils.Log(1, fmt.Sprintf("Starting monitoring process for %v Services", len(CoreApp.Services)))
for _, ser := range CoreApp.Services {
2018-07-02 06:21:41 +00:00
//go obj.StartCheckins()
2018-08-21 01:22:42 +00:00
go ser.CheckQueue(true)
2018-06-10 01:31:13 +00:00
}
}
func (s *Service) CheckQueue(record bool) {
2018-08-20 07:59:17 +00:00
s.Checkpoint = time.Now()
s.SleepDuration = time.Duration((time.Duration(s.Id) * 100) * time.Millisecond)
2018-08-19 00:37:00 +00:00
CheckLoop:
for {
select {
2018-08-19 00:37:00 +00:00
case <-s.Running:
utils.Log(1, fmt.Sprintf("Stopping service: %v", s.Name))
break CheckLoop
case <-time.After(s.SleepDuration):
2018-08-21 01:22:42 +00:00
s.Check(record)
2018-08-21 06:54:39 +00:00
s.Checkpoint = s.Checkpoint.Add(s.duration())
sleep := s.Checkpoint.Sub(time.Now())
if !s.Online {
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
}
2018-08-21 06:54:39 +00:00
func (s *Service) duration() time.Duration {
var amount time.Duration
if s.Interval >= 10000 {
amount = time.Duration(s.Interval) * time.Microsecond
} else {
amount = time.Duration(s.Interval) * time.Second
}
return amount
}
2018-08-21 01:22:42 +00:00
func (s *Service) dnsCheck() (float64, error) {
t1 := time.Now()
domain := s.Domain
hasPort, _ := regexp.MatchString(`\:([0-9]+)`, domain)
if hasPort {
splitDomain := strings.Split(s.Domain, ":")
domain = splitDomain[len(splitDomain)-2]
}
url, err := url.Parse(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
}
2018-08-21 01:22:42 +00:00
func (s *Service) checkTcp(record bool) *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 {
2018-08-19 00:37:00 +00:00
if record {
RecordFailure(s, fmt.Sprintf("TCP Dial Error %v", err))
}
return s
}
if err := conn.Close(); err != nil {
2018-08-19 00:37:00 +00:00
if record {
RecordFailure(s, fmt.Sprintf("TCP Socket Close Error %v", err))
}
return s
}
t2 := time.Now()
s.Latency = t2.Sub(t1).Seconds()
s.LastResponse = ""
2018-08-19 00:37:00 +00:00
if record {
RecordSuccess(s)
}
return s
}
func (s *Service) Check(record bool) {
switch s.Type {
case "http":
2018-08-21 01:22:42 +00:00
s.checkHttp(record)
case "tcp":
2018-08-21 01:22:42 +00:00
s.checkTcp(record)
}
}
2018-08-21 01:22:42 +00:00
func (s *Service) checkHttp(record bool) *Service {
dnsLookup, err := s.dnsCheck()
if err != nil {
2018-08-19 00:37:00 +00:00
if record {
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-08-19 00:37:00 +00:00
if record {
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-08-19 00:37:00 +00:00
if record {
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()
2018-06-10 01:31:13 +00:00
if s.Expected != "" {
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
utils.Log(2, err)
}
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-08-19 00:37:00 +00:00
if record {
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 {
//s.LastResponse = string(contents)
2018-06-23 08:42:50 +00:00
s.LastStatusCode = response.StatusCode
2018-08-19 00:37:00 +00:00
if record {
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.LastStatusCode = response.StatusCode
2018-06-10 03:44:47 +00:00
s.Online = true
2018-08-19 00:37:00 +00:00
if record {
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
}
2018-08-19 00:37:00 +00:00
func RecordSuccess(s *Service) {
2018-06-15 04:30:10 +00:00
s.Online = true
2018-06-23 08:42:50 +00:00
s.LastOnline = time.Now()
hit := &types.Hit{
Service: s.Id,
Latency: s.Latency,
CreatedAt: time.Now(),
2018-06-15 04:30:10 +00:00
}
utils.Log(1, fmt.Sprintf("Service %v Successful: %0.2f ms", s.Name, hit.Latency*1000))
s.CreateHit(hit)
notifiers.OnSuccess(s.Service)
2018-06-10 01:31:13 +00:00
}
2018-08-19 00:37:00 +00:00
func RecordFailure(s *Service, issue string) {
2018-06-15 04:30:10 +00:00
s.Online = false
fail := &types.Failure{
Service: s.Id,
Issue: issue,
CreatedAt: time.Now(),
2018-06-15 04:30:10 +00:00
}
2018-06-30 00:57:05 +00:00
utils.Log(2, fmt.Sprintf("Service %v Failing: %v", s.Name, issue))
s.CreateFailure(fail)
notifiers.OnFailure(s.Service, fail)
2018-06-10 01:31:13 +00:00
}