Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpapi not working with Zabbix 7.2.0 #1431

Open
czertique opened this issue Dec 15, 2024 · 14 comments · May be fixed by #1442
Open

httpapi not working with Zabbix 7.2.0 #1431

czertique opened this issue Dec 15, 2024 · 14 comments · May be fixed by #1442

Comments

@czertique
Copy link

SUMMARY

Authentication (both user/password and API token) not working with Zabbix 7.2.0.

This is happening because auth payload property has been deprecated ever since Zabbix 6.4 and finally removed in Zabbix 7.2.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

httpapi

ANSIBLE VERSION
ansible [core 2.17.7]
  config file = /etc/ansible/ansible.cfg
  python version = 3.10.12 (main, Nov  6 2024, 20:22:13) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
CONFIGURATION
CONFIG_FILE() = /etc/ansible/ansible.cfg
PERSISTENT_COMMAND_TIMEOUT(/etc/ansible/ansible.cfg) = 1000
PERSISTENT_CONNECT_TIMEOUT(/etc/ansible/ansible.cfg) = 1000
OS / ENVIRONMENT / Zabbix Version

I'm doing this on Ubuntu 22.04 but I believe it will behave the same on any other OS

STEPS TO REPRODUCE

Try to add new host to Zabbix (but will likely happen with any other API call against Zabbix). Tested both username/password and API auth key

- name: Add zabbix host to zabbix server
  delegate_to: zabbix_host
  tags:
    - zabbix
    - addhost
  vars:
    zabbix_interfaces:
      - type: 1
        ip: "1.2.3.4"
        dns: "host.domain"
        useip: "0"
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 80
    ansible_httpapi_use_ssl: false
    ansible_zabbix_url_path: ''
    ansible_zabbix_auth_key: abc123
    # ansible_user: _ansible
    # ansible_httpapi_pass: ansible_pass
  community.zabbix.zabbix_host:
    host_name: "{{ inventory_hostname }}"
    visible_name: "{{ inventory_hostname }}"
    state: present
    status: enabled
    interfaces: "{{ zabbix_interfaces }}"
    link_templates:
      - Linux by Zabbix agent
    host_groups:
      - Linux servers
EXPECTED RESULTS

Host is added to Zabbix server

ACTUAL RESULTS

Task fails with error: 'message': 'Invalid request.', 'data': 'Invalid parameter \"/\": unexpected parameter \"auth\".'}

    "msg": "connection error occurred: REST API returned {'code': -32600, 'message': 'Invalid request.', 'data': 'Invalid parameter \"/\": unexpected parameter \"auth\".'} when sending {\"jsonrpc\": \"2.0\", \"method\": \"template.get\", \"id\": \"090b5f4c-a0b9-437f-84d0-442a378f25b1\", \"params\": {\"output\": [\"templateid\"], \"filter\": {\"host\": \"Linux by Zabbix agent\"}}, \"auth\": \"e6f472a89054c8246177864547e905b1a3bee93af69b854a580d242c7d0df1db\"}"
@czertique
Copy link
Author

This is my first issue ever on Github, I have read the issue guidelines, did not find any previous reports of this and tried to do it right, but if I missed something I apologize.

I have also made a patch that seems to be working for me, but as I am unable to test it properly, I did not make it into a pull request. I am not really sure whether this will work with different Zabbix versions and/or if this breaks anything else but it seems to be working for me. Also I am not sure whether it is OK to add dependency on re in the script, so I'll leave that to someone else.

*** plugins/httpapi/zabbix.py.orig      2024-12-15 14:38:07.708512502 +0000
--- plugins/httpapi/zabbix.py   2024-12-15 14:23:24.858674887 +0000
***************
*** 52,57 ****
--- 52,58 ----

  import json
  import base64
+ import re

  from uuid import uuid4

***************
*** 134,146 ****
          if not data:
              data = {}

-         if self.connection._auth:
-             data['auth'] = self.connection._auth['auth']
-
          hdrs = {
              'Content-Type': 'application/json-rpc',
              'Accept': 'application/json',
          }
          http_login_user = self.get_option('http_login_user')
          http_login_password = self.get_option('http_login_password')
          if http_login_user and http_login_user != '-42':
--- 135,152 ----
          if not data:
              data = {}

          hdrs = {
              'Content-Type': 'application/json-rpc',
              'Accept': 'application/json',
          }
