接上文 python插件做nagios发报警邮件 http://www.nginxs.com/linux/371.html,由于python 传入的参数,python 会自动加 \ ,经过代码测试,代码如下:

nagios $> cat test.py
#!/usr/bin/python
import sys

 

str = sys.argv[1]
str = repr(str)
print str
nagios $> python test.py “aaaa\nfffff”
‘aaaa\\nfffff’

测试过很多方法,都不行,最后用了最笨的的方法就是读文件!就稍微改动了一下上文脚本

nagios $> cat /usr/local/nagios/libexec/sendmail
#!/usr/bin/python
import smtplib
import string
import sys
import getopt

def usage():
   print """sendmail is a send mail Plugins
   Usage:

   sendmail [-h|--help][-t|--to][-s|--subject][-m|--message]

   Options:
          --help|-h)
                 print sendmail help.
          --to|-t)
                 Sets sendmail to email.
          --subject|-s)
                  Sets the mail subject.
          --message|-m)
                  Sets the mail body
    Example:
          ./sendmail -t 'eric@nginxs.com' -s 'hello eric' -p /tmp/nagios-mail '"""

   sys.exit(3)

try:
   options,args = getopt.getopt(sys.argv[1:],"ht:s:p:",["--help","--to=","--subject=","--path="])
except getopt.GetoptError:
   usage()
for name,value in options:
    if name in ("-h","--help"):
       usage()
    if name in ("-t","--to"):
# accept message user
       TO = value
       TO = TO.split(",")
    if name in ("-s","--title"):
       SUBJECT = value
    if name in ("-p","--path"):
       MESSAGE = value                     #传入文件路径
       MESSAGE = open(MESSAGE,'r')    #只读模式打开文件
       MESSAGE = MESSAGE.read()       #读取文件内容
#smtp HOST
HOST = "smtp.126.com"
#smtp port
PORT = "25"
#FROM mail user
USER = 'eric'
#FROM mail password
PASSWD = '123456'
#FROM EMAIL
FROM = "yangzi2008@126.com"

try:
   BODY = string.join((
      "From: %s" % FROM,
      "To: %s" % TO,
      "Subject: %s" % SUBJECT,
      "",
      MESSAGE),"\r\n")

   smtp = smtplib.SMTP()
   smtp.connect(HOST,PORT)
   smtp.login(USER,PASSWD)
   smtp.sendmail(FROM,TO,BODY)
   smtp.quit()
except:
   print "UNKNOWN ERROR"
   print "please look help"
   print "./sendmail -h"

注意:
改动了 message 以读文件内容方式传入,放弃了以参数方法传入。
-m 擦数改为 -p参数 -p 后面 跟 文件的 绝对路径。

修改 nagios 的 commands.cfg

nagios $> vim /usr/local/nagios/etc/objects/commands.cfg
define command{
       command_name    notify-host-by-email
       command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" > /tmp/nagios-mail | $USER1$/sendmail -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -p /tmp/nagios-mail
       }

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$" > /tmp/nagios-mail| $USER1$/sendmail -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -p /tmp/nagios-mail
       }

下载:

sendmail