Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50130
  • 博文数量: 22
  • 博客积分: 941
  • 博客等级: 准尉
  • 技术积分: 242
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-27 13:20
文章分类
文章存档

2012年(3)

2011年(7)

2010年(12)

我的朋友

分类: Python/Ruby

2010-11-02 19:37:35

#!/usr/bin/perl
## ======================================
## cluster_cgi Used to check cluster
## Created by yangyi:2010-07-29
## Last modified by yangyi:2010-09-21
## ======================================
use strict;
use warnings;
use lib qw(./lib);
use DBI;
use DBD::mysql;
use dbconnect;
use CGI qw(:standard);

##=========================================================================================
## You can visit this CGI program by
## `curl ""`
print "Content-Type: text/plain\n\n";

my (@ret_state,@ret_attempt,@phosts);
my ($ok,$warn,$crit) = (0,0,0);
my ($i,$output,$index);

my $hostgroup = param("hostgroup");
my $service = param("service");
my $nagios_instance = param("instance");

## Connect To Database.
my ($sth,$sql);
$sql = " select current_state,current_check_attempt
        from
        nagios_servicestatus ss,
        nagios_services ns,
        nagios_hostgroups hg,
        nagios_hostgroup_members hm,
        nagios_instances ni,
        nagios_objects no
        where
        ni.instance_name = '$nagios_instance'
        and no.objecttype_id = 3
        and no.name1 = '$hostgroup'
        and no.object_id = hg.hostgroup_object_id
        and hg.instance_id = ni.instance_id
        and hg.hostgroup_id = hm.hostgroup_id
        and hm.host_object_id = ns.host_object_id
        and ns.display_name = '$service'
        and ns.service_object_id = ss.service_object_id
        and ss.notifications_enabled = 1"
;
my $dbh = dbconnect::dbconn();
$sth = $dbh->prepare($sql);
$sth -> execute();
my $numFields = $sth->rows;

# If returns are NULL
if ( $numFields == 0){
        print " Get nothing from the Centreon DB!Check Again!\n";
        exit 0;
}

## Get current ok、warn、crit states nums
while ( my $ref = $sth->fetchrow_hashref()){
        my $current_state = $ref->{current_state};
        my $current_attempt = $ref->{current_check_attempt};
        if ($current_state == 1 && $current_attempt >=1){
                $warn += 1;
        }
        elsif($current_state == 2 && $current_attempt >=1){
                $crit +=1;
        }
        else{}
}
$ok = $numFields - $warn - $crit;

if ( ($warn >0) || ( $crit >0)){
        my $sql_set_names = "set names gbk";
        $sth = $dbh->prepare($sql_set_names);
        $sth -> execute();
        my $sql_output = "
        select output,nh.display_name
        from
        nagios_servicestatus ss,
        nagios_services ns,
        nagios_hostgroups hg,
        nagios_hostgroup_members hm,
        nagios_instances ni,
        nagios_objects no,
        nagios_hosts nh
        where
        ni.instance_name = '$nagios_instance'
        and no.objecttype_id = 3
        and no.name1 = '$hostgroup'
        and no.object_id = hg.hostgroup_object_id
        and hg.instance_id = ni.instance_id
        and hg.hostgroup_id = hm.hostgroup_id
        and hm.host_object_id = ns.host_object_id
        and nh.host_object_id = hm.host_object_id
        and ns.display_name = '$service'
        and ns.service_object_id = ss.service_object_id
        and ss.notifications_enabled = 1
        and ss.current_state != 0
        and ss.current_check_attempt >=1 order by display_name"
;
        $sth = $dbh->prepare($sql_output);
        $sth -> execute();
        while ( my $ref = $sth->fetchrow_hashref()){
                my $display_name = $ref->{display_name};
                if ( $i != 1 ){
                        $output = $ref->{output};
                }
                push(@phosts,$display_name);
                $i = 1;
        }
}
undef $sth;
$dbh->disconnect();

## ====================================
## This CGI program print like:
## ok:30
## warning:2
## critical:0
## output:CRITICAL - Socket timeout after 10 seconds
## host:cache11.cm3 cache2.cm3
## ====================================
print "ok:$ok\nwarning:$warn\ncritical:$crit\nOutput:$output\nCriticalhost:@phosts.\n";
exit 0;


阅读(836) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-03 16:19:37

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com