def __init__(self, server, timeout, user, passwd, validate_certs,
              **kwargs):
     ZabbixAPI.__init__(self,
                        server,
                        timeout=timeout,
                        user=user,
                        passwd=passwd,
                        validate_certs=validate_certs)
Exemplo n.º 2
0
 def __init__(self, server, timeout, user, passwd, validate_certs,
              **kwargs):
     ZabbixAPI.__init__(self,
                        server,
                        timeout=timeout,
                        user=user,
                        passwd=passwd,
                        validate_certs=validate_certs)
     self.hostinterface = ZabbixAPISubClass(
         self, dict({"prefix": "hostinterface"}, **kwargs))
Exemplo n.º 3
0
 def __init__(self, server, timeout, user, passwd, validate_certs,
              **kwargs):
     ZabbixAPI.__init__(self,
                        server,
                        timeout=timeout,
                        user=user,
                        passwd=passwd,
                        validate_certs=validate_certs)
     self.screenitem = ZabbixAPISubClass(
         self, dict({"prefix": "screenitem"}, **kwargs))
Exemplo n.º 4
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(type='str', required=True, aliases=['url']),
            login_user=dict(type='str', required=True),
            login_password=dict(type='str', required=True, no_log=True),
            proxy_name=dict(type='str', required=True),
            http_login_user=dict(type='str', required=False, default=None),
            http_login_password=dict(type='str', required=False,
                                     default=None, no_log=True),
            validate_certs=dict(type='bool', required=False, default=True),
            status=dict(default="active", choices=['active', 'passive']),
            state=dict(default="present", choices=['present', 'absent']),
            description=dict(type='str', required=False),
            tls_connect=dict(default='no_encryption',
                             choices=['no_encryption', 'PSK', 'certificate']),
            tls_accept=dict(default='no_encryption',
                            choices=['no_encryption', 'PSK', 'certificate']),
            ca_cert=dict(type='str', required=False, default=None, aliases=['tls_issuer']),
            tls_subject=dict(type='str', required=False, default=None),
            tls_psk_identity=dict(type='str', required=False, default=None),
            tls_psk=dict(type='str', required=False, default=None),
            timeout=dict(type='int', default=10),
            interface=dict(type='dict', required=False, default={})
        ),
        supports_check_mode=True
    )

    if not HAS_ZABBIX_API:
        module.fail_json(msg="Missing required jctanner.monitoring_zabbix.zabbix-api module" +
                             " (check docs or install with:" +
                             " pip install jctanner.monitoring_zabbix.zabbix-api)")

    server_url = module.params['server_url']
    login_user = module.params['login_user']
    login_password = module.params['login_password']
    http_login_user = module.params['http_login_user']
    http_login_password = module.params['http_login_password']
    validate_certs = module.params['validate_certs']
    proxy_name = module.params['proxy_name']
    description = module.params['description']
    status = module.params['status']
    tls_connect = module.params['tls_connect']
    tls_accept = module.params['tls_accept']
    tls_issuer = module.params['ca_cert']
    tls_subject = module.params['tls_subject']
    tls_psk_identity = module.params['tls_psk_identity']
    tls_psk = module.params['tls_psk']
    state = module.params['state']
    timeout = module.params['timeout']
    interface = module.params['interface']

    # convert enabled to 0; disabled to 1
    status = 6 if status == "passive" else 5

    if tls_connect == 'certificate':
        tls_connect = 4
    elif tls_connect == 'PSK':
        tls_connect = 2
    else:
        tls_connect = 1

    if tls_accept == 'certificate':
        tls_accept = 4
    elif tls_accept == 'PSK':
        tls_accept = 2
    else:
        tls_accept = 1

    zbx = None
    # login to jctanner.monitoring_zabbix.zabbix
    try:
        zbx = ZabbixAPI(server_url, timeout=timeout,
                        user=http_login_user,
                        passwd=http_login_password,
                        validate_certs=validate_certs)
        zbx.login(login_user, login_password)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

    proxy = Proxy(module, zbx)

    # check if proxy already exists
    proxy_id = proxy.proxy_exists(proxy_name)

    if proxy_id:
        if state == "absent":
            # remove proxy
            proxy.delete_proxy(proxy_id, proxy_name)
        else:
            proxy.update_proxy(proxy_id, {
                'host': proxy_name,
                'description': description,
                'status': str(status),
                'tls_connect': str(tls_connect),
                'tls_accept': str(tls_accept),
                'tls_issuer': tls_issuer,
                'tls_subject': tls_subject,
                'tls_psk_identity': tls_psk_identity,
                'tls_psk': tls_psk,
                'interface': interface
            })
    else:
        if state == "absent":
            # the proxy is already deleted.
            module.exit_json(changed=False)

        proxy_id = proxy.add_proxy(data={
            'host': proxy_name,
            'description': description,
            'status': str(status),
            'tls_connect': str(tls_connect),
            'tls_accept': str(tls_accept),
            'tls_issuer': tls_issuer,
            'tls_subject': tls_subject,
            'tls_psk_identity': tls_psk_identity,
            'tls_psk': tls_psk,
            'interface': interface
        })
