Nagios(七)——Nagios 邮件,短信告警

       Nagios 除了监控功能很强大之外,它的报警功能也很不错。从运维的角度来讲,报警一般就是邮件和短信报警这两种方式,这次实验实现的是Nagios 的邮件报警功能。

 

1、  安装sendmail 组件

 这里要确保sendmail 相关组件的完整安装,

[root@server ~]# yum install sendmail* mailx

安装完成后重启sendmail 服务

[root@server ~]# service sendmail restart

Shutting down sendmail:                                   [  OK  ]

Starting sendmail:                                        [  OK  ]

Starting sm-client:                                        [  OK  ]

 

测试sendmail 发信情况

[root@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

[root@server ~]# vim /usr/local/nagios/etc/objects/contacts.cfg                

define contact{

        contact_name                    nagios

        use                            generic-contact

        alias                           Nagios

        email                          159158*****@163.com159158*****@139.com          // 改为自己的邮箱地址即可

        }

 

define contactgroup{

        contactgroup_name       admins

        alias                   Nagios Administrators

        members               nagios

        }

 

(2)    设置报警方式

[root@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

[root@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

[root@server ~]# service nagios restart

Running configuration check...done.

Stopping nagios: .done.

Starting nagios: done.

 

(5)    邮箱设置

这里以139邮箱为例,设置收信

点击左上角“设置”可以看到这个版块

 

点击“手机通知”,打开邮箱到达通知,设置接收时间段,设置完保存。

 

返回到这个版块,点击“白名单设置”,将服务器域名设置进白名单

 

 

(6)    测试收信情况

可以随便关闭客户机上的一个服务做测试

设置邮件到达提醒后,还可以收到告警短信,效果如下:(需要设置成长短信的形式)

 

PS139邮箱的短信报警免费是好,但是有时延,这里提供另外一种方式报警——微信告警

可以QQ邮箱将域名设置进白名单,然后微信设置QQ邮箱推送即可(由于设置简单,这里就不演示了)效果如下:

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值