[ZBXNEXT-543] Unlinking host from template, in template window, leaves items attached to host Created: 2010 Oct 27  Updated: 2019 Sep 09  Resolved: 2019 Sep 09

Status: Closed
Project: ZABBIX FEATURE REQUESTS
Component/s: Frontend (F)
Affects Version/s: None
Fix Version/s: None

Type: Change Request Priority: Major
Reporter: Vytautas Assignee: Zabbix Development Team
Resolution: Won't fix Votes: 14
Labels: patch, templatelinking, templates, unsquashable, usability
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File ZBXNEXT-543-patch1.patch     Text File ZBXNEXT-543-patch2.patch     Text File ZBXNEXT-543-patch3.patch     Text File ZBXNEXT-543_Unlinking_hosts_from_templates-2.4.7.patch     Text File ZBXNEXT-543_Unlinking_hosts_from_templates-3.2.1.patch     PNG File clear_unlink.png    
Issue Links:
Duplicate

 Description   

When unlinking host(-s) from template, using templates window (configuration --> templates), you choose a host, and move it from the left side to the right (Hosts|Templates).

This causes template to be removed from host, but all the items are not removed from the host, and become host's items. (instead of template's items)

This does not happen when you unlink host from the template using Host screen (button unlink and clear).



 Comments   
Comment by richlv [ 2010 Oct 27 ]

maybe a checkbox would be added - "Clear when unlinking template"
that would not allow fine grained control over each host, but might be just enough

Comment by Tatapoum [ 2014 Jun 05 ]

Is this something we can expect to see in the next release ? This is really an annoying behavior. We shouldn't need to use the API to "unlink and clear" a template from 10 hosts (or go on each host one by one).

Comment by Marc [ 2014 Jun 06 ]

In certain circumstances unlinking and clearing a template on host level in frontend might be a work-around too.
Either manually or by mass update functionality.

Comment by Pavels Jelisejevs (Inactive) [ 2014 Jun 16 ]

The proposed solution seems to be unclear and uncommon for Zabbix so we wouldn't want to implement it that way. We would probably need to rethink the linkage control to find a better solution.

Comment by richlv [ 2014 Jun 16 ]

note that such a checkbox would be very similar to template control in mass update form

Comment by Tatapoum [ 2014 Jul 08 ]

A checkbox might be enough. In fact, if you have a medium to big environment, you never want to create items attached to hosts, all is done with templates. You can mass link a template to multiple hosts easily. You can also replace or clear templates attached to hosts with the mass update form, but all the hosts should have the same templates. However, you cannot unlink and clear a template attached to multiple hosts. And if you don't clear the items when unlinking, you end up with tons of "orphaned" items, which you aren't even aware of...

In fact, I don't understand the use-case of unlinking a template WITHOUT clearing. Why would you leave the items if the template is removed ???

Comment by Tatapoum [ 2014 Jul 23 ]

richlv, the template control in the mass update form updates all the templates attached to a list of hosts. If we just want to unlink one template from several hosts without affecting the other templates, we can't. We just need a "unlink and clear" button in the template properties panel.

Comment by Ryan Younce [ 2014 Aug 18 ]

I've seen one use case for unlinking without clearing: if two separate templates have been added to a set of hosts originally, but later it's decided to link the templates together. I don't know of a way to do this without removing the template being linked first, since the link form will complain about the template being linked to a host more than once. The only way I've found to fix this is to first unlink the template that is being linked from all hosts, and then to link the templates together. If I did an unlink and clear during this process, I would lose all previous data, which is not what I wanted. I know of no other way to perform this.

Other than that, I haven't run into the situation where unlinking without actually clearing would be the "desired" behavior.

Comment by Tatapoum [ 2014 Aug 18 ]

Yes, this is really a pain for us. Imagine that a template is linked to multiple hosts and that someone unlinks it but forgets to clear the items at the same time. You end up with many hosts with many unneeded items. And there is no simple way to remove these items afterwards.
Please allow to unlink and clear multiple hosts from a template !

Comment by Tatapoum [ 2014 Aug 18 ]

I've created a patch for this purpose.
It adds a "unlink and clear" checkbox in the template configuration view. Let me know if it works for you (tested in 2.2.5).

Comment by Tatapoum [ 2014 Aug 18 ]

The patch2 sets the box as checked by default.

Comment by Marc [ 2014 Aug 19 ]

tatapoum, thanks for the patch
From my point of view 'Clear when unlinking' should not be checked by default. First it changes usual/ habitual behavior and secondly it leads to loosing data unrecoverable.
When un-linking without clearing by mistake (forgetting it) one can still re-link the template and do it right during the second attempt.

Comment by Tatapoum [ 2014 Aug 19 ]

So you need to use the patch1. Hope it gets merged.
Here is how it looks like :

Comment by Tatapoum [ 2014 Aug 19 ]

This third release of the patch fixed a bug with the templateID.

Comment by Tatapoum [ 2014 Sep 16 ]

Hi, what is needed for this patch to get merged into the trunk ?

Comment by Tatapoum [ 2014 Oct 07 ]

Hi, could you add this patch to the next release ? It's very simple but useful.

Comment by Tatapoum [ 2015 Mar 24 ]

So, there is no way to see this small patch implemented at some point ? Templates are handy, but when you need to unlink a template from 100+ of hosts, this is very painful.

Comment by Tatapoum [ 2015 Mar 24 ]

Here is a patch rebased on the latest trunk :

