add support for specifying http-headers and really use Method spezified
This commit is contained in:
@@ -53,9 +53,14 @@ func main() {
|
|||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
for _, mon := range cfg.Monitors {
|
for _, mon := range cfg.Monitors {
|
||||||
cfg.Logger.Printf(" Starting %s: %d seconds check interval\n - %v %s (%d second/s timeout)", mon.Name, mon.CheckInterval, mon.Method, mon.URL, mon.HttpTimeout)
|
cfg.Logger.Printf(" Starting %s: %d seconds check interval - %v %s (%d second/s timeout)", mon.Name, mon.CheckInterval, mon.Method, mon.URL, mon.HttpTimeout)
|
||||||
|
|
||||||
// print features
|
// print features
|
||||||
|
if len(mon.HttpHeaders) > 0 {
|
||||||
|
for _, h := range mon.HttpHeaders {
|
||||||
|
cfg.Logger.Printf(" - Add HTTP-Header '%s' '%s'", h.Name, h.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
if mon.ExpectedStatusCode > 0 {
|
if mon.ExpectedStatusCode > 0 {
|
||||||
cfg.Logger.Printf(" - Expect HTTP %d", mon.ExpectedStatusCode)
|
cfg.Logger.Printf(" - Expect HTTP %d", mon.ExpectedStatusCode)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
"component_id": 1,
|
"component_id": 1,
|
||||||
"interval": 10,
|
"interval": 10,
|
||||||
"timeout": 5,
|
"timeout": 5,
|
||||||
|
"headers": [
|
||||||
|
{
|
||||||
|
"header": "Authorization",
|
||||||
|
"value": "Basic <hash>"
|
||||||
|
}
|
||||||
|
],
|
||||||
"expected_status_code": 200,
|
"expected_status_code": 200,
|
||||||
"strict_tls": true
|
"strict_tls": true
|
||||||
}
|
}
|
||||||
|
|||||||
57
monitor.go
57
monitor.go
@@ -17,6 +17,11 @@ const DefaultInterval = 60
|
|||||||
const DefaultTimeout = 1
|
const DefaultTimeout = 1
|
||||||
const DefaultTimeFormat = "15:04:05 Jan 2 MST"
|
const DefaultTimeFormat = "15:04:05 Jan 2 MST"
|
||||||
|
|
||||||
|
type HttpHeader struct {
|
||||||
|
Name string `json:"header"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
// Monitor data model
|
// Monitor data model
|
||||||
type Monitor struct {
|
type Monitor struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -25,6 +30,7 @@ type Monitor struct {
|
|||||||
StrictTLS bool `json:"strict_tls"`
|
StrictTLS bool `json:"strict_tls"`
|
||||||
CheckInterval time.Duration `json:"interval"`
|
CheckInterval time.Duration `json:"interval"`
|
||||||
HttpTimeout time.Duration `json:"timeout"`
|
HttpTimeout time.Duration `json:"timeout"`
|
||||||
|
HttpHeaders []*HttpHeader `json:"headers"`
|
||||||
|
|
||||||
MetricID int `json:"metric_id"`
|
MetricID int `json:"metric_id"`
|
||||||
ComponentID int `json:"component_id"`
|
ComponentID int `json:"component_id"`
|
||||||
@@ -109,37 +115,42 @@ func (monitor *Monitor) doRequest() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Get(monitor.URL)
|
req, err := http.NewRequest(monitor.Method, monitor.URL, nil)
|
||||||
if err != nil {
|
for _, h := range monitor.HttpHeaders {
|
||||||
monitor.lastFailReason = err.Error()
|
req.Header.Add(h.Name, h.Value)
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
resp, err := client.Do(req)
|
||||||
}
|
if err != nil {
|
||||||
|
monitor.lastFailReason = err.Error()
|
||||||
|
|
||||||
defer resp.Body.Close()
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if monitor.ExpectedStatusCode > 0 && resp.StatusCode != monitor.ExpectedStatusCode {
|
defer resp.Body.Close()
|
||||||
monitor.lastFailReason = "Unexpected response code: " + strconv.Itoa(resp.StatusCode) + ". Expected " + strconv.Itoa(monitor.ExpectedStatusCode)
|
|
||||||
|
|
||||||
return false
|
if monitor.ExpectedStatusCode > 0 && resp.StatusCode != monitor.ExpectedStatusCode {
|
||||||
}
|
monitor.lastFailReason = "Unexpected response code: " + strconv.Itoa(resp.StatusCode) + ". Expected " + strconv.Itoa(monitor.ExpectedStatusCode)
|
||||||
|
|
||||||
if monitor.bodyRegexp != nil {
|
return false
|
||||||
// check body
|
}
|
||||||
responseBody, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
monitor.lastFailReason = err.Error()
|
|
||||||
|
|
||||||
return false
|
if monitor.bodyRegexp != nil {
|
||||||
}
|
// check body
|
||||||
|
responseBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
monitor.lastFailReason = err.Error()
|
||||||
|
|
||||||
match := monitor.bodyRegexp.Match(responseBody)
|
return false
|
||||||
if !match {
|
}
|
||||||
monitor.lastFailReason = "Unexpected body: " + string(responseBody) + ". Expected to match " + monitor.ExpectedBody
|
|
||||||
}
|
|
||||||
|
|
||||||
return match
|
match := monitor.bodyRegexp.Match(responseBody)
|
||||||
}
|
if !match {
|
||||||
|
monitor.lastFailReason = "Unexpected body: " + string(responseBody) + ". Expected to match " + monitor.ExpectedBody
|
||||||
|
}
|
||||||
|
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ Configuration
|
|||||||
"url": "Ping URL",
|
"url": "Ping URL",
|
||||||
// optional, http method (defaults GET)
|
// optional, http method (defaults GET)
|
||||||
"method": "get",
|
"method": "get",
|
||||||
|
// optional, http Headers to add (default none)
|
||||||
|
"headers": [
|
||||||
|
// specify Name and Value of Http-Header, eg. Authorization
|
||||||
|
{ "name": "Authorization", "value": "Basic <hash>" }
|
||||||
|
],
|
||||||
// self-signed ssl certificate
|
// self-signed ssl certificate
|
||||||
"strict_tls": true,
|
"strict_tls": true,
|
||||||
// seconds between checks
|
// seconds between checks
|
||||||
|
|||||||
Reference in New Issue
Block a user