[HOWTO] Testing the PHP Script Server

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
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

[HOWTO] Testing the PHP Script Server

Post by TheWitness »

All,

If you are having problems with the PHP Script Server, please follow the instructions below:

1) Start PHP Script Server

Use the following command to start the script server: "php <path_cacti>/script_server.php"

You wll receive a message from PHP indicating "PHP Script Server Started Properly". If you get any warning messages, then you have to fix them right away.

2) Obtain a Script Command from the Poller Cache

a) To do this Goto System Utilities->View Poller Cache
b) Search for "Action: 2"
c) Copy the portion of the script starting with the full path and working
to the end of line. For example:
c:\wwwroot\cacti086\scripts\ss_host_cpu.php ss_host_cpu localhost public 2 161 500 get usage 0
3) Execute the Script Server Command

Paste the command from 2c into your Script Server window and press enter.

You should receive an output statement. If not, please correct any warnings or script errors before proceeding.

4) To End the Script Server session

Type "quit" in the Script Server window and press enter.

Important Note:
For older versions of PHP < 4.3.3, you should be able to execute the command cut from setp 2c directly to a command line prefixed by "php" and run that script stand alone.

TheWitness :D
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
chris.y2k.r1
Posts: 31
Joined: Tue Mar 07, 2006 11:39 pm

Post by chris.y2k.r1 »

THAT!!!! Is good to know.

What are the symptoms of a problematic script server..... For us php dummies out there..?
ymartin59
Posts: 40
Joined: Fri Feb 17, 2006 7:42 am

Post by ymartin59 »

Hello,
I would like to know what are advantages of the method "script server" compared to "snmp query" ?
When I compare "net-snmp_disk.xml" description and "ss_host_disk.php" design, and according to the poller cache information, I think the "net-snmp_disk" should be faster to process than script server 'host_disk" because cmd.php or cactid can collect value from a single OID directly without invoking a script. Am I right ?

When designing a new data collector, is it better to use "snmp query" (when possible of course ) or "script server" ?
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Post by TheWitness »

SNMP Queries will always be orders of magnitude faster especially using Cactid due to the fact that we are using RAW protocol with SNMP vs. a script that essentially does the same. The Script Server speeds up scripts when compared to a native shell or perl call however.

TheWitness
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
dam11
Posts: 4
Joined: Wed May 30, 2007 2:39 am
Location: Biarritz, France

Post by dam11 »

Hello,

I've got a problem with the migration from an existing PHP script to Script Server method, I just follow the doc steps ("Migration of Existing PHP Scripts to Script Server") and my graphs are stopped (nothing in the poller cache).
So, I have to manually create new graphs with the script server method, it goes very well (results tested with the CLI script_server.php are right compared to stand alone script).
The opposite transition works fine (from stand alone script to script server). :o
Did I miss another step during the migration ?

Thanks for your answers.

PS : I'm using cacti-0.8.6j under Linux.
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Post by TheWitness »

I would have to see specificatlly what you did. It does work though. Prior to migration, you should always test your script as both script and non-script. The nice thing about the script itself, it should work both ways. Once you have that done, and graphing, you can change your data input method to be script server. Last step is to re-populate your poller cache.

TheWitness
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
dam11
Posts: 4
Joined: Wed May 30, 2007 2:39 am
Location: Biarritz, France

Post by dam11 »

Okay, I will try to explain :wink:
This is the verbose query for qos_plug.php :

Code: Select all

+ Running data query [10].
+ Found type = '4 '[script query].
+ Found data query XML file at '/var/www/public/cacti/resource/script_queries/class_qos.xml'
+ XML file parsed ok.
+ Executing script for list of indexes '/usr/bin/php -q /var/www/public/cacti/scripts/qos_plug.php class *.*.*.* comm 2 index'
+ Executing script query '/usr/bin/php -q /var/www/public/cacti/scripts/qos_plug.php class *.*.*.* comm 2 query index'
....
Here a request from poller cache :

