status.cgi using postgres query

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
linuxnag
Posts: 53
Joined: Tue Nov 12, 2013 4:05 pm

status.cgi using postgres query

Post by linuxnag »

Hello,

Once again apologies if this has been asked before.

I like using "/usr/local/nagios/sbin/status.cgi".
specifically, I do:

Code: Select all

export REQUEST_METHOD="GET"
export QUERY_STRING='hostgroup=all&style=detail&servicestatustypes=16&sorttype=1&sortoption=6&limit=0&limit=0'
export REMOTE_USER=nagiosadmin
/usr/local/nagios/sbin/status.cgi
And I get a very nice list of the current issues.
Is there a way to get this by querying the postgres database?
I am scraping the output of status.cgi but that's not very nice.
I need to do this in order to create a custom screen for our ops.

Your help is greatly appreciated.

George Hrysanthopoulos
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: status.cgi using postgres query

Post by sreinhardt »

Nope, the postgres db only handles XI specific settings. However you can query the nagios mysql database, and request a backend url if you wish. abrist will post with the exact link!
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: status.cgi using postgres query

Post by abrist »

You can also get the information from the backend api in XML. The following link will output XML showing a brief (brevity=3) overview of the all services (getservicestatus) where their current state is not equal to 0/OK (current_state=ne:0). Essentially showing you all the current service issues.

Code: Select all

http://<xi server ip>/nagiosxi/backend/?cmd=getservicestatus&brevity=3&current_state=ne:0
See:
http://www.slideshare.net/nagiosinc/eri ... and-nagios
http://assets.nagios.com/downloads/nagi ... nd_API.pdf
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
linuxnag
Posts: 53
Joined: Tue Nov 12, 2013 4:05 pm

Re: status.cgi using postgres query

Post by linuxnag »

You guys rock!

http://<xi server ip>/nagiosxi/backend/?cmd=getservicestatus&brevity=3&current_state=ne:0
This I can process from perl. Perfect!

I'll update this ticket if I find any other issues.
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: status.cgi using postgres query

Post by slansing »

Awesome! Let us know.
linuxnag
Posts: 53
Joined: Tue Nov 12, 2013 4:05 pm

Re: status.cgi using postgres query

Post by linuxnag »

Here is a simple parser, written in perl, for anyone who just wants to get started.
It creates 2 arrays. One for critical messages and one for warning messages.
This is specific to my needs since I need to create some custom
screens for our ops.
Code is Free for all to use.

