Repair corrupted graph-datasource associations

If you figure out how to do something interesting/cool in Cacti and want to share it with the community, please post your experience here.

Moderators: Developers, Moderators

shd
Cacti User
Posts: 96
Joined: Mon Apr 09, 2007 1:33 pm
Location: Nizhnevartovsk, Russia

Repair corrupted graph-datasource associations

Post by shd »

When changing traffic graph template, I have deleted every item in that template. Cacti silently dropped all associations of graphs with datasources. I have immediately returned all items of graph template, but harm was already done. Every traffic graph was corrupted, traffic_in and traffic_out was set to "none" in each graph of this template.

I have searched through the forum, but didn't find any solution to this problem. It would be really a pain to associate each graph with corresponding datasources in 2000+ graphs or to restore from outdated backup, so I made quick and dirty fixer on top of repair_templates.php.

May be it will save a day for someone like me, so here it is.

It should be placed in cacti/cli directory and called like this:

Code: Select all

php repair_graphs.php --check --host-id=658 --data-template-id=41 --graph-template-id=2
--check - parameter to run only checking procedure. (Replace it with --execute to actually run the fix)
--recreate - Delete and recreate graph items (USE THIS OPTION WITH CAUTION! Data should not be deleted, but you may corrupt all your graphs if something go wrong)
--host-id - id of desired host, can be empty to check every graph in cacti database.
--data-template-id - id of data template, required, can be viewed at cacti/data_templates.php page.
--graph-template-id - id of graph template, required, can be viewed at cacti/graph_templates.php page.

For example, I used Interface - Traffic (bits/sec) graph template (id=2) with Interface - Traffic data template (id=41).

If you are familiar with SQL, you can add --show-sql parameter to see the SQL queries which will be executed.

Use with caution! There are not much error-checking!
Cacti database backup before execution is strongly advised. (db only, rra files are intact)
Attachments
repair_graphs.php.txt
Version 0.3
added graphs recreate option (i.e. delete graph items and then recreate them immediately)
(7.85 KiB) Downloaded 1032 times
repair_graphs.php.txt
(7.24 KiB) Downloaded 1072 times
Last edited by shd on Mon Aug 26, 2013 6:21 am, edited 2 times in total.
wbo
Posts: 1
Joined: Sat Jan 15, 2011 1:18 pm

Re: Repair corrupted graph-datasource associations

Post by wbo »

This is very useful for me - if it works. Thank you very much for your efforts. I am still wondering why it happened and if we are the only ones who had this problem.
I will come back and report if I could solve my problems with your fix.
pparcell
Posts: 8
Joined: Thu Jul 23, 2009 11:27 am
Location: Utah

Re: Repair corrupted graph-datasource associations

Post by pparcell »

I was in the process of putting together the queries to do this exact same thing. Thanks so much for saving me the time of piecing together the queries :).
combellm
Cacti User
Posts: 111
Joined: Mon Oct 10, 2011 9:51 am
Location: PARIS, France

Re: Repair corrupted graph-datasource associations

Post by combellm »

Around two years later I did the same mistake....
My graphs only have 1 over 3 associations!
I lost almost everything with more than 1K graphs...

I tried to use this script but I don't have anything working...
Ijust have this line:

Code: Select all

-sh-3.2$ php repair_graphs.php --check --host-id=154 --data-template-id=41 --graph-template-id=2 --show-sql
NOTE: Performing Check of Graphs

-sh-3.2$
And nothing more without the --host-id=154 parameter...

CACTI version 0.8.8a

Thanks
--
knowledge is the only thing which increases, when shared
shd
Cacti User
Posts: 96
Joined: Mon Apr 09, 2007 1:33 pm
Location: Nizhnevartovsk, Russia

Re: Repair corrupted graph-datasource associations

Post by shd »

combellm wrote:I tried to use this script but I don't have anything working...
Ijust have this line:

Code: Select all

-sh-3.2$ php repair_graphs.php --check --host-id=154 --data-template-id=41 --graph-template-id=2 --show-sql
NOTE: Performing Check of Graphs

-sh-3.2$
And nothing more without the --host-id=154 parameter...
Try checking your graph-template-id and data-template-id, choose it appropriately:

--data-template-id - id of data template, required, can be viewed at cacti/data_templates.php page.
--graph-template-id - id of graph template, required, can be viewed at cacti/graph_templates.php page.
CentOS 64-bit, 20Gb RAM, 8xCPU, some virtual datastore. 600+ hosts, 200k+ datasources, avg polltime 45 sec
combellm
Cacti User
Posts: 111
Joined: Mon Oct 10, 2011 9:51 am
Location: PARIS, France

Re: Repair corrupted graph-datasource associations

Post by combellm »

shd wrote:
combellm wrote:I tried to use this script but I don't have anything working...
Ijust have this line:

Code: Select all

-sh-3.2$ php repair_graphs.php --check --host-id=154 --data-template-id=41 --graph-template-id=2 --show-sql
NOTE: Performing Check of Graphs

-sh-3.2$
And nothing more without the --host-id=154 parameter...
Try checking your graph-template-id and data-template-id, choose it appropriately:

