From 6b916f9e9dd84c6b614addcc1686cb3a2b1e08d0 Mon Sep 17 00:00:00 2001 From: danman113 Date: Sat, 30 Sep 2017 16:56:51 -0400 Subject: [PATCH] Added duration --- ping/ping.go | 25 ++++++++++++++++++++----- sampleconfig.json | 44 ++++++++++++++++++++++---------------------- site/site.go | 15 ++++++++++----- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/ping/ping.go b/ping/ping.go index c091bc7..a490784 100644 --- a/ping/ping.go +++ b/ping/ping.go @@ -35,19 +35,24 @@ func PingStatus(site *site.Page, resChan chan error) { return } fmt.Printf("\t %s = %d\n", site.Url, site.Status) + resChan <- nil } func PingWebsite(website *site.Website) { for { fmt.Println(website.Url) - for _, page := range website.Pages { + for i, _ := range website.Pages { ret := make(chan error) - go PingStatus(&page, ret) + go PingStatus(&(website.Pages[i]), ret) for e := range ret { - handleError(e, &page) + if e != nil { + handleError(e, &(website.Pages[i])) + } else { + website.Pages[i].DownSince = nil + } } } - time.Sleep(time.Duration(website.Interval) * time.Millisecond) + time.Sleep(time.Duration(website.Interval) * time.Second) } } @@ -55,5 +60,15 @@ func handleError(e error, pg *site.Page) { fmt.Println("Error: ") fmt.Println(e) fmt.Println(*pg) - email.SendAlert(e, pg) + if pg.DownSince != nil { + since := time.Since(*pg.DownSince) + if since.Seconds() > float64(pg.Duration) { + fmt.Println("Sent Email") + pg.DownSince = nil + email.SendAlert(e, pg) + } + } else { + now := time.Now() + pg.DownSince = &now + } } diff --git a/sampleconfig.json b/sampleconfig.json index 2e8a521..a1553f5 100644 --- a/sampleconfig.json +++ b/sampleconfig.json @@ -10,74 +10,74 @@ "sites": [ { "url": "www.dberezin.com", - "interval": 60000, + "interval": 60, "pages": [ { "status": 200, "url": "https://dberezin.com", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "http://dberezin.com", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "https://dberezin.com/portfolio", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "https://dberezin.com/about", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "https://dberezin.com/static/pdf/Resume.pdf", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 } ] }, { "url": "www.viviannghiem.com", - "interval": 60000, + "interval": 60, "pages": [ { "status": 200, "url": "http://viviannghiem.com", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "http://viviannghiem.com/pages/about", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "http://viviannghiem.com/pages/portfolio", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 200, "url": "http://viviannghiem.com/pages/", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 }, { "status": 404, "url": "http://viviannghiem.com/pages/adsfasfdgsdf", - "timeout": 2000, + "timeout": 5000, "method": "GET", - "duration": 10000 + "duration": 100 } ] } diff --git a/site/site.go b/site/site.go index fd3290f..4d855ab 100644 --- a/site/site.go +++ b/site/site.go @@ -1,5 +1,9 @@ package site +import ( + "time" +) + type Website struct { Url string `json: url` Interval int `json: interval` @@ -7,9 +11,10 @@ type Website struct { } type Page struct { - Url string `json: url` - Status int `json: status` - Method string `json: method` - Timeout int `json: timeout` - Duration int `json: duration` + Url string `json: url` + Status int `json: status` + Method string `json: method` + Timeout int `json: timeout` + Duration int `json: duration` + DownSince *time.Time }