Empty output after SQL-connecting?

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.
reinaldo.gomes
Posts: 59
Joined: Wed Apr 02, 2014 9:29 am

Empty output after SQL-connecting?

Post by reinaldo.gomes »

So I had to do my first plugin from scratch.
I'm basically querying a remote Windows SQL Server from my Nagios server. It works well when executed from linux CLI (./check_sql.pl), but it returns empty output while executing from Web UI. What is really bugging me is that the output fails to be printed only if it is AFTER the "connect" method. Here's the code:

Code: Select all

#!/usr/bin/perl -w
use DBI;
use Getopt::Long;

my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $data_source = "dbi:ODBC:dbxwc_app6";
my $dbh = DBI->connect($data_source, 'username', 'password') or die "Can't connect to $data_source: $DBI::errstr";
my $sql = "select client,acount,lastexecution from nagiosContaPOP";
my $sth = $dbh->prepare( $sql ) or die "Can't prepare statement: $DBI::errstr";
$sth->execute();

# Bind the results to the local variables
$sth->bind_columns(undef, \$cliente, \$acc, \$date);

# Retrieve values from the result set
while( $sth->fetch() ) {
		print("[Client:$client | Account:$acc | Since:$date] ");
}
$sth->finish();
$dbh->disconnect();
print "Testing...";
exit 0;
If there is any "print" anywhere BEFORE the DBI->connect:
- it will print at the test check
If there is any "exit" anywhere BEFORE the DBI->connect:
- the Web UI will return valid status code (ok, warn, crit)

If there is any "print" anywhere AFTER the DBI->connect:
- the test check will return empty output
If there is any "exit" anywhere AFTER the DBI->connect:
- the Web UI will return "(Return code of 255 is out of bounds)"
(^ this is exaclty what happens when you provide no exit status nor text output)

Both print and status code will always print at linux CLI, no matter where they are placed.
Last edited by reinaldo.gomes on Mon Aug 04, 2014 10:00 pm, edited 1 time in total.
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Empty output after SQL-connecting?

Post by eloyd »

Plugin development is fun. :)

Are you saying that it works when you execute "perl <my_plugin>" or are you saying it works when you execute the SQL separately?

If I were debugging this, I'd ditch the "or die" portions and run "perl -e" and step through to see what's going on.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
reinaldo.gomes
Posts: 59
Joined: Wed Apr 02, 2014 9:29 am

Re: Empty output after SQL-connecting?

Post by reinaldo.gomes »

eloyd wrote:Are you saying that it works when you execute "perl <my_plugin>" or are you saying it works when you execute the SQL separately?
Both. It works when I run "./check_sql.pl" on CLI.
But it ignores any "print" or "exit" placed after 'DBI->connect'.
How am I supposed to debug it, since this problems happens only at Web UI? :(
It took quite a while and a lot of effort to set everything up and get it running smoothly, and now it's "useless" cos it won't show any output on Nagios...
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Empty output after SQL-connecting?

Post by eloyd »

When you run it at the CLI, are you running it as root or as the nagios user? Try "su - nagios" first and see if the nagios user has similar problems running it.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
reinaldo.gomes
Posts: 59
Joined: Wed Apr 02, 2014 9:29 am

Re: Empty output after SQL-connecting?

Post by reinaldo.gomes »

Now you've shed some light on my problem.
ODBC uses a user-specific config file (~/.odbc.ini). I had it only for root, not for nagios user.

I did "su -l nagios" and then tried to execute the plugin. It obviously failed until I configured the odbc.ini file for the nagios user. However, I still got the empty output at the web UI, BUT, I realized this is not a issue with "print", "exit" or whatever. The problem is that the plugin is simply not executing past DBI->connect.

I tested this by trying an update on my remote sql server, and it didn't work when I ran it from the web UI, but it did when I ran from CLI, as nagios user. Could it be that the Web UI can't read the odbc.ini file? Is there any possibility that the Web UI is executing as anyone else other than 'nagios'?
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Empty output after SQL-connecting?

Post by eloyd »

It's likely that your GUI is running as the apache user.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
reinaldo.gomes
Posts: 59
Joined: Wed Apr 02, 2014 9:29 am

Re: Empty output after SQL-connecting?

Post by reinaldo.gomes »

Code: Select all

[root@localhost libexec]# su -l apache
This account is currently not available.
I'm looking into it. How can I change the GUI user?
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Empty output after SQL-connecting?

Post by eloyd »

Well, the GUI shouldn't be running the Nagios software, nagios should. Try this:

Code: Select all

ps -aef | grep nagios
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
reinaldo.gomes
Posts: 59
Joined: Wed Apr 02, 2014 9:29 am

Re: Empty output after SQL-connecting?

Post by reinaldo.gomes »

Code: Select all

[root@localhost /]# ps -aef | grep nagios
postgres   668  1349  0 05:06 ?        00:00:10 postgres: nagiosxi nagiosxi [local] idle
nagios    1567     1  0 Jul04 ?        00:00:00 /usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
postgres  2749  1349  0 06:27 ?        00:00:08 postgres: nagiosxi nagiosxi [local] idle
nagios    3849  1567  0 Aug04 ?        00:00:02 /usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
nagios    3850  3849  0 Aug04 ?        00:00:23 /usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
nagios    3853     1  0 Aug04 ?        00:03:53 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
nagios    4024  4023  0 11:34 ?        00:00:00 /bin/sh -c /usr/bin/php -q /usr/local/nagiosxi/cron/sysstat.php > /usr/local/nagiosxi/var/sysstat.log 2>&1
nagios    4025  4024  0 11:34 ?        00:00:00 /usr/bin/php -q /usr/local/nagiosxi/cron/sysstat.php
nagios    4026  4019  0 11:34 ?        00:00:00 /bin/sh -c /usr/bin/php -q /usr/local/nagiosxi/cron/perfdataproc.php > /usr/local/nagiosxi/var/perfdataproc.log 2>&1
nagios    4027  4022  0 11:34 ?        00:00:00 /bin/sh -c /usr/bin/php -q /usr/local/nagiosxi/cron/cmdsubsys.php > /usr/local/nagiosxi/var/cmdsubsys.log 2>&1
nagios    4029  4021  0 11:34 ?        00:00:00 /bin/sh -c /usr/bin/php -q /usr/local/nagiosxi/cron/eventman.php > /usr/local/nagiosxi/var/eventman.log 2>&1
nagios    4030  4026  0 11:34 ?        00:00:00 /usr/bin/php -q /usr/local/nagiosxi/cron/perfdataproc.php
nagios    4032  4020  0 11:34 ?        00:00:00 /bin/sh -c /usr/bin/php -q /usr/local/nagiosxi/cron/feedproc.php > /usr/local/nagiosxi/var/feedproc.log 2>&1
nagios    4034  4027  0 11:34 ?        00:00:00 /usr/bin/php -q /usr/local/nagiosxi/cron/cmdsubsys.php
nagios    4037  4032  0 11:34 ?        00:00:00 /usr/bin/php -q /usr/local/nagiosxi/cron/feedproc.php
nagios    4039  4029  1 11:34 ?        00:00:00 /usr/bin/php -q /usr/local/nagiosxi/cron/eventman.php
postgres  4044  1349  0 11:34 ?        00:00:00 postgres: nagiosxi nagiosxi [local] idle
postgres  4046  1349  0 11:34 ?        00:00:00 postgres: nagiosxi nagiosxi [local] idle
postgres  4047  1349  0 11:34 ?        00:00:00 postgres: nagiosxi nagiosxi [local] idle
postgres  4048  1349  0 11:34 ?        00:00:00 postgres: nagiosxi nagiosxi [local] idle
postgres  4062  1349  0 11:34 ?        00:00:00 postgres: nagiosxi nagiosxi [local] idle
root      4143 24924  0 11:34 pts/0    00:00:00 grep nagios
postgres  5655  1349  0 10:22 ?        00:00:01 postgres: nagiosxi nagiosxi [local] idle
postgres  5684  1349  0 10:22 ?        00:00:01 postgres: nagiosxi nagiosxi [local] idle
postgres  5687  1349  0 10:22 ?        00:00:01 postgres: nagiosxi nagiosxi [local] idle
postgres  5993  1349  0 10:23 ?        00:00:01 postgres: nagiosxi nagiosxi [local] idle
root      8783  8394  0 10:29 pts/0    00:00:00 su -l nagios
nagios    8784  8783  0 10:29 pts/0    00:00:00 -bash
postgres 15305  1349  0 09:29 ?        00:00:03 postgres: nagiosxi nagiosxi [local] idle
postgres 19462  1349  0 09:39 ?        00:00:02 postgres: nagiosxi nagiosxi [local] idle
root     21014 20654  0 10:58 pts/0    00:00:00 su -l nagios
nagios   21015 21014  0 10:58 pts/0    00:00:00 -bash
postgres 21772  1349  0 04:39 ?        00:00:11 postgres: nagiosxi nagiosxi [local] idle
root     23771 21613  0 11:04 pts/0    00:00:00 su -l nagios
nagios   23772 23771  0 11:04 pts/0    00:00:00 -bash
postgres 24416  1349  0 06:02 ?        00:00:08 postgres: nagiosxi nagiosxi [local] idle
root     24521 24455  0 11:06 pts/0    00:00:00 su -l nagios
nagios   24522 24521  0 11:06 pts/0    00:00:00 -bash
nagios   25376     1  0 Jul04 ?        00:21:16 /usr/local/nagios/bin/npcd -d -f /usr/local/nagios/etc/pnp/npcd.cfg
postgres 28783  1349  0 06:12 ?        00:00:08 postgres: nagiosxi nagiosxi [local] idle
postgres 28919  1349  0 04:56 ?        00:00:09 postgres: nagiosxi nagiosxi [local] idle
postgres 29590  1349  0 06:14 ?        00:00:08 postgres: nagiosxi nagiosxi [local] idle
postgres 29636  1349  0 06:14 ?        00:00:08 postgres: nagiosxi nagiosxi [local] idle
postgres 30096  1349  0 06:15 ?        00:00:08 postgres: nagiosxi nagiosxi [local] idle
reinaldo.gomes
Posts: 59
Joined: Wed Apr 02, 2014 9:29 am

Re: Empty output after SQL-connecting?

Post by reinaldo.gomes »

So, it turns out that even though the "Test Check Command" returns empty output, it returns valid output (both text and status) when I use the "Schedule an immediate check".
All I had to do was to set the nagios user config file.

That's not a critical matter, but it would be nice if we could solve this Test Check Command's empty output.
Locked