Ad Widget

Collapse

Unlink and clear template from multiple hosts at once

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rataplan626
    Junior Member
    • Jun 2013
    • 9

    #1

    Unlink and clear template from multiple hosts at once

    Hi, when in host configuration I unlink and clear a template, all templated items / triggers etc. are removed from that host. Imagine you have to do that for hundreds of hosts. I'd normally unlink the template by going to the template and remove those hosts from the template (rather then removing the template from the host) but there is no 'unlink and clear' function that way. This means all items still remain.

    Is there a way (without using SQL, I know to do that but its cumbersome) to unlink and clear a template from multiple hosts at once?
  • MaxM
    Member
    • Sep 2011
    • 42

    #2
    You can either use mass update through the GUI (be careful and try this in a test environment first) or use the API.

    Comment

    • Rataplan626
      Junior Member
      • Jun 2013
      • 9

      #3
      Thanks, although both are a bit cumbersome. I'd love to see that option 'unlink and clear' in the templates linkage view.

      Comment

      • MaxM
        Member
        • Sep 2011
        • 42

        #4

        Comment

        • Rataplan626
          Junior Member
          • Jun 2013
          • 9

          #5
          Ah ok, thanks for that. I hadn't checked that :-\

          Comment

          • tatapoum
            Senior Member
            • Jan 2014
            • 185

            #6
            I have added a patch for this :

            Comment

            • emz
              Junior Member
              • Mar 2014
              • 20

              #7
              Is there a solution for the problem applicable for Zabbix 3.x?

              Comment

              • kloczek
                Senior Member
                • Jun 2006
                • 1771

                #8
                Originally posted by MaxM
                You can either use mass update through the GUI (be careful and try this in a test environment first) or use the API.
                Problem is not with delete hosts using API or web frontend.
                Issue is that whole delete operation it is single SQL transaction (and it is very good reason doing this that way).
                More needs to be deleted than longer such operation will take.
                During all hosts/template add/delete/updates many tables are locked (to have consitiend data after for example delete).
                There are two things important here:
                • If as DB backend is used MySQL you must have transaction-isolation=READ-COMMITTED. With this all locked tables still will be readable for other selects which may need an access to the data already committed. Without this almost everything in zabbix will be locked until each delete operation will be finished.
                • Mass host delete needs to be done one by one with some periods of time between each host delete to allow insert and evaluate new monitoring data against triggers definition.
                Just found my old JFDI stript which I've been using long time ago on mass delete hosts in exact group.

                Code:
                #!/bin/sh
                zabbix_api_url='http://<zabbix.api.gw.url>/api_jsonrpc.php'
                
                #curl -X POST -H 'Content-Type:application/json' -d'{"jsonrpc": "2.0","method":"user.login","params":{"user":"tkloczko","password":"<pwd>"},"id":0}' $zabbix_api_url; exit
                
                for i in $(curl --silent -X POST -H 'Content-Type:application/json' \
                        -d'{"jsonrpc": "2.0","method":"host.get","params":{"output":"simple","groupids":"267"},"auth":"<auth_hash>","id":0}' $zabbix_api_url | \
                        sed 's/,/\n/g; s/\"result\"\:\[//; s/\]//' | grep -v jsonrpc | grep -v \"id\" | gawk -F\" '{print $4}'); do
                        curl --silent -X POST -H 'Content-Type:application/json' \
                                -d"{\"jsonrpc\": \"2.0\",\"method\":\"host.delete\",\"params\":[\"$i\"],\"auth\": \"<auth_hash>\",\"id\":0}" $zabbix_api_url
                               sleep 2
                                echo
                done
                echo
                After uncomment first line is possible to generate zabbix api auth hash. This hash needs to be placed below in <auth_hash> then this line can be commented.
                Next for loop in this example deletes all hosts from group with groupid=267. After each delete is 2s sleep.
                Using my "zapish" or any other scripting language zabbix api wrapper is possible to writhe above more elegant (above it is nothing more than JFDI solution)

                If someone wants to do the same without API it is possible to do this over web frontend by delete hosts in some number of smaller batches.
                Last edited by kloczek; 23-04-2018, 10:02.
                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

                • kloczek
                  Senior Member
                  • Jun 2006
                  • 1771

                  #9
                  Originally posted by tatapoum
                  I have added a patch for this :
                  https://support.zabbix.com/browse/ZBXNEXT-543
                  Leaving those items untemplated it is not a bug. It is feature. Because those items are still present in the host definition it is possible to unlink the host from one template then link to another one a bit different without delete monitoring data.
                  I'm pretty sure that those patches will be never integrated into zabbix.
                  Last edited by kloczek; 23-04-2018, 10:06.
                  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

                  Working...