Zabbix. Task. Getting JSON with SEVERAL variables. Python: различия между версиями

Материал из Wiki
Перейти к навигации Перейти к поиску
м (Cless переименовал страницу Zabbix. Task. Getting JSON with several variables for putting them to autodiscoverable Items and Triggers в Zabbix. Task. Getting JSON with SEVERAL variables. Python без оставления перенаправления)
 
(нет различий)

Текущая версия на 16:33, 5 октября 2023

Description

Getting JSON with several variables ({#DOMAINNAME}, {#SERVICETYPE}, {#SERVICEID}) for putting them to autodiscoverable Items and Triggers using Python.

Configuration

Source json "servicesList":

{'answer': {'services': [{'creation_date': '2017-02-26', 'dname': 'cless.pro', 'expiration_date': '2023-02-26', 'service_id': 26891995, 'servtype': 'domain', 'state': 'A', 'subtype': '', 'uplink_service_id': 0}]}, 'charset': 'utf-8', 'messagestore': None, 'result': 'success'}

Python Script

...
# null string list of services
trunksNamesListJsonForZabbix = ""

# add str data to str json
trunksNamesListJsonForZabbix = (trunksNamesListJsonForZabbix + "{\"data\":[")

# for each service in list
for x in range(len(servicesList)):

    # get dict of services
    serviceDict = servicesList[x]

    # debug
    #print(type(serviceDict).__name__)

    # define vars
    serviceName = serviceDict['dname']
    serviceType = serviceDict['servtype']
    serviceId = serviceDict['service_id']

    # debug
    #print(serviceName + ' ' + serviceType + ' ' + str(serviceId))

    # construct json for zabbix
    trunksNamesListJsonForZabbix = (
                trunksNamesListJsonForZabbix + "{\"{#DOMAINNAME}\"" + ":" + "\"" + serviceName + "\"" + ","
                + "\"{#SERVICETYPE}\"" + ":" + "\"" + serviceType + "\"" + ","
                + "\"{#SERVICEID}\"" + ":" + "\"" + str(serviceId) + "\"" + "}")

    # add coma if not final service in list
    if not x == len(servicesList) - 1:
        trunksNamesListJsonForZabbix = (trunksNamesListJsonForZabbix + ",")

# add final round bracket to json for zabbix
trunksNamesListJsonForZabbix = (trunksNamesListJsonForZabbix + "]}")

# print json of services for zabbix to output
print(trunksNamesListJsonForZabbix)
...

There will be a JSON "trunksNamesListJsonForZabbix" at the output:

{"data":[{"{#DOMAINNAME}":"cless.pro","{#SERVICETYPE}":"domain","{#SERVICEID}":"26891995"}]}