z

Commercial Support Clients: Clients with support contracts can get escalated support assistance by visiting Nagios Answer Hub. These forums are for community support services. Although we at Nagios try our best to help out on the forums here, we always give priority support to our support clients.

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.

Unable to connect to database with PDO - PHP script

Postby rubenhgua » Mon May 09, 2022 7:45 am

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

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

Postby VictorJohnsonandroni » Fri May 20, 2022 12:57 am

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 = echatrandom localhost';
$DB_echatspin DATABASE = 'app-db';
Last edited by VictorJohnsonandroni on Mon Jul 18, 2022 11:38 pm, edited 1 time in total.
VictorJohnsonandroni
 
Posts: 9
Joined: Sun Mar 27, 2022 7:19 am

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

Postby rubenhgua » Mon May 23, 2022 3:06 am

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

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

Postby VictorJohnsonandroni » Fri May 27, 2022 7:15 am

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

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

Postby rubenhgua » Fri May 27, 2022 7:51 am

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

Postby rubenhgua » Fri May 27, 2022 12:42 pm

I'm posting some screenshots I have take to show what I mean is happening.
Hope it helps in someway :)
Attachments
Image3.png
Script output when executed from browser. Working.
Image2.png
Script output from nagios server linux console. Working
Image2.png (6.04 KiB) Viewed 866 times
Image1.png
Script output when script is executed from nagios. Not working.
rubenhgua
 
Posts: 4
Joined: Mon May 09, 2022 7:17 am

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

Postby BabyTeanofred » Mon Jun 06, 2022 5:12 am

this is what your query looks like:

INSERT INTO servers (servername, mxwindow, reboot, environment, patchday, OS, GCM, InitialRemediation, time, remediation, category, updatesneeded, notes) values omegle.2yu.co (: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 omeglz echat references.
BabyTeanofred
 
Posts: 7
Joined: Sun May 01, 2022 8:42 am


Return to Open Source Nagios Projects

Who is online

Users browsing this forum: vlada1312@gmail.com and 36 guests