nagios监控log文件的方法举例
时间:2014-10-26 01:31 来源:linux.it.net.cn 作者:it
nagios监控log文件的shell脚本代码。
首先,要实现一个插件脚本:(file.sh)
说明:返回值。0为正常、1为警告、2为紧急。格式:file.sh –w 2 –c 3
使用:
将该脚本保存至/usr/local/nagios/libexec目录下,此时file.sh成为一个可调用的”命令”。
命令引用方式:
首先:需要在commands.cfg里面定义该命令的引用格式。
本例中的引用如下:
复制代码代码示例:
# 'check_monitor_file' command definition for wanggy 2013-03-13
define command{
command_name check_file
command_line $USER1$/check_monitor_file.sh -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}
其次:需要添加一监控对像:
复制代码代码示例:
define service{
host_name 192.168.1.9
service_description monitor_line_file
check_command check_file!1!1
max_check_attempts 5
normal_check_interval 4
retry_check_interval 1
check_period 24x7
notification_interval 4
notification_period 24x7
notification_options w,u,c,r
contact_groups admins
}
最后效果:

附,监控log文件的插件脚本:
复制代码代码示例:
#!/bin/bash
#**************************************#
# date 2013-03-13 15:03 #
#采用shell 插件监控文件内容 #
#格式必须符合USAGE定义 #
#note: # www.jbxue.com
#monitor.cfg为监控原文件 #
#$err_count判断异常个数(=1) #
#本例监控名为monitor_line_file在.9上 #
#对应command查看commands.cfg配置 #
#**************************************#
cd /usr/local/nagios/libexec
USAGE="`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>"
warning=""
critical=""
#STATE_OK=0
#STATE_WARNING=1
#STATE_CRITICAL=2
# read input
if [[ $# -lt 4 ]]
then
echo ""
echo "Wrong Syntax: `basename $0` $*"
echo ""
echo "Usage: $USAGE"
echo ""
exit 0
fi
while [[ $# -gt 0 ]]
do
case "$1" in
-w|--warning)
shift
warning=$1
;;
-c|--critical)
shift
critical=$1
;;
esac
shift
done
#echo $warning
#echo $critical
grep "=1" monitor.cfg >err_count_file
err_count=`wc -l err_count_file |awk '{print$1}'`
#本例主判断critical状态跟ok状态
if [ $err_count -ge $critical ];then
err_monitor=`cat err_count_file`
echo -n "告警!!!!共有:$err_count条链路出现异常.请确认!!!!"
echo $err_monitor
exit 2
else
list_monitor=`cat monitor.cfg`
echo -n "全部链路正常!!"
#在nagios显示内容信息
echo $list_monitor
exit 0
fi
(责任编辑:IT)
nagios监控log文件的shell脚本代码。
首先,要实现一个插件脚本:(file.sh)
使用:
命令引用方式:
本例中的引用如下:
复制代码代码示例:
# 'check_monitor_file' command definition for wanggy 2013-03-13
define command{ command_name check_file command_line $USER1$/check_monitor_file.sh -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ }
其次:需要添加一监控对像:
复制代码代码示例:
define service{
host_name 192.168.1.9 service_description monitor_line_file check_command check_file!1!1 max_check_attempts 5 normal_check_interval 4 retry_check_interval 1 check_period 24x7 notification_interval 4 notification_period 24x7 notification_options w,u,c,r contact_groups admins }
最后效果:
附,监控log文件的插件脚本:
复制代码代码示例:
#!/bin/bash
#**************************************# # date 2013-03-13 15:03 # #采用shell 插件监控文件内容 # #格式必须符合USAGE定义 # #note: # www.jbxue.com #monitor.cfg为监控原文件 # #$err_count判断异常个数(=1) # #本例监控名为monitor_line_file在.9上 # #对应command查看commands.cfg配置 # #**************************************# cd /usr/local/nagios/libexec USAGE="`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>" warning="" critical="" #STATE_OK=0 #STATE_WARNING=1 #STATE_CRITICAL=2 # read input if [[ $# -lt 4 ]] then echo "" echo "Wrong Syntax: `basename $0` $*" echo "" echo "Usage: $USAGE" echo "" exit 0 fi while [[ $# -gt 0 ]] do case "$1" in -w|--warning) shift warning=$1 ;; -c|--critical) shift critical=$1 ;; esac shift done #echo $warning #echo $critical grep "=1" monitor.cfg >err_count_file err_count=`wc -l err_count_file |awk '{print$1}'` #本例主判断critical状态跟ok状态 if [ $err_count -ge $critical ];then err_monitor=`cat err_count_file` echo -n "告警!!!!共有:$err_count条链路出现异常.请确认!!!!" echo $err_monitor exit 2 else list_monitor=`cat monitor.cfg` echo -n "全部链路正常!!" #在nagios显示内容信息 echo $list_monitor exit 0 fi |