Unable to connect to database with PDO - PHP script

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
rubenhgua
Posts: 4
Joined: Mon May 09, 2022 7:17 am

Unable to connect to database with PDO - PHP script

Post 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.
VictorJohnsonandroni
Posts: 10
Joined: Sun Mar 27, 2022 7:19 am

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

Post 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';
Last edited by VictorJohnsonandroni on Mon Jul 18, 2022 11:38 pm, edited 1 time in total.
rubenhgua
Posts: 4
Joined: Mon May 09, 2022 7:17 am

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

Post 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.
VictorJohnsonandroni
Posts: 10
Joined: Sun Mar 27, 2022 7:19 am

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

Post 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.
rubenhgua
Posts: 4
Joined: Mon May 09, 2022 7:17 am

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

Post 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 !!!
rubenhgua
Posts: 4
Joined: Mon May 09, 2022 7:17 am

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

Post by rubenhgua »

I'm posting some screenshots I have take to show what I mean is happening.
Hope it helps in someway :)
Attachments
Script output when executed from browser. Working.
Script output when executed from browser. Working.
Script output from nagios server linux console. Working
Script output from nagios server linux console. Working
Image2.png (6.04 KiB) Viewed 2204 times
Script output when script is executed from nagios. Not working.
Script output when script is executed from nagios. Not working.
BabyTeanofred
Posts: 7
Joined: Sun May 01, 2022 8:42 am

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

Post 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.
Locked