diff --git a/api/agent.go b/api/agent.go index 35c0676b517b..34cfaa7ee608 100644 --- a/api/agent.go +++ b/api/agent.go @@ -67,17 +67,19 @@ type AgentCheckRegistration struct { // AgentServiceCheck is used to define a node or service level check type AgentServiceCheck struct { - Script string `json:",omitempty"` - DockerContainerID string `json:",omitempty"` - Shell string `json:",omitempty"` // Only supported for Docker. - Interval string `json:",omitempty"` - Timeout string `json:",omitempty"` - TTL string `json:",omitempty"` - HTTP string `json:",omitempty"` - TCP string `json:",omitempty"` - Status string `json:",omitempty"` - Notes string `json:",omitempty"` - TLSSkipVerify bool `json:",omitempty"` + Script string `json:",omitempty"` + DockerContainerID string `json:",omitempty"` + Shell string `json:",omitempty"` // Only supported for Docker. + Interval string `json:",omitempty"` + Timeout string `json:",omitempty"` + TTL string `json:",omitempty"` + HTTP string `json:",omitempty"` + Header map[string][]string `json:",omitempty"` + Method string `json:",omitempty"` + TCP string `json:",omitempty"` + Status string `json:",omitempty"` + Notes string `json:",omitempty"` + TLSSkipVerify bool `json:",omitempty"` // In Consul 0.7 and later, checks that are associated with a service // may also contain this optional DeregisterCriticalServiceAfter field, diff --git a/command/agent/agent.go b/command/agent/agent.go index fd3cd88b7b42..32639da916ad 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -1575,6 +1575,8 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist Notify: &a.state, CheckID: check.CheckID, HTTP: chkType.HTTP, + Header: chkType.Header, + Method: chkType.Method, Interval: chkType.Interval, Timeout: chkType.Timeout, Logger: a.logger, diff --git a/command/agent/check.go b/command/agent/check.go index 7fbeacfe249d..0816be74ba7c 100644 --- a/command/agent/check.go +++ b/command/agent/check.go @@ -57,6 +57,8 @@ type CheckType struct { Script string HTTP string + Header map[string][]string + Method string TCP string Interval time.Duration DockerContainerID string @@ -347,6 +349,8 @@ type CheckHTTP struct { Notify CheckNotifier CheckID types.CheckID HTTP string + Header map[string][]string + Method string Interval time.Duration Timeout time.Duration Logger *log.Logger @@ -429,15 +433,25 @@ func (c *CheckHTTP) run() { // check is invoked periodically to perform the HTTP check func (c *CheckHTTP) check() { - req, err := http.NewRequest("GET", c.HTTP, nil) + method := c.Method + if method == "" { + method = "GET" + } + + req, err := http.NewRequest(method, c.HTTP, nil) if err != nil { c.Logger.Printf("[WARN] agent: http request failed '%s': %s", c.HTTP, err) c.Notify.UpdateCheck(c.CheckID, api.HealthCritical, err.Error()) return } - req.Header.Set("User-Agent", UserAgent) - req.Header.Set("Accept", "text/plain, text/*, */*") + req.Header = http.Header(c.Header) + if req.Header.Get("User-Agent") == "" { + req.Header.Set("User-Agent", UserAgent) + } + if req.Header.Get("Accept") == "" { + req.Header.Set("Accept", "text/plain, text/*, */*") + } resp, err := c.httpClient.Do(req) if err != nil { diff --git a/command/agent/config.go b/command/agent/config.go index 411c341278ba..b853b6bf737d 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -1437,6 +1437,20 @@ func FixupCheckType(raw interface{}) error { case "service_id": rawMap["serviceid"] = v delete(rawMap, k) + case "header": + vm, ok := v.(map[string]interface{}) + if ok { + m := map[string][]string{} + for k, vv := range vm { + vs, ok := vv.([]interface{}) + if ok { + for _, s := range vs { + m[k] = append(m[k], s.(string)) + } + } + } + rawMap["header"] = m + } case "docker_container_id": rawMap["DockerContainerID"] = v delete(rawMap, k) diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 12c8919647f5..38b62517fd63 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -1429,6 +1429,17 @@ func TestDecodeConfig_Checks(t *testing.T) { "timeout": "100ms", "service_id": "insecure-sslservice", "tls_skip_verify": true + }, + { + "id": "chk7", + "name": "service:insecure-sslservice", + "HTTP": "https://insecure-sslservice/status", + "Header": {"a":["b"], "c":["d", "e"]}, + "Method": "PUT", + "interval": "10s", + "timeout": "100ms", + "service_id": "insecure-sslservice", + "tls_skip_verify": true } ] }` @@ -1485,6 +1496,17 @@ func TestDecodeConfig_Checks(t *testing.T) { Timeout: 100 * time.Millisecond, TLSSkipVerify: true, }, + &CheckDefinition{ + ID: "chk7", + Name: "service:insecure-sslservice", + ServiceID: "insecure-sslservice", + HTTP: "https://insecure-sslservice/status", + Header: map[string][]string{"a": []string{"b"}, "c": []string{"d", "e"}}, + Method: "PUT", + Interval: 10 * time.Second, + Timeout: 100 * time.Millisecond, + TLSSkipVerify: true, + }, }, } verify.Values(t, "", got, want) diff --git a/command/agent/structs.go b/command/agent/structs.go index 8d950352b414..d269fbbdc5d4 100644 --- a/command/agent/structs.go +++ b/command/agent/structs.go @@ -62,6 +62,8 @@ type CheckDefinition struct { // Script string HTTP string + Header map[string][]string + Method string TCP string Interval time.Duration DockerContainerID string