Perl取数据的一段CGI脚本

#!/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 "http://1.1.1.1:200/cgi-bin/cluster_cgi?hostgroup=A&service=B&instance=C"`
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;


作者: jayamge   发布时间: 2010-11-02