Exemplo n.º 5
0
def main():
    module = AnsibleModule(
        argument_spec=dict(server_url=dict(type='str',
                                           required=True,
                                           aliases=['url']),
                           login_user=dict(type='str', required=True),
                           login_password=dict(type='str',
                                               required=True,
                                               no_log=True),
                           http_login_user=dict(type='str',
                                                required=False,
                                                default=None),
                           http_login_password=dict(type='str',
                                                    required=False,
                                                    default=None,
                                                    no_log=True),
                           validate_certs=dict(type='bool',
                                               required=False,
                                               default=True),
                           template_name=dict(type='str', required=False),
                           template_json=dict(type='json', required=False),
                           template_groups=dict(type='list', required=False),
                           link_templates=dict(type='list', required=False),
                           clear_templates=dict(type='list', required=False),
                           macros=dict(type='list', required=False),
                           state=dict(default="present",
                                      choices=['present', 'absent', 'dump']),
                           timeout=dict(type='int', default=10)),
        required_one_of=[['template_name', 'template_json']],
        supports_check_mode=True)

    if not HAS_ZABBIX_API:
        module.fail_json(
            msg="Missing required jctanner.monitoring_zabbix.zabbix-api module "
            + "(check docs or install with: " +
            "pip install jctanner.monitoring_zabbix.zabbix-api)")

    server_url = module.params['server_url']
    login_user = module.params['login_user']
    login_password = module.params['login_password']
    http_login_user = module.params['http_login_user']
    http_login_password = module.params['http_login_password']
    validate_certs = module.params['validate_certs']
    template_name = module.params['template_name']
    template_json = module.params['template_json']
    template_groups = module.params['template_groups']
    link_templates = module.params['link_templates']
    clear_templates = module.params['clear_templates']
    template_macros = module.params['macros']
    state = module.params['state']
    timeout = module.params['timeout']

    zbx = None

    # login to jctanner.monitoring_zabbix.zabbix
    try:
        zbx = ZabbixAPI(server_url,
                        timeout=timeout,
                        user=http_login_user,
                        passwd=http_login_password,
                        validate_certs=validate_certs)
        zbx.login(login_user, login_password)
    except ZabbixAPIException as e:
        module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

    template = Template(module, zbx)
    if template_json and not template_name:
        # Ensure template_name is not empty for safety check in import_template
        template_loaded = template.load_json_template(template_json)
        template_name = template_loaded[
            'jctanner.monitoring_zabbix.zabbix_export']['templates'][0][
                'template']
    elif template_name and not template_groups and state == 'present':
        module.fail_json(
            msg=
            "Option template_groups is required when template_json is not used"
        )

    template_ids = template.get_template_ids([template_name])
    existing_template_json = None
    if template_ids:
        existing_template_json = template.dump_template(template_ids)

    # delete template
    if state == "absent":
        # if template not found. no change, no fail
        if not template_ids:
            module.exit_json(changed=False,
                             msg="Template not found. " +
                             "No changed: %s" % template_name)
        template.delete_template(template_ids)
        module.exit_json(changed=True,
                         result="Successfully delete template %s" %
                         template_name)

    elif state == "dump":
        if not template_ids:
            module.fail_json(msg='Template not found: %s' % template_name)
        module.exit_json(changed=False, template_json=existing_template_json)

    elif state == "present":
        child_template_ids = None
        if link_templates is not None:
            existing_child_templates = None
            if existing_template_json:
                existing_child_templates = set(
                    list(tmpl['name'] for tmpl in existing_template_json[
                        'jctanner.monitoring_zabbix.zabbix_export']
                         ['templates'][0]['templates']))
            if not existing_template_json or set(
                    link_templates) != existing_child_templates:
                child_template_ids = template.get_template_ids(link_templates)

        clear_template_ids = []
        if clear_templates is not None:
            clear_template_ids = template.get_template_ids(clear_templates)

        group_ids = None
        if template_groups is not None:
            # If the template exists, compare the already set groups
            existing_groups = None
            if existing_template_json:
                existing_groups = set(
                    list(group['name'] for group in existing_template_json[
                        'jctanner.monitoring_zabbix.zabbix_export']['groups']))
            if not existing_groups or set(template_groups) != existing_groups:
                group_ids = template.get_group_ids_by_group_names(
                    template_groups)

        macros = None
        if template_macros is not None:
            existing_macros = None
            # jctanner.monitoring_zabbix.zabbix configuration.export does not differentiate python types (numbers are returned as strings)
            for macroitem in template_macros:
                for key in macroitem:
                    macroitem[key] = str(macroitem[key])

            if existing_template_json:
                existing_macros = existing_template_json[
                    'jctanner.monitoring_zabbix.zabbix_export']['templates'][
                        0]['macros']
            if not existing_macros or template_macros != existing_macros:
                macros = template_macros

        if not template_ids:
            template.add_template(template_name, template_json, group_ids,
                                  child_template_ids, macros)
            module.exit_json(changed=True,
                             result="Successfully added template: %s" %
                             template_name)
        else:
            changed = template.update_template(template_ids[0], template_json,
                                               group_ids, child_template_ids,
                                               clear_template_ids, macros,
                                               existing_template_json)
            module.exit_json(changed=changed,
                             result="Successfully updated template: %s" %
                             template_name)
