Lost of data because of a php error code in a plugin

Discussions on developing plugins for the Cacti Plugin Architecture

Moderators: Developers, Moderators

Post Reply
galco
Cacti User
Posts: 63
Joined: Fri Aug 03, 2007 3:09 am
Location: Barcelona, Catalunya

Lost of data because of a php error code in a plugin

Post by galco »

Hi,

I developed a single plugin that for a while only shows a new entry in a drop down menu. Yesterday my plugin had a function bad defined and that's why poller.php didn't get any value until I resolved this issue.
I'm very surprised about this behaviour because I don't understand that a plugin that shows a new entry in a dropdown menu is so dangerous. I 'm going to explain exactly the error but is a pain that plugins that are only related with the graphical presentation can damage the poller!


function plugin_init_link2BDCOPS(){

api_plugin_register_hook('link2BDCOPS', 'graph_buttons', 'link2BDCOPS_graph_button', 'setup.php');
api_plugin_register_hook('link2BDCOPS', 'config_arrays', 'link2BDCOPS_config_arrays', 'setup.php');
api_plugin_register_hook('link2BDCOPS', 'draw_navigation_text', 'link2BDCOPS_draw_navigation_text', 'setup.php');
api_plugin_register_hook('link2BDCOPS', 'graphs_action_array', 'link2BDCOPS_graphs_action_array', 'setup.php');
}
I had forget the parentheses:
function link2BDCOPS_graphs_action_array (){
$action['plugin_agrupa_x_institucio'] = 'Agrupa totes les gràfiques d\'una institució';
return $action;
}
I understand I registered a plugin and I associated a function that doesn't exist but what's the problem? I do a very specific mistake or poller.php (with spine) is checking too many things?

Tip: The logs are empty since the moment I forgot the parentheses. I didn't save the output from the poller.php that I run from the crontab.

Thanks
cigamit
Developer
Posts: 3369
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Post by cigamit »

Such is the nature of PHP and development in general. Not much we can or are willing to do about it (we possibly can't do a PHP syntax check on each and every file before including it!!!).
User avatar
Howie
Cacti Guru User
Posts: 5508
Joined: Thu Sep 16, 2004 5:53 am
Location: United Kingdom
Contact:

Post by Howie »

do_hook() could call function_exists() each time, but I agree it's a big hit for an unusual fault.

Some sort of 'dev mode' for the PA that adds trace logging and this type of checking might be worthwhile though. Several times I've tried to figure out where/when/how often my hook is being called.

I think the moral is: don't develop on your production system.
Weathermap 0.98a is out! & QuickTree 1.0. Superlinks is over there now (and built-in to Cacti 1.x).
Some Other Cacti tweaks, including strip-graphs, icons and snmp/netflow stuff.
(Let me know if you have UK DevOps or Network Ops opportunities, too!)
cigamit
Developer
Posts: 3369
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Post by cigamit »

Code: Select all

function api_plugin_hook ($name) {
	global $config, $plugin_hooks;
	$data = func_get_args();
	$ret = '';
	$p = array();

	$result = db_fetch_assoc("SELECT name, file, function FROM plugin_hooks WHERE status = 1 AND hook = '$name'", false);
	if (count($result)) {
		foreach ($result as $hdata) {
			$p[] = $hdata['name'];
			if (file_exists($config['base_path'] . '/plugins/' . $hdata['name'] . '/' . $hdata['file'])) {
				include_once($config['base_path'] . '/plugins/' . $hdata['name'] . '/' . $hdata['file']);
			}
			$function = $hdata['function'];
			if (function_exists($function)) {
				$function($data);
			}
		}
	}
It does call it. That's not the issue, the issue is that we have to include the file to get to the function, but the file had a syntax issue.

And agreed, as a rule don't develop on your production box (even though I do it occasionally to test stuff :oops: )
galco
Cacti User
Posts: 63
Joined: Fri Aug 03, 2007 3:09 am
Location: Barcelona, Catalunya

Post by galco »

Thanks guys!

I understand is not possible and not necessary to check every file but I thought the mistake was that I had registered the plugin:

Code: Select all

api_plugin_register_hook('link2BDCOPS', 'graphs_action_array', 'link2BDCOPS_graphs_action_array', 'setup.php'); 
and Cacti didn't check whether the function exist, in my case it didn't. Now I understand my problem was a syntax problem and Cacti (php) wasn't able to check whether the function exist because my source didn't pass the first syntax check.

However, if the mistake wasn't a syntax error and was an undefined function, is Cacti able to check whether the function exists and if it isn't, disable the plugin? Or may be that's not possible because you'll need to check every time if the function is defined? My question is, could you check that one time when you install or do you have to check every time Cacti runs this piece of code?

Thanks!
cigamit
Developer
Posts: 3369
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Post by cigamit »

It already checks every time the code is ran.
galco
Cacti User
Posts: 63
Joined: Fri Aug 03, 2007 3:09 am
Location: Barcelona, Catalunya

Post by galco »

So, this way the checking process would produce a big overload

Thanks cigamit
cigamit
Developer
Posts: 3369
Joined: Thu Apr 07, 2005 3:29 pm
Location: B/CS Texas
Contact:

Post by cigamit »

Yes, it checks for the function, which has little overhead to do. Checking the syntax of the files before inclusion would have a fairly massive overhead though.

Its already in the plans to have plugins automatically checked when uploaded to the Repository site we are working on, but that is a long way off.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests