Hi!
I just started ZABBIX recently and I've created a Python script to import our .xml templates into the zabbix webserver, but I've been having trouble importing both groups and templates via API. For starters, I guess it would be best to post what I have:
Running
Zabbix 2.0.5
Red Hat Enterprise Linux 5
Python 2.4.3 [Unfortunately I'm limited to using this version for work reasons]
Referenced from:
https://www.zabbix.com/documentation/doku.php?id=2.0/manual/appendix/api/configuration/import
When i run this code against an XML Template that I've exported from the Zabbix client
-- snippet of xml code --
If i import this template via the Zabbix frontend [aka Browser -> http:zabbix_server -> Configuration -> Templates -> etc], it works great -- I get a 'Successfully Imported' message in green, the groups that are missing are created, and all hosts that use templates have them linked.
However, If I run this python script from command line and lets say I modify the xml file so that the groupname is something that isn't there
and run the python file, I get.
Note that in the original python script, I've added the groups: createMissing : true in the zbxImport function. Is there something I'm forgetting? If I create the group manually in the webclient and then run the script again
The group will also be created if i import the xml template through the webclient, but not via command line.
Something also similar occurs with importing templates such that it'll say that the import was successful, but when I look at the lists of hosts on the zabbix webclient, none of my imported hosts have a template linked to them, whereas if I do the import from the webclient, the templates are listed. Is there a workaround I can do to fix this?
I was originally thinking that maybe it was because both templates and groups have another nested <template> and <group> xml tag so I tried changing the dictionaries to
but that didn't work as well =(. Any help would be greatly appreciated. Thanks
I just started ZABBIX recently and I've created a Python script to import our .xml templates into the zabbix webserver, but I've been having trouble importing both groups and templates via API. For starters, I guess it would be best to post what I have:
Running
Zabbix 2.0.5
Red Hat Enterprise Linux 5
Python 2.4.3 [Unfortunately I'm limited to using this version for work reasons]
Referenced from:
https://www.zabbix.com/documentation/doku.php?id=2.0/manual/appendix/api/configuration/import
Code:
import os
import sys
import urllib2
import hashlib
try:
import json
except ImportError:
import simplejson as json
from pprint import pprint
url = "http://xxx.xxx.xx.xxx/zabbix/api_jsonrpc.php"
usr = "admin"
passwrd = "zabbix"
hash_pass = ""
def zbxAuth():
global hash_pass
login = {
"jsonrpc" : "2.0",
"method" : "user.login",
"params" : {
"user" : usr,
"password" : passwrd
},
"id" : 1
}
res = printResult(login)
if 'error' in res:
print "Error: Connection Unsuccessful"
sys.exit(-1)
hash_pass=res["result"]
print "Successfully Authenticated. Auth ID: " + hash_pass
def zbxImport(filename):
global hash_pass
file = open(filename, 'rb')
imprt = {
"jsonrpc": "2.0",
"method" : "configuration.import",
"params" : {
"format" : "xml",
"rules" : {
"groups" : {
"createMissing" : True,
"updateExisting" : True
},
"hosts" : {
"createMissing" : True,
"updateExisting" : True
},
"templates" : {
"createMissing" : True,
"updateExisting" : True
},
"triggers" : {
"createMissing" : True,
"updateExisting" : True
},
"items" : {
"createMissing" : True,
"updateExisting" : True
}
},
"source" : file.read()
},
"auth" : hash_pass,
"id" : 2
}
file.close()
res = printResult(imprt)
if 'error' in res:
print "Import unsuccessful for file: " + str(filename)
print str(pprint(res))
sys.exit(-1)
def printResult(var):
data = json.dumps(var)
request = urllib2.Request(url, data, {"Content-Type" : "application/json"})
response = urllib2.urlopen(request)
return json.load(response)
-- snippet of xml code --
Code:
<host>
<host>test12345</host>
<name>test12345</name>
.
.
.
<templates>
<template>
<name>Template OS Linux</name>
</template>
</templates>
<groups>
<group>
<name>OS Hosts</name>
</group>
</groups>
<interfaces>
.
.
However, If I run this python script from command line and lets say I modify the xml file so that the groupname is something that isn't there
Code:
<groups>
<group>
<name>This Group Wont Exist</name>
</group>
<groups>
Code:
Successfully Authenticated. Auth ID: f102938sd0a3laldasdjlae9 [i made up the md5]
{'error':
{'code': -32500,
'data': 'Group "This Group Wont Exist" for host "test12345" does not exist.',
'message': 'Application error. '},
'id':2,
'jsonrpc' : '2.0"}
}
Code:
{'jsonrpc' : '2.0', 'result':True, 'id':2}
Successfully Imported File: test.xml
Something also similar occurs with importing templates such that it'll say that the import was successful, but when I look at the lists of hosts on the zabbix webclient, none of my imported hosts have a template linked to them, whereas if I do the import from the webclient, the templates are listed. Is there a workaround I can do to fix this?
I was originally thinking that maybe it was because both templates and groups have another nested <template> and <group> xml tag so I tried changing the dictionaries to
Code:
"rules" : {
"groups" : {
"group" : {
"createMissing" : True,
"updateExisting": True
}
}
Comment