Ad Widget

Collapse

Zabbix API -- Configuration.Import not working for Groups and Templates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diveratis
    Junior Member
    • Aug 2013
    • 4

    #1

    Zabbix API -- Configuration.Import not working for Groups and Templates

    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


    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)
    When i run this code against an XML Template that I've exported from the Zabbix client

    -- 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>
         .
         .
    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
    Code:
    <groups>
       <group>
          <name>This Group Wont Exist</name>
       </group>
    <groups>
    and run the python file, I get.
    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"}
    }
    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
    Code:
    {'jsonrpc' : '2.0', 'result':True, 'id':2}
    Successfully Imported File: test.xml
    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

    Code:
    "rules" : {
        "groups" : {
           "group" : {
              "createMissing" : True,
              "updateExisting": True
            }
         }
    but that didn't work as well =(. Any help would be greatly appreciated. Thanks
    Last edited by diveratis; 21-08-2013, 17:36. Reason: Updated with URL linking Configuration.import API
  • JCHeard81
    Junior Member
    • Feb 2014
    • 2

    #2
    Originally posted by diveratis
    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


    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)
    When i run this code against an XML Template that I've exported from the Zabbix client

    -- 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>
         .
         .
    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
    Code:
    <groups>
       <group>
          <name>This Group Wont Exist</name>
       </group>
    <groups>
    and run the python file, I get.
    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"}
    }
    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
    Code:
    {'jsonrpc' : '2.0', 'result':True, 'id':2}
    Successfully Imported File: test.xml
    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

    Code:
    "rules" : {
        "groups" : {
           "group" : {
              "createMissing" : True,
              "updateExisting": True
            }
         }
    but that didn't work as well =(. Any help would be greatly appreciated. Thanks
    I noticed two things...both pertaining to the rules code.
    1) you have a 'group' object nested w/in the 'groups' object--doesn't seem correct.
    2) updateExisting isn't an option of groups according to the api.
    it should look like
    Code:
    "rules" : {
        "groups" : {
            "createMissing" : True
         }

    Comment

    • deepak72840
      Junior Member
      • Sep 2017
      • 5

      #3
      script or tool for importing the template files to zabbix server

      Hi diveratis ,

      I am also trying to solve the same type of issue, unfortunately zabbix forum till now don't have any solution on importing multiple templates to zabbix server through command line/script.

      I am also searching for a script (shell or python) which will receive the template(.xml) files from local folder and import into zabbix server.

      Have you fixed the above issue? Can you please share with me the script(tool) that you have used to solve your issue or work around that, it will be very helpful.
      Last edited by deepak72840; 21-09-2017, 12:01.

      Comment

      Working...