pull/57/head
Hunter Long 2018-08-20 18:22:42 -07:00
parent fb15c5cfe3
commit b2f07a146f
11 changed files with 67 additions and 42 deletions

1
.gitignore vendored
View File

@ -22,3 +22,4 @@ source/rice-box.go
sass
.DS_Store
source/css/base.css.map
/dev/test/node_modules/

View File

@ -11,6 +11,8 @@ go_import_path: github.com/hunterlong/statup
cache:
directories:
- $GOPATH/pkg/dep
- ~/.npm
- ~/.cache
sudo: required

View File

@ -144,8 +144,8 @@ func RunOnce() {
if err != nil {
utils.Log(4, err)
}
for _, s := range core.CoreApp.DbServices {
out := core.ServiceCheck(core.ReturnService(s), true)
for _, s := range core.CoreApp.Services() {
out := s.Check(true)
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", out.Name, out.Domain, (out.Latency * 1000), out.Online)
}
}

View File

@ -104,7 +104,7 @@ func TestRunAll(t *testing.T) {
t.Run(dbt+" Select Users", func(t *testing.T) {
RunUser_SelectAll(t)
})
t.Run(dbt+" Select DbServices", func(t *testing.T) {
t.Run(dbt+" Select Services", func(t *testing.T) {
RunSelectAllServices(t)
})
t.Run(dbt+" Select One Service", func(t *testing.T) {
@ -353,7 +353,7 @@ func RunOneService_Check(t *testing.T) {
}
func RunService_Create(t *testing.T) {
service := &core.Service{Service: &types.Service{
service := core.ReturnService(&types.Service{
Name: "test service",
Domain: "https://google.com",
ExpectedStatus: 200,
@ -362,7 +362,7 @@ func RunService_Create(t *testing.T) {
Type: "http",
Method: "GET",
Timeout: 30,
}}
})
id, err := service.Create()
assert.Nil(t, err)
assert.Equal(t, int64(6), id)
@ -401,7 +401,7 @@ func RunService_GraphData(t *testing.T) {
}
func RunBadService_Create(t *testing.T) {
service := &core.Service{Service: &types.Service{
service := core.ReturnService(&types.Service{
Name: "Bad Service",
Domain: "https://9839f83h72gey2g29278hd2od2d.com",
ExpectedStatus: 200,
@ -410,7 +410,7 @@ func RunBadService_Create(t *testing.T) {
Type: "http",
Method: "GET",
Timeout: 30,
}}
})
id, err := service.Create()
assert.Nil(t, err)
assert.Equal(t, int64(7), id)
@ -421,7 +421,7 @@ func RunBadService_Check(t *testing.T) {
assert.NotNil(t, service)
assert.Equal(t, "Bad Service", service.Name)
for i := 0; i <= 10; i++ {
core.ServiceHTTPCheck(service, true)
service.Check(true)
}
assert.True(t, service.IsRunning())
}
@ -439,18 +439,13 @@ func RunDeleteService(t *testing.T) {
}
func RunCreateService_Hits(t *testing.T) {
services, err := core.CoreApp.SelectAllServices()
assert.Nil(t, err)
services := core.CoreApp.Services()
assert.NotNil(t, services)
assert.Equal(t, 6, len(services))
for i := 0; i <= 15; i++ {
for _, s := range services {
var service *core.Service
if s.Type == "http" {
service = core.ServiceHTTPCheck(core.ReturnService(s), true)
} else {
service = core.ServiceTCPCheck(core.ReturnService(s), true)
}
service = s.Check(true)
assert.NotNil(t, service)
}
}

View File

@ -28,15 +28,12 @@ import (
"time"
)
type FailureData types.FailureData
func CheckServices() {
CoreApp.SelectAllServices()
utils.Log(1, fmt.Sprintf("Starting monitoring process for %v DbServices", len(CoreApp.DbServices)))
for _, ser := range CoreApp.DbServices {
for _, ser := range CoreApp.Services() {
//go obj.StartCheckins()
s := ReturnService(ser)
go s.CheckQueue(true)
go ser.CheckQueue(true)
}
}
@ -51,7 +48,7 @@ CheckLoop:
break CheckLoop
default:
utils.Log(1, fmt.Sprintf("Checking service: %v", s.Name))
ServiceCheck(s, record)
s.Check(record)
// Set next time checkpoint and maybe sleep.
s.Checkpoint = s.Checkpoint.Add(time.Duration(s.Interval) * time.Second)
if sleepDuration := s.Checkpoint.Sub(time.Now()); sleepDuration > 0 {
@ -62,7 +59,7 @@ CheckLoop:
}
}
func DNSCheck(s *Service) (float64, error) {
func (s *Service) dnsCheck() (float64, error) {
t1 := time.Now()
url, err := url.Parse(s.Domain)
if err != nil {
@ -77,7 +74,7 @@ func DNSCheck(s *Service) (float64, error) {
return subTime, err
}
func ServiceTCPCheck(s *Service, record bool) *Service {
func (s *Service) checkTcp(record bool) *Service {
t1 := time.Now()
domain := fmt.Sprintf("%v", s.Domain)
if s.Port != 0 {
@ -105,18 +102,18 @@ func ServiceTCPCheck(s *Service, record bool) *Service {
return s
}
func ServiceCheck(s *Service, record bool) *Service {
func (s *Service) Check(record bool) *Service {
switch s.Type {
case "http":
ServiceHTTPCheck(s, record)
s.checkHttp(record)
case "tcp":
ServiceTCPCheck(s, record)
s.checkTcp(record)
}
return s
}
func ServiceHTTPCheck(s *Service, record bool) *Service {
dnsLookup, err := DNSCheck(s)
func (s *Service) checkHttp(record bool) *Service {
dnsLookup, err := s.dnsCheck()
if err != nil {
if record {
RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))

View File

@ -67,7 +67,7 @@ func TestUpdateAllServices(t *testing.T) {
func TestServiceHTTPCheck(t *testing.T) {
service := SelectService(1)
checked := ServiceCheck(service, true)
checked := service.Check(true)
assert.Equal(t, "Changed Updated Google", checked.Name)
assert.True(t, checked.Online)
}
@ -82,7 +82,7 @@ func TestCheckHTTPService(t *testing.T) {
func TestServiceTCPCheck(t *testing.T) {
service := SelectService(5)
checked := ServiceCheck(service, true)
checked := service.Check(true)
assert.Equal(t, "Changed Google DNS", checked.Name)
assert.True(t, checked.Online)
}
@ -189,7 +189,7 @@ func TestCreateFailingHTTPService(t *testing.T) {
func TestServiceFailedCheck(t *testing.T) {
service := SelectService(7)
checked := ServiceCheck(service, true)
checked := service.Check(true)
assert.Equal(t, "Bad URL", checked.Name)
assert.False(t, checked.Online)
}
@ -213,7 +213,7 @@ func TestCreateFailingTCPService(t *testing.T) {
func TestServiceFailedTCPCheck(t *testing.T) {
service := SelectService(8)
checked := ServiceCheck(service, true)
checked := service.Check(true)
assert.Equal(t, "Bad TCP", checked.Name)
assert.False(t, checked.Online)
}
@ -254,7 +254,36 @@ func TestServiceCloseRoutine(t *testing.T) {
s.Interval = 1
s.Start()
assert.True(t, s.IsRunning())
t.Log(s.Checkpoint)
go s.CheckQueue(false)
t.Log(s.Checkpoint)
time.Sleep(5 * time.Second)
t.Log(s.Checkpoint)
assert.True(t, s.IsRunning())
s.Close()
assert.False(t, s.IsRunning())
s.Close()
assert.False(t, s.IsRunning())
}
func TestServiceCheckQueue(t *testing.T) {
s := ReturnService(new(types.Service))
s.Name = "example"
s.Domain = "https://google.com"
s.Type = "http"
s.Method = "GET"
s.ExpectedStatus = 200
s.Interval = 1
s.Start()
assert.True(t, s.IsRunning())
go s.CheckQueue(false)
go func() {
time.Sleep(5 * time.Second)
t.Log(s.Checkpoint)
time.Sleep(6 * time.Second)
}()
time.Sleep(5 * time.Second)
assert.True(t, s.IsRunning())
s.Close()

View File

@ -19,5 +19,7 @@ ENV IS_DOCKER=true
RUN make dev-deps
RUN make install
WORKDIR /app
VOLUME /app
EXPOSE 8080
ENTRYPOINT make run
ENTRYPOINT statup

View File

@ -76,6 +76,9 @@ type ServiceInterface interface {
SelectHitsGroupBy(string) ([]*Hit, error)
// Go Routines
CheckQueue(bool)
Check(bool) *Service
checkHttp(bool) *Service
checkTcp(bool) *Service
// Checkin functions
AllCheckins() []*Checkin
}

View File

@ -106,7 +106,3 @@ type PluginJSON struct {
Author string `json:"author"`
Namespace string `json:"namespace"`
}
type FailureData struct {
Issue string
}

View File

@ -22,8 +22,8 @@ import (
"net/http"
"os"
"os/signal"
"syscall"
"sync"
"syscall"
)
var (
@ -140,7 +140,7 @@ func GetLastLine() *LogRow {
LockLines.Lock()
defer LockLines.Unlock()
if len(LastLines) > 0 {
return LastLines[len(LastLines) - 1]
return LastLines[len(LastLines)-1]
}
return nil
}

View File

@ -16,13 +16,13 @@
package utils
import (
"time"
"fmt"
"time"
)
type LogRow struct {
Date time.Time
Line interface{}
Date time.Time
Line interface{}
}
func NewLogRow(line interface{}) (logRow *LogRow) {