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 '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
       }

本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/461011,如需转载请自行联系原作者

相关文章
|
10天前
|
数据采集 存储 测试技术
Python生成随机数插件Faker的用法
Python生成随机数插件Faker的用法
46 0
|
10天前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
153 0
|
10天前
|
数据安全/隐私保护 Python
Python实现邮件发送(含详细设置方法),并总结自己遇到的错误
Python实现邮件发送(含详细设置方法),并总结自己遇到的错误
|
10天前
|
Python
Python自动化办公实战案例:文件整理与邮件发送
Python自动化办公实战案例:文件整理与邮件发送
17 0
|
10天前
|
运维 Shell Linux
第十四章 Python发送邮件(常见四种邮件内容)
第十四章 Python发送邮件(常见四种邮件内容)
|
10天前
|
监控 Python
Python监控主机是否存活,并发报警邮件
Python监控主机是否存活,并发报警邮件
|
10天前
|
数据安全/隐私保护 Python
Python Flask-Mail实现邮件发送
Python Flask-Mail实现邮件发送
|
10天前
|
存储 Python
终于,手把手教会 HR 实现 Python + Excel 「邮件自动化」发工资条了
终于,手把手教会 HR 实现 Python + Excel 「邮件自动化」发工资条了
33 0
|
10天前
|
API Python
Python邮件与日历管理
【4月更文挑战第13天】Python 通过 `smtplib` 和 `email` 发送邮件,`imaplib` 接收邮件。`google-api-python-client` 库用于管理 Google Calendar,示例代码展示了列出日历事件的功能。要使用 Google Calendar API,需设置服务帐户凭据和范围。
20 1
|
10天前
|
数据可视化 数据挖掘 Python
Python中使用Matplotlib插件绘制曲线
Python中使用Matplotlib插件绘制曲线
38 0