Zabbix template import command line tool/script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Zabbix template import command line tool/script

    Hi,

    I am new to zabbix environment and I have installed zabbix server on my local system.
    I don't know how to import the .xml template files from my local system to zabbix server.

    So here is what I want to do, a command line script or tool which will import all the templates(.xml file in local directory one by one) to locally running zabix server.

    Can someone point me out or send a script which import templates from a local directory to zabbix server?

    Can someone please show me a way and what all I need to install for this ?
    Thanks in advance and will appreciate your help.

    #2
    Some more info:

    It seems no-one tried to implement script which will import template file to zabbix server.
    If someone tried then not posted on zabbix.

    I am using zabbix server version 3.4.1 and installed in ubuntu 16.04 using ansible.

    I want to take zabbix template from my local system and import to zabbix server.

    Can someone please atleast tell/guide how the import feature is working i.e when importing from UI(from browser login) like where it is storing the template file and how it is doing internal operations.

    In my case from UI I am able to import templates successfully, but its too much time taking to import one by one all template files.

    Any idea or half implemented script will also help me to kick start the work.

    Comment


      #3
      Hello Deepak,
      it's not quite clear what actually you want? Importing/Exporting can be done successfully over the Web GUI. If you want to migrate the with the metrics - move the database from one instance to another.

      In other hand - you always can use Zabbix API -> https://www.zabbix.com/documentation.../api/reference

      Regards,
      Alex!

      Comment


        #4
        Originally posted by allexpetrov View Post
        Hello Deepak,
        it's not quite clear what actually you want? Importing/Exporting can be done successfully over the Web GUI. If you want to migrate the with the metrics - move the database from one instance to another.

        In other hand - you always can use Zabbix API -> https://www.zabbix.com/documentation.../api/reference
        Actually it is quit clear.
        Simple now on import template A when such template already i in zabbix cfg data are created new items A template when in imported template exist items with the same name but with different keys definitions.
        IMO as well it would be good to have possibility to import template choosing to overwrite/change items description when key item definition is the same.

        Changing items descriptions or names over web interface if really time consuming. because zabbix interface is not well suited for only such operations. It would be easier to export template -> using editor do all items names and descriptions using text editor -> import back modified template.
        For some reason on importing template name of the items are more important than key definitions.

        IMO to not break current behavior on import (over web or API) it would be good to add option allowing to choose new and current item definition are the same when name or key definition is the same.
        http://uk.linkedin.com/pub/tomasz-k%...zko/6/940/430/
        https://kloczek.wordpress.com/
        zapish - Zabbix API SHell binding https://github.com/kloczek/zapish
        My zabbix templates https://github.com/kloczek/zabbix-templates

        Comment


          #5
          Hi Alex!

          Thanks for your valuable time. Let me clearly make you understand what exactly I want to achieve.

          I have install zabbix server in multiple systems, and in each system I have to import templates for different devices(manually created template for device specific).

          Now I am installing zabbix server in the system and after that from zabbix server UI I am importing one by one template files to it manually. This is too much time taking. And when new templates need to import, then also I have to do it manually through UI.

          What exactly I want to achieve:
          Now after zabbix server install in system, I will run a script which will take one argument (path to zabbix template files *.xml present in the same system). This script will import all the template files present inside the directory to zabbix server.

          So that everytime I don't required to access the zabbix server UI to import new templates and no manual work needed, I will do "scp/copy" the new templates from central server to all the zabbix server running systems, and then will run the python script which will make my work easy.

          I have written a script using the zabbix API to achive this, however If anyone have better tool can also share on this forum.

          Here is my script :

          #!/usr/bin/python

          import argparse
          import ConfigParser
          import sys
          import time
          from zabbix.api import ZabbixAPI

          # Define commandline arguments
          parser = argparse.ArgumentParser(formatter_class=argparse.R awDescriptionHelpFormatter,description='Imports Zabbix templates from XML files. Default behaviour is to add missing elements and update any existing elements. Optionally elements that are missing from the XML file can be removed from existing template(s) as well.', epilog="""
          This program can use .ini style configuration files to retrieve the needed API connection information.
          To use this type of storage, create a conf file (the default is $HOME/.zbx.conf) that contains at least the [Zabbix API] section and any of the other parameters:

          [Zabbix API]
          username=johndoe
          password=verysecretpassword
          api=https://zabbix.mycompany.com/path/to/zabbix/frontend/
          no_verify=true
          """)
          parser.add_argument('-u', '--username', help='User for the Zabbix api')
          parser.add_argument('-p', '--password', help='Password for the Zabbix api user')
          parser.add_argument('-a', '--api', help='Zabbix API URL')
          parser.add_argument('--no-verify', help='Disables certificate validation when using a secure connection',action='store_true')
          parser.add_argument('-c','--config', help='Config file location (defaults to $HOME/.zbx.conf)')
          parser.add_argument('-v', '--verbose', help='Enables verbose output.',action='store_true')
          parser.add_argument('-T', '--templates', help='List of XML template files to import.',required=True, nargs="+")
          parser.add_argument('-D', '--delete-missing', help='If a template already exists in Zabbix, any missing elements from the .XML will be removed from Zabbix as well.',action='store_true')
          parser.add_argument('-U', '--update', help='If a template already exists in Zabbix, update any changes in template elements.',action='store_true')
          parser.add_argument('-C', '--continue-on-error', help='Continue on error, use with caution.',action='store_true')

          args = parser.parse_args()

          # Zabbix url username and password for connection
          URL='http://10.16.92.28/zabbix'
          USER='admin'
          PWD='zabbix'

          # open xml file for reading
          # Read the xml template in xml_file
          for template in args.templates:
          with file(template) as f:
          xml_file = f.read()
          f.close()

          # Create zabbix object using the ZabbixApi
          zabbix_conn_obj=ZabbixAPI(url=URL, user=USER, password=PWD)
          xml_file=xml_file.strip(' \n\t')

          result = zabbix_conn_obj.do_request("configuration.import", {"format": "xml","rules": {"templates": {"createMissing": True,"updateExisting": True}},"source": xml_file})

          print result

          # If result is true then import success else failed to import template

          If any clarification required, please let me know.

          Comment


            #6
            Huh, nice script ...

            I am not familiar with python, and I has some problems to run this script on debian OS, but "pip install py-zabbix" and "pip install zabbix-api" solved it.

            But, now, when I run script to install one template, against zabbix 3.2.11, I see this err ...

            ./push.template.py -u Admin -p somepassword -a https://10.0.246.223/zabbix/api_jsonrpc.php --no-verify -T Template_Cisco_Catalyst_Switches.xml -U
            Traceback (most recent call last):
            File "./push.template.py", line 37, in <module>
            zabbix_conn_obj=ZabbixAPI(url=URL, user=USER, password=PWD)
            File "/usr/local/lib/python2.7/dist-packages/pyzabbix/api.py", line 156, in __init__
            self._login(user, password)
            File "/usr/local/lib/python2.7/dist-packages/pyzabbix/api.py", line 186, in _login
            self.auth = self.user.login(user=user, password=password)
            File "/usr/local/lib/python2.7/dist-packages/pyzabbix/api.py", line 82, in fn
            args or kwargs
            File "/usr/local/lib/python2.7/dist-packages/pyzabbix/api.py", line 236, in do_request
            res = urlopen(req)
            File "/usr/local/lib/python2.7/dist-packages/pyzabbix/api.py", line 107, in inner
            res = func(req, context=ctx)
            File "/usr/local/lib/python2.7/dist-packages/pyzabbix/api.py", line 118, in urlopen
            return urllib2.urlopen(*args, **kwargs)
            File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
            return opener.open(url, data, timeout)
            File "/usr/lib/python2.7/urllib2.py", line 437, in open
            response = meth(req, response)
            File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
            'http', request, response, code, msg, hdrs)
            File "/usr/lib/python2.7/urllib2.py", line 469, in error
            result = self._call_chain(*args)
            File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
            result = func(*args)
            File "/usr/lib/python2.7/urllib2.py", line 656, in http_error_302
            return self.parent.open(new, timeout=req.timeout)
            File "/usr/lib/python2.7/urllib2.py", line 437, in open
            response = meth(req, response)
            File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
            'http', request, response, code, msg, hdrs)
            File "/usr/lib/python2.7/urllib2.py", line 475, in error
            return self._call_chain(*args)
            File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
            result = func(*args)
            File "/usr/lib/python2.7/urllib2.py", line 558, in http_error_default
            raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
            urllib2.HTTPError: HTTP Error 412: Precondition Failed

            Some idea?

            Comment

            Announcement

            Collapse
            No announcement yet.
            Working...
            X