===== Script Data Query Walkthrough ===== Goal of this HowTo will be to show the **principles of writing a Script Query**, including script, xml and all needed templates. Why should you create such a thing? Suppose, your target features some indexed readings, that are **not** available via SNMP but by some other method (e.g. wget/cgi, ssh, NRPE, ...). Writing a Script Data Queries works very much the same way as SNMP Data Queries. But nevertheless, I'll take you through all of the steps now. The example uses php. Why php? First, it's easier to copy stuff from already existing php scripts. Second, it would be possible to use cacti functions. It should be possible to imagine, how this works with other programming languages. Strictly speaking, I'm not that php expert. So be patient with me. Please pay attention. This HowTo will **not** explain how to write a **Script Server Data Query** (yes, there is such a thing!). It would not introduce that many changes. But this will be left to some other HowTo. Personally, my primary goal was to use an example, that all users should be able to copy to execute each and every step on its own. Unfortunately, there seems to be no example, that is common enough and interesting at the same time. So I'm sorry to announce, that this HowTo will show "Interface Traffic Data Gathering". Yes, I know, this is not that new. And surely, it will not be as fast as pure SNMP. So, to my shame, I suppose that this will never make it into any production environment. But, again, this is not the primary goal. Before starting the work, I feel encouraged to point out a drawback of this approach. Cacti will start a php instance, each time it has to fetch a value from the target device. This is not that fast, obviously. And it will not prosper from the performance boost when switching over from cmd.php to cactid. Of course, even cactid will need to start php! And that's exactly, where the thingy called **Script Server Data Query** drops in. But let's leave this for the next main chapter. The code runs on Cacti 0.8.7 versions. ==== Basic Script ==== The starting point will be some very basic php script. Put it into **/scripts/query_interface_traffic.php**. It will show interface indices only for the given target host. The script takes two parameters as input, the **hostname** of the target and the string **index**. You have to implement the **index method**, as OO programmers would say. In this case, there's an "if" clause to process index requests. Output is a list of indices, each one on a seperate line. This script is only meant to run at the command line."); } # deactivate http headers $no_http_headers = true; # include some cacti files for ease of use include(dirname(__FILE__) . "/../include/global.php"); include(dirname(__FILE__) . "/../lib/snmp.php"); # define all OIDs we need for further processing $oids = array( "index" => ".1.3.6.1.2.1.2.2.1.1", ); $xml_delimiter = "!"; # all required input parms $hostname = $_SERVER["argv"][1]; # hostname/IP@ $cmd = $_SERVER["argv"][2]; # one of: index/query/get # put your own community string here $snmp_community = "public"; # community string $snmp_version = 1; # snmp version $snmp_port = 161; # snmp port $snmp_timeout = 500; # snmp timeout $snmp_retries = 3; # snmp retries $max_oids = 1; # max oids for V2/V3 hosts # required for SNMP V3 $snmp_auth_username = ""; $snmp_auth_password = ""; $snmp_auth_protocol = ""; $snmp_priv_passphrase = ""; $snmp_priv_protocol = ""; $snmp_context = ""; # ------------------------------------------------------------------------- # main code starts here # ------------------------------------------------------------------------- # ------------------------------------------------------------------------- # script MUST respond to index queries # the command for this is defined within the XML file as # index # you may replace the string "index" both in the XML and here # ------------------------------------------------------------------------- # php -q