Page 1 of 1

JSON output to Nagios

Posted: Mon May 07, 2012 12:18 pm
by hodgewil
Hello, wondering if anyone might be able to help me out... I'm new to Nagios and received a JSON perl (custom) perl script from a developer... only problem is it doesn't seem to output any useful data to Nagios... I receive "(Service check did not exit properly)" ... I'm not sure that the script is correctly formated for proper output... but very lost :? ... here's a copy of the code if anyone can take a peek and advise... Thank you...

Code: Select all

#!/usr/bin/perl

use LWP::UserAgent;
use JSON;
use Time::Format qw(%time %strftime %manip);

$e = 0;
$s = 1;

        my $start=  $time{'yyyy/mm/dd hh:mm:ss', time-$s*60*60};
        my $end  = $time{'yyyy/mm/dd hh:mm:ss', time-$e*60*60};

        my $param = "{\"params\":{\"msg\":\"values\"
                                        ,\"id1\":0
                                        ,\"id2\":0
                                        ,\"size\":100
                                        ,\"options\":\"sort-descending\"
                                        ,\"fieldName\":\"did\"
                                        ,\"where\":\"time=".$start ."-" . $end . "\"}}'";

        $urlget = "http://10.1.1.1:12345/sdk?&force-content-type=application/json&expiry=0";
        my $ua = LWP::UserAgent->new;
        my $json = JSON->new;
        my $basic = "10.1.1.1:12345";
        $ua->agent('bot1.0');
        $ua->credentials($basic,"exampletechnology","username" => "password");
        my $request = $ua->post($urlget, Content_Type => "application/json", Content => $param);
        $test = $json->decode($request->content);
        my $count = 0;
        my @urls;
        my %balls;
        my $state = 0;
        foreach (@{$test->{"results"}->{"fields"}})
        {
                push(@urls, $_->{value});
                $count++;
                $balls{$_->{group}}->{ $_->{type}}  = $_->{value}  ;
        }
        #foreach $line (@urls) {
        #       print "$line \n";
        #                       }
        #print "$count\n";

        if ($count == 13) {
        print "OK\n";
        exit (0);
        } else {
        print "CRITICAL\n";
        exit (2);
        }

Re: JSON output to Nagios

Posted: Mon May 07, 2012 1:24 pm
by agriffin
My guess is that you have embedded perl enabled in nagios and this script does not obey the restrictions that entails. You should either disable embedded perl (set enable_embedded_perl=0 in nagios.cfg) or rewrite the script to follow these guidelines.

Re: JSON output to Nagios

Posted: Mon May 07, 2012 3:34 pm
by hodgewil
Thank you... I turned off embedded perl support and still "no go" on the script...so I guess it's back to the drawing board....

Re: JSON output to Nagios

Posted: Mon May 07, 2012 5:50 pm
by jsmurphy
What happens if you run it from the command line? It sounds like the script isn't finishing execution properly.

Re: JSON output to Nagios

Posted: Tue May 08, 2012 9:47 am
by hodgewil
it outputs successfully with a "Critical" or "OK"... but somewhere along the lines it seems as though Nagios doesn't acknowledge the output.

Re: JSON output to Nagios

Posted: Tue May 08, 2012 9:56 am
by mguthrie
After running the script from the command line, run:

Code: Select all

echo $?

To see if it exits with the proper exit code (Should be 0-3).
0 - ok
1- warning
2- critical
3- unknown

Re: JSON output to Nagios

Posted: Tue May 08, 2012 12:57 pm
by hodgewil
the script outputs "CRITICAL"... and displayed the number 2... which seems to match with the exit codes listed. Maybe something in the command structure is causing an issue... I left it pretty basic just for testing...


define command{
command_name SYSTEMX12_status
command_line $USER1$/nwtest.pl
}


Thanks for the help guys... really appreciate it.

Re: JSON output to Nagios

Posted: Tue May 08, 2012 7:21 pm
by jsmurphy
I can honestly say I've never encountered a problem like this before, the command definition looks alright as well. One last ditch effort might be to see if there's an issue running that script under the nagios user context, su - nagios then run the script and echo $? as mguthrie suggested and see if it fails then... I'm really hoping it spews out an error of some description. I think that's the first time I've ever hoped for an execution failure.

Re: JSON output to Nagios

Posted: Wed May 09, 2012 10:24 am
by hodgewil
no failure... seems to work considering the same output was successful. oh well, guess I'll need to find another JSON script... maybe something from the Nagios plugin archive...

I'll keep working at it... Thanks for the input.

Re: JSON output to Nagios

Posted: Wed May 09, 2012 6:55 pm
by jsmurphy
Sorry I can't help any further. I'm not sure how exactly you could go about debugging why it's only failing with Nagios exectuion... especially considering you have disabled the internal perl handler; it just doesn't make any sense. :?