#!/usr/bin/perl
#######################################
## Function: check nagios status
##
## Create data: 2011.6.23
#######################################
use warnings;
use strict;
use Time::Local;
use lib qw(/opt/nagios/libexec/lib);
use db;
my ($output_noupdate,$output_down,$output_notify);
my $output;
my $return;
my $curr_time = time();
######################
## Connect database
######################
my $script_path = "/opt/nagios/libexec";
require "$script_path/etc/nagios.pm";
my $n_dbh = db_connect();
require "$script_path/etc/centreon.pm";
my $c_dbh = db_connect();
my $sql;
my @items;
##############################
## Get all active nagios
##############################
$sql = "SELECT name FROM nagios_server WHERE ns_activate=1";
@items = @{db_fetch_assoc($sql,$c_dbh)};
my %nagios;
foreach my $ref (@items)
{
$nagios{$ref->{name}} = 1;
}
###########################
$sql = "SELECT instance_name, is_currently_running, notifications_enabled, status_update_time, unix_timestamp(status_update_time) AS timestamp ".
"FROM nagios_instances ni LEFT JOIN nagios_programstatus np ON (ni.instance_id = np.instance_id)";
@items = @{db_fetch_assoc($sql,$n_dbh)};
my $cur_time = time() - 300;
foreach my $ref (@items)
{
next if (!defined($nagios{"$ref->{instance_name}"}));
next if (!defined($ref->{is_currently_running}));
if ($ref->{is_currently_running} != 1) {
$output_down .= "$ref->{instance_name} ";
}elsif ($ref->{timestamp} < time() - 300)
{
$output_noupdate .= "$ref->{instance_name}(UpdateTime: $ref->{status_update_time}) ";
}elsif ($ref->{notifications_enabled} != 1){
$output_notify .= "$ref->{instance_name} ";
}
}
## disconnect db ##
db_close($n_dbh);
if (defined($output_down) || defined($output_noupdate))
{
if (defined($output_down))
{
$output = "Critical: $output_down - Nagios is down!";
}
if (defined($output_noupdate))
{
defined($output) ? ($output .= " $output_noupdate - Nagios status is not updated!") : ($output = "Critical: $output_noupdate - Nagios status is not updated!");
}
if (defined($output_notify))
{
$output .= " $output_notify - Nagios notifications is disabled!";
}
$return = 2;
} elsif (defined($output_notify))
{
$output = "Warning: $output_notify - notifications is disabled!";
$return = 1;
}else
{
$output = "Ok!";
$return = 0;
}
print "$output\n";
exit $return;
###########################
## Sub function
###########################
sub time_switch_timestp {
my $time = shift;
my @t = split( /-|\s|:/, $time );
my $timestp = timelocal( $t[5], $t[4], $t[3], $t[2], $t[1] - 1, $t[0] - 1900 );
return $timestp;
}
#######
# ./check_nagios.pl
Ok!