Skip to content

Commit

Permalink
Merge d0ad554 into 1f0e587
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Jul 17, 2018
2 parents 1f0e587 + d0ad554 commit 3964226
Show file tree
Hide file tree
Showing 43 changed files with 829 additions and 545 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.idea
rice-box.go
./rice-box.go
config.yml
statup.db
plugins/*.so
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:

env:
global:
- VERSION=0.29.9
- VERSION=0.30
- DB_HOST=localhost
- DB_USER=travis
- DB_PASS=
Expand Down
48 changes: 25 additions & 23 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/plugin"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/joho/godotenv"
"io/ioutil"
Expand Down Expand Up @@ -106,7 +106,7 @@ func RunOnce() {
utils.Log(4, err)
}
for _, s := range core.CoreApp.Services {
out := s.Check()
out := core.ServiceCheck(s.ToService())
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", out.Name, out.Domain, (out.Latency * 1000), out.Online)
}
}
Expand All @@ -127,7 +127,7 @@ func HelpEcho() {
fmt.Println("Give Statup a Star at https://github.com/hunterlong/statup")
}

func TestPlugin(plug plugin.PluginActions) {
func TestPlugin(plug types.PluginActions) {
defer utils.DeleteFile("./.plugin_test.db")
RenderBoxes()

Expand All @@ -148,41 +148,41 @@ func TestPlugin(plug plugin.PluginActions) {
core.OnLoad(core.DbSession)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnSuccess(Service)'")
core.OnSuccess(core.SelectService(1))
core.OnSuccess(core.SelectService(1).ToService())
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnFailure(Service, FailureData)'")
fakeFailD := core.FailureData{
Issue: "No issue, just testing this plugin. This would include HTTP failure information though",
}
core.OnFailure(core.SelectService(1), fakeFailD)
core.OnFailure(core.SelectService(1).ToService(), fakeFailD)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnSettingsSaved(Core)'")
fmt.Println(BRAKER)
core.OnSettingsSaved(core.CoreApp)
core.OnSettingsSaved(core.CoreApp.ToCore())
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnNewService(Service)'")
core.OnNewService(core.SelectService(2))
core.OnNewService(core.SelectService(2).ToService())
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnNewUser(User)'")
user, _ := core.SelectUser(1)
core.OnNewUser(user)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnUpdateService(Service)'")
srv := core.SelectService(2)
srv := core.SelectService(2).ToService()
srv.Type = "http"
srv.Domain = "https://yahoo.com"
core.OnUpdateService(srv)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnDeletedService(Service)'")
core.OnDeletedService(core.SelectService(1))
core.OnDeletedService(core.SelectService(1).ToService())
fmt.Println("\n" + BRAKER)
}

func FakeSeed(plug plugin.PluginActions) {
func FakeSeed(plug types.PluginActions) {
var err error
core.CoreApp = core.NewCore()

core.CoreApp.AllPlugins = []plugin.PluginActions{plug}
core.CoreApp.AllPlugins = []types.PluginActions{plug}

fmt.Printf("\n" + BRAKER)

Expand Down Expand Up @@ -212,57 +212,59 @@ func FakeSeed(plug plugin.PluginActions) {
core.CoreApp.ApiSecret = "0x0x0x0x0"
core.CoreApp.ApiKey = "abcdefg12345"

fakeSrv := &core.Service{
fakeSrv := &types.Service{
Name: "Test Plugin Service",
Domain: "https://google.com",
Method: "GET",
}
fakeSrv.Create()
core.CreateService(fakeSrv)

fakeSrv2 := &core.Service{
fakeSrv2 := &types.Service{
Name: "Awesome Plugin Service",
Domain: "https://netflix.com",
Method: "GET",
}
fakeSrv2.Create()
core.CreateService(fakeSrv2)

fakeUser := &core.User{
fakeUser := &types.User{
Id: 6334,
Username: "Bulbasaur",
Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
Email: "info@testdomain.com",
Admin: true,
CreatedAt: time.Now(),
}
fakeUser.Create()
core.CreateUser(fakeUser)

fakeUser = &core.User{
fakeUser = &types.User{
Id: 6335,
Username: "Billy",
Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
Email: "info@awesome.com",
CreatedAt: time.Now(),
}
fakeUser.Create()
core.CreateUser(fakeUser)

for i := 0; i <= 50; i++ {
dd := core.HitData{
Latency: rand.Float64(),
}
fakeSrv.CreateHit(dd)
core.CreateServiceHit(fakeSrv, dd)

dd = core.HitData{
Latency: rand.Float64(),
}
fakeSrv2.CreateHit(dd)
core.CreateServiceHit(fakeSrv2, dd)

fail := core.FailureData{
Issue: "This is not an issue, but it would container HTTP response errors.",
}
fakeSrv.CreateFailure(fail)
core.CreateServiceFailure(fakeSrv, fail)

fail = core.FailureData{
Issue: "HTTP Status Code 521 did not match 200",
}
fakeSrv2.CreateFailure(fail)
core.CreateServiceFailure(fakeSrv, fail)
}

fmt.Println("Seeding example data is complete, running Plugin Tests")
Expand Down
4 changes: 2 additions & 2 deletions core/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func CreateAllAssets() {
utils.Log(1, "Inserting scss, css, emails, and javascript files into assets..")
CopyToPublic(ScssBox, "scss", "base.scss")
CopyToPublic(ScssBox, "scss", "variables.scss")
CopyToPublic(EmailBox, "emails", "message.html")
CopyToPublic(EmailBox, "emails", "failure.html")
//CopyToPublic(EmailBox, "emails", "message.html")
//CopyToPublic(EmailBox, "emails", "failure.html")
CopyToPublic(CssBox, "css", "bootstrap.min.css")
CopyToPublic(JsBox, "js", "bootstrap.min.js")
CopyToPublic(JsBox, "js", "Chart.bundle.min.js")
Expand Down
42 changes: 21 additions & 21 deletions core/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ type FailureData types.FailureData
func CheckServices() {
CoreApp.Services, _ = SelectAllServices()
utils.Log(1, fmt.Sprintf("Starting monitoring process for %v Services", len(CoreApp.Services)))
for _, v := range CoreApp.Services {
obj := v
for _, ser := range CoreApp.Services {
s := ser.ToService()
//go obj.StartCheckins()
obj.stopRoutine = make(chan struct{})
go obj.CheckQueue()
s.StopRoutine = make(chan struct{})
go CheckQueue(s)
}
}

func (s *Service) CheckQueue() {
func CheckQueue(s *types.Service) {
for {
select {
case <-s.stopRoutine:
case <-s.StopRoutine:
return
default:
s.Check()
ServiceCheck(s)
if s.Interval < 1 {
s.Interval = 1
}
Expand All @@ -43,7 +43,7 @@ func (s *Service) CheckQueue() {
}
}

func (s *Service) DNSCheck() (float64, error) {
func DNSCheck(s *types.Service) (float64, error) {
t1 := time.Now()
url, err := url.Parse(s.Domain)
if err != nil {
Expand All @@ -58,13 +58,13 @@ func (s *Service) DNSCheck() (float64, error) {
return subTime, err
}

func (s *Service) Check() *Service {
dnsLookup, err := s.DNSCheck()
func ServiceCheck(s *types.Service) *types.Service {
dnsLookup, err := DNSCheck(s)
if err != nil {
s.Failure(fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
return s
}
s.dnsLookup = dnsLookup
s.DnsLookup = dnsLookup
t1 := time.Now()
client := http.Client{
Timeout: 30 * time.Second,
Expand All @@ -77,14 +77,14 @@ func (s *Service) Check() *Service {
response, err = client.Get(s.Domain)
}
if err != nil {
s.Failure(fmt.Sprintf("HTTP Error %v", err))
RecordFailure(s, fmt.Sprintf("HTTP Error %v", err))
return s
}
response.Header.Set("User-Agent", "StatupMonitor")
t2 := time.Now()
s.Latency = t2.Sub(t1).Seconds()
if err != nil {
s.Failure(fmt.Sprintf("HTTP Error %v", err))
RecordFailure(s, fmt.Sprintf("HTTP Error %v", err))
return s
}
defer response.Body.Close()
Expand All @@ -100,44 +100,44 @@ func (s *Service) Check() *Service {
if !match {
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
s.Failure(fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected))
RecordFailure(s, fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected))
return s
}
}
if s.ExpectedStatus != response.StatusCode {
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
s.Failure(fmt.Sprintf("HTTP Status Code %v did not match %v", response.StatusCode, s.ExpectedStatus))
RecordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", response.StatusCode, s.ExpectedStatus))
return s
}
s.LastResponse = string(contents)
s.LastStatusCode = response.StatusCode
s.Online = true
s.Record(response)
RecordSuccess(s, response)
return s
}

type HitData struct {
Latency float64
}

func (s *Service) Record(response *http.Response) {
func RecordSuccess(s *types.Service, response *http.Response) {
s.Online = true
s.LastOnline = time.Now()
data := HitData{
Latency: s.Latency,
}
s.CreateHit(data)
CreateServiceHit(s, data)
OnSuccess(s)
}

func (s *Service) Failure(issue string) {
func RecordFailure(s *types.Service, issue string) {
s.Online = false
data := FailureData{
Issue: issue,
}
utils.Log(2, fmt.Sprintf("Service %v Failing: %v", s.Name, issue))
s.CreateFailure(data)
CreateServiceFailure(s, data)
//SendFailureEmail(s)
OnFailure(s, data)
}
70 changes: 35 additions & 35 deletions core/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func (c *Checkin) String() string {
return c.Api
}

func FindCheckin(api string) *Checkin {
for _, s := range CoreApp.Services {
for _, c := range s.Checkins {
func FindCheckin(api string) *types.Checkin {
for _, ser := range CoreApp.Services {
for _, c := range ser.ToService().Checkins {
if c.Api == api {
return c
}
Expand All @@ -25,8 +25,8 @@ func FindCheckin(api string) *Checkin {
return nil
}

func (s *Service) SelectAllCheckins() []*Checkin {
var checkins []*Checkin
func SelectAllCheckins(s *types.Service) []*types.Checkin {
var checkins []*types.Checkin
col := DbSession.Collection("checkins").Find("service", s.Id).OrderBy("-id")
col.All(&checkins)
s.Checkins = checkins
Expand Down Expand Up @@ -78,33 +78,33 @@ func (f *Checkin) Ago() string {
return got
}

func (c *Checkin) Run() {
if c.Interval == 0 {
return
}
fmt.Println("checking: ", c.Api)
between := time.Now().Sub(c.Last).Seconds()
if between > float64(c.Interval) {
guard := make(chan struct{})
c.RecheckCheckinFailure(guard)
<-guard
}
time.Sleep(1 * time.Second)
c.Run()
}

func (s *Service) StartCheckins() {
for _, c := range s.Checkins {
checkin := c
go checkin.Run()
}
}

func CheckinProcess() {
for _, s := range CoreApp.Services {
for _, c := range s.Checkins {
checkin := c
go checkin.Run()
}
}
}
//func (c *Checkin) Run() {
// if c.Interval == 0 {
// return
// }
// fmt.Println("checking: ", c.Api)
// between := time.Now().Sub(c.Last).Seconds()
// if between > float64(c.Interval) {
// guard := make(chan struct{})
// c.RecheckCheckinFailure(guard)
// <-guard
// }
// time.Sleep(1 * time.Second)
// c.Run()
//}
//
//func (s *Service) StartCheckins() {
// for _, c := range s.Checkins {
// checkin := c.(*Checkin)
// go checkin.Run()
// }
//}
//
//func CheckinProcess() {
// for _, s := range CoreApp.Services {
// for _, c := range s.Checkins {
// checkin := c
// go checkin.Run()
// }
// }
//}

0 comments on commit 3964226

Please sign in to comment.