Code: Select all

Script: /usr/bin/php -q /var/www/public/cacti/scripts/qos_plug.php class  *.*.*.* comm 2 get cbQosCMDropByte 2.class-default
RRD: /var/www/public/cacti/rra/routeur_cisco_1_cmpostpolicybyte_76.rrd
And this is the verbose query for ss_qos_plug.php :

Code: Select all

+ Running data query [10].
+ Found type = '6 '[script query].
+ Found data query XML file at '/var/www/public/cacti/resource/script_server/class_qos.xml'
+ XML file parsed ok.
+ Executing script for list of indexes '/usr/bin/php -q /var/www/public/cacti/scripts/ss_qos_plug.php class *.*.*.* comm 2 index'
+ Executing script query '/usr/bin/php -q /var/www/public/cacti/scripts/ss_qos_plug.php class *.*.*.* comm 2 query index'
....
Poller :

Code: Select all

Script Server: /var/www/public/cacti/scripts/ss_qos_plug.php ss_qos_plug class *.*.*.* comm 2 get cbQosCMDropByte 2.class-default
RRD: /var/www/public/cacti/rra/routeur_cisco_1_cmpostpolicybyte_76.rrd
112 items, 16 rows in the two cases. There is no difference between the results.
When the ss_qos_plug.php is running and when I change to qos_plug.php, there is no problem, Cacti continue to graph, the poller cache is correctly rebuilt.

But in the other direction, when I rebuild poller cache, nothing happens (the poller requests disappear), so I have to delete Data Sources and create new graphs with old rrd files (to keep history).
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Post by TheWitness »

The "plller" quote you showed is actually the "poller cache". So from that, it would, in fact, appear to be being added. However, it is likely not running for some reason. Have you tested outside of the poller yet:

The scneario would be as follows:

Code: Select all

cd /var/www/html/cacti
php -q script_server.php
Response would be "Scrpt Server Started, Parent is cmd.php"

Then enter:

Code: Select all

/var/www/public/cacti/scripts/ss_qos_plug.php ss_qos_plug class *.*.*.* comm 2 get cbQosCMDropByte 2.class-default
Response should be, what you expect, on 1 line not empty lines. Then type

Code: Select all

quit
To exit.

TheWitness
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
dam11
Posts: 4
Joined: Wed May 30, 2007 2:39 am
Location: Biarritz, France

Post by dam11 »

I already followed this scenario and the result seems to be right, no empty line, no other character, just the requested value.

That's the cacti log for the two scripts :
CMDPHP: Poller[0] Host[2] DS[77] SERVER: /var/www/public/cacti/scripts/ss_qos_plug.php ss_qos_plug class *.*.*.* comm 2 get cbQosCMPrePolicyByte 2.class-default, output: 1492297591
CMDPHP: Poller[0] Host[6] DS[109] CMD: /usr/bin/php -q /var/www/public/cacti/scripts/qos_plug.php class *.*.*.* comm 2 get cbQosCMPrePolicyByte 2.class-default, output: 1492323609
User avatar
TheWitness
Developer
Posts: 17047
Joined: Tue May 14, 2002 5:08 pm
Location: MI, USA
Contact:

Post by TheWitness »

So, everything "should" be working. Do you see any issues when you run "php -q poller.php" from a command line? If you get that information in your log, then the only thing to break the graph would be an rrdupdate issue that you would be able to see by running "php -q poller.php".

TheWitness
True understanding begins only when we realize how little we truly understand...

Life is an adventure, let yours begin with Cacti!

Author of dozens of Cacti plugins and customization's. Advocate of LAMP, MariaDB, IBM Spectrum LSF and the world of batch. Creator of IBM Spectrum RTM, author of quite a bit of unpublished work and most of Cacti's bugs.
_________________
Official Cacti Documentation
GitHub Repository with Supported Plugins
Percona Device Packages (no support)
Interesting Device Packages


