Page 1 of 1

Unable to connect to database with PDO - PHP script

Posted: Mon May 09, 2022 7:45 am
by rubenhgua
Hi,
i'm having a problem with a PHP script which I use to monitor an Informix DB.

When I excute the nagios check that calls the php script, I allways get this message: "Unable to connect to database with PDO. Could not find driver".

The command in nagios is defined like this: usr/bin/php -f /usr/local/nagios/scripts/Check_BBDD_connections.php

But, when executing the php script throught CLI or direct with APACHE URL to the php file works fine in both cases and there is no problem with PDO driver.
Only when the script is executed from NAGIOS, i get the error message.

I'm running PHP 8.1.5, Apache 2.4.5.3 and Nagios Core 4.4.6.

Could anybody tell me what could be wrong? I'm quite lost at this point.

Thank you very much. Ruben.

Re: Unable to connect to database with PDO - PHP script

Posted: Fri May 20, 2022 12:57 am
by VictorJohnsonandroni
Are you sure that you DB engine is ON? And that user is without password? Maybe is root root?

And little tip : If your string shouldnt be interpreted use ' instead " . It will faster. For example:

define('DB_USER', 'root');
define('DB_PASSWORD', '');
$DB_SERVER = localhost';
$DB_DATABASE = 'app-db';

Re: Unable to connect to database with PDO - PHP script

Posted: Mon May 23, 2022 3:06 am
by rubenhgua
VictorJohnsonandroni wrote:Are you sure that you DB engine is ON? And that user is without password? Maybe is root root?

And little tip : If your string shouldnt be interpreted use ' instead " . It will faster. For example:

define('DB_USER', 'root');
define('DB_PASSWORD', '');
$DB_SERVER = localhost';
$DB_DATABASE = 'app-db';

Thank you Victor for your response!

I'm quite sure that the DB is ON. Actually, the DB is on a production server that is used by multiple users.

The most annoying thing is that, when I execute the script from the nagios server console (as root or even as nagios user), the script
works fine, connects with no problem to the DB and gives the result as expected.

But when the script is executed by NAGIOS process the connection fails.
I'm wondering if it could be related to the way nagios process executes the script through CGI..or something else.

This is the part of the script that connects to the DB.. It's very simple and, as I told you, works fine if launched from console:

try {
$conn = new PDO("informix:DSN=dbcon", "app_dba_user", "app_dba_pwd");
} catch (Exception $e) {
echo "Impossible to connect with DB by PDO. " . $e->getMessage();
exit($STATUS_UNKNOWN);

}

$sql="SELECT count(USERNAME) as connections_number FROM syssessions where USERNAME='user_app';";


Any further help will be appreciated.

Thank you. Ruben.

Re: Unable to connect to database with PDO - PHP script

Posted: Fri May 27, 2022 7:15 am
by VictorJohnsonandroni
Seems pretty clear to me.

this is what your query looks like:

INSERT INTO servers (servername, mxwindow, reboot, environment, patchday, OS, GCM, InitialRemediation, time, remediation, category, updatesneeded, notes) values (:servername, :mxwindow, :reboot, :environment, :patchday, :OS, :GCM, :InitialRemediation, :time, :remediation, :category, :updatesneeded, :notes)

but using :variable is a reference.

You need to still bind values to said references.

Re: Unable to connect to database with PDO - PHP script

Posted: Fri May 27, 2022 7:51 am
by rubenhgua
VictorJohnsonandroni wrote:Seems pretty clear to me.

this is what your query looks like:

INSERT INTO servers (servername, mxwindow, reboot, environment, patchday, OS, GCM, InitialRemediation, time, remediation, category, updatesneeded, notes) values (:servername, :mxwindow, :reboot, :environment, :patchday, :OS, :GCM, :InitialRemediation, :time, :remediation, :category, :updatesneeded, :notes)

but using :variable is a reference.

You need to still bind values to said references.
Hi, Victor. Thank you, but I can't understand what you exactly mean.
The query in my php script is never executed when launched from Nagios process. It fails when trying to establish the connection to the DB.
It fails at this point -> $conn = new PDO("informix:DSN=dbcon", "app_dba_user", "app_dba_pwd");

Do you mean I should change the query? But the query (that is part of the php script) works fine if I launch it directly in the webbrowser like this
https://nagios_url/script_check_DB.php

And it also works fine if I open a linux console and execute the script like this:
php -f /usr/local/nagios/scripts/script_check_DB.php

It only fails when is executed from nagios process :cry:
It's quite strange and I don't know what o where look at.

Thank you for your help anyway !!!

Re: Unable to connect to database with PDO - PHP script

Posted: Fri May 27, 2022 12:42 pm
by rubenhgua
I'm posting some screenshots I have take to show what I mean is happening.
Hope it helps in someway :)

Re: Unable to connect to database with PDO - PHP script

Posted: Mon Jun 06, 2022 5:12 am
by BabyTeanofred
this is what your query looks like:

INSERT INTO servers (servername, mxwindow, reboot, environment, patchday, OS, GCM, InitialRemediation, time, remediation, category, updatesneeded, notes) values (:servername, :mxwindow, :reboot, :environment, :patchday, :OS, :GCM, :InitialRemediation, :time, :remediation, :category, :updatesneeded, :notes)

but using :variable is a reference.

You need to still bind values to said references.