一、nagios服务配置
1.修改template.cfg
要让nagios能够发送邮件,首先要定义什么情况下可以触发nagios来发送邮件通知,邮件的接收对象,以及通知邮件发送的时间段及频率等,这些参数都需要事先在templates.cfg文件中定义好。示例如下:
define contact{
name generic-contact ; 调用名为generic-contact的联系人模板
service_notification_period 24x7 ; 什么时间段可以发送关于服务的通知,这里调用的是名为24x7的时间段模板
host_notification_period 24x7 ; 什么时间段可以发送关于主机的通知,同样调用24x7的时间段模板
service_notification_options w,u,c,r,f,s ; 哪些服务状态可以发送通知
host_notification_options d,u,r,f,s ; 哪些主机状态可以发送通知
service_notification_commands notify-service-by-email ; 通过哪个指令来
发送关于服务的通知,这里是调用名为notify-service-by-email的命令来
发送,它在commands.cfg里有定义
host_notification_commands notify-host-by-email ; 通过哪个指令来
发送关于主机的通知,这里调用名为notify-host-by-email的命令来发
送,同样在commands.cfg里有定义
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
define host{
name linux-server ; 主机模板的名称
notification_period workhours ; 针对主机的通知只
要workhours时间段发送,此处的配置优先于上面contract定义的
notification_interval 120 ; 故障没有解决时,多长时间重发一次通知,单位为分钟
notification_options d,u,r ; 只在主机处于d,u,r状态时发送通知,此处配置优先于上面contract处定义的。
contact_groups admins ; 将通知发给哪些人
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
关于服务类通知的条件定义和上述配置方法一样。
2.修改contacts.cfg
vi /etc/nagios/objects/contacts.cfg
define contact{
contact_name nagiosadmin ; 联系人名称
use generic-contact ; 调用在
;templates.cfg中定义的名为generic-contract联系人模板
alias Nagios Admin ; 联系人别名
email xxx@qq.com ; nagios通知邮件接收者的邮件地址
}
define contactgroup{ ;定义联系人组
contactgroup_name admins ;联系人组名
;admins,在templates.cfg中通知即是发给这一联系人组的
alias Nagios Administrators
members nagiosadmin ;将nagiosadmin加入这一联系人组中
}
3.配置邮件服务*
vi /etc/mail.rc
set from=nagios-admin@163.com
set smtp=smtp.163.com
set smtp-auth-user=nagios-admin
set smtp-auth-password=aggjiek3DbVv #此处为之前给
#nagios-admin@163邮箱设置的授权码,此处它的意思 相当于授linux
#中的mail程序(这一第三方程序)来登录这个邮箱。
set smtp-auth=login
4.测试邮件
mail -s "Mail Tittle" [xxx@qq.com](https://link.jianshu.com/?t=mailto:xxx@qq.com)
按下回车然后直接按下Ctrl+D来结束输入,发送邮件,会出现如下提示: