Ps: 完美的解決了python傳入參數帶有回車換行符,不換行問題。修改本文時間:2010年12月21號,腳本有變動·nagios commands.cfg無需修改·用了此版本·就解決了不用去讀文本方式。
安裝好nagios後,一直利用nagios服務器上的sendmail服務器去發郵件,修改配置文件可以僞造別的郵件地址發郵件,但是一直會被郵局視爲垃圾郵件,我把email地址加在了QQ上,好處是一來郵件QQ右下角會彈提示。工作時間會第一時間知道服務器出狀況(手機短信暫時不提),但是如果是垃圾郵件,QQ不會提醒的,當然可以在QQ郵箱把其添加爲信任郵局,可以避免。我就不那麼浪費時間了。自己寫一個發郵件的插件來替換掉本機的sendmail服務,可以爲服務器節省資源,大家也知道啓動sendmail服務稍稍費點時間,如果主機名沒有設置好的話sendmail服務會啓動很久才能起來的哦。廢話不多說了,發源代碼。
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: only one to email user ./sendmail -t '[email protected]' -s 'hello eric' -m 'hello eric,this is sendmail test! many to email user ./sendmail -t '[email protected],[email protected],[email protected]' -s 'hello eric' -m 'hello eric,this is sendmail test!""" sys.exit(3) try: options,args = getopt.getopt(sys.argv[1:],"ht:s:m:",["help","to=","subject=","message="]) 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 ("-m","--message"): MESSAGE = value MESSAGE = MESSAGE.split('\\n') MESSAGE = '\n'.join(MESSAGE) #smtp HOST HOST = "smtp.126.com" #改爲你的郵局SMTP 主機地址 #smtp port PORT = "25" #改爲你的郵局的SMTP 端口 #FROM mail user USER = 'eric' # 改爲你的郵箱用戶名 #FROM mail password PASSWD = '123456' # 改爲你的郵箱密碼 #FROM EMAIL FROM = "[email protected]" # 改爲你的郵箱 email 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"
使用方法:
只給一個用戶發:
nagios $> ./sendmail -t '[email protected]' -s 'hello eric' -m 'hello eric,this is sendmail test!
給多個用戶發:
./sendmail -t '[email protected],[email protected],[email protected]' -s 'hello eric' -m 'hello eric,this is sendmail test!
如果利用在nagios 上修改 commands.cfg
# 還爲測試·馬上就測試。怕的是換行符有問題·
nagios $> vim /usr/local/nagios/etc/objects/commands.cfg
define command{ command_name notify-host-by-email command_line $USER1$/sendmail -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" } define command{ command_name notify-service-by-email command_line $USER1$/sendmail -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" }
有圖有真相:
插件下載地址