[SOLVED] Data Input Method via PHP

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Developers, Moderators

Post Reply
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

[SOLVED] Data Input Method via PHP

Post by newmanity »

Hi.

I'm very new to Cacti, but think it is perfect for what I need. I've managed to import a few templates and have been collecting data properly, but I'd like to get more specific stuff from our system.

I do not know Perl, but am quite comfortable with PHP. I noticed in the documentation and How To that the examples tend to be in Perl. Is there a step by step example for PHP?

Oh, and I'm also a Linux newbie to make things even more awkward. I'm working in an Ubuntu distro, and have downloaded and installed xampp. After following the PHP Script Server section of the manual, I'm still at a bit of a loss. When I try to test my script in the Script Server it does not seem to be being interpreted as php. These are the errors I'm getting:

/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php: line 1: ?php: No such file or directory
/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php: line 2: =: command not found
/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php: line 4: /bin: is a directory
/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php: line 5: syntax error near unexpected token `E_ERROR'
/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php: line 5: `error_reporting(E_ERROR);'

which is based on the following first few lines of the standard php script that is defined in the manual:

<?php
$no_http_headers = true;

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

Thanks for any info.
Last edited by newmanity on Thu Apr 17, 2008 7:49 am, edited 3 times in total.
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

If it wasn't clear from the subject, I'd like to get a simple Data Input Method setup via PHP. Merely pullling an SNMP OID would be enough for me to start from.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Data Input Method via PHP

Post by gandalf »

newmanity wrote:I noticed in the documentation and How To that the examples tend to be in Perl. Is there a step by step example for PHP?
No need for Perl. PHP is fine. An example is not available online. What did you enter when defining the data input method? Do you provide <path_php> -q <path_cacti>/scripts/my-script.php or the like?
Reinhard
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

Ok. I updated my data input method to include the prefix "<path_php> -q" as you wrote. I am still getting the same output on the script server, though now I'm not even sure if I'm actually using the script server. I have php-cli installed, and I type "php /opt/lampp/htdocs/cacti/script_server.php" at the terminal prompt, and it seems to accept the command, but no different shell prompt appears after that (as it seems to in the documentation).

Anyway. This is the very simple php script (ss_SMtest.php) I'm currently trying to get working (I'll make it into what it needs to be once I see that/how cacti and PHP are interacting):

Code: Select all

<?php
$no_http_headers = true;

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

include_once(dirname(__FILE__) . "/../include/config.php");
include_once(dirname(__FILE__) . "/../lib/snmp.php");

if (!isset($called_by_script_server)) {
	//array_shift($_SERVER["argv"]);
	//print call_user_func_array("ss_SMtest", $_SERVER["argv"]);
        print call_user_func_array("ss_SMtest", "");
}


function ss_SMtest() {
	//$ip = $_SERVER["argv"][0];
	//$community = $_SERVER["argv"][1];	$a = 100;
	//echo 'snmpget -v 2c -c '.$community.' '.$ip.' .1.3.6.1.4.1.161.19.3.2.2.3.0';
	return 'snmpget -v 2c -c localnet 10.5.12.135 .1.3.6.1.4.1.161.19.3.2.2.3.0';
}
?>
I call it in the data input method like so :

Code: Select all

<path_php> -q <path_cacti>/scripts/ss_SMtest.php
(the snmpget command works fine when simply run from the terminal prompt)

Apart from the error I already showed you, I'm getting this in the cacti log:

CMDPHP: Poller[0] Host[8] DS[50] WARNING: Result from SERVER not valid. Partial Result: WARNING: Function do
PHPSVR: Poller[0] WARNING: Function does not exist
PHPSVR: Poller[0] WARNING: PHP Script File to be included, does not exist

I'm probably not doing something that would be obvious to you, but I'd appreciate some help.

Thanks.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Let's start with a screenshot of your data input method, please
Reinhard
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

Ok...here it is.
Attachments
Screenshot-Cacti - Mozilla Firefox-1.png
Screenshot-Cacti - Mozilla Firefox-1.png (76.24 KiB) Viewed 3329 times
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: Data Input Method via PHP

Post by gandalf »

gandalf wrote:Do you provide <path_php> -q <path_cacti>/scripts/my-script.php
This recommendation is WRONG for script server. Please enter <path_cacti>/scripts/my-script.php only.
Please refer to http://forums.cacti.net/viewtopic.php?t=7738 for debugging script server scripts
Reinhard
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

