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);
}
}
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.