最近用CentOS 6.3 x86_64 + Mutt 1.5.2. + Msmtp 1.4.27 + Nagios 3.2 搭建監控平臺,遇見一個鬱悶的問題,就是Mutt+Msmtp 在commands.cfg中沒法發送郵件報警的問題。mysql
問題描述:ios
單獨使用mutt命令能夠發送郵件,sql
例如: echo "This is a new mail." | mutt -s "new message" mymail@163.comide
而在配置文件(commands.cfg)裏面定義了命令,當主機和服務當掉,不能發送郵件。查看/usr/local/msmtp/etc/msmtp.log,沒有任何的發送郵件日誌。查看Nagios日誌,發現命令已經執行,可是就是收不到郵件,Nagios日誌代碼以下spa
- Feb 6 05:59:35 localhost nagios: SERVICE NOTIFICATION: nagios;mysql_server;check_slave;CRITICAL;notify-by-sms;ERROR!:The Slave Mysql Server Was Down!!!
- Feb 6 05:59:36 localhost nagios: SERVICE NOTIFICATION: nagiosadmin;mysql_server;check_slave;CRITICAL;notify-by-sms;ERROR!:The Slave Mysql Server Was Down!!!
經過命令/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 檢查配置文件,並無錯誤報告,單獨查看commands.cfg配置文件,也沒有檢查出錯誤,commands.cfg配置文件代碼以下日誌
- define command{
- command_name notify-by-sms
- command_line /usr/bin/printf "%b" "***** Nagios-service *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
- }
- define command{
- command_name service-by-email
- command_line /usr/bin/printf "%b ***** Nagios-host *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
- }
contacts.cfg配置文件中也正確,這個問題非常讓人糾結啊,沒有報錯,就是在Nagios中沒法發送報警郵件。後來經過查詢資料瞭解,才知道Mutt會在家目錄下生成一個sent的文件,你發送的記錄都會寫到此文件中,而我用的是root用戶登陸,nagios調用mutt後臺執行,可是沒有權限在sent文件中是寫入數據,因此纔會出現這個問題。server
好了,問題根源找到了,解決也就好說多了,直接把/root目錄 和/root/sent 給nagios寫入權限,問題解決了。xml