Exemplo n.º 6
0
def main():
    module = AnsibleModule(argument_spec=dict(
        server_url=dict(type='str', required=True, aliases=['url']),
        login_user=dict(type='str', required=True),
        login_password=dict(type='str', required=True, no_log=True),
        http_login_user=dict(type='str', required=False, default=None),
        http_login_password=dict(type='str',
                                 required=False,
                                 default=None,
                                 no_log=True),
        timeout=dict(type='int', default=10),
        validate_certs=dict(type='bool', required=False, default=True),
        name=dict(type='str', required=True, aliases=['map_name']),
        data=dict(type='str', required=False, aliases=['dot_data']),
        width=dict(type='int', default=800),
        height=dict(type='int', default=600),
        state=dict(default="present", choices=['present', 'absent']),
        default_image=dict(type='str', required=False, aliases=['image']),
        margin=dict(type='int', default=40),
        expand_problem=dict(type='bool', default=True),
        highlight=dict(type='bool', default=True),
        label_type=dict(
            type='str',
            default='name',
            choices=['label', 'ip', 'name', 'status', 'nothing', 'custom']),
    ),
                           supports_check_mode=True)

    if not HAS_ZABBIX_API:
        module.fail_json(
            msg=
            "Missing required jctanner.monitoring_zabbix.zabbix-api module (check docs or install with: pip install jctanner.monitoring_zabbix.zabbix-api)"
        )
    if not HAS_PYDOTPLUS:
        module.fail_json(
            msg=
            "Missing required pydotplus module (check docs or install with: pip install pydotplus)"
        )
    if not HAS_WEBCOLORS:
        module.fail_json(
            msg=
            "Missing required webcolors module (check docs or install with: pip install webcolors)"
        )
    if not HAS_PIL:
        module.fail_json(
            msg=
            "Missing required Pillow module (check docs or install with: pip install Pillow)"
        )

    server_url = module.params['server_url']
    login_user = module.params['login_user']
    login_password = module.params['login_password']
    http_login_user = module.params['http_login_user']
    http_login_password = module.params['http_login_password']
    timeout = module.params['timeout']
    validate_certs = module.params['validate_certs']

    zbx = None

    # login to jctanner.monitoring_zabbix.zabbix
    try:
        zbx = ZabbixAPI(server_url,
                        timeout=timeout,
                        user=http_login_user,
                        passwd=http_login_password,
                        validate_certs=validate_certs)
        zbx.login(login_user, login_password)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

    sysmap = Map(module, zbx)

    if sysmap.state == "absent":
        if sysmap.map_exists():
            sysmap.delete_map()
            module.exit_json(changed=True,
                             result="Successfully deleted map: %s" %
                             sysmap.map_name)
        else:
            module.exit_json(changed=False)
    else:
        map_config = sysmap.get_map_config()
        if sysmap.map_exists():
            if sysmap.is_exist_map_correct(map_config):
                module.exit_json(changed=False)
            else:
                sysmap.update_map(map_config)
                module.exit_json(changed=True,
                                 result="Successfully updated map: %s" %
                                 sysmap.map_name)
        else:
            sysmap.create_map(map_config)
            module.exit_json(changed=True,
                             result="Successfully created map: %s" %
                             sysmap.map_name)
