--- zabbix_api.orig 2017-05-23 18:25:01.000000000 +0300 +++ zabbix_api 2017-12-04 17:55:34.631651402 +0200 @@ -322,6 +322,66 @@ except (KeyError, IndexError): raise ZabbixAPIError('Error: cannot update host id: %s' % hostid) + def __update_host_tls_connect(self, hostid, tls_connect): + req = { + 'method': 'host.update', + 'params': { + 'hostid': hostid, + 'tls_connect': tls_connect, + }, + } + + result = self.__zconn.send(req) + try: + return result['hostids'][0] + except (KeyError, IndexError): + raise ZabbixAPIError('Error: cannot update host id: %s' % hostid) + + def __update_host_tls_accept(self, hostid, tls_accept): + req = { + 'method': 'host.update', + 'params': { + 'hostid': hostid, + 'tls_accept': tls_accept, + }, + } + + result = self.__zconn.send(req) + try: + return result['hostids'][0] + except (KeyError, IndexError): + raise ZabbixAPIError('Error: cannot update host id: %s' % hostid) + + def __update_host_tls_psk(self, hostid, tls_psk): + req = { + 'method': 'host.update', + 'params': { + 'hostid': hostid, + 'tls_psk': tls_psk, + }, + } + + result = self.__zconn.send(req) + try: + return result['hostids'][0] + except (KeyError, IndexError): + raise ZabbixAPIError('Error: cannot update host id: %s' % hostid) + + def __update_host_tls_psk_identity(self, hostid, tls_psk_identity): + req = { + 'method': 'host.update', + 'params': { + 'hostid': hostid, + 'tls_psk_identity': tls_psk_identity, + }, + } + + result = self.__zconn.send(req) + try: + return result['hostids'][0] + except (KeyError, IndexError): + raise ZabbixAPIError('Error: cannot update host id: %s' % hostid) + def __compare_ip(self, hostdump, ip): for interface in hostdump['interfaces']: if interface['main'] == '1' and interface['type'] == '1': @@ -368,6 +428,26 @@ self.__changed.append('link to template(s): %s' %(', ').join(templates)) break + def __compare_tls_connect(self, hostdump, tls_connect): + if hostdump['tls_connect'] != tls_connect: + self.__update_host_tls_connect(hostdump['hostid'], tls_connect) + self.__changed.append('set tls_connect: %s' %tls_connect) + + def __compare_tls_accept(self, hostdump, tls_accept): + if hostdump['tls_accept'] != tls_accept: + self.__update_host_tls_accept(hostdump['hostid'], tls_accept) + self.__changed.append('set tls_accept: %s' %tls_accept) + + def __compare_tls_psk(self, hostdump, tls_psk): + if hostdump['tls_psk'] != tls_psk: + self.__update_host_tls_psk(hostdump['hostid'], tls_psk) + self.__changed.append('set tls_psk: %s' %tls_psk) + + def __compare_tls_psk_identity(self, hostdump, tls_psk_identity): + if hostdump['tls_psk_identity'] != tls_psk_identity: + self.__update_host_tls_psk_identity(hostdump['hostid'], tls_psk_identity) + self.__changed.append('set tls_psk_identity: %s' %tls_psk_identity) + def __list_to_dict(self, list): i = iter(list) return dict(itertools.izip(i, i)) @@ -404,7 +484,7 @@ list_validated.append(item) return list_validated - def sync_host(self, hostname, ip, port1, port4, hostgroups, macros_list, templates): + def sync_host(self, hostname, ip, port1, port4, hostgroups, macros_list, templates, tls_connect, tls_accept, tls_psk_identity, tls_psk): hostdump = self.__get_host_if_exists(hostname) hostgroups = self.__validate_list(hostgroups) templates = self.__validate_list(templates) @@ -417,11 +497,15 @@ self.__compare_hostgroups(hostdump, hostgroups) self.__compare_templates(hostdump, templates) self.__compare_macros(hostdump, macros_list) + self.__compare_tls_connect(hostdump, tls_connect) + self.__compare_tls_accept(hostdump, tls_accept) + self.__compare_tls_psk(hostdump, tls_psk) + self.__compare_tls_psk_identity(hostdump, tls_psk_identity) if hostdump['status'] != '0': self.__update_host_status(hostdump['hostid'], 0) self.__changed.append('enable host') else: - hostid = self.__create_host(hostname, ip, port1, port4, hostgroups, templates) + hostid = self.__create_host(hostname, ip, port1, port4, hostgroups, templates, tls_connect, tls_accept, tls_psk_identity, tls_psk) self.__changed.append('create host') macros = self.__list_to_dict(macros_list) for macro, value in macros.items(): @@ -439,7 +523,7 @@ templateids.append({'templateid': self.__find_templateid(template)}) return templateids - def __create_host(self, hostname, ip, port1, port4, hostgroups, templates): + def __create_host(self, hostname, ip, port1, port4, hostgroups, templates, tls_connect, tls_accept, tls_psk_identity, tls_psk): if port4 != '': req = { 'method': 'host.create', @@ -465,6 +549,10 @@ ], 'groups': self.__get_hostgroupids(hostgroups), 'templates': self.__get_templateids(templates), + 'tls_connect': tls_connect, + 'tls_accept': tls_accept, + 'tls_psk_identity': tls_psk_identity, + 'tls_psk': tls_psk, }, } else: @@ -482,6 +570,10 @@ }, 'groups': self.__get_hostgroupids(hostgroups), 'templates': self.__get_templateids(templates), + 'tls_connect': tls_connect, + 'tls_accept': tls_accept, + 'tls_psk_identity': tls_psk_identity, + 'tls_psk': tls_psk, }, } @@ -588,6 +680,10 @@ groups=dict(default=None), templates=dict(default=None), macros=dict(default=None), + tls_connect=dict(default=1), + tls_accept=dict(default=1), + tls_psk_identity=dict(default=None), + tls_psk=dict(default=None), all_hosts=dict(default=None), remove=dict(default=False, type='bool'), api_url=dict(required=True, default=None), @@ -616,6 +712,10 @@ module.params['groups'] module.params['default_group'] module.params['macros'] + module.params['tls_connect'] + module.params['tls_accept'] + module.params['tls_psk_identity'] + module.params['tls_psk'] except NameError: module.fail_json(msg='missing parameter(s) for action %s' % module.params['action']) @@ -639,6 +739,10 @@ groups, macros, templates, + module.params['tls_connect'], + module.params['tls_accept'], + module.params['tls_psk_identity'], + module.params['tls_psk'], ) except ZabbixAPIError, e: module.fail_json(msg=str(e))