Page 1 of 2
Empty output after SQL-connecting?
Posted: Mon Aug 04, 2014 1:38 pm
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.
Re: Empty output after SQL-connecting?
Posted: Mon Aug 04, 2014 7:00 pm
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.
Re: Empty output after SQL-connecting?
Posted: Mon Aug 04, 2014 9:57 pm
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...
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 7:29 am
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.
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 8:50 am
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'?
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 8:56 am
by eloyd
It's likely that your GUI is running as the apache user.
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 9:17 am
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?
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 9:25 am
by eloyd
Well, the GUI shouldn't be running the Nagios software, nagios should. Try this:
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 9:34 am
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
Re: Empty output after SQL-connecting?
Posted: Tue Aug 05, 2014 10:12 am
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.