Up until today we have been using Zabbix version 6.0lts, we decided to upgrade to version 7.0lts Currently running Alma Linux 8, zabbix 7.0.9lts and mysql. We had a script that we use to add multiple servers at once to zabbix via the command line. The script is a python 3 script that uses the ZabbixAPI. You add the servers into a CSV file, then the script checks if the servers are already added, if not it runs through and adds them.
Here is a copy of the script:
This worked fine on version 6, but it seems to have changed on version 7.
When I try to run the script I get the following error:
I logged a call with zabbix support, they said that between zabbix 6 and 7 the login authentication has changed. I am having trouble getting it to autheticate.
Here is a copy of the script:
Code:
#!/usr/bin/env python3
# import hosts from txt file to Zabbix via API
# and assign template and host group to them
# python3 required
# dnf install python3-pip
# pip3 install zabbix_api
# script is tested and works with module 'zabbix_api'
# which is not the same as 'pyzabbix' !
import csv
from zabbix_api import ZabbixAPI
import urllib3
urllib3.disable_warnings(urllib3.exceptions.Insecu reRequestWarning)
import sys
sys.path.insert(0,'/var/lib/zabbix')
# pip install config
import config
ZABBIX_SERVER = config.url
zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.session.verify=False
zapi.login(config.username, config.password)
file = open("servers.csv",'rt')
reader = csv.DictReader( file )
# take the file and read line by line
for line in reader:
# check if this host exists in zabbix
if not zapi.host.get({"filter":{"host" :line['name']}}):
print (line['name'],"not yet registred")
if zapi.proxy.get({"output": "proxyid","selectInterface": "extend","filter":{"host":line['proxy']}}):
proxy_id=zapi.proxy.get({"output": "proxyid","selectInterface": "extend","filter":{"host":line['proxy']}})[0]['proxyid']
print (line['proxy'])
if int(proxy_id)>0:
templates=line['template'].split(";")
groups=line['group'].split(";")
group_id = zapi.hostgroup.get({"filter" : {"name" : groups}, "output": "groupid"})
template_id = zapi.template.get({"filter" : {"name" : templates}, "output": "templateid"})
# crete a host an put hostid instantly in the 'hostid' variable
hostid = zapi.host.create ({
"host":line['name'],"interfaces":[{"type":1,"dns": line['dns'],"main":1,"ip": line['address'],"port": 10050,"useip": 1,"details":{"version":"2","bulk":"1","community ": "{$SNMP_COMMUNITY}"}}],
"groups": group_id ,
"proxy_hostid":proxy_id,
"templates": template_id })['hostids']
# if there are no proxy
else:
print ("proxy does not exist. creating with none")
templates=line['template'].split(";")
groups=line['group'].split(";")
group_id = zapi.hostgroup.get({"filter" : {"name" : groups}, "output": "groupid"})
template_id = zapi.template.get({"filter" : {"name" : templates}, "output": "templateid"})
# create a host an put hostid instantly in the 'hostid' variable
hostid = zapi.host.create ({
"host":line['name'],"interfaces":[{"type":1,"dns": line['dns'],"main":1,"ip": line['address'],"port": 10050,"useip": 1,"details":{"version":"2","bulk":"1","community ": "{$SNMP_COMMUNITY}"}}],
"groups": group_id,
"templates": template_id })['hostids']
else:
print (line['name'], " already exists")
file.close()
When I try to run the script I get the following error:
Code:
Traceback (most recent call last):
File "./vm.py", line 25, in <module>
zapi.login(config.username, config.password)
File "/usr/local/lib/python3.6/site-packages/zabbix_api.py", line 207, in login
result = self.do_request(obj)
File "/usr/local/lib/python3.6/site-packages/zabbix_api.py", line 299, in do_request
raise ZabbixAPIException(msg, jobj['error']['code'])
zabbix_api.ZabbixAPIException: ('Error -32602: Invalid params., Invalid parameter "/": unexpected parameter "user". while sending {"jsonrpc": "2.0", "method": "user.login", "params": {"user": "example", "password": "example"}, "id": 0}', -32602)
I logged a call with zabbix support, they said that between zabbix 6 and 7 the login authentication has changed. I am having trouble getting it to autheticate.
Comment