139邮箱申请:http://mail.10086.cn/
http://caspian.dotconf.net/menu/Software/SendEmail/
1.下载
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
2.编译安装
tar xvzf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56/
cp sendEmail /usr/local/bin
chmod +x /usr/local/bin/sendEmail
3.测试邮件发送:
/usr/local/bin/sendEmail -f hugwww@163.com -t hugwww@139.com -s smtp.163.com -u “send by hugwww” -xu hugwww -xp gaojinbo.com -m “邮件发送成功,good”
4.配置Nagios
cd /usr/local/nagios/etc/objects
vi commands.cfg
建议,nagios报警尽量不要使用本机sendmail,在大网站注册个免费邮箱发去吧,也减少了很多麻烦,比如sendmail问题导致的报警不能及时到达
以下为command中定义的发邮件命令,根据自己的实际情况修改
command.cfg中加入
define command{
command_name notify-host-by-sendEmail
command_line /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f ceshi@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu ceshi -xp xxxxxxxx -l /var/log/sendEmail.log
}
define command{
command_name notify-service-by-sendEmail
command_line /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTNAME$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail -f ceshi@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu ceshi -xp xxxxxxxx -l /var/log/sendEmail.log
}
解释:
-f 表示发送者的邮箱
-t 表示接收者的邮箱
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
-m 表示邮件的内容
如果你不带-m参数的话,就会提示你自行输入
5.定义联系人
vi contacts.cfg
define contact {
contact_name admin
alias system admin
host_notification_period 24×7
service_notification_period 24×7
host_notification_options d,r
service_notification_options w,u,c,r
service_notification_commands notify-service-by-sendEmail
host_notification_commands notify-host-by-sendEmail
email 13800138000@139.com
pager 13800138000@139.com
}
测试故障时是否能够触发短信报警
/usr/local/139/sendsms.sh “nagios Host alert”