Ok. I'm not sure what I did to get this working, but I now have my php scripts being recognized. The problem now is that the php snmpget command is not returning anything. At first it wasn't even being recognized, and then I realized that the xampp/cacti walk through installation (http://howtoforge.com/cacti_xampp_ubuntu) only provided snmp functionality for shell, and not for php (in fact, I had to install php5 as well to have cli). I have now installed php-snmp. The snmpget function now seems to be being recognized, but it is only returning nulls at the moment. My syntax is fine, and I'm pulling info from the MIBs via the shell terminal with no problem. Is this a problem with my installation?

Thanks for any advice.
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

Ok. I've made some headway.

Although I can't find it in the php manual, there is apparently an snmpget function called snmp2_get strictly reserved for snmp version 2c which of course makes sense. Why I had to find it on php's posts rather than in the manual is a little strange to me, but that part is now working, and I'm very pleased.

Whatever my script is returning to cacti is not working, however. I am currently just returning an integer value of the MIB that I am trying to pull through PHP (I know this can be done automatically without PHP, by the way, I'm just wanting to get something simple working first before I do what I really want to do). What should I be returning to Cacti from PHP?

The error I'm getting continues to be the following:

03/28/2008 10:55:18 AM - CMDPHP: Poller[0] Host[8] DS[50] WARNING: Result from SERVER not valid. Partial Result: WARNING: Function do
03/28/2008 10:55:18 AM - PHPSVR: Poller[0] WARNING: Function does not exist
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

Does this poller error mean that Cacti is not even able to see my php script? I can't see why it doesn't see it. Cacti is installed in the following directory:

Code: Select all

/opt/lampp/htdocs/
The Input Type is defined as "Script - Script Server (PHP)" and my data input method's Input string is the following:

Code: Select all

<path_cacti>/scripts/ss_SMtest.php <hostIP> <community>
The script, which is in /opt/lampp/htdocs/cacti/scripts has the following characteristics (canopy being my cacti user and linux user name):

Code: Select all

-rwxrwxrwx 1 canopy canopy  795 2008-03-28 12:58 ss_SMtest.php
Does anyone have any idea what I need to be doing, or checking?

Much appreciated.
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

I'm not sure if anyone's listening at this point, but I've gotten a little closer. I ran the log in DEBUG, and I realized that my script wasn't being recognized because of the syntax of my input string. I followed the instructions of naming my function the same as the PHP file it was in, but that doesn't seem to be working as it is supposed to. The DEBUG log file reads as follows with my input string defined as above:

Code: Select all

PHPSVR: Poller[0] DEBUG: INC: '/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php' FUNC: '10.5.12.105' PARMS: 'localnet'
When I change my input string of the data input method to the following:

Code: Select all

<path_cacti>/scripts/ss_SMtest.php ss_SMtest <hostIP> <community>
I get the following output (but at least it is now seeing the function!):

Code: Select all

CMDPHP: Poller[0] Host[44] DS[352] SERVER: /opt/lampp/htdocs/cacti/scripts/ss_SMtest.php ss_SMtest 10.5.12.105 localnet, output: U
CMDPHP: Poller[0] Host[44] DS[352] WARNING: Result from SERVER not valid. Partial Result:
PHPSVR: Poller[0] DEBUG: INC: '/opt/lampp/htdocs/cacti/scripts/ss_SMtest.php' FUNC: 'ss_SMtest' PARMS: '10.5.12.105 localnet'
Not sure what to do next.
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

Next step made: I changed my php script to return '55' because I was sick of seeing "Partial Result:" with nothing after it. My script finally returned something when I looked at the DEBUG output of the log file.

I've discovered from finding the Tech Support tab in the System Utilities section that PHP-SNMP is not installed, which would explain why my script is never returning anything. It works when I run it from cli, but my worry is that the PHP which Cacti is using is different than the one that cli is using (I downloaded php and php-snmp after my xampp install which is what cacti is using).

How do I go about installing PHP-SNMP into an already existing lampp environment onto linux (ubuntu)?
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

I had to get rid of XAMPP and start from scratch, intsalling everything individually. The PHP SNMP functionality is now working. However, I am not able to pass values to the function at all, and I'm not sure why. I'm in the same situation I was in above. The doc says I must call the function in the data input method like so like so:

Code: Select all

<path_cacti>/scripts/ss_SMtest.php
and this is supposed to automatically call the function named the same as the file. But for me this results in "function does not exist" errors. in order to get the function recognized and working, I have to include the function in the call in addition, like this:

Code: Select all

<path_cacti>/scripts/ss_SMtest.php ss_SMtest arg1 arg2
However, with this method, I cannot pass values to the function. Whether in the test server shell or within cacti itself, the function does not work. If I echo $_SERVER|"argv"||0|, I get /scripts/ss_SMtest.php, and if I echo $_SERVER|"argv"||n| for n>0, I get nothing at all.

Anyone out there with any ideas?
Thanks.
newmanity
Posts: 17
Joined: Wed Mar 19, 2008 3:31 pm
Location: Halifax, Canada

Post by newmanity »

I finally got it working.

I'm not sure whether this was what I was supposed to be doing from the start or not, but this is what ended up working for me. The data input method is:

Code: Select all

<path_cacti>/scripts/ss_SMtest.php ss_SMtest <ip> <community>
And an example of a function within ss_SMtest.php that can successfully have arguments passed to it is the following:

Code: Select all

function ss_SMtest($ip, $cm) {
   $snmpStr = snmp2_get($ip, $cm, ".1.3.6.1.4.1.161.19.3.2.2.3.0");
   $pos = stripos($snmpStr, ":");
   if ($pos)
      return intval(substr($snmpStr,$pos+2,strlen($snmpStr)-$pos));
}
$_SERVER|"argv"||n| is useless for me, and the above works perfectly.

I hope this helps someone.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests