Add watchdog package

This commit is contained in:
TwinProduction
2019-09-04 19:37:13 -04:00
parent f84f84086d
commit b753443516
13 changed files with 225 additions and 0 deletions

49
watchdog/watchdog.go Normal file
View File

@ -0,0 +1,49 @@
package watchdog
import (
"net"
"net/http"
"net/url"
"time"
)
type Request struct {
Url string
}
type Result struct {
HttpStatus int
Hostname string
Ip string
Duration time.Duration
Errors []error
}
func (request *Request) GetIp(result *Result) {
urlObject, err := url.Parse(request.Url)
if err != nil {
result.Errors = append(result.Errors, err)
return
}
result.Hostname = urlObject.Hostname()
ips, err := net.LookupIP(urlObject.Hostname())
if err != nil {
result.Errors = append(result.Errors, err)
return
}
result.Ip = ips[0].String()
}
func (request *Request) GetStatus(result *Result) {
client := &http.Client{
Timeout: time.Second * 10,
}
startTime := time.Now()
response, err := client.Get(request.Url)
if err != nil {
result.Errors = append(result.Errors, err)
return
}
result.Duration = time.Now().Sub(startTime)
result.HttpStatus = response.StatusCode
}