Skip to content

Commit

Permalink
Added duration
Browse files Browse the repository at this point in the history
  • Loading branch information
danman113 committed Sep 30, 2017
1 parent 8340136 commit 6b916f9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
25 changes: 20 additions & 5 deletions ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,40 @@ 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)
}
}

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
}
}
44 changes: 22 additions & 22 deletions sampleconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand Down
15 changes: 10 additions & 5 deletions site/site.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package site

import (
"time"
)

type Website struct {
Url string `json: url`
Interval int `json: interval`
Pages []Page `json: pages`
}

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
}

0 comments on commit 6b916f9

Please sign in to comment.