For those wondering, I'm still here, but lost in the shadows. Yearning for less bugs. Who want's a Cacti 1.3/2.0? Streams anyone?
dam11
Posts: 4
Joined: Wed May 30, 2007 2:39 am
Location: Biarritz, France

Post by dam11 »

Thanks fo your answers, I will continue my investigation...
roxy
Posts: 17
Joined: Wed May 04, 2016 7:56 am

Re: [HOWTO] Testing the PHP Script Server

Post by roxy »

Hi,

I have a system on Cacti 0.8.8a in VM environment. I have created a clone of the same and upgraded Cacti from 0.8.8a to 0.8.8d on it. After upgrade every thing is working fine except few script server queries. I am getting NAN value on all the graphs that are getting data from these script server queries. On my Cacti 0.8.8a server when I tested poller cache query manually I got correct result against a get query (see below).
[root@seth cacti]$php script_server.php
PHP Script Server has Started - Parent is cmd
/tools/cacti-0.8.8a/scripts/ss_globalStats.php ss_globalStats TEST-01 2413 get ActiveEpsBearer global
608896
quit
PHP Script Server Shutdown request received, exiting
But when I tried same test on new Cacti version on clone system.
PHP Script Server has Started - Parent is cmd
/tools/cacti-0.8.8d/scripts/ss_globalStats.php ss_globalStats TEST-01 2413 get ActiveEpsBearer global

quit
PHP Script Server Shutdown request received, exiting
608966You have mail in /var/spool/mail/root
So here result of get is coming after "quit", don't know why. This has resulted NAN value on all the graphs.
Is there any problem with my below code?

Please help me to troubleshoot this issue.

Code: Select all

        } elseif ($cmd == "get") {
                $arg = $arg1;
                $index = $arg2;

                foreach ( $arrmeasInfo as $measInfo ) {

                        //print_r(${$measInfo.'_values'});

                        if ( isset(${$measInfo.'_types'}[$arg]) ) {
                                print ${$measInfo.'_values'}[${$measInfo.'_types'}[$arg]][$index];
                        }
                }
        } else {
        }
I am using below:

Cacti 0.8.8d
Spine 0.8.8a [0.8.8d did not worked after compilation so I have used old spine 0.8.8a as per suggestion from a Cacti forum]
Plugin Architecture Version: 3.1
Cacti Multipoller server 0.8.8d
roxy
Posts: 17
Joined: Wed May 04, 2016 7:56 am

Re: [HOWTO] Testing the PHP Script Server

Post by roxy »

It has been resolved now after changing the print command with return. It was working on Cacti 0.8.8a but now need to use return for "get" function in the server script on Cacti 0.8.8d or Cacti 0.8.8e. Now this issue is resolved.
sjohn
Posts: 1
Joined: Wed Nov 23, 2022 1:02 pm

Re: [HOWTO] Testing the PHP Script Server

Post by sjohn »

I wrote perl and php scripts to connect by ssh to our new AirFiber AF5X HD devices. I haven't yet found a way to do this on the Airfiber with SNMP though I was able to get cpu load averages. Probably I am not going about it the easiest way but wanted to use the php script server to preload the code and make it more efficient. I did research and was somewhat confused by the Data Query XML specifications vs just running as a script server with appropriate modification to the regular script. So I am posting here for other cacti newbies step by step and looking for tips to improve and/or correct mistakes. I did use the cacti site and some php ssh2 references so hat's off to my sources.

<?php
$no_http_headers = true;

// to test script in script server
// php /usr/share/cacti/script_server.php
// /usr/share/cacti/scripts/ss_afthroughput.php ss_afthroughput IP_ADDRESS admin ssh_passwd

/* display No errors */
error_reporting(E_ERROR);

