Pass custom parameters to Script Server Query arg_prepend

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

Post Reply
Toddles18
Posts: 22
Joined: Fri Feb 20, 2009 1:28 pm

Pass custom parameters to Script Server Query arg_prepend

Post by Toddles18 »

The problem I found was I needed to be able to pass a custom value to a DataQuery per host. This functionality is available natively with regular Script Input Methods, but not via Script Queries.

In my case I'm collecting Microsoft SQL Server Statistics. I needed to be able to pass the ODBCName for the script to connect via. The trick is to pass the |host_id|, and then query the cacti database for that value in the notes field.

XML script

Code: Select all

<interface>
	<name>MSSQL DB IO Metrics</name>
	<description>Uses PHP to get DB IO Metrics</description>
	<script_path>|path_cacti|/scripts/ss_php_mssql_dbIO.php</script_path>
	<script_function>php_mssql_dbIO</script_function>
	<script_server>php</script_server>
	<arg_prepend>|host_hostname| |host_id| cactiuser cactipassword</arg_prepend>
	<arg_index>index</arg_index>
	<arg_query>query</arg_query>
	<arg_get>get</arg_get>
	<arg_num_indexes>num_indexes</arg_num_indexes>
	<output_delimeter>!</output_delimeter>
	<index_order>MetricDescription</index_order>
	<index_order_type>alphabetic</index_order_type>
	<index_title_format>|chosen_order_field|</index_title_format>


PHP Script Server snippet

Code: Select all

	array_shift($_SERVER["argv"]);
	
	print call_user_func_array("php_mssql_dbIO", $_SERVER["argv"]);
}	



/*Unfortunately, Cacti does not support passing a custom argument to the script server when using the DataQuery Input method.  */
/*This is required in order to tell cacti which ODBC connection to connect to.  In order to get around this we'll create custom xml
/* in the notes field, and then get it by passing the host_id and parsing the xml.*/

/*This script works by utilizing already defined ODBC connections to select data from a MSSQL Database */
/*Based on the parameters passed by cacti to this script, it executes functions.  The Index and Query*/
/*functions can return its results using print, however get has to use "return" to return the actual result*/
function php_mssql_dbIO($odbcname, $host_id, $dbuser, $dbpass, $purpose, $queryfor = "", $indexid = "") {	

//get the ODBCName by referencing the $host_id to Custom XML placed in the Notes field.
$NotesXML = db_fetch_cell("select notes from host where id='" . $host_id . "'");
$xml=simplexml_load_string($NotesXML) or die("Error: XML not configured in the notes field as expected");
$odbcname=$xml->ODBCName;

 if($purpose == "num_indexs"){
	print num_indexs($odbcname, $dbuser, $dbpass);
 }elseif($purpose == "index"){
	$arr_index = index($odbcname, $dbuser, $dbpass);
	for ($i=0;($i<sizeof($arr_index));$i++) {
			print $arr_index[$i] . "\n";
		}
 }elseif($purpose == "query"){
	$arr_index = query($odbcname, $dbuser, $dbpass, $queryfor, $indexid);
	for ($i=0;($i<sizeof($arr_index));$i++) {
			print $arr_index[$i] . "\n";
		}
 }elseif($purpose == "get"){
	return get($odbcname, $dbuser, $dbpass, $queryfor, $indexid);
 }
}
Example of the XML I added to my notes field

Code: Select all

<Notes>
<Details>This is the Production SQL Server</Details>
<ODBCName>MY_ODBCName</ODBCName>
</Notes>

I hope this helps other people, as this was a major road block for me. Once I figured this trick out it is easy to write scripts as needed and should be able to be used in regular script queries the same way.
Technical Support
General Information
Date Wed, 02 Mar 2016 13:35:32 -0500
Cacti Version 0.8.8f
Cacti OS win32
SNMP Version NET-SNMP version: 5.5
RRDTool Version RRDTool 1.4.x
Hosts 4
Graphs 34
Data Sources Script - Script Server (PHP): 16
Script Query - Script Server: 18
Total: 34
Poller Information
Interval 60
Type cmd.php
Items Action[2]: 72
Total: 72
Concurrent Processes 8
Max Threads 4
PHP Servers 4
Script Timeout 25
Max OID 10
Last Run Statistics Time:12.0814 Method:cmd.php Processes:8 Threads:N/A Hosts:5 HostsPerProcess:1 DataSources:72 RRDsProcessed:30
PHP Information
PHP Version 5.5.20
PHP OS WINNT
PHP uname Windows NT MIHQDBMONITOR01 6.3 build 9200 (Windows Server 2012 R2 Standard Edition) AMD64
PHP SNMP Installed
max_execution_time 60
memory_limit 512M
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests