Nagios 除了监控功能很强大之外,它的报警功能也很不错。从运维的角度来讲,报警一般就是邮件和短信报警这两种方式,这次实验实现的是Nagios 的邮件报警功能。 1、 安装sendmail 组件 这里要确保sendmail 相关组件的完整安装, [iyunv@server ~]# yum install sendmail* mailx 安装完成后重启sendmail 服务 [iyunv@server ~]# service sendmail restart Shutting down sendmail: [ OK ] Starting sendmail: [ OK ] Starting sm-client: [ OK ] 测试sendmail 发信情况 [iyunv@server ~]# /bin/mail -s "`date +%Y-%m-%d` Server SMS Test" 159158*****@163.com hello admin . EOT 2、 邮件报警的配置 一般而言,如果在整个系统中只有一个管理员,那么只需要在contact.cfg文件中,添加一下管理员的邮箱即可。但如果监控的内容中服务器有单独的管理员,那就需要定义多个contact(联系人),然后再通过contactgroup(联系组)对各个contact 进行分组。 (1) 修改contact.cfg [iyunv@server ~]# vim /usr/local/nagios/etc/objects/contacts.cfg define contact{ contact_name nagios use generic-contact alias Nagios email 159158*****@163.com,159158*****@139.com // 改为自己的邮箱地址即可 } define contactgroup{ contactgroup_name admins alias Nagios Administrators members nagios } (2) 设置报警方式 [iyunv@server ~]# vim /usr/local/nagios/etc/objects/templates.cfg define contact{ name generic-contact ; The name of this contact template service_notification_period 24x7 ; service notifications can be sent anytime host_notification_period 24x7 ; host notifications can be sent anytime service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events service_notification_commands notify-service-by-email ; send service notifications via email host_notification_commands notify-host-by-email ; send host notifications via email register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE! } (3) 修改commands.cfg [iyunv@server ~]# vim /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 define command{ command_name notify-service-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ } 看到这里已经默认配置好,我们不用修改直接调用就行了。 (4) 通过-v参数检查配置无误之后,重启nagios [iyunv@server ~]# service nagios restart Running configuration check...done. Stopping nagios: .done. Starting nagios: done. (5) 邮箱设置 这里以139邮箱为例,设置收信 点击左上角“设置”可以看到这个版块
点击“手机通知”,打开邮箱到达通知,设置接收时间段,设置完保存。
返回到这个版块,点击“白名单设置”,将服务器域名设置进白名单
(6) 测试收信情况 可以随便关闭客户机上的一个服务做测试
设置邮件到达提醒后,还可以收到告警短信,效果如下:(需要设置成长短信的形式)
PS:139邮箱的短信报警免费是好,但是有时延,这里提供另外一种方式报警——微信告警
可以QQ邮箱将域名设置进白名单,然后微信设置QQ邮箱推送即可(由于设置简单,这里就不演示了)效果如下:
|