//$fh = fopen("/var/log/cacti/ss_afthoughput.log", "a");
if (!isset($called_by_script_server)) {
// array_shift($_SERVER["argv"]);
print call_user_func('ss_afthroughput', $_SERVER["argv"][1],$_SERVER["argv"][2],$_SERVER["argv"][3]);
}
//if ($fh) { fclose($fh); }

function ss_afthroughput($host='IP_ADDRESS',$user='admin',$pass='ssh_passwd') {
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");

$methods = array(
'hostkey'=>'ssh-rsa,ssh-dss',
'client_to_server' => array(
'crypt' => 'aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-cbc,blowfish-cbc',
'comp' => 'none'));

$callbacks = array('disconnect' => 'ssh_disconnect');

$cmd = '/usr/www/status.cgi | sed \'1,2d\' | sed s/\,\"time_sec.*// | sed s/.*rx_throughput/RX/ | sed s/\,\"tx_bytes.*tx_throughput/\ TX/ | sed s/\"\:/\:\/g';

$connection = ssh2_connect($host, 22, $methods, $callbacks);
if (!$connection) die("Connection failed:");

ssh2_auth_password($connection, $user, $pass) or die("Unable to authenticate");
$stream = ssh2_exec($connection, $cmd);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
return stream_get_contents($stream_out);
}

/* Notify the user if the server terminates the connection */
function ssh_disconnect($reason, $message, $language) {
printf("Server disconnected with reason code [%d] and message: %s\n", $reason, $message);
}

?>

I copied this to /usr/share/cacti/scripts. I initially tried making an XML and decided this probably wasn't necessary ... and it works.
So hopefully the code is cached in the script server though not a Data Query?

Go to Console / Data Collection / Data Input Methods / and add a new data source
Name AirFiber - SS_Throughput
Input Type Script Server
Input String<path_cacti>/scripts/ss_afthroughput.php ss_afthroughput <hostname> <username> <password>
Input Fields
hostname Hostname 1
username Username 2
password Password 3
Output Fields
RX Recieved Throughput Selected (update rrd file toggle on)
TX Transmitted Throughput Selected (update rrd file toggle on)

I am not sure what the use is of passing the function name to the script when I don't need it to run the script though it might be useful if I wanted to select than one function.
I also wasn't able to pass the $_SERVER['argv'] as an array so passed individual values, I'm a little weak in php.

Go to Console / Template / Data Source / add a new data source
AirFiber - SS_Throughput *Edit Data Input Method.

Name |host_description| - AirFiber SS_Throughput
Data Source Name AirFiber - SS_Throughput
Name |host_description| - SS_Throughput
Data Input Method Airfiber SS_Throughput
Data Source Profile
Data Source Active

1: RX
2: TX

Data Source Item [RX]
Internal Data Source Name
Minimum Value ("U" for No Minimum) 0
Maximum Value ("U" for No Maximum) U
Data Source Type
Output Field
Custom Data [data input: AirFiber - SS_Throughput]
Hostname
Password ssh_password
Username admin_username in this case airfiber uses admin

You need to add input and output fields, input will be hostname username and password .. check the toggle to allow changes to username and password after creation
For output add first RX then TX and select the correct Output Field RX - Recieved Output for RX and TX - Transmitted Throughput for TX
and Save
Go to Templates / Graph / I would just copy one of the other graphs for Interface throughput I think I used the 95th percentile graph. You manually copy the template or just select the graph and make a copy and make the copie's name AirFiber - SS_Througkput.
Delete all the data sources from graph item inputs, these will be recreated.
Go to each indivdual graph item and change the Data Input Filter to Airfiber SS_Throughput and Data Source to the RX for old RX entries and TX for old TX entries aka in and out.
Save the graph when done.
Go to Management / Devices and select the device to edit
Go to add a graph template and add Airfiber SS_Throughput - modify username and password though you can set defaults in the data source template

And, it should say graphing, after the poller runs once it should show up under graphs
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests