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
|
|
|
|
2018-06-11 00:20:42 +00:00
|
|
|
import (
|
2018-06-29 05:32:50 +00:00
|
|
|
"fmt"
|
2018-06-22 04:02:57 +00:00
|
|
|
"github.com/ararog/timeago"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/types"
|
|
|
|
"github.com/hunterlong/statping/utils"
|
2018-11-14 03:05:36 +00:00
|
|
|
"sort"
|
2018-06-29 05:32:50 +00:00
|
|
|
"strings"
|
2018-06-11 00:20:42 +00:00
|
|
|
"time"
|
|
|
|
)
|
2018-06-10 01:31:13 +00:00
|
|
|
|
2018-12-06 19:03:55 +00:00
|
|
|
type Failure struct {
|
2018-08-20 07:20:05 +00:00
|
|
|
*types.Failure
|
|
|
|
}
|
|
|
|
|
2018-11-09 04:10:52 +00:00
|
|
|
const (
|
2018-11-14 03:05:36 +00:00
|
|
|
limitedFailures = 32
|
2018-11-09 04:10:52 +00:00
|
|
|
)
|
|
|
|
|
2018-12-06 19:03:55 +00:00
|
|
|
// CreateFailure will create a new Failure record for a service
|
2018-11-08 10:50:06 +00:00
|
|
|
func (s *Service) CreateFailure(fail types.FailureInterface) (int64, error) {
|
2018-12-06 19:03:55 +00:00
|
|
|
f := fail.(*Failure)
|
2018-08-20 07:20:05 +00:00
|
|
|
f.Service = s.Id
|
2018-09-05 10:54:57 +00:00
|
|
|
row := failuresDB().Create(f)
|
|
|
|
if row.Error != nil {
|
|
|
|
utils.Log(3, row.Error)
|
|
|
|
return 0, row.Error
|
2018-07-03 21:39:56 +00:00
|
|
|
}
|
2018-11-14 03:05:36 +00:00
|
|
|
sort.Sort(types.FailSort(s.Failures))
|
|
|
|
s.Failures = append(s.Failures, f)
|
|
|
|
if len(s.Failures) > limitedFailures {
|
2018-11-09 04:10:52 +00:00
|
|
|
s.Failures = s.Failures[1:]
|
|
|
|
}
|
2018-09-05 10:54:57 +00:00
|
|
|
return f.Id, row.Error
|
2018-06-15 04:30:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// AllFailures will return all failures attached to a service
|
2018-12-06 19:03:55 +00:00
|
|
|
func (s *Service) AllFailures() []*Failure {
|
|
|
|
var fails []*Failure
|
2018-10-07 22:38:56 +00:00
|
|
|
col := failuresDB().Where("service = ?", s.Id).Not("method = 'checkin'").Order("id desc")
|
2018-09-05 10:54:57 +00:00
|
|
|
err := col.Find(&fails)
|
|
|
|
if err.Error != nil {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(3, fmt.Sprintf("Issue getting failures for service %v, %v", s.Name, err))
|
2018-08-20 07:20:05 +00:00
|
|
|
return nil
|
2018-07-03 21:39:56 +00:00
|
|
|
}
|
2018-06-23 00:10:37 +00:00
|
|
|
return fails
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// DeleteFailures will delete all failures for a service
|
2018-10-07 04:48:33 +00:00
|
|
|
func (s *Service) DeleteFailures() {
|
|
|
|
err := DbSession.Exec(`DELETE FROM failures WHERE service = ?`, s.Id)
|
2018-09-05 10:54:57 +00:00
|
|
|
if err.Error != nil {
|
2018-08-23 07:28:48 +00:00
|
|
|
utils.Log(3, fmt.Sprintf("failed to delete all failures: %v", err))
|
2018-06-22 04:02:57 +00:00
|
|
|
}
|
2018-10-07 04:48:33 +00:00
|
|
|
s.Failures = nil
|
2018-06-22 04:02:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 10:50:06 +00:00
|
|
|
// LimitedFailures will return the last amount of failures from a service
|
2018-12-06 19:03:55 +00:00
|
|
|
func (s *Service) LimitedFailures(amount int64) []*Failure {
|
|
|
|
var failArr []*Failure
|
2018-11-21 03:39:40 +00:00
|
|
|
failuresDB().Where("service = ?", s.Id).Not("method = 'checkin'").Order("id desc").Limit(amount).Find(&failArr)
|
2018-07-17 09:18:20 +00:00
|
|
|
return failArr
|
2018-11-28 20:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LimitedFailures will return the last amount of failures from a service
|
2018-12-06 19:03:55 +00:00
|
|
|
func (s *Service) LimitedCheckinFailures(amount int64) []*Failure {
|
|
|
|
var failArr []*Failure
|
2018-11-28 20:01:10 +00:00
|
|
|
failuresDB().Where("service = ?", s.Id).Where("method = 'checkin'").Order("id desc").Limit(amount).Find(&failArr)
|
|
|
|
return failArr
|
2018-06-19 04:48:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 19:03:55 +00:00
|
|
|
// Ago returns a human readable timestamp for a Failure
|
|
|
|
func (f *Failure) Ago() string {
|
2018-06-22 04:02:57 +00:00
|
|
|
got, _ := timeago.TimeAgoWithTime(time.Now(), f.CreatedAt)
|
|
|
|
return got
|
|
|
|
}
|
|
|
|
|
2018-11-08 10:50:06 +00:00
|
|
|
// Select returns a *types.Failure
|
2018-12-06 19:03:55 +00:00
|
|
|
func (f *Failure) Select() *types.Failure {
|
2018-11-08 10:50:06 +00:00
|
|
|
return f.Failure
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:03:55 +00:00
|
|
|
// Delete will remove a Failure record from the database
|
|
|
|
func (f *Failure) Delete() error {
|
2018-09-05 10:54:57 +00:00
|
|
|
db := failuresDB().Delete(f)
|
|
|
|
return db.Error
|
2018-06-19 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// Count24HFailures returns the amount of failures for a service within the last 24 hours
|
2018-09-08 22:16:26 +00:00
|
|
|
func (c *Core) Count24HFailures() uint64 {
|
|
|
|
var count uint64
|
|
|
|
for _, s := range CoreApp.Services {
|
|
|
|
service := s.(*Service)
|
|
|
|
fails, _ := service.TotalFailures24()
|
|
|
|
count += fails
|
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// CountFailures returns the total count of failures for all services
|
2018-06-24 06:17:31 +00:00
|
|
|
func CountFailures() uint64 {
|
2018-09-05 10:54:57 +00:00
|
|
|
var count uint64
|
|
|
|
err := failuresDB().Count(&count)
|
|
|
|
if err.Error != nil {
|
|
|
|
utils.Log(2, err.Error)
|
2018-06-24 06:17:31 +00:00
|
|
|
return 0
|
|
|
|
}
|
2018-09-05 10:54:57 +00:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// TotalFailures24 returns the amount of failures for a service within the last 24 hours
|
2018-09-05 10:54:57 +00:00
|
|
|
func (s *Service) TotalFailures24() (uint64, error) {
|
|
|
|
ago := time.Now().Add(-24 * time.Hour)
|
|
|
|
return s.TotalFailuresSince(ago)
|
2018-06-11 00:20:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// TotalFailures returns the total amount of failures for a service
|
2018-08-19 00:37:00 +00:00
|
|
|
func (s *Service) TotalFailures() (uint64, error) {
|
2018-09-05 10:54:57 +00:00
|
|
|
var count uint64
|
|
|
|
rows := failuresDB().Where("service = ?", s.Id)
|
2018-09-06 05:28:35 +00:00
|
|
|
err := rows.Count(&count)
|
2018-09-05 10:54:57 +00:00
|
|
|
return count, err.Error
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2018-06-10 03:44:47 +00:00
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// TotalFailuresSince returns the total amount of failures for a service since a specific time/date
|
2018-09-05 10:54:57 +00:00
|
|
|
func (s *Service) TotalFailuresSince(ago time.Time) (uint64, error) {
|
|
|
|
var count uint64
|
2018-10-07 22:38:56 +00:00
|
|
|
rows := failuresDB().Where("service = ? AND created_at > ?", s.Id, ago.UTC().Format("2006-01-02 15:04:05")).Not("method = 'checkin'")
|
2018-09-05 10:54:57 +00:00
|
|
|
err := rows.Count(&count)
|
|
|
|
return count, err.Error
|
2018-06-10 03:44:47 +00:00
|
|
|
}
|
2018-06-29 05:32:50 +00:00
|
|
|
|
2018-12-06 19:03:55 +00:00
|
|
|
// ParseError returns a human readable error for a Failure
|
|
|
|
func (f *Failure) ParseError() string {
|
2018-10-07 22:38:56 +00:00
|
|
|
if f.Method == "checkin" {
|
|
|
|
return fmt.Sprintf("Checkin is Offline")
|
|
|
|
}
|
2018-08-30 04:49:44 +00:00
|
|
|
err := strings.Contains(f.Issue, "connection reset by peer")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Connection Reset")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "operation timed out")
|
2018-06-29 05:32:50 +00:00
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("HTTP Request Timed Out")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "x509: certificate is valid")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("SSL Certificate invalid")
|
|
|
|
}
|
2018-07-01 03:54:28 +00:00
|
|
|
err = strings.Contains(f.Issue, "Client.Timeout exceeded while awaiting headers")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Connection Timed Out")
|
|
|
|
}
|
2018-06-29 05:32:50 +00:00
|
|
|
err = strings.Contains(f.Issue, "no such host")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Domain is offline or not found")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "HTTP Status Code")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Incorrect HTTP Status Code")
|
|
|
|
}
|
2018-06-30 00:57:05 +00:00
|
|
|
err = strings.Contains(f.Issue, "connection refused")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Connection Failed")
|
|
|
|
}
|
2018-07-01 03:54:28 +00:00
|
|
|
err = strings.Contains(f.Issue, "can't assign requested address")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Unable to Request Address")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "no route to host")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Domain is offline or not found")
|
|
|
|
}
|
2018-06-29 05:32:50 +00:00
|
|
|
return f.Issue
|
|
|
|
}
|