PHP sybase_connect from nagios script - php

Has anyone had problems connecting to a sybase database using sybase_connect in PHP running as a nagios plugin?
My script runs perfectly under the same nagios user on the command line but if I run it via nagios it can never connect to the database, sybase_connect always returns a false connector and sybase_get_last_message is empty.
I've checked the environment settings and even though I'm doing, putenv the only thing I can see is the SYBASE variable may not be set
putenv ("SYBPLATFORM=linux");
putenv ("LC_ALL=default");
putenv ("DSQUERY=SYBASE");
putenv ("SYBASE=/usr/local/freetds");
putenv ("LD_LIBRARY_PATH=/usr/local/freetds/lib");
putenv ('PATH=/usr/local/freetds/bin'); // With this on or off makes no difference
$connector = sybase_connect ( "HOST-IN-FREETDS", "USER", "PASSWORD" );
if ($connector === false) {
// Always falls into here if run via nagios, works if run as the same user from the command line
}
I've compared the settings by running:
$return = `set`;
trigger_error ($return, E_USER_NOTICE);
And checked the php error log, I've also pushed phpinfo to a file and done a diff when running it manually and via nagios.
Environment - Ubuntu LTS 14.04, Nagios Core 4.0
Any ideas anyone? Thanks in advance

Related

Xdebug PhpStorm: debugging scripts started with exec("php index.php")

I'm trying to add debugging to an old project that uses exec() to start a new session asynchronously from within another PHP script:
exec("php /var/www/html/validata/index.php",$result)
The normal PHP script is fully debugable with Xdebug but the script started with the exec command isn't because it can't map from file:///var/www/html/index.php to a local file location since it's started within CLI shell. The session started this way does trigger the debugger but can't find the file locally:
Cannot find file '/var/www/html/validata/index.php' locally.
To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session.
I've followed the instructions to add the PHP_IDE_CONFIG to the env. I've also added this to the server with 127.0.0.1 replaced with the desktop PC IP address (server is running in a docker container):
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"
Any pointers are greatly appreciated!
UPDATE
Solution:
Use the cli interpreter set to the docker container (in settings > Languages & Frameworks > PHP > CLI interpreter, add new, select docker and point it to the php binary) so that a debug session can be started with a new debugging configuration. I've copied the arguments from the exec command into the new configuration and it can now fully debug the script. I have to prepare a database table to make it fully testable but this is a working solution for me.
Thanks for all the replies!
You can solve the issue about the wrong php.ini file being loaded by specifying it in the command line:
exec("php -c " . escapeshellarg(php_ini_loaded_file()) .
" /var/www/html/validata/index.php",$result);
Though I doubt that would make much of a difference as far as xdebug is concerned.
I think a better solution would be to just require the file, which would cause xdebug to not become lost when you fork a new process.
require_once "/var/www/html/validata/index.php";
I've made a mistake how to refer to the server in PHP_IDE_CONFIG:
export PHP_IDE_CONFIG="serverName=SomeName"
should be run in the container where php runs, and SomeName should exactly match what is in PHPStorm/IntelliJ Settings > Languages & Frameworks > php > Servers > Name (not host). It's not a fqdn, just whatever is in the name field.

Zend doesn't load pdo_dblib

I'm trying to connect a Zend application (version 1.11.11) to an mssql server via pdo_dblib (using PHP version 7.0.22). I know that my PHP installation has pdo_dblib since running print(extension_loaded('pdo_dblib')); in the terminal returns 1. In the .ini file I have:
resources.db.adapter = "pdo_mssql"
resources.db.params.pdoType = "pdo_dblib"
When I run the application with a debugger, the value of the statement $a = extension_loaded('pdo_dblib'); is false.
As a result of this PDO::getAvailableDrivers() doesn't contain pdo_dblib (of course), which causes the application to throw an exception.
Does anyone know why this problem occurs, and how to fix it?
The key is not Zend, but in a subtle detail of your tests:
running print(extension_loaded('pdo_dblib')); in the terminal returns 1
when I run the application with a debugger, the value of the statement $a = extension_loaded('pdo_dblib'); is false
PHP on the terminal is not guaranteed to have the same configuration, or even be the same version, as PHP on the web server of the same machine.
You will need to look into your hosting setup to find out how to enable the extension in the php.ini (or equivalent) in the web server's copy of PHP. The output of phpinfo() run in your application, not the terminal, may help.

