杨叔叔 发表于 2019-1-16 13:16:56

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
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 &quot;&quot;&quot;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 '&quot;&quot;&quot;
sys.exit(3)
try:
options,args = getopt.getopt(sys.argv,&quot;ht:s:p:&quot;,[&quot;--help&quot;,&quot;--to=&quot;,&quot;--subject=&quot;,&quot;--path=&quot;])
except getopt.GetoptError:
usage()
for name,value in options:
if name in (&quot;-h&quot;,&quot;--help&quot;):
usage()
if name in (&quot;-t&quot;,&quot;--to&quot;):
# accept message user
TO = value
TO = TO.split(&quot;,&quot;)
if name in (&quot;-s&quot;,&quot;--title&quot;):
SUBJECT = value
if name in (&quot;-p&quot;,&quot;--path&quot;):
MESSAGE = value                     #传入文件路径
MESSAGE = open(MESSAGE,'r')    #只读模式打开文件
MESSAGE = MESSAGE.read()       #读取文件内容
#smtp HOST
HOST = &quot;smtp.126.com&quot;
#smtp port
PORT = &quot;25&quot;
#FROM mail user
USER = 'eric'
#FROM mail password
PASSWD = '123456'
#FROM EMAIL
FROM = &quot;yangzi2008@126.com&quot;
try:
BODY = string.join((
&quot;From: %s&quot; % FROM,
&quot;To: %s&quot; % TO,
&quot;Subject: %s&quot; % SUBJECT,
&quot;&quot;,
MESSAGE),&quot;\r\n&quot;)
smtp = smtplib.SMTP()
smtp.connect(HOST,PORT)
smtp.login(USER,PASSWD)
smtp.sendmail(FROM,TO,BODY)
smtp.quit()
except:
print &quot;UNKNOWN ERROR&quot;
print &quot;please look help&quot;
print &quot;./sendmail -h&quot;
  注意:
改动了 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 &quot;%b&quot; &quot;***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n&quot; > /tmp/nagios-mail | $USER1$/sendmail -t $CONTACTEMAIL$ -s &quot;** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **&quot; -p /tmp/nagios-mail
}
define command{
command_name    notify-service-by-email
command_line    /usr/bin/printf &quot;%b&quot; &quot;***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$&quot; > /tmp/nagios-mail| $USER1$/sendmail -t $CONTACTEMAIL$ -s &quot;** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **&quot; -p /tmp/nagios-mail
}

  下载:
  sendmail







页: [1]
查看完整版本: python插件做nagios发报警邮件<二>