From 28f31a8dd889e24b3c2ed983e81ee77a3a056a85 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Sun, 26 Oct 2014 13:11:25 -0700 Subject: [PATCH 1/3] agent: add support for multiple checks and config mixing --- command/agent/config.go | 26 ++++++-- command/agent/config_test.go | 114 +++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 5 deletions(-) diff --git a/command/agent/config.go b/command/agent/config.go index 791f5ede9d..7037a81d24 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -403,16 +403,32 @@ func DecodeConfig(r io.Reader) (*Config, error) { } result.Services = append(result.Services, service) } - return &result, nil } - } else if sub, ok := obj["service"]; ok { + } + if sub, ok := obj["service"]; ok { service, err := DecodeServiceDefinition(sub) + if err != nil { + return nil, err + } result.Services = append(result.Services, service) - return &result, err - } else if sub, ok := obj["check"]; ok { + } + if sub, ok := obj["checks"]; ok { + if list, ok := sub.([]interface{}); ok { + for _, chk := range list { + check, err := DecodeCheckDefinition(chk) + if err != nil { + return nil, err + } + result.Checks = append(result.Checks, check) + } + } + } + if sub, ok := obj["check"]; ok { check, err := DecodeCheckDefinition(sub) + if err != nil { + return nil, err + } result.Checks = append(result.Checks, check) - return &result, err } } diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 3b162ea08b..f73fd92992 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -591,6 +591,120 @@ func TestDecodeConfig_Services(t *testing.T) { } } +func TestDecodeConfig_Checks(t *testing.T) { + input := `{ + "checks": [ + { + "id": "chk1", + "name": "mem", + "script": "/bin/check_mem", + "interval": "5s" + }, + { + "id": "chk2", + "name": "cpu", + "script": "/bin/check_cpu", + "interval": "10s" + } + ] + }` + + config, err := DecodeConfig(bytes.NewReader([]byte(input))) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := &Config{ + Checks: []*CheckDefinition{ + &CheckDefinition{ + ID: "chk1", + Name: "mem", + CheckType: CheckType{ + Script: "/bin/check_mem", + Interval: 5 * time.Second, + }, + }, + &CheckDefinition{ + ID: "chk2", + Name: "cpu", + CheckType: CheckType{ + Script: "/bin/check_cpu", + Interval: 10 * time.Second, + }, + }, + }, + } + + if !reflect.DeepEqual(config, expected) { + t.Fatalf("bad: %#v", config) + } +} + +func TestDecodeConfig_Multiples(t *testing.T) { + input := `{ + "services": [ + { + "id": "red0", + "name": "redis", + "tags": [ + "master" + ], + "port": 6000, + "check": { + "script": "/bin/check_redis -p 6000", + "interval": "5s", + "ttl": "20s" + } + } + ], + "checks": [ + { + "id": "chk1", + "name": "mem", + "script": "/bin/check_mem", + "interval": "10s" + } + ] + }` + + config, err := DecodeConfig(bytes.NewReader([]byte(input))) + if err != nil { + t.Fatalf("err: %s", err) + } + + expected := &Config{ + Services: []*ServiceDefinition{ + &ServiceDefinition{ + Check: CheckType{ + Interval: 5 * time.Second, + Script: "/bin/check_redis -p 6000", + TTL: 20 * time.Second, + }, + ID: "red0", + Name: "redis", + Tags: []string{ + "master", + }, + Port: 6000, + }, + }, + Checks: []*CheckDefinition{ + &CheckDefinition{ + ID: "chk1", + Name: "mem", + CheckType: CheckType{ + Script: "/bin/check_mem", + Interval: 10 * time.Second, + }, + }, + }, + } + + if !reflect.DeepEqual(config, expected) { + t.Fatalf("bad: %#v", config) + } +} + func TestDecodeConfig_Service(t *testing.T) { // Basics input := `{"service": {"id": "red1", "name": "redis", "tags": ["master"], "port":8000, "check": {"script": "/bin/check_redis", "interval": "10s", "ttl": "15s" }}}` From f03bbb8daaa5b12e9c97994327f64d84fd9053fb Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Sun, 26 Oct 2014 13:24:23 -0700 Subject: [PATCH 2/3] website: update docs for multiple checks in config --- .../source/docs/agent/checks.html.markdown | 25 +++++++++++++++++++ .../source/docs/agent/services.html.markdown | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/website/source/docs/agent/checks.html.markdown b/website/source/docs/agent/checks.html.markdown index 33714d3bcb..277a89c4bb 100644 --- a/website/source/docs/agent/checks.html.markdown +++ b/website/source/docs/agent/checks.html.markdown @@ -85,3 +85,28 @@ a specific meaning. Specifically: This is the only convention that Consul depends on. Any output of the script will be captured and stored in the `notes` field so that it can be viewed by human operators. + +## Multiple Check Definitions + +Multiple check definitions can be provided at once using the `checks` (plural) +key in your configuration file. + +```javascript +{ + "checks": [ + { + "id": "chk1", + "name": "mem", + "script": "/bin/check_mem", + "interval": "5s", + }, + { + "id": "chk2", + "name": "cpu", + "script": "/bin/check_cpu", + "interval": "10s", + }, + ... + ] +} +``` diff --git a/website/source/docs/agent/services.html.markdown b/website/source/docs/agent/services.html.markdown index 6dc8e30eea..ad66da571a 100644 --- a/website/source/docs/agent/services.html.markdown +++ b/website/source/docs/agent/services.html.markdown @@ -64,7 +64,8 @@ service can be registered dynamically using the [HTTP API](/docs/agent/http.html ## Multiple Service Definitions -Multiple services definitions can be provided at once. Single and mutiple service definitions can't be provided together in one configuration file. +Multiple services definitions can be provided at once using the `services` +(plural) key in your configuration file. ```javascript { From d472825181e9c9b01f2fa9261cda9ca98c13f624 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Mon, 27 Oct 2014 11:58:01 -0700 Subject: [PATCH 3/3] website: fix JSON in multiple checks documentation --- website/source/docs/agent/checks.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/source/docs/agent/checks.html.markdown b/website/source/docs/agent/checks.html.markdown index 277a89c4bb..a5a4bdb566 100644 --- a/website/source/docs/agent/checks.html.markdown +++ b/website/source/docs/agent/checks.html.markdown @@ -98,13 +98,13 @@ key in your configuration file. "id": "chk1", "name": "mem", "script": "/bin/check_mem", - "interval": "5s", + "interval": "5s" }, { "id": "chk2", "name": "cpu", "script": "/bin/check_cpu", - "interval": "10s", + "interval": "10s" }, ... ]