Skip to content

remove_graphs.php doesn't allow for removal of graphs where Host ID = 0 #7035

@tddcacti

Description

@tddcacti

Sometimes when a team member deletes a device out of Cacti, they mistakenly select the option to "Leave all Graph(s) and Data Source(s) untouched. Data Source(s) will be disabled however."

This leaves data sources that are disabled and graphs that are never updated again. In our environment, we don't want these hanging around, so I'm looking for a way to get rid of them automatically.

Side note, if there is already a way to do this built into Cacti, I haven't been able to find it. These graphs and data sources don't meet the current definition of "orphaned" that Cacti uses.

So I'm looking into creating a cron job that looks for graphs for which the Host ID has been set to 0 ("None" in the web UI). Then I would use remove_graphs.php to delete both the graph and the data source(s).

However the issue I'm running into, remove_graphs.php as currently written doesn't support --host-id=0. It gives an error:

FATAL: Host ID 0 is invalid

I've made a few edits to this file on my development server and gotten it working, so I would like to submit this as a potential enhancement / bug fix.

There are two edits to be made. First, change this section of code:

if (cacti_sizeof($host_ids)) {
	foreach ($host_ids as $id) {
		if (!is_numeric($id) || $id <= 0) {
			print "FATAL: Host ID $id is invalid" . PHP_EOL;

			exit(1);
		}
	}
}

To this (remove the equals sign):

if (cacti_sizeof($host_ids)) {
	foreach ($host_ids as $id) {
		if (!is_numeric($id) || $id < 0) {
			print "FATAL: Host ID $id is invalid" . PHP_EOL;

			exit(1);
		}
	}
}

This allows valid host IDs to include zero, and not just positive numbers.

Next, edit this section of code:

	$graphs = db_fetch_assoc("SELECT gl.id, gtg.title_cache
		FROM graph_local AS gl
		INNER JOIN graph_templates_graph AS gtg
		ON gl.id=gtg.local_graph_id
		INNER JOIN host AS h
		ON h.id = gl.host_id
		$sql_where");

Remove the reference to the host table altogether:

	$graphs = db_fetch_assoc("SELECT gl.id, gtg.title_cache
		FROM graph_local AS gl
		INNER JOIN graph_templates_graph AS gtg
		ON gl.id=gtg.local_graph_id
		$sql_where");

This last change would impact the ability to use the --host-template-id option, so some additional edits would be necessary. I'm open to suggestions on other ways to do this, if someone's already solved this another way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUndesired behaviourunverifiedSome days we don't have a clue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions