Chinaunix首页 | 论坛 | 博客
  • 博客访问: 194318
  • 博文数量: 44
  • 博客积分: 2021
  • 博客等级: 大尉
  • 技术积分: 937
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-03 22:55
文章分类

全部博文(44)

文章存档

2009年(2)

2008年(42)

我的朋友

分类:

2008-08-16 14:36:38

#!/bin/sh

########################              config file                  ####################

HOST_FILE=/tmp/object_host_tmp_file
OBJECTS_MID_FILE=/tmp/objects_filtered_tmp
STATUS_MID_FILE=/tmp/status_filtered_tmp
STATUS_FILE=/usr/local/nagios/var/status.dat
CACHE_FILE=/usr/local/nagios/var/objects.cache
DOWN_TIME_FILE=/usr/local/nagios/var/downtime.dat

##############################  check host's contact_person  ##########################

check_host() {
        host_contact_groups=`grep -A26 "define host {" $CACHE_FILE | grep -A25 $host_name$ | grep contact_groups | awk '{print $2}'`
        all_person=""
        rowspan=$((`cat $OBJECTS_MID_FILE | grep service_description | wc -l` + 1))
        echo $host_contact_groups | grep -q ","
        if [ $? -eq 0 ]
        then
                for group in `echo $contact_groups |sed -e 's/,/ /g'`
                do
                        persons=`cat $CACHE_FILE | grep -A4 "define contactgroup {" |grep -A3 $group | grep members | awk '{print $2}'`
                        all_person="$persons $all_person"
                done
        else
                all_person=`grep -A4 "define contactgroup {" $CACHE_FILE |grep -A3 $host_contact_groups | grep members | awk '{print $2}'`
        fi
        echo -e "$ip($host_name)HOST_PING$host_notiNaNNaN$all_personNaNNaN"
}

##############################   check service details   ###############################

check_service() {
        contact_groups=`grep -A1 "\<$services\>" $OBJECTS_MID_FILE | grep "contact_groups" |awk '{print $2}'`
        normal_check_interval=`grep -A2 "\<$services\>" $OBJECTS_MID_FILE | grep "normal_check_interval" |awk '{print $2}'`
        notification_interval=`grep -A3 "\<$services\>" $OBJECTS_MID_FILE | grep "notification_interval" |awk '{print $2}'`
        check_mommand=`grep -A1 =$services$ $STATUS_MID_FILE | grep "check_command" | awk -F "check_command=" '{print $2}'`
        output=`grep -A2 =$services$ $STATUS_MID_FILE | grep "plugin_output=" |awk -F= '{print $2}'`
        all_person=
        echo $contact_groups | grep -q ","
        if [ $? -eq 0 ]
        then
                for group in `echo $contact_groups |sed -e 's/,/ /g'`
                do
                                    
                for group in `echo $contact_groups |sed -e 's/,/ /g'`
                do
                        persons=`cat $CACHE_FILE | grep -A4 "define contactgroup {" |grep -A3 $group | grep members | awk '{print $2}'`
                        all_person="$persons $all_person"
                done
        else
                all_person=`cat $CACHE_FILE | grep -A4 "define contactgroup {" |grep -A3 $contact_groups | grep members | awk '{print $2}'`
        fi
        echo -e "$services$service_noti${normal_check_interval}0s${notification_interval}0s$all_person$check_mommand$output"
}

check_main() {
                for host_name in `echo $host_name`
                do
                grep -A42 "$host_name$" $STATUS_FILE |egrep "$host_name|notifications_enabled|service_description|plugin_output|scheduled_downtime_depth|check_command" >$STATUS_MID_FILE
                grep -A29 "define service {" $CACHE_FILE |grep -A28 "$host_name$" |egrep "service_description|contact_groups|normal_check_interval|notification_interval" >$OBJECTS_MID_FILE
                ip=`grep -A4 $host_name$ $CACHE_FILE | grep address | awk '{print $2}'`
########################       check the host downtime or not       ####################

                downtime_flag=`cat $STATUS_MID_FILE | head -5 | grep -q "scheduled_downtime_depth=1"`
                if [ $? -eq 0 ]
                then
                        start_time=`grep -A8 "hostdowntime {" $DOWN_TIME_FILE | grep -A7 $host_name | grep start_time | awk -F "start_time=" '{print $2}'`
                        real_start_time=`date -d "1970-01-01 UTC $start_time seconds" "+%F %R:%S"`
                        end_time=`grep -A8 "hostdowntime {" $DOWN_TIME_FILE | grep -A7 $host_name | grep end_time | awk -F "end_time=" '{print $2}'`
                        real_end_time=`date -d "1970-01-01 UTC $end_time seconds" "+%F %R:%S"`
                        host_noti="downtime(${real_start_time}-${real_end_time})"
                        check_host
                        for services in `cat $OBJECTS_MID_FILE | grep service_description | awk '{print $2}'`
                        do
                                service_noti="downtime(${real_start_time}-${real_end_time})"
                                check_service
                        done
                else

########################     check the host notification or not     ####################

                        cat $STATUS_MID_FILE | head -4 | grep -q "notifications_enabled=1"
                        if [ $? -eq 0 ]
                        then
                                host_noti=enabled
                                                    
                        then
                                host_noti=enabled
                                check_host
                        else
                                host_noti=disabled
                                check_host
                        fi
               
########################    check the service notification or not    ###################
       
                        for services in `cat $OBJECTS_MID_FILE | grep service_description | awk '{print $2}'`
                        do
                                downtime_flag=`grep -A4 =$services$ $STATUS_MID_FILE | grep -q "scheduled_downtime_depth=1"`
                                if [ $? -eq 0 ]
                                then
                                        start_time=`grep -A8 "servicedowntime {" $DOWN_TIME_FILE | grep -A7 $host_name |grep -A6 $services | grep start_time= | awk -F "start_time=" '{print $2}'`
                                        real_start_time=`date -d "1970-01-01 UTC $start_time seconds" "+%F %R:%S"`
                                        end_time=`grep -A8 "servicedowntime {" $DOWN_TIME_FILE | grep -A7 $host_name |grep -A6 $services | grep end_time= | awk -F "end_time=" '{print $2}'`   
                                        real_end_time=`date -d "1970-01-01 UTC $end_time seconds" "+%F %R:%S"`
                                        service_noti="downtime(${real_start_time}-${real_end_time})"
                                        check_service
                                else
                                        grep -A3 =$services$ $STATUS_MID_FILE | grep -q "notifications_enabled=1"
                                        if [ $? -eq 0 ]
                                        then
                                                service_noti=enabled
                                                check_service
                                        else   
                                                service_noti=disabled
                                                check_service
                                        fi
                                fi
                        done
                fi     
done                   
}                              
#######################################################################################
grep -A6 "define host {" $CACHE_FILE >$HOST_FILE
input=$1       
if [ -z $input ]
then
        echo "
wrong input,please check
"
        exit 1         
fi                     
if echo $input |grep -q "^[0-9\.]"
then                           
if echo $input |grep -q "^[0-9\.]"
then
        if grep -q "$input$" $CACHE_FILE
        then
                host_name=`grep -B4 "\<$input\>" $CACHE_FILE | grep host_name | awk '{print $2}'`
                check_main
        fi
else
        if grep -q "\<$input\>" $HOST_FILE
        then
                host_name=$input
                check_main
        else
                if grep -q "\<$input\>" $CACHE_FILE
                then
                        host_name=`grep -A4 "\<$input\>" $CACHE_FILE | grep members | awk '{print $2}' | sed -e 's/,/ /g'`
                        check_main
                else
                        echo -e "
$input is not monitored in nagios,please check
"
                fi
        fi
fi
阅读(1827) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~