Beispiel #1
0
    def get_host(self, zabbix_id, log=None):
        """Return zabbix host according to vm_uuid or node_uuid (zabbix_id)"""
        log = log or self.log

        try:
            res = self.zapi.host.get({
                'filter': {
                    'host': zabbix_id
                },
                'output':
                'extend',
                'selectInterfaces':
                'extend',
                'selectMacros':
                'extend',
                'selectGroups':
                'extend',
                'selectParentTemplates': ['templateid', 'hostid'],
            })
        except ZabbixAPIException as e:
            log(ERROR, 'Zabbix API Error in get_host(%s): %s', zabbix_id, e)
            raise e

        try:
            return parse_zabbix_result(res)
        except RemoteObjectDoesNotExist:
            return {}
Beispiel #2
0
    def _create_service(self, name, parentid, algorithm=PROBLEM_ONE, sla=99.0, sortorder=0, triggerid=None):
        """Create Zabbix IT Service"""
        params = {
            'name': name,
            'parentid': parentid,
            'algorithm': algorithm,
            'sortorder': sortorder,
        }

        if sla:
            params['showsla'] = self.YES
            params['goodsla'] = sla
        else:
            params['showsla'] = self.NO

        if triggerid:
            params['triggerid'] = triggerid

        try:
            res = self.zapi.service.create(params)
        except ZabbixAPIException as e:
            logger.exception(e)
            raise InternalMonitoringError(text_type(e))

        return parse_zabbix_result(res, 'serviceids', from_get_request=False)
Beispiel #3
0
    def _create_host(self, obj, interface, groups=(), templates=(), macros=None, status=HOST_MONITORED,
                     proxy_id=NO_PROXY, log=None):
        """Create new Zabbix host from model object. Return hostid of the created host or False if an error occurred"""
        log = log or self.log
        params = {
            'host': self.host_id(obj),
            'name': self.host_name(obj),
            'status': status,
            'interfaces': [interface],
            'groups': self._gen_host_groups(groups),
            'templates': self._gen_host_templates(templates),
            'proxy_hostid': proxy_id,
        }

        if macros is not None:
            params['macros'] = self._gen_host_macros(macros)

        try:
            res = self.zapi.host.create(params)
        except ZabbixAPIException as e:
            # noinspection PyProtectedMember
            log(ERROR, 'Zabbix API Error in create_host(%s %s): %s', obj._meta.verbose_name_raw, obj, e)
            return False

        return parse_zabbix_result(res, 'hostids', from_get_request=False)
Beispiel #4
0
    def _update_service(self, serviceid, **params):
        """Update Zabbix IT Service"""
        params['serviceid'] = serviceid

        try:
            res = self.zapi.service.update(params)
        except ZabbixAPIException as e:
            logger.exception(e)
            raise InternalMonitoringError(text_type(e))

        return parse_zabbix_result(res, 'serviceids', from_get_request=False)
Beispiel #5
0
    def delete_host(self, hostid, log=None):
        """Delete host from Zabbix"""
        log = log or self.log

        try:
            res = self.zapi.host.delete([hostid])
        except ZabbixAPIException as e:
            log(ERROR, 'Zabbix API Error in delete_host(%s): %s', hostid, e)
            return False

        return parse_zabbix_result(res, 'hostids', from_get_request=False)
Beispiel #6
0
    def update_host(self, hostid, log=None, **params):
        """Update parameters of one Zabbix host identified by hostid. Return hostid of updated host or False/None"""
        log = log or self.log
        params['hostid'] = hostid

        try:
            res = self.zapi.host.update(params)
        except ZabbixAPIException as e:
            log(ERROR, 'Zabbix API Error in update_host(%s): %s', hostid, e)
            return False

        return parse_zabbix_result(res, 'hostids', from_get_request=False)
Beispiel #7
0
    def delete_node_service(self, zabbix_name):
        """Delete Zabbix IT Service for a Compute node.
        This operation is performed along with node removal from Zabbix, therefore it should fail silently"""
        serviceid = self._zabbix_get_serviceid(zabbix_name)
        child_serviceids = self._zabbix_get_children_serviceids(serviceid)

        # First delete all children
        for i in child_serviceids:
            try:
                self.zapi.service.delete([i])
            except ZabbixAPIException as e:
                logger.exception(e)

        # Finally delete the node IT service
        try:
            res = self.zapi.service.delete([serviceid])
        except ZabbixAPIException as e:
            logger.exception(e)
            raise InternalMonitoringError(e)

        return parse_zabbix_result(res, 'serviceids', from_get_request=False)
Beispiel #8
0
 def _zabbix_result(result, key):
     """Parse zabbix result"""
     return parse_zabbix_result(result, key)
Beispiel #9
0
 def parse_zabbix_create_result(*args, **kwargs):
     kwargs['from_get_request'] = False
     return parse_zabbix_result(*args, **kwargs)
Beispiel #10
0
 def parse_zabbix_get_result(*args, **kwargs):
     kwargs['from_get_request'] = True
     return parse_zabbix_result(*args, **kwargs)