Ad Widget

Collapse

Api link template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perun.84
    Member
    • May 2016
    • 73

    #1

    Api link template

    Hi. Is there any way to link template to host by API. I have 2000 hosts which I nedd to link one template on. I'm going to do it by script.. Is it possible?
  • kloczek
    Senior Member
    • Jun 2006
    • 1771

    #2
    Originally posted by perun.84
    Hi. Is there any way to link template to host by API. I have 2000 hosts which I nedd to link one template on. I'm going to do it by script.. Is it possible?
    Did you try to use google to have look for "zabbix add temlate"?
    on search results first link is to https://www.zabbix.com/documentation/2.0/manual/api/reference/template/massadd

    BTW. You can add template to multiple hosts over web interface as well. Configuration -> Hosts -> mark multiple hosts ->Mass update --> link template -> update.
    Fact is that in this case you have limit to only 1k hosts which can be modified this way so you need to repeat this operation two times .. which will be faster than trying to understand API and write script using it.
    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

    • perun.84
      Member
      • May 2016
      • 73

      #3
      Thanks for the answer. But I must decide which hosts I have to link template to. I have to discover all of network devices made by one vendor and on it link that template. I don't know which hosts when I see name and address on web interface. Maybe I might mark those hosts for linking templates manually..

      Comment

      • kloczek
        Senior Member
        • Jun 2006
        • 1771

        #4
        If you don't now what and where is working or from where obtain such precise details on such big scale I must say that your work must be nightmare.

        First of all my experience says me that more hosts to monitor than less likely that any detection will be working correctly.
        On very big scale even teams of people racking hosts should be signing (maybe even using own blood) what and where they are mounting. Most of the companies deliver even bar coded descriptions of the hardware with serial numbers, mac addresses and other crucial details.

        On scale of thousands hosts you should have asset tracker db and content of this db should be used on making decision what and where should be monitored. If you don't have such level of organisation you are already doomed. Any incorrectness in templates assignment should be first corrected in asset tracker then assignment should be replayed

        Second: using list of host in web frontend you can very precisely choose where a template needs to be added
        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

        • perun.84
          Member
          • May 2016
          • 73

          #5
          OK. You're right. It is nightmare. But in theory problem is very simple. I wrote script which goes through database and gets information about ip address and snmp community of device. After that it asks device for vendor information and check if it is the one for linking template. The only unknown thing for me is "how to link existing template to that host"..

          Comment

          • perun.84
            Member
            • May 2016
            • 73

            #6
            I did it on some strange way.
            I 've found way to add all of one vendor hosts in temporary group, and then manually massupdate every host in that group (link my template for that vendor).
            I wrote two scripts. The first one (written in bash) connects to db and gets ip, hostid and community of hosts and then asks by snmpwalk for sysDescr and matches result with vendor name. If it matches it calls second script (written in python) which adds that host to temporary group.

            Scripts:
            Code:
            #!/bin/bash
            
            ###DB Credentials ###
            dbuser="dbuser"
            dbpsw="dbpass"
            dbname="dbname"
            dbhost="dbhost"
            ####################
            
            ### 5 is groupid of group where are autodiscovered hosts
            mysql -h ${dbhost} --user ${dbuser} -p${dbpsw} ${dbname} -Bse 'select hostid from hosts_groups where groupid=5' | while read hostid; do
            	ip=$(mysql --host ${dbhost} --user ${dbuser} -p${dbpsw} ${dbname} -sNe "select ip from interface where hostid=$hostid order by interfaceid limit 1")
            	dcheckid=$(mysql --host ${dbhost} --user ${dbuser} -p${dbpsw} ${dbname} -sNe "select dcheckid from dservices where ip='$ip'")
               	comm=$(mysql --host ${dbhost} --user ${dbuser} -p${dbpsw} ${dbname} -sNe "select snmp_community from dchecks where dcheckid='$dcheckid'")
            	descr=$(snmpwalk -v 2c -c $comm $ip SNMPv2-MIB::sysDescr.0)
            	if [[ $descr == *"vendor"* ]]
            	then
            ### addvendor.py is python script, 13 is id (hardcoded) of temporary group
            		(./addvendor.py 13 $hostid)
            	fi	
            done
            Python script:

            Code:
            #!/usr/bin/env python
            import sys
            from pyzabbix import ZabbixAPI
            
            zapi = ZabbixAPI("https://zabbix.test.com")
            zapi.login("zabbixuser", "zabbixpass")
            
            group_id=sys.argv[1]
            host_id=sys.argv[2]
            zapi.hostgroup.massadd(hosts={"hostid":host_id},groups={"groupid":group_id})
            It defintely isn't most elegant way but it is working for me. Maybe someone else finds it as usefull. Cheers

            Comment

            Working...