nagios一个接口脚本示范
时间:2008-08-16 来源:jordanfang
#!/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 "<tr><td rowspan='$rowspan'>$ip($host_name)</td><td>HOST_PING</td><td>$host_noti</td><td>NaN</td><td>NaN</td><td>$all_person</td><td>NaN</td><td>NaN</td></tr>"
}
############################## 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 "<tr><td>$services</td><td>$service_noti</td><td>${normal_check_interval}0s</td><td>${notification_interval}0s</td><td>$all_person</td><td>$check_mommand</td><td>$output</td></tr>"
}
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 "<div>wrong input,please check</div>"
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 "<div>$input is not monitored in nagios,please check</div>"
fi
fi
fi
######################## 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 "<tr><td rowspan='$rowspan'>$ip($host_name)</td><td>HOST_PING</td><td>$host_noti</td><td>NaN</td><td>NaN</td><td>$all_person</td><td>NaN</td><td>NaN</td></tr>"
}
############################## 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 "<tr><td>$services</td><td>$service_noti</td><td>${normal_check_interval}0s</td><td>${notification_interval}0s</td><td>$all_person</td><td>$check_mommand</td><td>$output</td></tr>"
}
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 "<div>wrong input,please check</div>"
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 "<div>$input is not monitored in nagios,please check</div>"
fi
fi
fi
相关阅读 更多 +