The PHP Script Server is a new feature in Cacti 0.8.6. This new feature allows for the rapid execution of PHP based Data Queries in Cacti. The Script Server process is launched by the poller during every polling cycle. It listens for commands from the poller, executes them, and then waits for an quit signal.
The reason that it is so fast is that PHP is started in memory only one time, and for every Data Query called, it's code is interpreted only once. The resulting Data Query binaries are therefore very efficient. Using the Script Server process over the traditional POPEN process nets a 20+ fold speed improvement in Cacti.
Since PHP scripts are so powerful, this new feature in Cacti, makes it an excellent choice for collecting non-SNMP and SNMP based data.
The technique that is applied here uses the PHP include method. Assume, that your script is called myscript. For convenience, we prepend PHP script server modules with a prefix of ss_, denoting the use of script server. This makes the following name: ss_myscript.php. Now, write your code as part of a function, that will be included by the script server. Again, for convenience, we use the module's name as the name (of the first) function: ss_myfunction. Now we have a PHP module called ss_myscript.php which includes a function called ss_myfunction that has all your code.
As an extension, please note that a single module ss_myscript.php may hold several distinct functions, e.g. ss_myfunction1 and ss_myfunction2 and so on.
Cacti 0.8.7 contains two sample script server routines. They are for the collection of HostMib CPU and Disk Partition information. These two examples are based off the traditional POPEN version of the HostMib functions found in earlier versions of Cacti.
For new installs, the HostMib functions are defaulted to using the PHP Script Server, therefore, you don't need to do anything to use it.
For upgrades, you must make several changes to start using the PHP Script Server for the HostMib CPU and HostMib Partitions Data Queries. To migrate you must follow the step
If you are using the two built in script queries, “SNMP - Get Mounted Partitions” and “SNMP - Get Processor Information”, you can migrate to the PHP Script Server using the steps below:
<path_cacti>/resource/script_server/host_disk.xml
<path_cacti>/resource/script_server/host_cpu.xml
Following those steps should complete your migration to the new PHP Script Server for the two example HostMIB Data Queries.
If you have other PHP scripts that you wish to migrate, you must follow the steps below to migrate your scripts to the PHP Script Server required format.
Each PHP Script file must be changed to the new Script Server format. The changes are not dramatic, but required for the proper operation of the PHP Script Server. Follow the steps below to complete.
<?php $no_http_headers = true; /* display No errors */ error_reporting(E_ERROR); include_once(dirname(__FILE__) . "/../include/global.php"); include_once(dirname(__FILE__) . "/../lib/snmp.php"); if (!isset($called_by_script_server)) { array_shift($_SERVER["argv"]); print call_user_func_array("ss_myfunction", $_SERVER["argv"]); }
<?php $a = 100; $b = $a / 10; print $b; ?>
Would become:
function ss_myfunction() { $a = 100; $b = $a / 10; return $b; }
If you are using a ”Script Query” type function, then you must also change your XML file. Please reference the XML files in the <path_cacti>/resource/script_server directory for the specifics related to your required modifications. However, you may also follow the instructions below:
<script_path>|path_php_binary| -q |path_cacti|/scripts/myscript.php</script_path>
to simply the following:
<script_path>|path_cacti|/scripts/ss_myscript.php</script_path>
<script_function>ss_myfunction</script_function> <script_server>php</script_server>
Your Data Queries and Data Templates must be also changed. Although somewhat self explanatory by now, you must make the following changes:
Your final step is to go to the System Utilities and Clear Poller Cache to apply the new settings. If you script is operating correctly, you should now be migrated to the script server.
To test your script in the script server, simply follow the instructions below. When you have finished you testing, simply type “quit” <cr> at the Script Server command line to exit the script server.
shell> php <path_cacti>/script_server.php
NOTE: Due to a bug in Windows implementation of PHP, you must type the full path name to the script_server.php file.
script server> <path_myfunction> my_function argument1 argument2 ...
In the Windows environment, your example could be the following:
script server> c:\\wwwroot\\cacti\\scripts\\ss_myfunction.php ss_myfunction localhost public 1 get duddle
NOTE: If there are errors in your script, you must restart the script server before your retest your code.