本帖最后由 monicazhang 于 2015-10-30 21:40 编辑
echo -n "Restarting Apache service..." /sbin/service httpd restart ;; esac ;; HARD) echo -n "Restarting Apache service..." /sbin/service httpd restart ;; esac ;; esac exit 0 1) 配置Nagios监控机上的配置文件 Ø 修改nagios.cfg enable_event_handlers=1 #开启全局
事件处理 Ø 在命令配置文件中定义重启apache的命令 # cd /usr/local/nagios/etc/ #vi objects/commands.cfg #restart the apache service on a remote server nagios安装 define command{ command_name restart-httpd command_line /usr/bin/ssh nagios@$HOSTADDRESS$ "sudo /usr/local/nagios/libexec/eventhandlers/restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$" } Ø 配置主机监控文件 修改services.cfg配置,加入事件处理配置
#vi /usr/local/nagios/etc/objects/services.cfg define service { use srv-pnp host_name nagios-test1 service_description check_tcp 80 check_period 24x7 max_check_attempts 4 normal_check_interval 3 retry_check_interval 2 contact_groups admins notification_interval 10 notification_period 24x7 notification_options w,u,c,r event_handler_enabled 1 #启用事件处理 event_handler restart-httpd #事件处理执行的命令 check_command check_tcp!80 }
1) 测试 在被监控机停止apache服务,等待几分钟后检查被监控机的apache服务是否自动启动。 Nagios通过NRPE执行event_hander 1) 在远程机器上编写service重启脚本
vi /usr/local/nagios/libexec/service_hander 内容如下: #!/bin/sh # # Event handler script for restarting the service on the remote machine 开源监控软件 # # Note: This script will only restart service if the service is # retried 2 times (in a "soft" state) or if the services somehow # manages to fall into a "hard" error state. # # # What state is 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 service on the first # check, because it may just be a fluke! case "$3" in 2) echo -n "Restarting $4 service..." /sbin/service $4 restart ;; esac ;; HARD) echo -n "Restarting $4 service..." /sbin/service $4 restart ;; esac ;; esac exit 0
重新编译nrpe,需要让nrpe传递参数 ./configure --prefix=/usr/local/nagios --enable-command-args nagios配置 修改nrpe.cfg vi /usr/local/nagios/etc/nrpe.cfg dont_blame_nrpe 0改为1 command[restart_service]=/usr/local/nagios/libexec/service_hander $ARG1$ $ARG2$ $ARG3$ $ARG4$ 配置完nrpe后需要重启启动nrpe服务 服务端配置(修改ssh方式下的command.cfg) vi /usr/local/nagios/etc/objects/commands.cfg
找到以下内容: define command{ command_name restart-httpd command_line /usr/bin/ssh nagios@$HOSTADDRESS$ "sudo /usr/local/nagios/libexec/eventhandlers/restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$" } 修改为: define command{ command_name restart-httpd command_line /usr/local/nagios/libexec/check_nrpe -H 172.26.188.202 -c restart_service -a $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ httpd } 报警通知邮件报警通知安装配置sendmail sendmail安装 yum -y install sendmail sendmail-cf sendmail-devel dovecot cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain sendmail配置 n 配置MTA vim /etc/mail/sendmail.mc A、找到DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl 监控软件 修改为DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl B、找到TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl 去掉注释及前面的dnl,把两行移到最前面。 n 生成sendmail.cf主配置文件。 mv /etc/mail/sendmail.cf /etc/mail/sendmail.cf-bak
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf n 设置local-host-names文件 echo "nagios-server.com" >>/etc/mail/local-host-names
此处abc.com是域名,即DNS解析MX解析的域名 n 设置访问控制access和access.db。 Acces的配置为:地址 控制 Vi /etc/mail/access localhost.localdomain RELAY localhost RELAY 127.0.0.1 RELAY 192.168.0.71 RELAY 192.168.0.129 RELAY Makemap hash access < access n 添加账户: groupadd usermail
adduser -g usermail -s /sbin/nologin user1 nagios实施
passwd user1 n 设置别名 vim /etc/aliases nagios邮件报警配置 Ø 配置邮件报警command #vi /usr/local/nagios/etc/objects/commands.cfg # 'notify-host-by-email' command definition define command{ command_name notify-host-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ } # 'notify-service-by-email' command definition nagios培训
|