2018-12-04 05:57:11 +00:00
|
|
|
// Statping
|
2018-08-16 06:22:20 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-08-16 06:22:20 +00:00
|
|
|
//
|
|
|
|
// 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 (
|
2018-07-01 10:24:35 +00:00
|
|
|
"bytes"
|
2018-06-10 01:31:13 +00:00
|
|
|
"fmt"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core/notifier"
|
|
|
|
"github.com/hunterlong/statping/types"
|
|
|
|
"github.com/hunterlong/statping/utils"
|
2018-07-01 03:54:28 +00:00
|
|
|
"net"
|
2018-06-10 01:31:13 +00:00
|
|
|
"net/http"
|
2018-07-01 03:54:28 +00:00
|
|
|
"net/url"
|
2018-06-10 01:31:13 +00:00
|
|
|
"regexp"
|
2018-08-22 05:41:15 +00:00
|
|
|
"strings"
|
2018-06-10 01:31:13 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-09-15 01:18:21 +00:00
|
|
|
// checkServices will start the checking go routine for each service
|
|
|
|
func checkServices() {
|
2018-09-08 22:16:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// CheckQueue is the main go routine for checking a service
|
2018-08-20 07:20:05 +00:00
|
|
|
func (s *Service) CheckQueue(record bool) {
|
2018-08-20 07:59:17 +00:00
|
|
|
s.Checkpoint = time.Now()
|
2018-09-08 22:16:26 +00:00
|
|
|
s.SleepDuration = time.Duration((time.Duration(s.Id) * 100) * time.Millisecond)
|
2018-08-19 00:37:00 +00:00
|
|
|
CheckLoop:
|
2018-07-04 19:14:28 +00:00
|
|
|
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
|
2018-09-08 22:16:26 +00:00
|
|
|
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())
|
2018-09-08 22:16:26 +00:00
|
|
|
sleep := s.Checkpoint.Sub(time.Now())
|
|
|
|
if !s.Online {
|
|
|
|
s.SleepDuration = s.duration()
|
|
|
|
} else {
|
|
|
|
s.SleepDuration = sleep
|
2018-08-19 16:06:59 +00:00
|
|
|
}
|
2018-07-04 19:14:28 +00:00
|
|
|
}
|
2018-09-08 22:16:26 +00:00
|
|
|
continue
|
2018-06-15 04:30:10 +00:00
|
|
|
}
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// duration returns the amount of duration for a service to check its status
|
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-10-03 03:48:40 +00:00
|
|
|
func (s *Service) parseHost() string {
|
2018-10-07 02:44:31 +00:00
|
|
|
if s.Type == "tcp" || s.Type == "udp" {
|
2018-10-03 03:48:40 +00:00
|
|
|
return s.Domain
|
|
|
|
} else {
|
|
|
|
domain := s.Domain
|
|
|
|
hasPort, _ := regexp.MatchString(`\:([0-9]+)`, domain)
|
|
|
|
if hasPort {
|
|
|
|
splitDomain := strings.Split(s.Domain, ":")
|
|
|
|
domain = splitDomain[len(splitDomain)-2]
|
|
|
|
}
|
|
|
|
host, err := url.Parse(domain)
|
|
|
|
if err != nil {
|
|
|
|
return s.Domain
|
|
|
|
}
|
|
|
|
return host.Host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// dnsCheck will check the domain name and return a float64 for the amount of time the DNS check took
|
2018-08-21 01:22:42 +00:00
|
|
|
func (s *Service) dnsCheck() (float64, error) {
|
2018-10-03 03:48:40 +00:00
|
|
|
var err error
|
2018-07-01 03:54:28 +00:00
|
|
|
t1 := time.Now()
|
2018-10-03 03:48:40 +00:00
|
|
|
host := s.parseHost()
|
|
|
|
if s.Type == "tcp" {
|
|
|
|
_, err = net.LookupHost(host)
|
|
|
|
} else {
|
|
|
|
_, err = net.LookupIP(host)
|
2018-07-01 03:54:28 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
t2 := time.Now()
|
|
|
|
subTime := t2.Sub(t1).Seconds()
|
|
|
|
return subTime, err
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// checkTcp will check a TCP service
|
2018-08-21 01:22:42 +00:00
|
|
|
func (s *Service) checkTcp(record bool) *Service {
|
2018-10-03 03:48:40 +00:00
|
|
|
dnsLookup, err := s.dnsCheck()
|
|
|
|
if err != nil {
|
|
|
|
if record {
|
|
|
|
recordFailure(s, fmt.Sprintf("Could not get IP address for TCP service %v, %v", s.Domain, err))
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
s.PingTime = dnsLookup
|
2018-07-18 05:24:52 +00:00
|
|
|
t1 := time.Now()
|
|
|
|
domain := fmt.Sprintf("%v", s.Domain)
|
|
|
|
if s.Port != 0 {
|
|
|
|
domain = fmt.Sprintf("%v:%v", s.Domain, s.Port)
|
|
|
|
}
|
2018-10-07 02:44:31 +00:00
|
|
|
conn, err := net.DialTimeout(s.Type, domain, time.Duration(s.Timeout)*time.Second)
|
2018-07-18 05:24:52 +00:00
|
|
|
if err != nil {
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-10-07 02:44:31 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("%v Dial Error %v", s.Type, err))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-07-18 05:24:52 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
if err := conn.Close(); err != nil {
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("TCP Socket Close Error %v", err))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-07-18 05:24:52 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
t2 := time.Now()
|
|
|
|
s.Latency = t2.Sub(t1).Seconds()
|
|
|
|
s.LastResponse = ""
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordSuccess(s)
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-07-18 05:24:52 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// checkHttp will check a HTTP service
|
2018-08-21 01:22:42 +00:00
|
|
|
func (s *Service) checkHttp(record bool) *Service {
|
|
|
|
dnsLookup, err := s.dnsCheck()
|
2018-07-01 03:54:28 +00:00
|
|
|
if err != nil {
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-07-01 03:54:28 +00:00
|
|
|
return s
|
|
|
|
}
|
2018-10-03 03:48:40 +00:00
|
|
|
s.PingTime = dnsLookup
|
2018-06-10 01:31:13 +00:00
|
|
|
t1 := time.Now()
|
2018-11-25 10:18:21 +00:00
|
|
|
|
|
|
|
timeout := time.Duration(s.Timeout) * time.Second
|
|
|
|
var content []byte
|
|
|
|
var res *http.Response
|
2018-07-01 10:24:35 +00:00
|
|
|
if s.Method == "POST" {
|
2018-11-25 10:18:21 +00:00
|
|
|
content, res, err = utils.HttpRequest(s.Domain, s.Method, "application/json", nil, bytes.NewBuffer([]byte(s.PostData.String)), timeout)
|
2018-07-01 10:24:35 +00:00
|
|
|
} else {
|
2018-11-25 10:18:21 +00:00
|
|
|
content, res, err = utils.HttpRequest(s.Domain, s.Method, nil, nil, nil, timeout)
|
2018-07-01 10:24:35 +00:00
|
|
|
}
|
2018-06-30 22:37:01 +00:00
|
|
|
if err != nil {
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("HTTP Error %v", err))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-06-30 22:37:01 +00:00
|
|
|
return s
|
|
|
|
}
|
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 {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("HTTP Error %v", err))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
return s
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2018-11-25 10:18:21 +00:00
|
|
|
s.LastResponse = string(content)
|
|
|
|
s.LastStatusCode = res.StatusCode
|
2018-08-30 04:49:44 +00:00
|
|
|
|
2018-11-07 05:06:44 +00:00
|
|
|
if s.Expected.String != "" {
|
2018-08-30 04:49:44 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Log(2, err)
|
|
|
|
}
|
2018-11-25 10:18:21 +00:00
|
|
|
match, err := regexp.MatchString(s.Expected.String, string(content))
|
2018-07-01 10:24:35 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Log(2, err)
|
|
|
|
}
|
2018-06-10 01:31:13 +00:00
|
|
|
if !match {
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
return s
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-25 10:18:21 +00:00
|
|
|
if s.ExpectedStatus != res.StatusCode {
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-11-25 10:18:21 +00:00
|
|
|
recordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", res.StatusCode, s.ExpectedStatus))
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
return s
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2018-06-10 03:44:47 +00:00
|
|
|
s.Online = true
|
2018-08-19 00:37:00 +00:00
|
|
|
if record {
|
2018-09-25 07:03:49 +00:00
|
|
|
recordSuccess(s)
|
2018-08-19 00:37:00 +00:00
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// Check will run checkHttp for HTTP services and checkTcp for TCP services
|
|
|
|
func (s *Service) Check(record bool) {
|
|
|
|
switch s.Type {
|
|
|
|
case "http":
|
|
|
|
s.checkHttp(record)
|
2018-10-07 02:44:31 +00:00
|
|
|
case "tcp", "udp":
|
2018-09-10 22:16:23 +00:00
|
|
|
s.checkTcp(record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:03:49 +00:00
|
|
|
// recordSuccess will create a new 'hit' record in the database for a successful/online service
|
|
|
|
func recordSuccess(s *Service) {
|
2018-06-15 04:30:10 +00:00
|
|
|
s.Online = true
|
2018-11-19 07:13:53 +00:00
|
|
|
s.LastOnline = utils.Timezoner(time.Now().UTC(), CoreApp.Timezone)
|
2018-09-10 09:01:04 +00:00
|
|
|
hit := &types.Hit{
|
2018-09-05 10:54:57 +00:00
|
|
|
Service: s.Id,
|
|
|
|
Latency: s.Latency,
|
2018-10-03 03:48:40 +00:00
|
|
|
PingTime: s.PingTime,
|
2018-09-05 10:54:57 +00:00
|
|
|
CreatedAt: time.Now(),
|
2018-06-15 04:30:10 +00:00
|
|
|
}
|
2018-10-03 03:48:40 +00:00
|
|
|
utils.Log(1, fmt.Sprintf("Service %v Successful Response: %0.2f ms | Lookup in: %0.2f ms", s.Name, hit.Latency*1000, hit.PingTime*1000))
|
2018-09-10 09:01:04 +00:00
|
|
|
s.CreateHit(hit)
|
2018-09-12 04:14:22 +00:00
|
|
|
notifier.OnSuccess(s.Service)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 19:03:55 +00:00
|
|
|
// recordFailure will create a new 'Failure' record in the database for a offline service
|
2018-09-25 07:03:49 +00:00
|
|
|
func recordFailure(s *Service, issue string) {
|
2018-06-15 04:30:10 +00:00
|
|
|
s.Online = false
|
2018-12-06 19:03:55 +00:00
|
|
|
fail := &Failure{&types.Failure{
|
2018-09-10 09:01:04 +00:00
|
|
|
Service: s.Id,
|
|
|
|
Issue: issue,
|
2018-10-03 03:48:40 +00:00
|
|
|
PingTime: s.PingTime,
|
2018-09-10 09:01:04 +00:00
|
|
|
CreatedAt: time.Now(),
|
2018-11-08 10:50:06 +00:00
|
|
|
}}
|
2018-10-03 03:48:40 +00:00
|
|
|
utils.Log(2, fmt.Sprintf("Service %v Failing: %v | Lookup in: %0.2f ms", s.Name, issue, fail.PingTime*1000))
|
2018-09-10 09:01:04 +00:00
|
|
|
s.CreateFailure(fail)
|
2018-11-08 10:50:06 +00:00
|
|
|
notifier.OnFailure(s.Service, fail.Failure)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|