diff --git a/frontends/php/include/views/configuration.template.edit.php b/frontends/php/include/views/configuration.template.edit.php
index 4665234..8ccbeab 100644
--- a/frontends/php/include/views/configuration.template.edit.php
+++ b/frontends/php/include/views/configuration.template.edit.php
@@ -147,6 +147,15 @@ $templateList->addRow(_('Hosts / templates'), $hostsTB->Get(_('In'), array(

 $templateList->addRow(_('Description'), new CTextArea('description', $this->data['description']));

+$templatesDiv = new CDiv(array(
+                       new CCheckBox('mass_clear_hosts', $this->data['mass_clear_hosts']),
+                       SPACE,
+                       _('Clear when unlinking')
+               ), 'floatleft');
+
+$templatesDiv->setAttribute('id', 'templateDiv');
+$templateList->addRow($templatesDiv);
+
 // FULL CLONE {
 if ($data['form'] === 'full_clone') {
        // template applications
diff --git a/frontends/php/templates.php b/frontends/php/templates.php
index 44e14f0..7bfb5c4 100644
--- a/frontends/php/templates.php
+++ b/frontends/php/templates.php
@@ -56,6 +56,7 @@ $fields = array(
        'visiblename'           => array(T_ZBX_STR, O_OPT, null,                null,   'isset({add}) || isset({update})'),
        'groupid'                       => array(T_ZBX_INT, O_OPT, P_SYS,               DB_ID,  null),
        'twb_groupid'           => array(T_ZBX_INT, O_OPT, P_SYS,               DB_ID,  null),
+       'mass_clear_hosts'      => array(T_ZBX_STR, O_OPT, null,                        null,           null),
        'newgroup'                      => array(T_ZBX_STR, O_OPT, null,                null,   null),
        'description'           => array(T_ZBX_STR, O_OPT, null,                null,   null),
        'macros_rem'            => array(T_ZBX_STR, O_OPT, P_SYS|P_ACT, null,   null),
@@ -216,6 +217,13 @@ elseif (hasRequest('add') || hasRequest('update')) {
                        'templated_hosts' => true,
                        'filter' => array('flags' => ZBX_FLAG_DISCOVERY_NORMAL)
                ));
+
+               $hostIds = zbx_objectValues($dbHosts, 'hostid');
+               $templateHosts = API::Host()->get(array(
+                                               'templateids' => $templateId
+                                       ));
+               $templateHostsIds = zbx_objectValues($templateHosts, 'hostid');
+               $hostsToDelete = array_diff($templateHostsIds, $hostIds);

                // create / update template
                $template = array(
@@ -231,6 +239,18 @@ elseif (hasRequest('add') || hasRequest('update')) {
                if ($templateId) {
                        $template['templateid'] = $templateId;
                        $template['templates_clear'] = $templatesClear;
+
+                       if (isset($_REQUEST['mass_clear_hosts'])) {
+                               if ($hostsToDelete) {
+                                       $hosts = array(
+                                               'hosts' => zbx_toObject($hostsToDelete, 'hostid' ),
+                                               'templates_clear' => array('templateid' => $templateId)
+                                       );
+                                       if (!API::Host()->massupdate($hosts)) {
+                                               throw new Exception();
+                                       }
+                               }
+                       }

                        $messageSuccess = _('Template updated');
                        $messageFailed = _('Cannot update template');
@@ -440,7 +460,8 @@ if (hasRequest('form')) {
        $data = array(
                'form' => getRequest('form'),
                'groupId' => getRequest('groupid', 0),
-               'groupIds' => getRequest('groups', array())
+               'groupIds' => getRequest('groups', array()),
+               'mass_clear_hosts' => getRequest('mass_clear_hosts')
        );

        if ($templateId) {
Comment by storex [ 2015 Dec 22 ]

Ported to 2.4.7

Comment by Dmitry Verkhoturov [ 2016 Jun 09 ]

storex, could you please contribute your patch to https://github.com/zabbix/zabbix-patches for better visibility?

Comment by Alex Hart [ 2016 Jun 24 ]

This should be added to 3.0 as well

Comment by Marc [ 2016 Sep 22 ]

After having this patch applied for quite a while, a Zabbix user recognized today that 'Clear when unlinking' seems not work with nested templates.

Given is:
T3 -> T2 -> T1 -> H1

Unlinking and clearing of T3 from T2 did still only unlink T3. However, I haven't investigated the details yet.

Comment by storex [ 2017 Mar 20 ]

Ported to 3.2.1

Comment by Dmitry Verkhoturov [ 2017 Mar 20 ]

Hi, storex!

I've added your patch for 2.4 and 3.2 to https://github.com/zabbix/zabbix-patches - now it could be applied easily with command-line tool.
Also, 3.2.1 patch have a mistake: it reads "--- a/include/views/templates.php <...>", should be "--- a/templates.php <...>" — I've fixed that before adding to the repo.

Comment by Dmitry Verkhoturov [ 2017 Aug 24 ]

Patch was updated to 3.4 release version and still available by link https://github.com/zabbix/zabbix-patches.

Comment by Dmitry Verkhoturov [ 2017 Dec 11 ]

This patch could be fixed to unlink and clear from nested templates as well and included into 4.0. It's really annoying this functionality not being available in upstream still.

Comment by Stoyan Stoyanov [ 2018 Apr 23 ]

8 years and not added to Zabbix 3? Please add it.

Comment by raph ael [ 2018 May 23 ]

Wow, I can't believe this is not fixed yet. I have to go unlink and clear things manually still. Please help to fix it.

Comment by Alexander Vladishev [ 2019 Sep 09 ]

This functionality is not present from version 4.0. Closed as "Won't Fix".

Generated at Fri Apr 26 21:34:57 EEST 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.