statusmon/core/healthcheck_httpstatus.go

43 lines
780 B
Go

package core
import (
"fmt"
"net/http"
)
var checkHTTPStatus = checkType{
name: "http-status",
toString: func(r CheckResult) string {
return fmt.Sprintf("Bad HTTPStatus %s on GET %s", r.Value, r.Target)
},
checkFunc: func(e Check) (CheckResult, error) {
result := CheckResult{
Check: e,
Status: CheckOk,
}
res, err := http.Get(e.Target)
if err != nil {
result.Status = CheckErr
result.Value = err.Error()
return result, nil
}
res.Body.Close()
result.Value = res.Status
var thresh int = 400
if e.Threshold != nil {
var ok bool
if thresh, ok = e.Threshold.(int); !ok {
return result, fmt.Errorf("threshold must be an int")
}
}
if res.StatusCode >= thresh {
result.Status = CheckErr
}
return result, nil
},
}