Ad Widget

Collapse

Add hosts by batch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mike13
    Member
    • Apr 2010
    • 30

    #1

    Add hosts by batch

    Hi,

    I need to monitor nearly 2000 hosts. Is there a way to add them in zabbix using a script?

    Any suggestions would be appreciated.

    Many Thanks
    Mike13
  • Aly
    ZABBIX developer
    • May 2007
    • 1126

    #2
    Use Zabbix API (JSON RPC)
    Zabbix | ex GUI developer

    Comment

    • alixen
      Senior Member
      • Apr 2006
      • 474

      #3
      Hi,

      Aly's suggestion is a good one.
      Another way to do that is to use XML import.

      Regards,
      Alixen
      http://www.alixen.fr/zabbix.html

      Comment

      • mike13
        Member
        • Apr 2010
        • 30

        #4
        First of all thank you for your advice.
        To be honest, I'm new to zabbix, Hence i don't know how to do it? Do you have any example using API/XML? Can you tell me how to use the API from php or perl?

        Many thanks,
        Mike13

        Comment

        • alixen
          Senior Member
          • Apr 2006
          • 474

          #5
          Hi,

          Originally posted by mike13
          First of all thank you for your advice.
          To be honest, I'm new to zabbix, Hence i don't know how to do it? Do you have any example using API/XML? Can you tell me how to use the API from php or perl?
          You can search on this forum for API usage.
          I use XML import this way:
          - Create an host by hand
          - XML export this host
          - Write a shell script that will generate an XML file with all hosts (exported XML is used to define per host XML structure, if your hosts are identical you just need to change host name and IP)
          - Import this XML file to Zabbix

          Regards,
          Alixen
          http://www.alixen.fr/zabbix.html

          Comment

          • mike13
            Member
            • Apr 2010
            • 30

            #6
            I'm trying to create a script in python to add hosts by batch. Before that i wanted to run the sample code as shown in url:http://www.zabbix.com/forum/showthre...t=15218&page=3

            import sys
            from getpass import getpass
            import zabbix_api

            # The hostname at which the Zabbix web interface is available
            zabbix_server = '192.168.0.1'

            # Enter administrator credentials for the Zabbix Web Frontend
            username = raw_input('Username: ')
            password = getpass()

            zapi = zabbix_api.ZabbixAPI(zabbix_server,'/','http',username,password)
            zapi.login(username, password)
            print "Connected to Zabbix API Version %s" % zapi.api_version()

            for i in zapi.item.get(extendoutput=True):
            if i['itemid'] == '22609':
            print i


            When i execute it I receive some errors:
            Traceback (most recent call last):
            File "test.py", line 14, in <module>
            zapi.login(username, password)
            File "/home/script/zabbix_api.py", line 176, in login
            result = self.do_request(obj)
            File "/home/script/zabbix_api.py", line 222, in do_request
            jobj = json.loads(response.read())
            File "/var/lib/python-support/python2.5/simplejson/__init__.py", line 313, in loads
            return _default_decoder.decode(s)
            File "/var/lib/python-support/python2.5/simplejson/decoder.py", line 321, in decode
            obj, end = self.raw_decode(s, idx=_w(s, 0).end())
            File "/var/lib/python-support/python2.5/simplejson/decoder.py", line 340, in raw_decode
            raise ValueError("No JSON object could be decoded")
            ValueError: No JSON object could be decoded


            Could i have some advice?

            Many thanks,
            Mike

            Comment

            • dave_t
              Junior Member
              • Apr 2007
              • 28

              #7
              shell script / api call advice/guidance needed (as well)

              feel like I'm asking a really dumb question here, but I REALLY want to use the API to perform mass-update of a template host, but as I'm 100% comfortable with shell scripts I'm not looking for 'other' solutions (perl/python etc etc..)

              ...so far, I've got a config file (pipe delimited) that is managed by our DBA team that contains only the information that the DBA's would like to monitor from within their databases.
              The structure of the config file is:
              "Application"|"ITEM"|"Description"|"VALUE_TYPE"|"D ELTA"
              i.e.
              "License"|"License.ActiveConns"|"Active Connections"|"0"|"0"

              I've got a trigger (cksum) in zabbix that is detects any changes in the config file, and and as soon as a change is detected, I want to update the Template host "Template_VST_Host" with new items, or changes to any existing items (for the same template host)

              ....at the moment, the action is to run a script that parses the config file and generates the XML.
              ....simple so far, but what I'm trying to achieve is a totally hands-free way of updating the template which of course this method doesn't provide (at least as far as I can see....)

              Therefore, my first question is: is there a way to import the XML from command-line?

              Secondly, (and I think the more elegant way) is to use the API by means of Zabcon - or similar, so I've taken a look at this, and so far I've come up with the following:

              The action calls a script that pickups up the correct hostid for the template host I'm interested in:
              The first line of thie script goes like this, which sets $HOSTID (in my case) to 10207, which I can confirm (by means of the zabbix frontend) is the correct host I'm looking to update

              HOSTID=`( echo 'raw json {"jsonrpc":"2.0","method":"template.get","params": {"output":"hostid","filter":{"host":"Template_VST_ Host"}},"auth":"$auth","id":2}' | /usr/local/bin/zabcon.rb -l /etc/zabcon/zabcon.conf --no-header ) | cut -d "," -f 1`

              ...this leads me onto the next - more interesting bit...

              I can see that Zabcon has the functionality to add items to the host 10207, which works fine, but I don't see anything that will update changes. therefore, I'm looking to use a similar method (i.e. "echo raw json{"some stuff...."}..... | zabcon"

              ...however I don't feel like this is the most economical way of doing things - similarly, it also gives me the """"""quoting"""""" headache of using $HOSTID as I build up the command list to be executed through Zabcon.


              I guess what I'm trying to say is - is there an easier / more elegant way of doing this (without having to adopt a language that isn't my first choice ) ?

              If anyone has taken the time to read all through all of this, then thanks for any response :-)

              Dave

              Oh, BTW if it's of any relevance:

              zabcon(221) RHEL(PPC)
              zabbix(1.8.4) RHEL(PPC)
              rubygems(1.8.6) RHEL(PPC)
              database (MySQL AIX 5.3)

              Comment

              • nelsonab
                Senior Member
                Zabbix Certified SpecialistZabbix Certified Professional
                • Sep 2006
                • 1233

                #8
                At one point there was XML import capability within Zabcon, however the person who was working on that has not been active in a while and unfortunately that section of Zabcon has not seen much love lately. If you or anyone else is interested let me know. :-)

                Aside from making the XML import work again, what can I do to make your life easier?
                RHCE, author of zbxapi
                Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
                Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

                Comment

                Working...