From b5844c86cdd005b8ea681371d18ebc1d2ea5783f Mon Sep 17 00:00:00 2001 From: aviau Date: Thu, 9 Jul 2015 17:39:15 -0400 Subject: [PATCH] mod-mongodb -> mod-surveil-config Change-Id: I77fcce69b32ed74b9999f9ee41ac8278eb2039dc --- Dockerfile | 3 + docker-compose.yml | 7 ++ surveil/api/controllers/v2/config/hosts.py | 8 +- surveil/api/controllers/v2/config/services.py | 8 +- surveil/api/datamodel/config/command.py | 2 + surveil/api/datamodel/config/host.py | 43 ++++------ surveil/api/datamodel/config/service.py | 6 ++ .../api/handlers/config/service_handler.py | 7 +- surveil/api/handlers/mongo_object_handler.py | 10 ++- .../api/controllers/v2/config/test_hosts.py | 80 +++---------------- .../controllers/v2/config/test_services.py | 10 ++- tools/docker/alignak_container/Dockerfile | 14 ++-- .../etc/alignak/arbiters/arbiter-master.cfg | 2 +- .../etc/alignak/modules/mongodb.cfg | 12 --- .../etc/alignak/modules/surveil-config.cfg | 7 ++ 15 files changed, 89 insertions(+), 130 deletions(-) delete mode 100644 tools/docker/alignak_container/etc/alignak/modules/mongodb.cfg create mode 100644 tools/docker/alignak_container/etc/alignak/modules/surveil-config.cfg diff --git a/Dockerfile b/Dockerfile index b3520aa..9283d4e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,6 +51,9 @@ ENV SURVEIL_OS_USERNAME=admin ENV SURVEIL_OS_PASSWORD=password ENV SURVEIL_OS_TENANT_NAME=admin +# Surveil API +EXPOSE 5311 + CMD cd /opt/surveil && \ ./setup.sh && \ ((sleep 40 && surveil-init --influxdb --packs --mongodb) &) && \ diff --git a/docker-compose.yml b/docker-compose.yml index 14ab693..1d53bb7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,9 @@ +ambassador: + image: cpuguy83/docker-grand-ambassador + volumes: + - "/var/run/docker.sock:/var/run/docker.sock" + command: "-name surveil_surveil_1 -name surveil_alignak_1" + surveil: build: . links: @@ -19,6 +25,7 @@ alignak: - mongo - influxdb - redis + - "ambassador:surveil" ports: - "7767:7767" environment: diff --git a/surveil/api/controllers/v2/config/hosts.py b/surveil/api/controllers/v2/config/hosts.py index caaf90a..de7cd09 100644 --- a/surveil/api/controllers/v2/config/hosts.py +++ b/surveil/api/controllers/v2/config/hosts.py @@ -115,11 +115,13 @@ class HostsController(rest.RestController): return HostController(host_name), remainder @util.policy_enforce(['authenticated']) - @wsme_pecan.wsexpose([host.Host]) - def get_all(self): + @wsme_pecan.wsexpose([host.Host], int) + def get_all(self, templates=0): """Returns all hosts.""" handler = host_handler.HostHandler(pecan.request) - hosts = handler.get_all() + hosts = handler.get_all( + templates=bool(templates) + ) return hosts @util.policy_enforce(['authenticated']) diff --git a/surveil/api/controllers/v2/config/services.py b/surveil/api/controllers/v2/config/services.py index 96f17a4..10a5017 100644 --- a/surveil/api/controllers/v2/config/services.py +++ b/surveil/api/controllers/v2/config/services.py @@ -24,11 +24,13 @@ from surveil.common import util class ServicesController(rest.RestController): @util.policy_enforce(['authenticated']) - @wsme_pecan.wsexpose([service.Service]) - def get_all(self): + @wsme_pecan.wsexpose([service.Service], int) + def get_all(self, templates=0): """Returns all services.""" handler = service_handler.ServiceHandler(pecan.request) - services = handler.get_all() + services = handler.get_all( + templates=bool(templates) + ) return services @util.policy_enforce(['authenticated']) diff --git a/surveil/api/datamodel/config/command.py b/surveil/api/datamodel/config/command.py index 20a4811..ec1736d 100644 --- a/surveil/api/datamodel/config/command.py +++ b/surveil/api/datamodel/config/command.py @@ -33,6 +33,8 @@ class Command(types.Base): command_line = wsme.wsattr(wtypes.text, mandatory=True) """This directive is used to define what is actually executed by Shinken""" + module_type = wsme.wsattr(wtypes.text, mandatory=False) + @classmethod def sample(cls): return cls( diff --git a/surveil/api/datamodel/config/host.py b/surveil/api/datamodel/config/host.py index 7d28dce..5cf30fe 100644 --- a/surveil/api/datamodel/config/host.py +++ b/surveil/api/datamodel/config/host.py @@ -25,25 +25,32 @@ class Host(types.Base): address = wsme.wsattr(wtypes.text, mandatory=False) """The address of the host. Normally, this is an IP address.""" - max_check_attempts = wsme.wsattr(int, mandatory=False, default=3) + max_check_attempts = wsme.wsattr(int, mandatory=False) - check_period = wsme.wsattr(wtypes.text, mandatory=False, default='24x7') + check_period = wsme.wsattr(wtypes.text, mandatory=False) """The time period during which active checks of this host can be made.""" - contacts = wsme.wsattr(wtypes.text, mandatory=False, default='') + contacts = wsme.wsattr(wtypes.text, mandatory=False) """A list of the short names of the contacts that should be notified.""" - contact_groups = wsme.wsattr(wtypes.text, mandatory=False, default='') + contact_groups = wsme.wsattr(wtypes.text, mandatory=False) """List of the short names of the contact groups that should be notified""" - notification_interval = wsme.wsattr(int, mandatory=False, default=30) + notification_interval = wsme.wsattr(int, mandatory=False) - notification_period = wsme.wsattr(wtypes.text, mandatory=False, - default='24x7') + notification_period = wsme.wsattr(wtypes.text, mandatory=False) use = wsme.wsattr(wtypes.text, mandatory=False) """The template to use for this host""" + name = wsme.wsattr(wtypes.text, mandatory=False) + + register = wsme.wsattr(wtypes.text, mandatory=False) + + check_interval = wsme.wsattr(int, mandatory=False) + + retry_interval = wsme.wsattr(int, mandatory=False) + # TODO(aviau): Custom fields starting without '_' should raise an error. custom_fields = wsme.wsattr( wtypes.DictType(wtypes.text, wtypes.text), @@ -51,28 +58,6 @@ class Host(types.Base): ) """Custom fields for the host""" - def __init__(self, **kwargs): - super(Host, self).__init__(**kwargs) - - # Custom fields start with '_'. Detect them ans assign them. - custom_fields = [i for i in kwargs.items() - if (isinstance(i[0], str) - or isinstance(i[0], unicode)) - and i[0][0] == '_'] - - if len(custom_fields) > 0: - self.custom_fields = {} - for item in custom_fields: - self.custom_fields[item[0]] = item[1] - - def as_dict(self): - host_dict = super(Host, self).as_dict() - custom_fields = host_dict.pop("custom_fields", None) - if custom_fields: - for item in custom_fields.items(): - host_dict[item[0]] = item[1] - return host_dict - @classmethod def sample(cls): return cls( diff --git a/surveil/api/datamodel/config/service.py b/surveil/api/datamodel/config/service.py index 67dd045..271af2a 100644 --- a/surveil/api/datamodel/config/service.py +++ b/surveil/api/datamodel/config/service.py @@ -43,6 +43,12 @@ class Service(types.Base): passive_checks_enabled = wsme.wsattr(wtypes.text, mandatory=False) + use = wsme.wsattr(wtypes.text, mandatory=False) + + name = wsme.wsattr(wtypes.text, mandatory=False) + + register = wsme.wsattr(wtypes.text, mandatory=False) + @classmethod def sample(cls): return cls( diff --git a/surveil/api/handlers/config/service_handler.py b/surveil/api/handlers/config/service_handler.py index 315fbc6..6424905 100644 --- a/surveil/api/handlers/config/service_handler.py +++ b/surveil/api/handlers/config/service_handler.py @@ -52,9 +52,12 @@ class ServiceHandler(handler.Handler): data.as_dict() ) - def get_all(self, host_name=None): + def get_all(self, host_name=None, templates=False): """Return all services.""" - filters = {"register": {"$ne": "0"}} + if templates is True: + filters = {} + else: + filters = {"register": {"$ne": "0"}} if host_name is not None: filters['host_name'] = host_name diff --git a/surveil/api/handlers/mongo_object_handler.py b/surveil/api/handlers/mongo_object_handler.py index bec108f..88fed90 100644 --- a/surveil/api/handlers/mongo_object_handler.py +++ b/surveil/api/handlers/mongo_object_handler.py @@ -66,11 +66,15 @@ class MongoObjectHandler(handler.Handler): resource.as_dict() ) - def get_all(self): + def get_all(self, templates=False): """Return all resources.""" + if templates is True: + filters = {} + else: + filters = {"register": {"$ne": "0"}} + resources = [r for r in self._get_resource_collection() - .find({"register": {"$ne": "0"}}, - {'_id': 0})] + .find(filters, {'_id': 0})] resources = [self.resource_datamodel(**r) for r in resources] return resources diff --git a/surveil/tests/api/controllers/v2/config/test_hosts.py b/surveil/tests/api/controllers/v2/config/test_hosts.py index cdaa93f..a10e1c9 100644 --- a/surveil/tests/api/controllers/v2/config/test_hosts.py +++ b/surveil/tests/api/controllers/v2/config/test_hosts.py @@ -30,7 +30,6 @@ class TestHostController(functionalTest.FunctionalTest): "max_check_attempts": 5, "check_period": "24x7", "contacts": "admin,carl", "contact_groups": "router-admins", "notification_interval": 30, "notification_period": "24x7", - "_CRITICAL": "10" }, { "host_name": "bogus-router2", "address": "192.168.1.254", @@ -72,21 +71,13 @@ class TestHostController(functionalTest.FunctionalTest): def test_get_all_hosts(self): response = self.get('/v2/config/hosts') - # Adjust self.host content to reflect custom_fields sub-dict - c_fields = {} - for h in self.hosts: - if '_CRITICAL' in h.keys(): - c_fields['_CRITICAL'] = h['_CRITICAL'] - h.pop('_CRITICAL') - h['custom_fields'] = c_fields - self.assert_count_equal_backport( json.loads(response.body.decode()), self.hosts ) self.assertEqual(response.status_int, 200) - def test_get_all_hosts_no_templates(self): + def test_get_all_hosts_templates(self): self.mongoconnection.shinken.hosts.insert( copy.deepcopy( {"host_name": "bogus-router", "address": "192.168.1.254", @@ -110,6 +101,13 @@ class TestHostController(functionalTest.FunctionalTest): json.loads(response.body.decode()), self.hosts ) + + response = self.get('/v2/config/hosts', params={'templates': 1}) + self.assertEqual( + len(json.loads(response.body.decode())), + len(self.hosts) + 1 + ) + self.assertEqual(response.status_int, 200) def test_get_specific_host(self): @@ -121,14 +119,6 @@ class TestHostController(functionalTest.FunctionalTest): ) self.assertEqual(response.status_int, 200) - def test_get_specific_host_custom_field(self): - response = self.get('/v2/config/hosts/bogus-router') - my_host = json.loads(response.body.decode()) - - self.assertIn("custom_fields", my_host.keys()) - self.assertNotIn("_CRITICAL", my_host.keys()) - self.assertIsNone(my_host.get("_CRITICAL")) - def test_update_host(self): put_host = { u'host_name': u'bogus-router333', @@ -150,9 +140,9 @@ class TestHostController(functionalTest.FunctionalTest): 'notification_interval': 30, 'contacts': u'newcontacts', 'notification_period': u'24x7', - 'contact_groups': u'', + 'contact_groups': u'router-admins', 'host_name': u'bogus-router333', - 'max_check_attempts': 3, + 'max_check_attempts': 5, 'use': u'test' } @@ -187,56 +177,6 @@ class TestHostController(functionalTest.FunctionalTest): self.assertTrue(new_host in hosts) self.assertEqual(response.status_int, 201) - def test_add_host_custom_fields(self): - my_host = { - "host_name": "custom_field_host", "address": "192.168.1.254", - "max_check_attempts": 5, "check_period": "24x7", - "contacts": "admin,carl", "contact_groups": "router-admins", - "notification_interval": 30, "notification_period": "24x7", - "_TEST_CUSTOM_FIELD": "10" - } - - self.mongoconnection.shinken.hosts.insert(my_host) - mongo_host = self.mongoconnection.shinken.hosts.find_one( - {"host_name": "custom_field_host"} - ) - # In-MongoDB representation should hold custom fields similarly to - # Shinken: - # - # define host { - # _CUSTOM value - # } - # (no "custom_fields" sub-dict) - self.assertNotIn("custom_fields", mongo_host.keys()) - self.assertIn("_TEST_CUSTOM_FIELD", mongo_host.keys()) - self.assertIsNotNone(mongo_host["_TEST_CUSTOM_FIELD"]) - - def test_post_add_host_custom_fields(self): - my_host = { - "host_name": "custom_field_host", "address": "192.168.1.254", - "max_check_attempts": 5, "check_period": "24x7", - "contacts": "admin,carl", "contact_groups": "router-admins", - "notification_interval": 30, "notification_period": "24x7", - "custom_fields": { - "_TEST_CUSTOM_FIELD": "10" - } - } - - self.post_json('/v2/config/hosts', my_host) - mongo_host = self.mongoconnection.shinken.hosts.find_one( - {"host_name": "custom_field_host"} - ) - # In-MongoDB representation should hold custom fields similarly to - # Shinken: - # - # define host { - # _CUSTOM value - # } - # (no "custom_fields" sub-dict) - self.assertNotIn("custom_fields", mongo_host.keys()) - self.assertIn("_TEST_CUSTOM_FIELD", mongo_host.keys()) - self.assertIsNotNone(mongo_host["_TEST_CUSTOM_FIELD"]) - def test_get_associated_services(self): response = self.get('/v2/config/hosts/bogus-router/services') diff --git a/surveil/tests/api/controllers/v2/config/test_services.py b/surveil/tests/api/controllers/v2/config/test_services.py index ff876d5..d9f859e 100644 --- a/surveil/tests/api/controllers/v2/config/test_services.py +++ b/surveil/tests/api/controllers/v2/config/test_services.py @@ -77,7 +77,7 @@ class TestServiceController(functionalTest.FunctionalTest): ) self.assertEqual(response.status_int, 200) - def test_get_all_services_no_templates(self): + def test_get_all_services_templates(self): self.mongoconnection.shinken.services.insert( copy.deepcopy( {"host_name": "sample-server3", @@ -100,6 +100,14 @@ class TestServiceController(functionalTest.FunctionalTest): json.loads(response.body.decode()), self.services ) + + response = self.get('/v2/config/services', params={"templates": 1}) + + self.assertEqual( + len(json.loads(response.body.decode())), + len(self.services) + 1 + ) + self.assertEqual(response.status_int, 200) def test_add_service(self): diff --git a/tools/docker/alignak_container/Dockerfile b/tools/docker/alignak_container/Dockerfile index 6311703..2f170bb 100644 --- a/tools/docker/alignak_container/Dockerfile +++ b/tools/docker/alignak_container/Dockerfile @@ -31,13 +31,14 @@ RUN cd /tmp && \ mv /tmp/mod-booster-nrpe-*/module /var/lib/alignak/modules/mod-booster-nrpe && \ rm -rfv /tmp/mod-booster-nrpe* -# mod-mongodb -RUN pip install pymongo==3.0.2 +# mod-surveil-config +RUN apt-get update && apt-get install -y git +RUN pip install -e git+https://github.com/aviau/python-surveilclient.git@templates#egg=python-surveilclient RUN cd /tmp && \ - wget -O mod-mongodb.tar.gz https://github.com/shinken-monitoring/mod-mongodb/archive/5396fded1c56d57202236d1236703a160aec7375.tar.gz && \ - tar -zxvf mod-mongodb.tar.gz && \ - mv /tmp/mod-mongodb-*/module /var/lib/alignak/modules/mod-mongodb && \ - rm -rfv /tmp/mod-mongodb* + wget -O mod-surveil-config.tar.gz https://github.com/Alignak-monitoring/mod-surveil-config/archive/6331d3626fd92deaeb5e372c5cdb1eedd8b09d9b.tar.gz && \ + tar -zxvf mod-surveil-config.tar.gz && \ + mv /tmp/mod-surveil-config-*/alignak/modules/mod_surveil_config /var/lib/alignak/modules/mod-surveil-config && \ + rm -rfv /tmp/mod-surveil-config* # mod-influxdb RUN pip install influxdb==2.3.0 @@ -55,6 +56,7 @@ RUN cd /tmp && \ rm -rfv /tmp/mod-ws-arbiter* # mod-mongo-live-config +RUN pip install pymongo==3.0.2 RUN cd /tmp && \ wget -O mod-mongo-live-config.tar.gz https://github.com/savoirfairelinux/mod-mongo-live-config/archive/0.3.2.tar.gz && \ tar -zxvf mod-mongo-live-config.tar.gz && \ diff --git a/tools/docker/alignak_container/etc/alignak/arbiters/arbiter-master.cfg b/tools/docker/alignak_container/etc/alignak/arbiters/arbiter-master.cfg index 69fae80..a98d893 100644 --- a/tools/docker/alignak_container/etc/alignak/arbiters/arbiter-master.cfg +++ b/tools/docker/alignak_container/etc/alignak/arbiters/arbiter-master.cfg @@ -35,7 +35,7 @@ define arbiter { # - FileTag = Tag an host if it's on a flat file # - CSVTag = Tag an host from the content of a CSV file - modules mongodb,ws-arbiter,mongo_live_config + modules surveil_config,ws-arbiter,mongo_live_config #modules named-pipe, mongodb, nsca, VMWare_auto_linking, ws-arbiter, Collectd, mport-landscape, SnmpBooster, AWS # Enable https or not diff --git a/tools/docker/alignak_container/etc/alignak/modules/mongodb.cfg b/tools/docker/alignak_container/etc/alignak/modules/mongodb.cfg deleted file mode 100644 index 0c70835..0000000 --- a/tools/docker/alignak_container/etc/alignak/modules/mongodb.cfg +++ /dev/null @@ -1,12 +0,0 @@ -## Module: Mongodb -## Loaded by: Arbiter, WebUI -# In Arbiter: Read objects in a mongodb database (like hosts or services). -# In WebUI: Save/read user preferences. -define module { - module_name mongodb - module_type mongodb - uri mongodb://mongo/ - database shinken - #username username ;optional - #password password ;optional -} diff --git a/tools/docker/alignak_container/etc/alignak/modules/surveil-config.cfg b/tools/docker/alignak_container/etc/alignak/modules/surveil-config.cfg new file mode 100644 index 0000000..54418c6 --- /dev/null +++ b/tools/docker/alignak_container/etc/alignak/modules/surveil-config.cfg @@ -0,0 +1,7 @@ +define module { + module_name surveil_config + module_type surveil_config + surveil_api_url http://surveil:5311/v2 + surveil_auth_url http://surveil:5311/v2/auth + surveil_version 2_0 +}