Exemplo n.º 7
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(required=False,
                       default='present',
                       choices=['present', 'absent']),
            server_url=dict(type='str',
                            required=True,
                            default=None,
                            aliases=['url']),
            host_names=dict(type='list',
                            required=False,
                            default=None,
                            aliases=['host_name']),
            minutes=dict(type='int', required=False, default=10),
            host_groups=dict(type='list',
                             required=False,
                             default=None,
                             aliases=['host_group']),
            login_user=dict(type='str', required=True),
            login_password=dict(type='str', required=True, no_log=True),
            validate_certs=dict(type='bool', required=False, default=True),
            http_login_user=dict(type='str', required=False, default=None),
            http_login_password=dict(type='str',
                                     required=False,
                                     default=None,
                                     no_log=True),
            name=dict(type='str', required=True),
            desc=dict(type='str', required=False,
                      default="Created by Ansible"),
            collect_data=dict(type='bool', required=False, default=True),
            timeout=dict(type='int', default=10),
        ),
        supports_check_mode=True,
    )

    if not HAS_ZABBIX_API:
        module.fail_json(
            msg=
            "Missing required jctanner.monitoring_zabbix.zabbix-api module (check docs or install with: pip install jctanner.monitoring_zabbix.zabbix-api)"
        )

    host_names = module.params['host_names']
    host_groups = module.params['host_groups']
    state = module.params['state']
    login_user = module.params['login_user']
    login_password = module.params['login_password']
    http_login_user = module.params['http_login_user']
    http_login_password = module.params['http_login_password']
    validate_certs = module.params['validate_certs']
    minutes = module.params['minutes']
    name = module.params['name']
    desc = module.params['desc']
    server_url = module.params['server_url']
    collect_data = module.params['collect_data']
    timeout = module.params['timeout']

    if collect_data:
        maintenance_type = 0
    else:
        maintenance_type = 1

    try:
        zbx = ZabbixAPI(server_url,
                        timeout=timeout,
                        user=http_login_user,
                        passwd=http_login_password,
                        validate_certs=validate_certs)
        zbx.login(login_user, login_password)
    # jctanner.monitoring_zabbix.zabbix_api can call sys.exit() so we need to catch SystemExit here
    except (Exception, SystemExit) as e:
        module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

    changed = False

    if state == "present":

        if not host_names and not host_groups:
            module.fail_json(
                msg=
                "At least one host_name or host_group must be defined for each created maintenance."
            )

        now = datetime.datetime.now().replace(second=0)
        start_time = time.mktime(now.timetuple())
        period = 60 * int(minutes)  # N * 60 seconds

        if host_groups:
            (rc, group_ids, error) = get_group_ids(zbx, host_groups)
            if rc != 0:
                module.fail_json(msg="Failed to get group_ids: %s" % error)
        else:
            group_ids = []

        if host_names:
            (rc, host_ids, error) = get_host_ids(zbx, host_names)
            if rc != 0:
                module.fail_json(msg="Failed to get host_ids: %s" % error)
        else:
            host_ids = []

        (rc, maintenance, error) = get_maintenance(zbx, name)
        if rc != 0:
            module.fail_json(
                msg="Failed to check maintenance %s existence: %s" %
                (name, error))

        if maintenance and (
                sorted(group_ids) != sorted(maintenance["groupids"])
                or sorted(host_ids) != sorted(maintenance["hostids"])
                or str(maintenance_type) != maintenance["maintenance_type"]
                or str(int(start_time)) != maintenance["active_since"] or
                str(int(start_time + period)) != maintenance["active_till"]):
            if module.check_mode:
                changed = True
            else:
                (rc, _,
                 error) = update_maintenance(zbx, maintenance["maintenanceid"],
                                             group_ids, host_ids, start_time,
                                             maintenance_type, period, desc)
                if rc == 0:
                    changed = True
                else:
                    module.fail_json(msg="Failed to update maintenance: %s" %
                                     error)

        if not maintenance:
            if module.check_mode:
                changed = True
            else:
                (rc, _, error) = create_maintenance(zbx, group_ids, host_ids,
                                                    start_time,
                                                    maintenance_type, period,
                                                    name, desc)
                if rc == 0:
                    changed = True
                else:
                    module.fail_json(msg="Failed to create maintenance: %s" %
                                     error)

    if state == "absent":

        (rc, maintenance, error) = get_maintenance(zbx, name)
        if rc != 0:
            module.fail_json(
                msg="Failed to check maintenance %s existence: %s" %
                (name, error))

        if maintenance:
            if module.check_mode:
                changed = True
            else:
                (rc, _,
                 error) = delete_maintenance(zbx, maintenance["maintenanceid"])
                if rc == 0:
                    changed = True
                else:
                    module.fail_json(msg="Failed to remove maintenance: %s" %
                                     error)

    module.exit_json(changed=changed)
