pyzabbix icon indicating copy to clipboard operation
pyzabbix copied to clipboard

pyzabbix.api.ZabbixAPIException: ('Error -32602: Invalid params., Not authorized.', -32602)

Open macejko8 opened this issue 1 year ago • 10 comments

Hello I am using pyzabbix module and I am trying to connect to our zabbix but I am getting the following error:

File "/home/matej/.local/lib/python3.12/site-packages/pyzabbix/api.py", line 249, in do_request raise ZabbixAPIException( pyzabbix.api.ZabbixAPIException: ('Error -32602: Invalid params., Not authorized.', -32602)

here is my code:

#!/usr/bin/python3 from pyzabbix import ZabbixAPI import requests import os import json import csv

ZABBIX_USER= "************" ZABBIX_PASSWORD = "" zapi = ZabbixAPI("**********************") zapi.login(ZABBIX_USER, ZABBIX_PASSWORD)

descs = list() tags = list() b=zapi.template.get(output="extend") print(b)

Any idea how to fix it?

Thanks in advance

macejko8 avatar Oct 21 '24 16:10 macejko8

The code looks fine. What is the User Role for ZABBIX_USER? You might want to try with zapi.host.get first.

szuro avatar Oct 21 '24 18:10 szuro

user role is Super admin and it is the same also with zapi.host.get

macejko8 avatar Oct 22 '24 06:10 macejko8

I can confirm i have the same issue, i think the package python3-pyzabbix provided by Ubuntu is corrupted or not up to date. I made a test to understand: i got the api.py file from this repository and i tried to run the same script by changing the main import, and it works! Moreover, it seems the library version on the package is very old (for example it not support the login by api_key)....

Script run with api.py (IT WORKS):

import api
zapi = api.ZabbixAPI("http://ggggggggg/zabbix")

zapi.session.verify = False
zapi.timeout = 5

try:
	zapi.login("xxxxxxx", "yyyyyy")
	print("Versione API Zabbix:", zapi.api_version())

	for host in zapi.host.get(output="extend"):
		print(f"Host ID: {host['hostid']} - Hostname: {host['host']}")

except Exception as e:
	print(f"Errore di connessione: {e}")

Script run with standard package (IT DOESN'T WORK):

from pyzabbix import ZabbixAPI
zapi = api.ZabbixAPI("http://ggggggggg/zabbix")

zapi.session.verify = False
zapi.timeout = 5

try:
    zapi.login("xxxxxxx", "yyyyyy")

    print("Versione API Zabbix:", zapi.api_version())

    for host in zapi.host.get(output="extend"):
        print(f"Host ID: {host['hostid']} - Hostname: {host['host']}")

except Exception as e:
    print(f"Errore di connessione: {e}")

ducarpit avatar Oct 22 '24 12:10 ducarpit

@ducarpit I tried it as you descrived but I am getting the same error as before

macejko8 avatar Oct 22 '24 13:10 macejko8

@macejko8 my error was a little bit different:

('Error -32602: Invalid params., Invalid parameter "/": unexpected parameter "user".', -32602)

But probably it depends due to a similar issue.

ducarpit avatar Oct 22 '24 14:10 ducarpit

I can confirm i have the same issue, i think the package python3-pyzabbix provided by Ubuntu is corrupted or not up to date.

From what I see, Ubuntu shipps version 0.8.2 (https://launchpad.net/ubuntu/oracular/+package/python3-pyzabbix), which is a few years old.

Moreover, it does not include changes to the user.login method introduced by Zabbix 5.4 and 7.0.

Please install the newest pyzabbix via pip and try again.

szuro avatar Oct 22 '24 15:10 szuro

@szuro is there a way to request Ubuntu to update the package on the repository?

https://answers.launchpad.net/ubuntu/+source/pyzabbix/+question/819175

ducarpit avatar Oct 22 '24 16:10 ducarpit

Think we probably should update the spec: https://github.com/lukecyca/pyzabbix/blob/59ecdf9074678b0bedcaaeaeb24edec1081f1841/packaging/rpm/python-pyzabbix.spec#L20

Somehow it went under the radar...

szuro avatar Oct 22 '24 18:10 szuro

Hi all, i submitted a bug (update request) to Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1085875

ducarpit avatar Oct 23 '24 08:10 ducarpit

I actually had the newest pyzabbix version, I managed to fix the issue by downgrading to 0.8.2 and then adjusting the init.py file as described here: https://github.com/lukecyca/pyzabbix/issues/212

macejko8 avatar Oct 23 '24 13:10 macejko8