lukecyca/pyzabbix

Updating Zabbix Global Macros

Falcon-XR opened this issue · 1 comments

Hi,
I've searched all over and can't seem to see anything that would answer my question.

I am trying to update a global macro inside our zabbix server. My reason for doing this is Zabbix doesn't allow values from an item to be added to a macro. So my intention is to run this code everyday so that a date 30 days ahead will be updated into a macro. This macro will make up part of a web scenario - basically trying to find the response time to a query for one of our web servers. (you probably didn't need to know all that but it gives it some context)
I am absolutely, and in no way, a coder. I decided on Python as it appears to be far less evil than Javascript :)

I know the code below gives the date and date format I want however but when it tries to update the macro it doesn't enjoy life at all and creates the following error:

pyzabbix.api.ZabbixAPIException: ('Error -32602: Invalid params., Invalid parameter "/1": unexpected parameter "globalmacro".', -32602)

The global macro does exist and the date that is the other parameter is 2023-04-22 (well for today anyway :)). I am thinking that pyzabbix doesn't support updating global macros?

import pyzabbix
import datetime

Set up the Zabbix API connection

zabbix_url = 'http://blahblahblah/zabbix/api_jsonrpc.php'
zabbix_user = 'username'
zabbix_pass = 'password'
zabbix_api = pyzabbix.ZabbixAPI(zabbix_url)
zabbix_api.login(zabbix_user, zabbix_pass)

Calculate the date 30 days from now

today = datetime.date.today()
future_date = today + datetime.timedelta(days=30)
future_date_string = future_date.strftime('%Y-%m-%d')

Update the Zabbix global macro with the new date

macro_name = '{$DATE_TEST_30}'
macro_value = future_date_string
zabbix_api.usermacro.update(globalmacro=macro_name, value=macro_value)

Close the Zabbix API connection

zabbix_api.user.logout()

Any assistance would be greatly appreciated
Cheers
Marc

jooola commented

I'll assume you have zabbix 6.4 since you didn't provide any version.

If you look at the zabbix docs:

You should see that you need globalmacroid to update the macro.

I recommend you to query any object before trying to update it, for macros or any other operation with this library.