python插件做nagios發報警郵件<二>

接上文 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 '[email protected]' -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 = "[email protected]"

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

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章