JSON output to Nagios

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
hodgewil
Posts: 5
Joined: Mon May 07, 2012 12:08 pm

JSON output to Nagios

Post 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);
        }
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: JSON output to Nagios

Post 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.
hodgewil
Posts: 5
Joined: Mon May 07, 2012 12:08 pm

Re: JSON output to Nagios

Post 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....
User avatar
jsmurphy
Posts: 989
Joined: Wed Aug 18, 2010 9:46 pm

Re: JSON output to Nagios

Post by jsmurphy »

What happens if you run it from the command line? It sounds like the script isn't finishing execution properly.
hodgewil
Posts: 5
Joined: Mon May 07, 2012 12:08 pm

Re: JSON output to Nagios

Post 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.
mguthrie
Posts: 4380
Joined: Mon Jun 14, 2010 10:21 am

Re: JSON output to Nagios

Post 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
hodgewil
Posts: 5
Joined: Mon May 07, 2012 12:08 pm

Re: JSON output to Nagios

Post 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.
User avatar
jsmurphy
Posts: 989
Joined: Wed Aug 18, 2010 9:46 pm

Re: JSON output to Nagios

Post 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.
hodgewil
Posts: 5
Joined: Mon May 07, 2012 12:08 pm

Re: JSON output to Nagios

Post 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.
User avatar
jsmurphy
Posts: 989
Joined: Wed Aug 18, 2010 9:46 pm

Re: JSON output to Nagios

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