From 8fae1a236f9905d51813d43e074b676e3760b9b7 Mon Sep 17 00:00:00 2001 From: aviau Date: Wed, 10 Jun 2015 12:14:24 -0400 Subject: [PATCH] Implemented passive checks Change-Id: Id46d15f11293062a79818f7974144445d64d9e19 --- .../tests/v2_0/config/test_services.py | 2 +- surveilclient/tests/v2_0/status/test_hosts.py | 19 ++++++++++++++ .../tests/v2_0/status/test_services.py | 24 +++++++++++++++++- surveilclient/v2_0/shell.py | 25 ++++++++++++++++++- surveilclient/v2_0/status/hosts.py | 8 ++++++ surveilclient/v2_0/status/services.py | 10 ++++++++ 6 files changed, 85 insertions(+), 3 deletions(-) diff --git a/surveilclient/tests/v2_0/config/test_services.py b/surveilclient/tests/v2_0/config/test_services.py index 896b20a..2854e35 100644 --- a/surveilclient/tests/v2_0/config/test_services.py +++ b/surveilclient/tests/v2_0/config/test_services.py @@ -66,4 +66,4 @@ class TestServices(clienttest.ClientTest): self.assertEqual( body, "body" - ) \ No newline at end of file + ) diff --git a/surveilclient/tests/v2_0/status/test_hosts.py b/surveilclient/tests/v2_0/status/test_hosts.py index 0c2b3ff..107a112 100644 --- a/surveilclient/tests/v2_0/status/test_hosts.py +++ b/surveilclient/tests/v2_0/status/test_hosts.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import json + import httpretty from surveilclient.tests.v2_0 import clienttest @@ -46,3 +48,20 @@ class TestHosts(clienttest.ClientTest): host, {"host_name": "host1"} ) + + @httpretty.activate + def test_submit_host_check_result(self): + httpretty.register_uri( + httpretty.POST, "http://localhost:8080/v2/status/hosts/localhost" + "/results", + body='' + ) + + self.client.status.hosts.submit_check_result( + "localhost", output="someoutput" + ) + + self.assertEqual( + json.loads(httpretty.last_request().body.decode()), + {"output": u"someoutput"} + ) \ No newline at end of file diff --git a/surveilclient/tests/v2_0/status/test_services.py b/surveilclient/tests/v2_0/status/test_services.py index cc5f85e..cb99735 100644 --- a/surveilclient/tests/v2_0/status/test_services.py +++ b/surveilclient/tests/v2_0/status/test_services.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import json + import httpretty from surveilclient.tests.v2_0 import clienttest @@ -31,4 +33,24 @@ class TestServices(clienttest.ClientTest): self.assertEqual( services, [{"service_name": "service1"}] - ) \ No newline at end of file + ) + + @httpretty.activate + def test_submit_service_check_result(self): + httpretty.register_uri( + httpretty.POST, + "http://localhost:8080/v2/status/hosts/localhost" + "/services/testservice/results", + body='' + ) + + self.client.status.services.submit_check_result( + "localhost", + 'testservice', + output="someoutputt" + ) + + self.assertEqual( + json.loads(httpretty.last_request().body.decode()), + {"output": u"someoutputt"} + ) diff --git a/surveilclient/v2_0/shell.py b/surveilclient/v2_0/shell.py index ec31b6e..f9bc472 100644 --- a/surveilclient/v2_0/shell.py +++ b/surveilclient/v2_0/shell.py @@ -75,7 +75,7 @@ def do_config_host_update(sc, args): if "custom_fields" in host: host["custom_fields"] = json.loads(host["custom_fields"]) - sc.config.hosts.update(**host) + sc.config.hosts.update(args.host_name, **host) @cliutils.arg("--host_name", help="Name of the host") @@ -166,6 +166,7 @@ def do_config_service_list(sc, args): @cliutils.arg("--notification_period") @cliutils.arg("--contacts") @cliutils.arg("--contact_groups") +@cliutils.arg("--passive_checks_enabled") def do_config_service_create(sc, args): """Create a config service.""" arg_names = ['host_name', @@ -179,6 +180,7 @@ def do_config_service_create(sc, args): 'notification_period', 'contacts', 'contact_groups', + 'passive_checks_enabled', 'use'] service = _dict_from_args(args, arg_names) sc.config.services.create(**service) @@ -428,6 +430,27 @@ def do_status_metrics_show(sc, args): utils.print_item(metric, metricProperties) +@cliutils.arg("--host_name", help="Name of the host") +@cliutils.arg("--service_description", help="Service description") +@cliutils.arg("--output", help="The output of the plugin") +@cliutils.arg("--return_code", help="The return code of the plugin") +def do_status_submit_check_result(sc, args): + """Submit a check result.""" + if args.service_description: + sc.status.services.submit_check_result( + args.host_name, + args.service_description, + output=args.output, + return_code=int(args.return_code), + ) + else: + sc.status.hosts.submit_check_result( + args.host_name, + output=args.output, + return_code=int(args.return_code), + ) + + @cliutils.arg("--host_name", help="Name of the host") @cliutils.arg("--service_description", help="Service description") @cliutils.arg("--time_stamp") diff --git a/surveilclient/v2_0/status/hosts.py b/surveilclient/v2_0/status/hosts.py index 76131b7..f936347 100644 --- a/surveilclient/v2_0/status/hosts.py +++ b/surveilclient/v2_0/status/hosts.py @@ -36,3 +36,11 @@ class HostsManager(surveil_manager.SurveilManager): HostsManager.base_url + "/" + host_name, 'GET' ) return body + + def submit_check_result(self, host_name, **kwargs): + """Submit a check result.""" + resp, body = self.http_client.json_request( + HostsManager.base_url + '/' + host_name + '/results', 'POST', + body=kwargs + ) + return body diff --git a/surveilclient/v2_0/status/services.py b/surveilclient/v2_0/status/services.py index 9ca619e..7a69766 100644 --- a/surveilclient/v2_0/status/services.py +++ b/surveilclient/v2_0/status/services.py @@ -24,3 +24,13 @@ class ServicesManager(surveil_manager.SurveilManager): ServicesManager.base_url, 'POST', body=live_query ) return body + + def submit_check_result(self, host_name, service_description, **kwargs): + """Submit a check result.""" + resp, body = self.http_client.json_request( + '/status/hosts/%s/services/%s/results' % (host_name, + service_description), + 'POST', + body=kwargs + ) + return body