Exemplo n.º 8
0
def main():
    module = AnsibleModule(argument_spec=dict(
        server_url=dict(type='str', required=True, aliases=['url']),
        login_user=dict(type='str', required=True),
        login_password=dict(type='str', required=True, no_log=True),
        http_login_user=dict(type='str', required=False, default=None),
        http_login_password=dict(type='str',
                                 required=False,
                                 default=None,
                                 no_log=True),
        validate_certs=dict(type='bool', required=False, default=True),
        host_groups=dict(type='list', required=True, aliases=['host_group']),
        state=dict(default="present", choices=['present', 'absent']),
        timeout=dict(type='int', default=10)),
                           supports_check_mode=True)

    if not HAS_ZABBIX_API:
        module.fail_json(
            msg=
            "Missing required jctanner.monitoring_zabbix.zabbix-api module (check docs or install with: pip install jctanner.monitoring_zabbix.zabbix-api)"
        )

    server_url = module.params['server_url']
    login_user = module.params['login_user']
    login_password = module.params['login_password']
    http_login_user = module.params['http_login_user']
    http_login_password = module.params['http_login_password']
    validate_certs = module.params['validate_certs']
    host_groups = module.params['host_groups']
    state = module.params['state']
    timeout = module.params['timeout']

    zbx = None

    # login to jctanner.monitoring_zabbix.zabbix
    try:
        zbx = ZabbixAPI(server_url,
                        timeout=timeout,
                        user=http_login_user,
                        passwd=http_login_password,
                        validate_certs=validate_certs)
        zbx.login(login_user, login_password)
    except Exception as e:
        module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

    hostGroup = HostGroup(module, zbx)

    group_ids = []
    group_list = []
    if host_groups:
        group_ids, group_list = hostGroup.get_group_ids(host_groups)

    if state == "absent":
        # delete host groups
        if group_ids:
            delete_group_names = []
            hostGroup.delete_host_group(group_ids)
            for group in group_list:
                delete_group_names.append(group['name'])
            module.exit_json(changed=True,
                             result="Successfully deleted host group(s): %s." %
                             ",".join(delete_group_names))
        else:
            module.exit_json(changed=False,
                             result="No host group(s) to delete.")
    else:
        # create host groups
        group_add_list = hostGroup.create_host_group(host_groups)
        if len(group_add_list) > 0:
            module.exit_json(changed=True,
                             result="Successfully created host group(s): %s" %
                             group_add_list)
        else:
            module.exit_json(changed=False)