版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/github_38854224/article/details/89379234
事前准备:服务器可发送邮件
centos7 发送邮件实测
一、安装:
安装 mailx 和 sendmail:
yum install mailx -y
yum install sendmail -y
systemctl start sendmail
二、设置发件人信息:vim /etc/mail.rc
set [email protected] #作为发送邮件的账号
set smtp=smtp.163.com #发送邮件的服务器
set smtp-auth-user=邮箱帐号 #你的邮箱帐号
set smtp-auth-password=授权码 #授权码
set smtp-auth=login
三、发送:
通过文件内容发送:
mail -s '主题' [email protected] < test.txt #发送的邮件标题是 “主题” 内容是test.txt 文件的内容
通过管道符直接发送:
echo '内容' | mail -s '主题' [email protected]
注意:发送的主题或者内容太简单的话有可能被邮件服务器认为是垃圾邮件拦截掉!
四、查看队列: mailq
五、查看日志: tail /var/log/maillog
nagios发送通知邮件的配置过程
修改templates.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!
}
关于服务类通知的条件定义和上述配置方法一样。
修改 localhost.cfg(阈值)
注意对有些服务的监控是没有使用check_nrpe指令的,如ping测试,ssh,http服务都是直接使用的相应的plugin来监测的。所以如果要修改这些服务的监控告警阈值需要修改这个localhost.cfg文件,
而对于其它调用了check_nrep指令的服务则同样需要修改/opt/nagios/etc/nrpe.cfg
,并且确保两者中的指令名称一样。localhost.cfg示例如下:
[root@test2-5 objects]# vim localhost.cfg
# Define a service to "ping" the local machine
define service{
use local-service ; Name of service template to use
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60% ;设定ping包平均响应时长100ms,丢包率20%触发w告警;平均响应时长500ms,丢包率60%,触发c告警
}
修改contacts.cfg(邮箱)
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email [email protected] ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******管理员邮箱 多个用空格隔开
}
修改comment.cfg(邮件内容)
# '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$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
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$\n" | /usr/bin/mail -s "**Nagios 监控警报信息,请及
时查看,此邮件一小时发送一次**" $CONTACTEMAIL$
}
注意:使用nagios默认的主题,有可能会导致邮件发不出,被拦截?(当作垃圾邮件?)