Code: Select all

 01 #!/usr/bin/perl
 02 #
 03 use warnings;
 04 use strict;
 05 use XML::Parser;
 06 use LWP::Simple;  # Used to fetch the URL
 07 use Data::Dumper;
 08 use DateTime::Format::Strptime;
 09 #
 10 use vars qw (@critical_list @warning_list $critical_count $warning_count $TCount);
 11 use vars qw ($Nhost_name $Nname $Nstatus_update_time $Nstatus_text $Ncurrent_state $NContext);
 12 use vars qw ($NContextServicestatus $DEBUG);
 13 #
 14 $critical_count=0; $warning_count=0; $TCount=0;
 15 $Ncurrent_state=0;
 16 $NContext="";
 17 $DEBUG=0;
 18 $NContextServicestatus=0;
 19 # Put your custom ticket number here, in place of XXXX
 20 my $XURL = 'http://localhost/nagiosxi/backend/?cmd=getservicestatus&username=reports&ticket=XXXX&brevity=3&current_state=ne:0';
 21 my $cb_ticker = get($XURL);
 22 #
 23 # We should really check if it succeeded or not
 24 if( (not defined $cb_ticker) || $cb_ticker =~ /^ERROR: NOT AUTHENTICATED/ ) {
 25     if( defined $cb_ticker ) {
 26         print "$cb_ticker\n";
 27     } else {
 28         print "ERROR: Cannot get the URL: \"$XURL\"\n";
 29     }
 30     exit 1;
 31 }
 32
 33 my $parser = new XML::Parser ( Handlers => {   # Creates our parser object
 34         Start   => \&hdl_start,
 35         End     => \&hdl_end,
 36         Char    => \&hdl_char,
 37         Default => \&hdl_def,
 38     });
 39 $parser->parse($cb_ticker);
 40 #
 41 # Print stats
 42 $TCount=$critical_count+$warning_count;
 43 print "Total Incident count = $TCount\n";
 44 exit 0;
 45 #
 46 # _list[0]="host_name"
 47 # _list[1]="name"                       # Service name
 48 # _list[2]="status_update_time"         # Human readable
 49 # _list[3]="status_update_time"         # UNIX
 50 # _list[4]="status_text"                # The actual error message
 51 # _list[5]="current_state"              # The Nagios state
 52 #
 53 # The Handlers
 54 #
 55 # Start of XML tag
 56 sub hdl_start{
 57     my ($p, $elt, %atts) = @_;
 58     print "> $elt\n" if $DEBUG;
 59     $NContext=$elt;
 60     if( $elt =~ /^servicestatus$/i ) {
 61         $NContextServicestatus=1;       # Ok, we are inside "servicestatus"
 62     }
 63 }
 64 #
 65 # The end of the tag
 66 sub hdl_end{
 67     my ($p, $elt) = @_;
 68     print "< $elt\n" if $DEBUG;
 69     if( $elt =~ /^servicestatus$/i ) {
 70         &format_message;
 71         # Reset all variables (just to be safe)
 72         $NContext="";
 73         $Nhost_name="";
 74         $Nname="";
 75         $Nstatus_update_time="";
 76         $Nstatus_text="";
 77         $Ncurrent_state=0;
 78         $NContextServicestatus=0;       # We are out of "servicestatus"
 79     }
 80 }
 81 #
 82 # Tag data (if any)
 83 sub hdl_char {
 84     my ($p, $elt) = @_;
 85     if( $elt !~ /^\s*$/ ) {
 86         print "\t<DATA> $elt\n" if $DEBUG;
 87     } else {
 88         return;
 89     }
 90     #
 91     if( $NContextServicestatus ) {      # If we are in "servicestatus"
 92         if( $NContext =~ /host_name/ ) {
 93             $Nhost_name = $elt;
 94         } elsif( $NContext =~ /^name/ ) {
 95             $Nname = $elt;
 96         } elsif( $NContext =~ /^status_update_time/ ) {
 97             $Nstatus_update_time = $elt;
 98         } elsif( $NContext =~ /^status_text/ ) {
 99             $Nstatus_text = $elt;
100         } elsif( $NContext =~ /^current_state/ ) {
101             $Ncurrent_state = $elt;
102         }
103     }
104 }
105 #
106 # The initial definition line comes here
107 # <?xml version="1.0" encoding="utf-8"?>
108 #
109 sub hdl_def {
110     #
111     # We just throw out everything else
112     my ($p, $elt) = @_;
113 }
114 sub format_message {                # Helper sub to nicely format what we got from the XML
115     my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d%t%T' );
116     my $dt;
117     print "$Ncurrent_state -- $Nhost_name -- $Nname -- $Nstatus_update_time -- $Nstatus_text\n" if $DEBUG;
118     #
119     if( ($Ncurrent_state == 2) || ($Ncurrent_state == 3) ) {
120         $critical_list[$critical_count][0]=$Nhost_name;
121         $critical_list[$critical_count][1]=$Nname;
122         $critical_list[$critical_count][2]=$Nstatus_update_time;
123         $dt=$parser->parse_datetime($Nstatus_update_time);
124         $critical_list[$critical_count][3]=$dt->epoch;
125         $critical_list[$critical_count][4]=$Nstatus_text;
126         $critical_count++;
127     } elsif( $Ncurrent_state == 1 ) {
128         $warning_list[$warning_count][0]=$Nhost_name;
129         $warning_list[$warning_count][1]=$Nname;
130         $warning_list[$warning_count][2]=$Nstatus_update_time;
131         $dt=$parser->parse_datetime($Nstatus_update_time);
132         $warning_list[$warning_count][3]=$dt->epoch;
133         $warning_list[$warning_count][4]=$Nstatus_text;
134         $warning_count++;
135     }
136 }
slansing
Posts: 7698
Joined: Mon Apr 23, 2012 4:28 pm
Location: Travelling through time and space...

Re: status.cgi using postgres query

Post by slansing »

Neat script! You might want to upload it to:

exchange.nagios.com

If you want to give it some more light.
linuxnag
Posts: 53
Joined: Tue Nov 12, 2013 4:05 pm

Re: status.cgi using postgres query

Post by linuxnag »

Ok, that sounds good.
In the meantime we can close this ticket.
Locked