--data-template-id - id of data template, required, can be viewed at cacti/data_templates.php page.
--graph-template-id - id of graph template, required, can be viewed at cacti/graph_templates.php page.
Yeah the ID's are correct!
I searched in the database because I didn't find it in the WEB interface...
--
knowledge is the only thing which increases, when shared
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Repair corrupted graph-datasource associations

Post by gandalf »

Code: Select all

	foreach($graph as $g) {
		$query = "SELECT * FROM graph_templates_item where local_graph_id=" . $g["id"];
		$graph_items = db_fetch_assoc($query);
		
		if (sizeof($graph_items)) {
			continue;
		}
I wonder, which specific case is tackled here. In case you find ANY graph_item, you skip the graph. So if only part of the graph is broken, this will be skipped. Do I understand that part of the code correctly?
R.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Repair corrupted graph-datasource associations

Post by gandalf »

gandalf wrote:

Code: Select all

	foreach($graph as $g) {
		$query = "SELECT * FROM graph_templates_item where local_graph_id=" . $g["id"];
		$graph_items = db_fetch_assoc($query);
		
		if (sizeof($graph_items)) {
			continue;
		}
I wonder, which specific case is tackled here. In case you find ANY graph_item, you skip the graph. So if only part of the graph is broken, this will be skipped. Do I understand that part of the code correctly?
R.
BTB: yes, I modified the code a bit for more SQL debugging.
As you used the standard Cacti file header, you submit this to The Cacti Group under GPL V2, do you?

R.
shd
Cacti User
Posts: 96
Joined: Mon Apr 09, 2007 1:33 pm
Location: Nizhnevartovsk, Russia

Re: Repair corrupted graph-datasource associations

Post by shd »

gandalf wrote:I wonder, which specific case is tackled here. In case you find ANY graph_item, you skip the graph. So if only part of the graph is broken, this will be skipped. Do I understand that part of the code correctly?
Yes, correct. I didn't have any graph items, so thought that's the way to search graphs for fixing.
gandalf wrote:As you used the standard Cacti file header, you submit this to The Cacti Group under GPL V2, do you?
True. :)
CentOS 64-bit, 20Gb RAM, 8xCPU, some virtual datastore. 600+ hosts, 200k+ datasources, avg polltime 45 sec
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Repair corrupted graph-datasource associations

Post by gandalf »

shd wrote:
gandalf wrote:I wonder, which specific case is tackled here. In case you find ANY graph_item, you skip the graph. So if only part of the graph is broken, this will be skipped. Do I understand that part of the code correctly?
Yes, correct. I didn't have any graph items, so thought that's the way to search graphs for fixing.

@combellm: I suppose that this is why it won't work for you. You still have "some" data source in right place. But I will work on this script to make it accept your case as well and submit it as an officially supported script to the Cacti codebase. I will honor shd as the original author, for sure.

R.
combellm
Cacti User
Posts: 111
Joined: Mon Oct 10, 2011 9:51 am
Location: PARIS, France

Re: Repair corrupted graph-datasource associations

Post by combellm »

gandalf wrote:
shd wrote:
gandalf wrote:I wonder, which specific case is tackled here. In case you find ANY graph_item, you skip the graph. So if only part of the graph is broken, this will be skipped. Do I understand that part of the code correctly?
Yes, correct. I didn't have any graph items, so thought that's the way to search graphs for fixing.

@combellm: I suppose that this is why it won't work for you. You still have "some" data source in right place. But I will work on this script to make it accept your case as well and submit it as an officially supported script to the Cacti codebase. I will honor shd as the original author, for sure.

R.
Thanks
My case was due to something stupid I did! I repaired all my graphs by selecting again all datasources!
Took me 2 days!
Next time I will read everything before start something!
--
knowledge is the only thing which increases, when shared
ebitda
Posts: 6
Joined: Tue Aug 13, 2013 2:39 am

Re: Repair corrupted graph-datasource associations

Post by ebitda »

Thank you for sharing your fix - it solved problems I had after using the 'Nexus' template
shd
Cacti User
Posts: 96
Joined: Mon Apr 09, 2007 1:33 pm
Location: Nizhnevartovsk, Russia

Re: Repair corrupted graph-datasource associations

Post by shd »

New version: 0.3
Added graphs recreate option (i.e. delete graph items and then recreate them immediately). Comes handy if only a part of graph template is corrupted. But use it with caution, and please backup Cacti database beforehand. Data should not be deleted, but just in case.
CentOS 64-bit, 20Gb RAM, 8xCPU, some virtual datastore. 600+ hosts, 200k+ datasources, avg polltime 45 sec
geno
Posts: 11
Joined: Tue Jan 04, 2011 5:52 pm

Re: Repair corrupted graph-datasource associations

Post by geno »

Just want to say thank you. I added some new fields in my template that I had forgotten.
The check or repair didn't work for me but the recreate function works perfectly!
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Repair corrupted graph-datasource associations

Post by gandalf »

shd wrote:New version: 0.3
Added graphs recreate option (i.e. delete graph items and then recreate them immediately). Comes handy if only a part of graph template is corrupted. But use it with caution, and please backup Cacti database beforehand. Data should not be deleted, but just in case.
It seems, that you are faster than me. I was thinking about a different approach, but yours might be even simpler.
Thx for contributing!
R.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest