我们可以使用事件处理来在任何人收到通知之前由Nagios来做一些前期故障修复。
事件处理会在下面情况触发:
1).主机或服务处于一个软态故障状态时
2).主机或服务初始进入一个硬态故障时
3).主机或服务从软态或硬态的故障状态中初始恢复时
通过在主配置文件(nagios.cfg)中设置enable_event_handlers=1来打开全局事件处理,特定主机的和服务的事件处理可用主机和服务对象里的event_handler_enabled域来开关。如果全局的enable_event_handlers域是关闭的,那么特定主机的和服务的事件处理也不会运行。
事件处理命令可以用shell或是perl脚本,脚本中应该处理以下宏:
对服务的:$SERVICESTATE$、$SERVICESTATETYPE$和$SERVICEATTEMPT$;
对主机的:$HOSTSTATE$、$HOSTSTATETYPE$和$HOSTATTEMPT$。
脚本须检测这些作为命令行参数传入的值,并采取必要动作来处理这些值。
事件处理命令通常是与运行于本机上的Nagios程序的权限是相同的(下面例子中Nagios服务是以nagios用户运行的)。这可能会有问题,如果你想写成一个用于系统服务重启的命令,它需要有root权限才能执行一系列命令与任务。你或许会尝使用sudo命令来实现它。
本例通过Nagios检测远程机器上的MySQL服务,当服务出现问题时通过Nagios的事件处理逻辑来重启远程机器上的MySQL服务。
1.配置在Nagios服务器(192.168.0.200)上无密码登录远程机器(MySQL服务运行在上面-192.168.0.210)
[root@nagios ~]# su – nagios
[nagios@nagios ~]$ ssh-keygen -t rsa
# 下面一直回车,不要设置密码
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nagios/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nagios/.ssh/id_rsa.
Your public key has been saved in /home/nagios/.ssh/id_rsa.pub.
The key fingerprint is:
# 将生成的密钥拷贝到要远程登录的机器上
[nagios@nagios ~]$ scp .ssh/id_rsa.pub 192.168.0.210:/home/nagios/
id_rsa.pub 100% 233 0.2KB/s 00:00
# 在要远程登录的机器上配置公钥
Last login: Sat Nov 29 22:30:55 2008 from 192.168.0.200
[nagios@nagios1 ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[nagios@nagios1 ~]$ chmod 600 .ssh/authorized_keys
[nagios@nagios1 ~]$ exit
logout
Connection to 192.168.0.210 closed.
# 测试无密码登录
Last login: Sat Nov 29 22:35:27 2008 from 192.168.0.200
2.在远程机器上配置sudo
使nagios用户可以以root身份运行/usr/local/nagios/libexec/eventhandlers/restart-mysql脚本
[root@MySQL ~]# visudo
nagios ALL=(root) NOPASSWD:/usr/local/nagios/libexec/eventhandlers/restart-mysql
3.在远程机器上编写MySQL重启脚本
[root@MySQL ~]# vi /usr/local/nagios/libexec/eventhandlers/restart-mysql
#!/bin/sh
#
# Event handler script for restarting the MySQL server on the remote machine
#
# Note: This script will only restart the MySQL server if the service is
# retried 2 times (in a "soft" state) or if the web service somehow
# manages to fall into a "hard" error state.
#
#
# What state is the MySQL service in?
case "$1" in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)
# Is this a "soft" or a "hard" state?
case "$2" in
SOFT)
# What check attempt are we on? We don’t want to restart the MySQL server on the first
# check, because it may just be a fluke!
case "$3" in
2)
echo -n "Restarting MySQL service…"
/sbin/service mysqld restart
;;
esac
;;
HARD)
echo -n "Restarting MySQL service…"
/sbin/service mysqld restart
;;
esac
;;
esac
exit 0
上面的脚本只会在MySQL处于软状态,且第二次检查出现故障时或者进入硬状态时重启MySQL。
4.配置Nagios服务器上的配置文件
[root@nagios ~]# cd /usr/local/nagios/etc/
# 检查全局事件处理是否打开
[root@nagios etc]# vi nagios.cfg
enable_event_handlers=1
# 在命令配置文件中定义重启MySQL的命令
[root@nagios etc]# vi objects/commands.cfg
# restart the service on a remote server
define command{
command_name restart-mysql
command_line /usr/bin/ssh nagios@$HOSTADDRESS$ "sudo /usr/local/nagios/libexec/eventhandlers/restart-mysql $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$"
}
# 配置主机监控文件
[root@nagios etc]# vi servers/mysql.cfg
# 省略主机定义和其他服务定义
define service{
use generic-service ; Name of service template to use
host_name MySQL
service_description MySQL
check_command check_nrpe!check_mysql
notifications_enabled 1
event_handler_enabled 1
event_handler restart-mysql
}
这个脚本理论上在服务转入硬态故障之前可以重启MySQL服务以修复故障,这里包含了首次重启没有成功的情况。须注意的是事件处理将只是第一次进入硬态紧急状态时才会被触发,这将阻止Nagios在服务一直处于硬态故障状态时反复地重启MySQL服务。