sunny03 发表于 2019-1-16 07:21:34

Nagios状态监控

  #!/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 .= &quot;$ref->{instance_name}(UpdateTime: $ref->{status_update_time}) &quot;;
    }elsif ($ref->{notifications_enabled} != 1){
            $output_notify .= &quot;$ref->{instance_name} &quot;;
    }

}

## disconnect db ##
db_close($n_dbh);


if (defined($output_down) || defined($output_noupdate))
{
    if (defined($output_down))
    {
      $output = &quot;Critical: $output_down - Nagios is down!&quot;;
    }
    if (defined($output_noupdate))
    {
      defined($output) ? ($output .= &quot; $output_noupdate - Nagios status is not updated!&quot;) : ($output = &quot;Critical: $output_noupdate - Nagios status is not updated!&quot;);
    }
    if (defined($output_notify))
    {
      $output .= &quot; $output_notify - Nagios notifications is disabled!&quot;;
    }
    $return = 2;
} elsif (defined($output_notify))
{
   $output = &quot;Warning: $output_notify - notifications is disabled!&quot;;
   $return = 1;
}else
{
    $output = &quot;Ok!&quot;;
    $return = 0;
}

print &quot;$output\n&quot;;
exit $return;

###########################
## Sub function
###########################

sub time_switch_timestp {
    my $time    = shift;
    my @t       = split( /-|\s|:/, $time );
    my $timestp = timelocal( $t, $t, $t, $t, $t - 1, $t - 1900 );
    return $timestp;
}

#######
# ./check_nagios.pl
Ok!




页: [1]
查看完整版本: Nagios状态监控