dsfsfs 发表于 2019-1-16 09:40:34

nagios之监控策略

  
3 Nagios 之监控策略



一 在Nagios配置文件templates.cfg 定义监控主机模板和监控service 模板,这样在定义监控对象时直接继承模板中的监控策略,而不需要在每定义一个监控对象时,而重新需要


定义监控策略


#vim usr/local/nagios/etc/object/templates.cfg 在其尾部添加如下内容

1 定义主机模板


define host {
#模板名称
name                            etnet
#该模板的父模板
use                           generic-host
#最大尝试次数
max_check_attempts            3
#正常检测的时间间隔
normal_check_interval         3
#当检测到监控对象不在线时,尝试检测的次数
retry_check_interval            1
#监测的时间段7X24
check_period                  24x7
#报警的时间段
notification_period             24x7
#报警的时间间隔,如果是短信报警的换,意思就是说多长时间用短信通知系统管理员一次
notification_interval         60
#当监控对象出现什么样的状态时发短信通知您
notification_options            d,r,u,f
#监测命令
check_command                   check-host-alive
#联系人
contact_groups               admins
register                        0
}
注:
Notification_options   d ,r,u,f
d 代表当监控对象出现down 是发送通知
r 当监控对象recoveries 时发送通知
f 当主机启动或者停机时发送通知
u 当有unreachable 状态时发送通知


例如
定义主机
#定义主机组
define hostgroup{
hostgroup_name       mysql
alias                   mysql-group
#该组的成员
members               linux141,linux134
}
define host {
#继承etnet模板
use   etnet
host_name    linux141
alias         mysql41
address         192.168.3.141
}

2 定义service 模板


###define service###
define service {
# 定义service的名称
name                            tomcat
use                           generic-service
max_check_attempts            2
normal_check_interval         1
retry_check_interval            1
check_period                  24x7
notification_period             24x7
notification_interval         60
notification_options            w,u,c,f
contact_groups               admins
register                        0
}
注: notification_optionsw,u,c,f
C 代表critival 状态时发送通知
同主机模板中代表的含义是相同的
例如
define hostgroup{
hostgroup_name         Memcached
alias                  Memcache
members                Memcache137
}
definehost {
use             etnet
host_name       Memcache137
alias            linux137
address         192.168.3.137
}
defineservice{
use                     tomcat
host_name               Memcache137
service_description       CPU Load
check_command       check_nrpe!check_load
}
注:一般情况先定义监控service 时,是离不开定义host的

二 为防止nagios服务器出现down ,实现的nagios的高可用性,即是nagios的失效性监控,当nagios master 出现down,nagios slave 能够主动接替nagios master


1 要在nagiosMaster 的服务器的nrpe 主配置文件定义一下,命令,以便远程能够执行进行对nagios的健康监测
=/usr/local/nagios/libexec/ check_nagios -e 5 -F /usr/local/nagios/var/status.log -C /usr/local/nagios/bin/nagios
2在nagios Slave 服务器上创建脚本如下
#cat/usr/local/nagios/libexec/check_nagios_service
#!/bin/bash
AWK=/bin/awk
CUT=/bin/cut
SED=/bin/sed
GREP=/bin/grep
##
##set monitor nagios status log
echo "`/bin/date+%Y-%m-%d-%H:%M:%S` The nagios Master statusis $STATUS">>$NAGIOS_DIR/var/monitor.log
NAGIOS=($( grep "SLAVE" /usr/local/nagios/etc/nagios.cfg| grep -v '^#' | awk -F/ '{print $NF}'))
case $STATUS in
OK)
if [ "$NAGIOS" = "SLAVE" ]
then
$SED-i '/SLAVE$/d' $NAGIOS_DIR/etc/nagios.cfg
/sbin/servicenagios restart >>/dev/null 2>&1
else
exit 0
fi
;;
CRITICAL)
if [ "$NAGIOS" != "SLAVE" ]
then
echo -e "#monitornagios service ,and take the places of nagios,SLAVE">>$NAGIOS_DIR/etc/nagios.cfg
echo "cfg_dir=/usr/local/nagios/etc/objects/SLAVE">>$NAGIOS_DIR/etc/nagios.cfg
fi
#nagios restart
/sbin/servicenagios restart>>/dev/null 2>&1
;;
refused)
exit 1
esac

3 在nagios slave 上的服务器上添加工作任务,当然这可以根据自己的要求来对nagios Master的 健康监测的时间间隔


#crontab-e
#*/1 * * * *    /usr/local/nagios/libexec/check_nagios_service






页: [1]
查看完整版本: nagios之监控策略