资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

nagios事件处理机制-创新互联

接到zz的任务,实现自动化处理nagios某项报警

脑海里有个印象,这个功能之前线下做过实验

一、首先必须查看下nagios的官方文档,确认可行,以下是笔者整理的一些自认为有用的信息

1)
了解命令的定义方法
Writing Event Handler Commands
Event handler commands will likely be shell or perl scripts, but they can be any type of executable that can run from a
command prompt. At a minimum, the scripts should take the following macros as arguments:
For Services: $SERVICESTATE$, $SERVICESTATETYPE$, $SERVICEATTEMPT$
For Hosts: $HOSTSTATE$, $HOSTSTATETYPE$, $HOSTATTEMPT$
这段说的是,针对于主机处理需要的一些参数,跟针对于服务需要的一些参数,这方面配置是在objects/commands.cfg配置的
官方文档记录
define command{
command_name restart-httpd
command_line /usr/local/nagios/libexec/eventhandlers/restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}

2)
了解主机配置文件的方法(笔者线上host和service合并的一个文件,一般是分开的,主host跟service是分开的)
官方文档记录:
define service{
host_name somehost
service_description HTTP
max_check_attempts 4
event_handler restart-httpd
...
}

二、一些解释:

1)
变量解释:
$SERVICESTATE$:服务的当前状态(OK、WARNING、UNKNOWN、CRITICAL)
$SERVICESTATETYPE$:服务器状态类型,分为两种,软状态,硬状态
$SERVICEATTEMPT$:软状态的尝试check的次数
这些值是后面自动恢复脚本必须要处理的三个参数

2)
HARD:硬状态
SOFT:软状态
nagios在检测服务正常的过程中,如果第一次检测失败,状态成SOFT 尝试设置的大次数后,状态就改变成为HARD

3)
事件处理的一些参数配置
event_handler_timeout=30 超时时间
enable_event_handlers=1  开机事件处理机制
event_handler

三、操作步骤:
1)
确认事件处理开关有没有打开
enable_event_handlers=1
0:关闭
1:打开

2)
自恢复脚本制作
需要处理的参数太多,建议使用case,下面是官网的脚本例子,当然可以写成其他的
#!/bin/sh
#
# Event handler script for restarting the web server on the local machine
#
# Note: This script will only restart the web server if the service is
# retried 3 times (in a "soft" state) or if the web service somehow
# manages to fall into a "hard" error state.
#
# What state is the HTTP service in?
case "$1" in
OK)
# The service just came back up, so don't do anything...
;;
WARNING)
# We don't really care about warning states, since the service is probably still running...
;;
UNKNOWN)
# We don't know what might be causing an unknown error, so don't do anything...
;;
CRITICAL)
# Aha! The HTTP service appears to have a problem - perhaps we should restart the server...
# Is this a "soft" or a "hard" state?
case "$2" in
# We're in a "soft" state, meaning that Nagios is in the middle of retrying the
# check before it turns into a "hard" state and contacts get notified...
SOFT)
# What check attempt are we on? We don't want to restart the web server on the first
# check, because it may just be a fluke!
case "$3" in
# Wait until the check has been tried 3 times before restarting the web server.
# If the check fails on the 4th time (after we restart the web server), the state
# type will turn to "hard" and contacts will be notified of the problem.
# Hopefully this will restart the web server successfully, so the 4th check will
# result in a "soft" recovery. If that happens no one gets notified because we
# fixed the problem!
3)
echo -n "Restarting HTTP service (3rd soft critical state)..."
# Call the init script to restart the HTTPD server
/etc/rc.d/init.d/httpd restart
;;
esac
;;
#

3)报警产生之后,如何当nagios服务器得到下面传上来的报警呢,做出自维护呢(objects/commands.cfg)
在objects/commands.cfg该文件中表明当报警产生,如何去执行远端的脚本,

4)
在主机服务的配置文件中,启用时间处理机制
event_handler shell_name

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
检测上面修改的文件,有没有报错

杀掉个服务,做下简单的测试,ok

不辜负zz的信任,解决了

搞定收工

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站制作、岳阳楼网络推广、成都微信小程序、岳阳楼网络营销、岳阳楼企业策划、岳阳楼品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联公司为所有大学生创业者提供岳阳楼建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:nagios事件处理机制-创新互联
URL标题:http://cdkjz.cn/article/degoep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220