一、问题:

命令测试发送邮件:

#echo "mailtest"|mail -s "test   mail"  ceshi@staff.test.com

查看mail log,报错如下:

# tail -f /var/log/maillog

Oct 31 11:30:13 nagios90-219 sendmail[20981]: q9V3UCvt020981: from=<root@nagios90-219.test.com>, size=358, class=0, nrcpts=1, msgid=<201210310330.q9V3UCmb020972@nagios90-219.test.com>, proto=ESMTP,daemon=MTA, relay=localhost.localdomain [127.0.0.1]

 

Oct 31 11:30:13 nagios90-219 sendmail[20987]: q9V3UCvt020981: to=<admins@system.test.com>, ctladdr=<root@nagios90-219.test.com> (0/0), delay=00:00:01, xdelay=00:00:00, mailer=esmtp, pri=120358, relay=system.test.com. [60.28.250.40], dsn=5.6.0, stat=Data format error

 

Oct 31 11:30:13 nagios90-219 sendmail[20987]: q9V3UCvt020981: q9V3UDvt020987: DSN: Data format error

 

 二、分析原因

根据邮件报错得知跟mta域名解析有关,因为报警邮件内部使用,未在DNS配置mail的域名、mx记录,发送邮件时导致不能正常解析mail域名

 

三、解决问题:

解决问题前,先介绍两个关键文件:

1、域名解析过程受文件nsswitch.conf(数据库和名字服务转换配置文件)定义,解析域名默认先读/etc/hosts,若没有才去dns查询,具体定义如下:

# vim /etc/nsswitch.conf 

hosts:      files dns

 

说明:

#dns   Use DNS (Domain Name Service)

#files  Use the local files

 

2hosts文件,主机名查询静态表,是Linux系统中一个负责IP地址与域名快速解析的文件

原内容:

# vim /etc/hosts

127.0.0.1     localhost.localdomain localhost

 

格式说明:

第一部份:网络IP地址;

 

第二部份:主机名或域名;

 

第三部份:主机名别名

修改后的hosts文件内容:

127.0.0.1               nagios90-219.test.com  localhost nagios90-219

3

正常的邮件日志:

Oct 31 11:58:03 nagios90-219 sendmail[13891]: q9V3w3GA013891: from=<root@nagios90-219.test.com>, size=1395, class=0, nrcpts=1, msgid=<201210310358.q9V3w1e4013824@nagios90-219.test.com>, proto=ESMTP,daemon=MTA, relay=nagios90-219.test.com [127.0.0.1]

Oct 31 11:58:04 nagios90-219 sendmail[13824]: q9V3w1e4013824: to=root, ctladdr=root (0/0), delay=00:00:03, xdelay=00:00:01, mailer=relay, pri=31112, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent(q9V3w3GA013891 Message accepted for delivery)

Oct 31 11:58:04 nagios90-219 sendmail[13969]: q9V3w3GA013891: to=<root@nagios90-219.test.com>, ctladdr=<root@nagios90-219.test.com> (0/0), delay=00:00:01, xdelay=00:00:00, mailer=local, pri=31631,dsn=2.0.0, stat=Sent

 

总结:无论是内部使用还是提供外部服务,应用服务都需要严谨标准配置,避免后期隐患。在Nagios邮件报警中,大家都会选择linux系统自带的sendmail做为MTA,此问题也是容易忽略和常见的,以此供参考,欢迎和大家一起讨论运维中发现的问题,谢谢。