+
+         if hasattr(self.connection, 'zbx_api_version') and re.match('^7\..*$', self.connection.zbx_api_version):
+             if self.connection._auth:
+                 hdrs['Authorization'] = ('Bearer %s' % (self.connection._auth['auth']))
+         else:
+             if self.connection._auth:
+                 data['auth'] = self.connection._auth['auth']
+
          http_login_user = self.get_option('http_login_user')
          http_login_password = self.get_option('http_login_password')
          if http_login_user and http_login_user != '-42':

@Airler
Copy link

Airler commented Dec 16, 2024

There are more changes for deprecated params required which were removed on 7.2 release.
For example groupids and hostids on maintenance.create and maintenance.update are also removed now.

@Airler
Copy link

Airler commented Dec 16, 2024

There are more changes for deprecated params required which were removed on 7.2 release. For example groupids and hostids on maintenance.create and maintenance.update are also removed now.

Just found a list of I hope all deprecated params removed in 7.2: https://www.zabbix.com/documentation/7.2/en/manual/api/changes

@czaban
Copy link

czaban commented Dec 25, 2024

The matter is urgent to solve because downgrading 7.2 to 7.0 is not easy. Where can I download the version with the cztique patch or when will the new version be released, please?

@czertique
Copy link
Author

I have applied the patch (slightly modified) in a fork: https://github.com/czertique/community.zabbix/tree/zabbix-720-apifix

Feel free to test / review that, but as I am unable to test it properly (my use case is pretty basic and I only run zabbix 7.2.0), I would not consider it production ready :)

@czaban
Copy link

czaban commented Dec 29, 2024

Thank you, I applied this by:
ansible-galaxy collection install git+https://github.com/czertique/community.zabbix.git,zabbix-720-apifix --force
There is a progress.
Unfortunetly the task:

- name: Create a new host or rewrite an existing host's info in Zabbix
    community.zabbix.zabbix_host:
      host_name: "{{ clientId }}"
      visible_name: "{{ clientName }}"
      description: iGuard
      host_groups:
        - Greggs
      link_templates:
        - ICMP Ping
        - Linux by Zabbix agent
        - PostgreSQL by Zabbix agent 2
        - Redis by Zabbix agent 2
        - SMART by Zabbix agent 2
        - NVidia Sensors
        - MySQL by Zabbix agent 2
        - Template iGuard streams
      status: enabled
      state: present
      inventory_mode: manual
      interfaces:
        - type: 1
          main: 1
          useip: 1
          ip: "{{ clientIp }}"
          dns: ""
          port: "10050"
    delegate_to: "{{ ansible_zabbix_server_ip }}"

fail with message:
fatal: [S0778V5 -> 192.168.100.112]: FAILED! => {"changed": false, "msg": "Template not found: ICMP Ping"}

Template exist on server side.

@PrismaComputer
Copy link

Are we absolutely sure that this is deep deep down not related to each other?
#1437
Note, the ticket above isn't solved, but closed by the maintainer.

@czaban
Copy link

czaban commented Jan 9, 2025

ansible_zabbix_url_path: '' according to #1437 was set.

@polcape
Copy link
Contributor

polcape commented Jan 9, 2025

I have applied the patch (slightly modified) in a fork: https://github.com/czertique/community.zabbix/tree/zabbix-720-apifix

Feel free to test / review that, but as I am unable to test it properly (my use case is pretty basic and I only run zabbix 7.2.0), I would not consider it production ready :)

I applied your fix but now I get error about groupids as @Airler said.

@AnatomicJC
Copy link

Your patch did the trick !

ansible-galaxy collection install git+https://github.com/czertique/community.zabbix.git,zabbix-720-apifix --force

@polcape
Copy link
Contributor

polcape commented Jan 13, 2025

Your patch did the trick !

ansible-galaxy collection install git+https://github.com/czertique/community.zabbix.git,zabbix-720-apifix --force

But it works only for auth not for groupids.

@mu1f407 mu1f407 linked a pull request Jan 13, 2025 that will close this issue
@polcape
Copy link
Contributor

polcape commented Jan 16, 2025

I confirm with this PR it works! Thanks @mu1f407

@tolgaulas
Copy link

I confirm with this PR it works! Thanks @mu1f407

Yes, i applied and started working on 7.2 too.. Thanks @mu1f407

@mu1f407
Copy link
Contributor

mu1f407 commented Jan 18, 2025

Thanks for testing guys! The PR is now ready for review by maintainers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants