How to move a branch from one tree to another ?

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Developers, Moderators

Post Reply
Dom...
Posts: 29
Joined: Tue Aug 21, 2007 1:36 pm

How to move a branch from one tree to another ?

Post by Dom... »

Hello,

I have created 3 trees and have a few branches in the 3 trees (with sub nodes, graphs)
I would like to move (or copy) , a few branches from one tree to another ; and also to place a node from the second level of a tree to the first level of this tree.
I have read the command 'add-tree.php', but I think, it's only to add a new node, graph, device and not also to do a copy.
Is there a solution, because, to delete each item of the branch (with the red cross) and to add again in the other tree takes long time.

Thanks

Cacti : 0.8.7.b
OS : windows server 2003
User avatar
BSOD2600
Cacti Moderator
Posts: 12171
Joined: Sat May 08, 2004 12:44 pm
Location: USA

Re: How to move a branch from one tree to another ?

Post by BSOD2600 »

General question, moving.
Dom... wrote:I would like to move (or copy) , a few branches from one tree to another ;
There is not a way to move tree's around currently.
Dom... wrote:and also to place a node from the second level of a tree to the first level of this tree.
You should be able to add a host/graph from anything to any level of a Tree (i.e. root or node).
Dom...
Posts: 29
Joined: Tue Aug 21, 2007 1:36 pm

Post by Dom... »

Thanks for the answers.
If it's not possible to copy or move yet between trees (root), I have a question : in the database cacti, there are a few files about the graph trees, is it posssible to edit some files and to do a change without crash the db :-?
Thanks
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Dom... wrote:in the database cacti, there are a few files about the graph trees, is it posssible to edit some files
Are you talking about "tables" when talking about "files"? Surely it would be able to add a sorting feature for trees, and you may provide code if possible. But I'm quite sure it will at least require a db change (for the sorting option)
Reinhard
User avatar
stevew
Posts: 26
Joined: Tue Aug 17, 2004 12:39 pm
Location: London
Contact:

Post by stevew »

If you have some tool like phpMysql it is possible to move tree items between trees quite easily by editing database records directly.

You can update the graph_tree_id in the graph_tree_items table to move graph items to another tree, and then you must use the web UI to rearrange items within the new tree.

Note that this will mess up ordering in the new tree but you could overcome this if you understood and editted the order key field (which I don't, but I guess the schema explains this)

e.g.
update `graph_tree_items` set `graph_tree_id` = 42 WHERE `graph_tree_id` = 30

I have never needed to copy items, I guess it would be possible.
[url=http://www.welham.net/]Steve W[/url]
User avatar
stevew
Posts: 26
Joined: Tue Aug 17, 2004 12:39 pm
Location: London
Contact:

Post by stevew »

Actually I went back and checked this, I must have been lucky last time because this time I totally screwed the tree order.

The order key field plays a more significant part than I realised, and now I have a reason to learn what that is.... :-?
[url=http://www.welham.net/]Steve W[/url]
User avatar
stevew
Posts: 26
Joined: Tue Aug 17, 2004 12:39 pm
Location: London
Contact:

Post by stevew »

I revisited this today and came up with a way of moving graph trees around. This method requires some serious hacking about in the database but does allow you to move stuff between top-level graph trees without needing to recreate the graph tree.

Of course once you have graph_tree items in a common top-level tree it is really easy to move them about using the UI.

:evil: BE WARNED - YOU COULD REALLY MESS UP YOUR DATABASE SO DO SOME TEST RUNS FIRST ON A TEST SERVER AND BACKUP YOUR DATABASE.

The SQL to move an entire top-level graph tree "A" into a sub tree "Z.S" below top-level graph tree "Z" is as follows:

Code: Select all

UPDATE `graph_tree_items` SET graph_tree_id = 'Z-tree-id',
`order_key` = concat("Z.S-order_key_prefix",left(order_key,length(order_key)-Z.S-order_key_prefix_length)) WHERE `graph_tree_id` = A-tree-id;
I did it as follows:
1) Create a new sub tree (tree header object) Z.S in the destination tree Z. Make sure it's empty of child items.
2) Fire up phpMyAdmin
3) In phpMyAdmin, browse to the graph_tree table and locate the id for the source tree (A-tree-id) and the destination tree (B-tree-id)
4) In phpMyAdmin, run the following SQL query to browse the graph_tree_item table in a nicer fashion:

Code: Select all

select graph_tree.name N, G.order_key, G.title, host1.hostname, host2.hostname, graph_templates.name, G.*
from graph_tree_items G
left join host AS host1 on host1.id=G.host_id
left join graph_tree on graph_tree.id=G.graph_tree_id
left join graph_local on graph_local.id=G.local_graph_id
left join host AS host2 on host2.id=graph_local.host_id
left join graph_templates on graph_templates.id=graph_local.graph_template_id
order by N,G.order_key
LIMIT 0, 600
You need to identify the order_key prefix for your newly created header item Z.S. This will be something like 001 for the first header, 002 for the second etc.

Note how the hierarchy works:
- The 1st second level header under the 1st first level header will be 001001
- The 4th third level header under the above will be 001001004 etc etc

Once you have derived the prefix you can run the update statement. So to move everything in tree 12 to tree 16 header 002 you would run:

Code: Select all

UPDATE `graph_tree_items` SET graph_tree_id = '16',
`order_key` = concat("002",left(order_key,length(order_key)-3)) WHERE `graph_tree_id` = 12;
Similarly to move everything in tree 19 into tree 42 header 001003

Code: Select all

UPDATE `graph_tree_items` SET graph_tree_id = '42',
`order_key` = concat("001003",left(order_key,length(order_key)-6)) WHERE `graph_tree_id` = 19;
This saved me a bunch of time moving about 500 graph items, but it has potential to be quite dangerous so be warned!
[url=http://www.welham.net/]Steve W[/url]
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests