设为首页 收藏本站
查看: 793|回复: 0

nagios Event Handlers

[复制链接]

尚未签到

发表于 2019-1-13 09:44:49 | 显示全部楼层 |阅读模式
  nagios-event-triger-AutoRecover
  思路:
  use NRPE to execute the necessary commands on the remote hosts
  In order to adapt the scheme from the Nagios docs to work on remote servers as well three things need to be done:
  

  1.The command that is executed by the event handler script should be changed to use NRPE
  2.On the remote machine the nagios user (under which the NRPE service is running) should be given some sudo rights so that it is actually allowed to start a service.
  3.The NRPE configuration on the remote machine should of course be changed to include the new command(s) for starting services.
  

  1. nagios manage server
  (1)vi localhost.cfg
define service{
        use                     generic-service
        host_name               test2.bigdata.com
        service_description     gmond
        check_command           check_nrpe_eventhandler!check_gmond
        notifications_enabled   1
        notification_interval   0
        max_check_attempts      4
        event_handler           restart-service!gmond
        }
define service{
        use                     generic-service
        host_name               test2.bigdata.com
        service_description     mysqld
        check_command           check_nrpe_eventhandler!check_mysqld
        notifications_enabled   1
        notification_interval   0
        max_check_attempts      5
        event_handler           restart-service!mysqld
        }  

  (2)vi commands.cfg
define command{
        command_name    check_nrpe_eventhandler
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -t 40 -c $ARG1$
        }
define command {
        command_name    restart-service
        command_line    $USER1$/eventhandlers/event_handler_script.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $HOSTADDRESS$ $ARG1$ $SERVICEDESC$
        }  

  利用后面编写的通用的事件处理脚本模块文件 /usr/local/nagios/libexec/eventhandlers/event_handler_script.sh 传递监控服务所需的参数$ARG1$
  

  2. on remote machine with NRPE running
  (1)vi nrpe.cfg
command[check_gmond]=/usr/local/nagios/libexec/check_gmond
command[restart_gmond]=/usr/bin/sudo /etc/init.d/gmond restart
command[check_mysqld]=/usr/local/nagios/libexec/check_mysqld
command[restart_mysqld]=/usr/bin/sudo /usr/local/nagios/libexec/restart_mysqld  (2)edit your service manage script on remote machine.
  /usr/local/nagios/libexec/check_mysqld
  /usr/local/nagios/libexec/restart_mysqld
  

  

  3. 通用的事件处理脚本模块文件 /usr/local/nagios/libexec/eventhandlers/event_handler_script.sh内容如下
#!/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.
# update 2015/10/23
# version: 0.2
date=`date`
# 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)
        # We don't really care about warning states, since the service is probably still running...
        # 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 service $6 (3rd soft critical state)...\n"
                    # Call NRPE to restart the service on the remote machine
                    /usr/local/nagios/libexec/check_nrpe -H $4 -c restart_$5
                    echo "$date -restart $6 on server $4 -at retry $3 times -SOFT"  >> /tmp/eventhandlers
                    ;;
                    esac
                ;;
        # The HTTP service somehow managed to turn into a hard error without getting fixed.
        # It should have been restarted by the code above, but for some reason it didn't.
        # Let's give it one last try, shall we?
        # Note: Contacts have already been notified of a problem with the service at this
        # point (unless you disabled notifications for this service)
        HARD)
                  echo -n "Restarting $6 service...\n"
                  # Call the init script to restart the NRPE server
                  echo "$date -restart $6 on server $4 -at retry $3 times -HARD"  >> /tmp/eventhandlers
                  /usr/local/nagios/libexec/check_nrpe -H $4 -c restart_$5
                ;;
        esac
        ;;
esac
exit 0  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-662620-1-1.html 上篇帖子: nagios配置过程详解 下篇帖子: 安装NAGIOS图形(PNP)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表