From ab7e2040e7302de54c21cdf588364be6cfe6ad73 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Wed, 18 Jul 2018 16:01:24 -0700 Subject: [PATCH] http timeout in DB - tcp checker - ui updates --- core/checker.go | 6 ++++-- core/setup.go | 19 ++++++++++++++++--- handlers/services.go | 7 ++++--- main_test.go | 19 +++++++++++++------ source/js/main.js | 20 ++++++++++++++++++++ source/sql/mysql_up.sql | 3 ++- source/sql/mysql_upgrade.sql | 3 +++ source/sql/postgres_up.sql | 3 ++- source/sql/postgres_upgrade.sql | 3 +++ source/sql/sqlite_up.sql | 3 ++- source/sql/sqlite_upgrade.sql | 3 +++ source/tmpl/service.html | 22 ++++++++++++++-------- source/tmpl/services.html | 10 ++++++++-- types/types.go | 2 ++ 14 files changed, 96 insertions(+), 27 deletions(-) diff --git a/core/checker.go b/core/checker.go index d14520c8..628b5049 100644 --- a/core/checker.go +++ b/core/checker.go @@ -59,7 +59,7 @@ func ServiceTCPCheck(s *types.Service) *types.Service { if s.Port != 0 { domain = fmt.Sprintf("%v:%v", s.Domain, s.Port) } - conn, err := net.Dial("tcp", domain) + conn, err := net.DialTimeout("tcp", domain, time.Duration(s.Timeout)*time.Second) if err != nil { RecordFailure(s, fmt.Sprintf("TCP Dial Error %v", err)) return s @@ -93,8 +93,9 @@ func ServiceHTTPCheck(s *types.Service) *types.Service { } s.DnsLookup = dnsLookup t1 := time.Now() + timeout := time.Duration(s.Timeout) client := http.Client{ - Timeout: 30 * time.Second, + Timeout: timeout * time.Second, } var response *http.Response @@ -154,6 +155,7 @@ func RecordSuccess(s *types.Service) { data := HitData{ Latency: s.Latency, } + utils.Log(1, fmt.Sprintf("Service %v Successful: %0.2f ms", s.Name, data.Latency*1000)) CreateServiceHit(s, data) OnSuccess(s) } diff --git a/core/setup.go b/core/setup.go index 80df737a..bea7f69f 100644 --- a/core/setup.go +++ b/core/setup.go @@ -25,27 +25,27 @@ func LoadSampleData() error { Domain: "https://google.com", ExpectedStatus: 200, Interval: 10, - Port: 0, Type: "http", Method: "GET", + Timeout: 10, } s2 := &types.Service{ Name: "Statup Github", Domain: "https://github.com/hunterlong/statup", ExpectedStatus: 200, Interval: 30, - Port: 0, Type: "http", Method: "GET", + Timeout: 20, } s3 := &types.Service{ Name: "JSON Users Test", Domain: "https://jsonplaceholder.typicode.com/users", ExpectedStatus: 200, Interval: 60, - Port: 443, Type: "http", Method: "GET", + Timeout: 30, } s4 := &types.Service{ Name: "JSON API Tester", @@ -56,6 +56,15 @@ func LoadSampleData() error { Type: "http", Method: "POST", PostData: `{ "title": "statup", "body": "bar", "userId": 19999 }`, + Timeout: 30, + } + s5 := &types.Service{ + Name: "Postgres TCP Check", + Domain: "0.0.0.0", + Interval: 20, + Type: "tcp", + Port: 5432, + Timeout: 120, } id, err := CreateService(s1) if err != nil { @@ -73,6 +82,10 @@ func LoadSampleData() error { if err != nil { utils.Log(3, fmt.Sprintf("Error creating Service %v: %v", id, err)) } + id, err = CreateService(s5) + if err != nil { + utils.Log(3, fmt.Sprintf("Error creating TCP Service %v: %v", id, err)) + } //checkin := &Checkin{ // Service: s2.Id, diff --git a/handlers/services.go b/handlers/services.go index 0e7bea86..f2a741d5 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -38,6 +38,7 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) { status, _ := strconv.Atoi(r.PostForm.Get("expected_status")) interval, _ := strconv.Atoi(r.PostForm.Get("interval")) port, _ := strconv.Atoi(r.PostForm.Get("port")) + timeout, _ := strconv.Atoi(r.PostForm.Get("timeout")) checkType := r.PostForm.Get("check_type") postData := r.PostForm.Get("post_data") @@ -51,6 +52,7 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) { Type: checkType, Port: port, PostData: postData, + Timeout: timeout, } _, err := core.CreateService(service) if err != nil { @@ -78,9 +80,6 @@ func ServicesDeleteHandler(w http.ResponseWriter, r *http.Request) { func ServicesViewHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) serv := core.SelectService(utils.StringInt(vars["id"])) - - fmt.Println(serv.ToService()) - ExecuteResponse(w, r, "service.html", serv) } @@ -116,6 +115,7 @@ func ServicesUpdateHandler(w http.ResponseWriter, r *http.Request) { status, _ := strconv.Atoi(r.PostForm.Get("expected_status")) interval, _ := strconv.Atoi(r.PostForm.Get("interval")) port, _ := strconv.Atoi(r.PostForm.Get("port")) + timeout, _ := strconv.Atoi(r.PostForm.Get("timeout")) checkType := r.PostForm.Get("check_type") postData := r.PostForm.Get("post_data") serviceUpdate := &types.Service{ @@ -129,6 +129,7 @@ func ServicesUpdateHandler(w http.ResponseWriter, r *http.Request) { Type: checkType, Port: port, PostData: postData, + Timeout: timeout, } service = core.UpdateService(serviceUpdate) ExecuteResponse(w, r, "service.html", service) diff --git a/main_test.go b/main_test.go index 7850b93b..f3f54368 100644 --- a/main_test.go +++ b/main_test.go @@ -249,7 +249,7 @@ func RunSelectAllMysqlServices(t *testing.T) { var err error services, err := core.SelectAllServices() assert.Nil(t, err) - assert.Equal(t, 4, len(services)) + assert.Equal(t, 5, len(services)) } func RunSelectAllMysqlCommunications(t *testing.T) { @@ -320,7 +320,7 @@ func RunSelectAllServices(t *testing.T) { var err error services, err := core.SelectAllServices() assert.Nil(t, err) - assert.Equal(t, 4, len(services)) + assert.Equal(t, 5, len(services)) } func RunOneService_Check(t *testing.T) { @@ -339,10 +339,11 @@ func RunService_Create(t *testing.T) { Port: 0, Type: "http", Method: "GET", + Timeout: 30, } id, err := core.CreateService(service) assert.Nil(t, err) - assert.Equal(t, int64(5), id) + assert.Equal(t, int64(6), id) t.Log(service) } @@ -379,10 +380,11 @@ func RunBadService_Create(t *testing.T) { Port: 0, Type: "http", Method: "GET", + Timeout: 30, } id, err := core.CreateService(service) assert.Nil(t, err) - assert.Equal(t, int64(6), id) + assert.Equal(t, int64(7), id) } func RunBadService_Check(t *testing.T) { @@ -405,7 +407,12 @@ func RunCreateService_Hits(t *testing.T) { assert.NotNil(t, services) for i := 0; i <= 10; i++ { for _, s := range services { - service := core.ServiceCheck(s.ToService()) + var service *types.Service + if s.ToService().Type == "http" { + service = core.ServiceHTTPCheck(s.ToService()) + } else { + service = core.ServiceTCPCheck(s.ToService()) + } assert.NotNil(t, service) } } @@ -459,7 +466,7 @@ func RunPrometheusHandler(t *testing.T) { rr := httptest.NewRecorder() route.ServeHTTP(rr, req) t.Log(rr.Body.String()) - assert.True(t, strings.Contains(rr.Body.String(), "statup_total_services 5")) + assert.True(t, strings.Contains(rr.Body.String(), "statup_total_services 6")) } func RunFailingPrometheusHandler(t *testing.T) { diff --git a/source/js/main.js b/source/js/main.js index 9d1d0154..ecc2cbc8 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -10,6 +10,26 @@ $('form').submit(function() { $(this).find("button[type='submit']").prop('disabled',true); }); +$('select#service_type').on('change', function() { + var selected = $('#service_type option:selected').val(); + if (selected == "tcp") { + $("#service_port").parent().parent().removeClass("d-none"); + $("#service_check_type").parent().parent().addClass("d-none"); + + $("#post_data").parent().parent().addClass("d-none"); + $("#service_response").parent().parent().addClass("d-none"); + $("#service_response_code").parent().parent().addClass("d-none"); + } else { + $("#post_data").parent().parent().removeClass("d-none"); + $("#service_response").parent().parent().removeClass("d-none"); + $("#service_response_code").parent().parent().removeClass("d-none"); + $("#service_check_type").parent().parent().removeClass("d-none"); + + $("#service_port").parent().parent().addClass("d-none"); + } + +}); + $(".confirm-btn").on('click', function() { var r = confirm("Are you sure you want to delete?"); diff --git a/source/sql/mysql_up.sql b/source/sql/mysql_up.sql index f3ef7f70..c87a7a29 100644 --- a/source/sql/mysql_up.sql +++ b/source/sql/mysql_up.sql @@ -34,8 +34,9 @@ CREATE TABLE services ( expected_status INT(6), check_interval int(11), post_data text, - order_id integer, + order_id integer default 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + timeout INT(6) DEFAULT 30, INDEX (id) ); CREATE TABLE hits ( diff --git a/source/sql/mysql_upgrade.sql b/source/sql/mysql_upgrade.sql index 2dc303ef..1401a1af 100644 --- a/source/sql/mysql_upgrade.sql +++ b/source/sql/mysql_upgrade.sql @@ -1,3 +1,6 @@ +=========================================== 1531891670 +ALTER TABLE services ALTER COLUMN order_id SET DEFAULT 0; +ALTER TABLE services ADD COLUMN timeout integer DEFAULT 30; =========================================== 1530841150 ALTER TABLE core ADD COLUMN use_cdn BOOL NOT NULL DEFAULT '0'; =========================================== 1 diff --git a/source/sql/postgres_up.sql b/source/sql/postgres_up.sql index 755835e7..b6fc3132 100644 --- a/source/sql/postgres_up.sql +++ b/source/sql/postgres_up.sql @@ -34,7 +34,8 @@ CREATE TABLE services ( expected_status integer, check_interval integer, post_data text, - order_id integer, + order_id integer default 0, + timeout integer default 30, created_at TIMESTAMP ); diff --git a/source/sql/postgres_upgrade.sql b/source/sql/postgres_upgrade.sql index 1cf914f6..34f08e36 100644 --- a/source/sql/postgres_upgrade.sql +++ b/source/sql/postgres_upgrade.sql @@ -1,3 +1,6 @@ +=========================================== 1531891670 +ALTER TABLE services ALTER COLUMN order_id SET DEFAULT 0; +ALTER TABLE services ADD COLUMN timeout integer DEFAULT 30; =========================================== 1530841150 ALTER TABLE core ADD COLUMN use_cdn bool DEFAULT FALSE; =========================================== 1 diff --git a/source/sql/sqlite_up.sql b/source/sql/sqlite_up.sql index 2516e14d..07876c2d 100644 --- a/source/sql/sqlite_up.sql +++ b/source/sql/sqlite_up.sql @@ -35,7 +35,8 @@ CREATE TABLE services ( expected_status integer, check_interval integer, post_data text, - order_id integer, + order_id integer default 0, + timeout integer default 30, created_at DATETIME ); diff --git a/source/sql/sqlite_upgrade.sql b/source/sql/sqlite_upgrade.sql index 50acbd3a..d9749d07 100644 --- a/source/sql/sqlite_upgrade.sql +++ b/source/sql/sqlite_upgrade.sql @@ -1,3 +1,6 @@ +=========================================== 1531891670 +ALTER TABLE services ALTER COLUMN order_id SET DEFAULT 0; +ALTER TABLE services ADD COLUMN timeout integer DEFAULT 30; =========================================== 1530841150 ALTER TABLE core ADD COLUMN use_cdn bool DEFAULT FALSE; =========================================== 1 diff --git a/source/tmpl/service.html b/source/tmpl/service.html index b2105971..90a560ea 100644 --- a/source/tmpl/service.html +++ b/source/tmpl/service.html @@ -104,7 +104,7 @@ -
+
-
- +
+
-
+
-
+
-
+
@@ -143,6 +143,12 @@
+
+ +
+ +
+
@@ -155,7 +161,7 @@
-
+

Last Response

@@ -168,7 +174,7 @@
-
+

Service Checkins

{{ range $s.Checkins }}
Check #{{.Id}} Checked in {{.Ago}}
diff --git a/source/tmpl/services.html b/source/tmpl/services.html index 83c4f5f5..4ba57f2c 100644 --- a/source/tmpl/services.html +++ b/source/tmpl/services.html @@ -97,10 +97,10 @@
- +
-
+
@@ -112,6 +112,12 @@
+
+ +
+ +
+
diff --git a/types/types.go b/types/types.go index a767b682..2993d735 100644 --- a/types/types.go +++ b/types/types.go @@ -79,6 +79,8 @@ type Service struct { PostData string `db:"post_data" json:"post_data"` Port int `db:"port" json:"port"` CreatedAt time.Time `db:"created_at" json:"created_at"` + Timeout int `db:"timeout" json:"timeout"` + Order int `db:"order_id" json:"order_id"` Online bool `json:"online"` Latency float64 `json:"latency"` Online24Hours float32 `json:"24_hours_online"`