cfha/checks/http.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

2015-08-23 03:20:58 +00:00
package checks
import (
"net/http"
"../core"
"time"
"log"
"fmt"
)
func NewHttpChecker(config core.CheckCreateConfig) *core.GenericCheck {
return core.NewGenericCheck(&HttpChecker{
2015-08-23 03:20:58 +00:00
&http.Client{},
config,
config.Host.Type + "://" + config.Host.Host + "/",
})
2015-08-23 03:20:58 +00:00
}
type HttpChecker struct {
client *http.Client
config core.CheckCreateConfig
endpoint string
}
func (this *HttpChecker) Check() time.Duration {
this.config.Engine.Input<- core.Result{
this.config.Host.Host,
this.check(),
2015-08-23 03:20:58 +00:00
}
return this.config.Interval
}
func (this *HttpChecker) Stop() bool {
return true
2015-08-23 03:20:58 +00:00
}
func (this *HttpChecker) check() core.Status {
req, err := http.NewRequest("GET", this.endpoint, nil)
if err != nil {
log.Print(fmt.Sprintf("Stopping: %v due to http NewRequest error\n", this.config))
return core.Unknown
}
req.Header.Set("Host", this.config.Host.Options["hostname"])
return this.determineHttpCheckStatus(this.client.Do(req))
}
func (this *HttpChecker) determineHttpCheckStatus(res *http.Response, err error) core.Status {
if err != nil || res.StatusCode != 200 {
return core.Down
}
return core.Up
}