php script to access remote linux server and execute bash script

I am trying to do the following.
Have php execute shell commands on our remote servers. This because i would like to be able to install scripts through bash So php is going to execute the bash scripts on the remote server.
The issue is the can't connect to the remote server for some reason. I have tried several way to do this and i have to do with the root and password. Later i can look at encrypting them or something like that, but i can't use a ssh key so that is not going to work. I am pretty new to php part of it so i need some help figuring out hot to connect to remote servers.
the code i am trying right now is
$ip = '37.130.000.00';
$user = 'admin';
$pass = 'xxxxxxxxx';
$connection = ssh2_connect($ip);
ssh2_auth_password($connection,$user,$pass);
$shell = ssh2_shell($connection,"uptime");
echo $shell;
For some reason this code is giving me the following error
Fatal error: Call to undefined function ssh2_connect() in
How can i make this work or what else should i do to properly connect to the remote servers and this all should be very flexible as we are talking around 100 server who should be able to connect this way.
Thanks for any input.
That function is provided by an optional extension that you don't seem to have installed. Provided you have libssh and other dependencies in your system, get pecl-ssh by executing
sudo pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3
and then restart php-fpm or apache if you're using modphp.
Edit: btw, this question has been asked before and one of the wisest advises I've seen in regards to is was to switch over to a pure php implementation, no pecl extensions whatsoever:
php-function-ssh2-connect-is-not-working

PHP CLI Connecting to WebSerbvice

I am having an issue with running my PHP application from the command line.
I recently created a new Google Cloud Engine CentOS instance to host my PHP application.
This application has been running away fine on a different RHEL box.
The application is kicked off from a PHP script using a command similar to...
$command = 'bash -c "exec nohup setsid runPHPScript > /dev/null 2>&1 &"';
exec($command, $output, $returnVar);
runPHPSCript is a linux script that essentially runs the actual PHP command...
php myScript.php
myScript.php then goes off and connects to various webservices etc...
When I try to run this on my new instance (bearing in mind this all worked fine on my RHEL box) I get the following SOAP error...
(faultcode: HTTP, faultstring: Could not connect to host)
The SOAP setup/connection to the endPoint WSDL is actually successful but as soon as I try to send the request I get the error above.
I've been debugging this and reading up a bit and can confirm the following...
HTTP & HTTPS are both enabled on the GCE instance.
I have verified that both PHP & Apache are using the same php.ini (more on this below)
I have checked both configurations using phpinfo() and php -i and can see the various SSL entries in the data
The strange part is that if I open myScript.php in a browser (its a LAMP stack) it connects fine to the webservice and I can see the valid response. This lead me to think the problem was different php.ini's being used.
Also, at the command prompt, if I just run myScript.php directly it also works fine...
php 'myScript.php'
returns a valid response too.
So the problem only seems to occur when I try to kick off the application using BASH.
Anyone got any ideas?
Robert
Cleaning up.
As per Raidenace suggestion, just running exec directly worked fine too...
"just curious...why cant you just do an exec("php myScript.php"); in your code direcctly?"

python script executed from php gives error

On my debian i run xampp. I want to execute a python script using php shell_exec.
This is my php code:
shell_exec("/opt/lampp/htdocs/news/hello.py 2>1 &");
When i run it from the browser i get this error:
/usr/bin/python: /opt/lampp/lib/libz.so.1: no version information available (required by /usr/bin/python)
If i run it from the terminal window using this: php /opt/lampp/htdocs/page/index.php it works without any problems.
So any ideas how can i make it work from the browser?
Thanks
I'm guessing xampp comes with it's own libraries instead of using the system libraries, and that probably means it's setting LD_LIBRARY_PATH to it's local library directory.
This will also cause other programs started from php to use this libraries, which they may not be compatible with.
To veryfy that, try system("env");, that sould show you all the exported environment variables. If LD_LIBRARY_PATH is set, use:
shell_exec("LD_LIBRARY_PATH= /opt/lampp/htdocs/news/hello.py 2>1 &");
That unsets it before running the python script.

Categories