Example #1
0
    def hook_save_retention(self, scheduler):
        """
        Save retention data from alignak-backend

        :param scheduler: scheduler instance of alignak
        :type scheduler: object
        :return: None
        """
        backend = Backend()
        headers = {'Content-Type': 'application/json'}
        data_to_save = scheduler.get_retention_data()
        # clean all hosts first
        backend.method_delete(self.endpoint('retentionhost'))
        # Add all hosts after
        for host in data_to_save['hosts']:
            data_to_save['hosts'][host]['host'] = host
            backend.method_post(self.endpoint('retentionhost'),
                                ujson.dumps(data_to_save['hosts'][host]),
                                headers=headers)

        # clean all services first
        backend.method_delete(self.endpoint('retentionservice'))
        # Add all services after
        for service in data_to_save['services']:
            data_to_save['services'][service]['service'] = service
            backend.method_post(self.endpoint('retentionservice'),
                                ujson.dumps(data_to_save['services'][service]),
                                headers=headers)
Example #2
0
    def get_refs(self, type_data):
        """
        Get the _id in the backend for hosts and services

        :param type_data: livestate type to get: livehost or liveservice
        :type type_data: str
        :return: None
        """
        backend = Backend()
        if type_data == 'livehost':
            content = backend.method_get(self.endpoint('host?projection={"host_name":1}'
                                                       '&where={"register":true}'))
            hosts = {}
            for item in content:
                hosts[item['_id']] = item['host_name']
                self.mapping['host'][item['host_name']] = item['_id']
            # get all livehost
            contentlh = backend.method_get(self.endpoint('livehost?embedded={"host_name":1}'
                                                         '&projection={"host_name":1}'))
            for item in contentlh:
                self.ref_live['host'][item['host_name']['_id']] = {
                    '_id': item['_id'],
                    '_etag': item['_etag']
                }
                del hosts[item['host_name']['_id']]
            # create livehost for hosts not added
            for key_id in hosts:
                data = {'host_name': key_id}
                headers = {'Content-Type': 'application/json'}
                contentadd = backend.method_post(self.endpoint('livehost'), ujson.dumps(data),
                                                 headers=headers)
                self.ref_live['host'][key_id] = {
                    '_id': contentadd['_id'],
                    '_etag': contentadd['_etag']
                }
        elif type_data == 'liveservice':
            content = backend.method_get(self.endpoint('service?projection={'
                                                       '"service_description":1,"host_name":1}'
                                                       '&embedded={"host_name":1}'
                                                       '&where={"register":true}'))
            services = {}
            for item in content:
                services[item['_id']] = item['service_description']
                self.mapping['service'][''.join([item['host_name']['host_name'],
                                                 item['service_description']])] = item['_id']
            # get all liveservice
            contentls = backend.method_get(self.endpoint('liveservice?'
                                                         'embedded={"service_description":1}'
                                                         '&projection={"service_description":1}'))
            for item in contentls:
                self.ref_live['service'][item['service_description']['_id']] = {
                    '_id': item['_id'],
                    '_etag': item['_etag']
                }
                del services[item['service_description']['_id']]
            # create liveservice for services not added
            for key_id in services:
                data = {'service_description': key_id}
                headers = {'Content-Type': 'application/json'}
                contentadd = backend.method_post(self.endpoint('liveservice'), ujson.dumps(data),
                                                 headers=headers)
                self.ref_live['service'][key_id] = {
                    '_id': contentadd['_id'],
                    '_etag': contentadd['_etag']
                }
Example #3
0
    def send_to_backend(self, type_data, name, data):
        """
        Send data to alignak backend livehost or liveservice

        :param type_data: one of ['livehost', 'liveservice', 'loghost', 'logservice']
        :type type_data: str
        :param name: name of host or service
        :type name: str
        :param data: dictionary with data to add / update
        :type data: dict
        :return: True if send is ok, False otherwise
        :rtype: bool
        """
        backend = Backend()
        headers = {
            'Content-Type': 'application/json',
        }
        ret = True
        if type_data == 'livehost':
            headers['If-Match'] = self.ref_live['host'][self.mapping['host'][name]]['_etag']
            response = backend.method_patch(
                self.endpoint('livehost/%s'
                              % self.ref_live['host'][self.mapping['host'][name]]['_id']),
                ujson.dumps(data),
                headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
            else:
                self.ref_live['host'][self.mapping['host'][name]]['_etag'] = response['_etag']
        elif type_data == 'liveservice':
            headers['If-Match'] = self.ref_live['service'][self.mapping['service'][name]]['_etag']
            response = backend.method_patch(
                self.endpoint('liveservice/%s'
                              % self.ref_live['service'][self.mapping['service'][name]]['_id']),
                ujson.dumps(data),
                headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
            else:
                self.ref_live['service'][self.mapping['service'][name]]['_etag'] = response['_etag']
        elif type_data == 'loghost':
            data['host_name'] = self.mapping['host'][name]
            response = backend.method_post(
                self.endpoint('loghost'), ujson.dumps(data), headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
        elif type_data == 'logservice':
            data['service_description'] = self.mapping['service'][name]
            response = backend.method_post(
                self.endpoint('logservice'), ujson.dumps(data), headers=headers
            )
            if response['_status'] == 'ERR':
                logger.error(response['_issues'])
                ret = False
        return ret