Skip to content

Instantly share code, notes, and snippets.

@selivan selivan/
Last active Apr 24, 2018

What would you like to do?
#!/usr/bin/env python3
Import XML configuration files using Zabbix API:
import argparse
from urllib import request
import json
import sys
from pprint import pformat
def zbxrequest(url, method, auth, params):
if params is None:
params = {}
data = { "jsonrpc": "2.0", "id": 1, "method": method, "auth": auth, "params": params }
# Convert to string and then to byte
data = json.dumps(data).encode('utf-8')
req = request.Request(args.url, headers={'Content-Type': 'application/json'}, data=data)
resp = request.urlopen(req)
# Get string
resp ='utf-8')
# Convert to object
resp = json.loads(resp, encoding='utf-8')
return resp
# Parse command line arguments
parser = argparse.ArgumentParser(description='Import XML configuration files using Zabbix API')
parser.add_argument('-u', '--user', required=True, help='user name')
parser.add_argument('-p', '--password', '--pass', required=True, help='password', metavar='PASSWORD')
parser.add_argument('-s', '--url', default='',
help='Zabbix API URL, default is')
args = parser.parse_args()
# TODO: add API version check
# r=zbxrequest(args.url, method="apiinfo.version", auth=None, params={})
# print(r)
# Get authentication token
auth_result = zbxrequest(args.url, method="user.login", auth=None,
params={"user": args.user, "password": args.password})
# If authentication was not OK
if 'result' not in auth_result:
raise Exception('ERROR: auth failed\n' + pformat(auth_result))
auth_token = auth_result['result']
# Read template file content
with open(args.template_file, 'r', encoding='utf-8') as f:
source =
# Set import parameters, including template file content
params = {'format': 'xml',
'rules': {'groups': {'createMissing': True},
'hosts': {'createMissing': True, 'updateExisting': True},
'items': {'createMissing': True, 'updateExisting': True},
'applications': {'createMissing': True},
'templates': {'createMissing': True, 'updateExisting': True},
'templateLinkage': {'createMissing': True},
'templateScreens': {'createMissing': True, 'updateExisting': True},
'discoveryRules': {'createMissing': True, 'updateExisting': True},
'triggers': {'createMissing': True, 'updateExisting': True},
'graphs': {'createMissing': True, 'updateExisting': True},
'valueMaps': {'createMissing': True},
'images': {'createMissing': True, 'updateExisting': True},
'maps': {'createMissing': True, 'updateExisting': True},
'screens': {'createMissing': True, 'updateExisting': True}
'source': source
import_result = zbxrequest(args.url, method="configuration.import", auth=auth_token, params=params)
# Something like: {'id': 1, 'jsonrpc': '2.0', 'result': True}
if 'result' in import_result and import_result['result']:
print('SUCCESS: configuration import')
raise Exception('ERROR: configuration import failed\n' + pformat(import_result))
exit_code = 0
except Exception as e:
print(str(e), file=sys.stderr)
# Logout to prevent generation of unnecessary open sessions
if 'auth_token' in vars():
zbxrequest(args.url, method="user.logout", auth=auth_token, params={})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.