温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nagios 事件处理机制

发布时间:2020-07-11 05:17:48 来源:网络 阅读:1683 作者:a731849838 栏目:移动开发

接到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的信任,解决了

搞定收工

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI