首页 > nagios > python插件做nagios发报警邮件<二>

python插件做nagios发报警邮件<二>

2010年12月21日 eric 发表评论 阅读评论

Ps: 完美的解决了python传入参数带有回车换行符,不换行问题。修改本文时间:2010年12月21号,脚本有变动·nagios commands.cfg无需修改·用了此版本·就解决了不用去读文本方式,看个人吧,本人推荐使用参数方式发送报警邮件!

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'

测试过很多方法,都不行,最后用了最笨的的方法就是读文件!就稍微改动了一下上文脚本!经过2010年12月21号,脚本有变动目前已经支持参数方式发送报警,解决了换行符问题。

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

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/374.html

分类: nagios 标签: , ,
  1. 2018年4月4日22:01 | #1

    buy viagra brighton
    kamagra
    viagra buy mastercard

  2. 2018年4月5日20:52 | #2

    Available cash advance settlement loans 6 month cash advance loans cash advance loans 60 day cash advance loans do cash advance loans affect your credit.

  3. 2018年4月6日03:18 | #3

    Cleans and prepares ground for prime polish.

  4. 2018年4月6日19:57 | #4

    Transforming a completed basement is costlier.

  5. 2018年4月8日07:47 | #5

    how much viagra
    viagra online without script
    buy viagra pills

  6. 2018年4月10日06:34 | #6

    lowest price generic http://drugstore-usa.science buy real online.

  7. 2018年4月11日09:51 | #7

    best place buy generic cialis online
    tadalafil generic
    cialis sale us

  8. 2018年4月11日21:49 | #8

    discount coupon for cialis
    tadalafil generic
    cheap cialis generic canada

  9. 2018年4月12日06:16 | #9

    Fasteners tear the paper on flooring sanders.

  10. 2018年4月12日13:25 | #10

    Mildew spores is not going to survive in dry climates.

  11. 2018年4月12日23:46 | #11

    cheap cialis once day
    cialis pills
    cheapest generic cialis uk

  12. 2018年4月13日01:06 | #12

    imvu download

  13. 2018年4月13日01:33 | #13

    where to buy in las vegas http://online-medications.men where to buy online.

  14. 2018年4月13日11:06 | #14

    viagra sale high street
    viagra generic
    is it legal to order viagra online

  15. 2018年4月13日16:51 | #15

    viagra gel sale
    generic viagra
    buy viagra cheap no prescription

  16. 2018年4月13日18:06 | #16

    i magneti per smettere di fumare

  17. 2018年4月15日01:12 | #17

    Criminal case report format, http://background-check-services-for-employers.science/ westchester county new york public records search.

  18. 2018年4月15日16:00 | #18

    Buy Essay Germany | Essay Writing Service in Germany

  19. 2018年4月15日16:41 | #19

    cheapest cialis in new zealand
    5 mg cialis coupon printable
    where to buy cialis online in canada

  20. 2018年4月15日17:20 | #20

    where can i buy viagra in la
    viagra sales
    cheap viagra for women

  21. 2018年4月15日17:30 | #21

    Bridgeport connecticut public records, http://background-check-renter.science/ how to check criminal history california.

  22. 2018年4月16日11:06 | #22

    Discover native contractors to install or repair your roof.

  23. 2018年4月16日14:27 | #23

    b&b levitrande catania [url=http://levitrag.us/#]order Levitra online[/url] neurontin online [url=http://neurontinw.us/#]buy Neurontin online[/url] neurontin a blood thinner.

  24. 2018年4月16日19:27 | #24

    viagra buy in canada
    viagra cheap
    buy viagra cheap uk

  25. 2018年4月17日16:53 | #25

    Amoxicillin meaning [url=http://amoxicillinr.us/#]order Amoxicillin online[/url] amoxil [url=http://amoxicillinr.us/#]amoxil[/url] amoxil and Amoxicillin.

  26. 2018年4月18日21:38 | #26

    Took my 2011 Infiniti g25 to ultimate window tinting.

  27. 2018年4月18日22:13 | #27

    smettere di fumare

  28. 2018年4月19日03:56 | #28

    Choose the perfect trim on your HardwoodInstallation.

  29. 2018年4月19日06:58 | #29

    I’d positively suggest Okay.C. Waterproofing.

  30. 2018年4月19日23:46 | #30

    Buy Essay Canada

  31. 2018年4月20日11:15 | #31

    Clomid weight loss pilules [url=http://cledmed.unblog.fr]sans ordonnance[/url] lasix increased urinary output by Achat clomid online acheter clomid [url=http://frclomid.unblog.fr/clomid-acheter/#]acheter clomid[/url] en pharmacie en ligne, acheter lasilix en France [url=http://achatlasix.unblog.fr/#]lasilix sans ordonnance[/url] lasix pill form. chlorogenic acid health benefits [url=http://www.enjoycre.com/index.php?option=com_k2&view=itemlist&task=user&id=158836&lang=it]pure garcinia cambogia and green coffee cleanse combo diet[/url], best treatment for thinning hair [url=http://www.tobacco1.net/index.php?option=com_k2&view=itemlist&task=user&id=396330]best medicine for hair fall and regrowth[/url] hair growth products online.

  32. 2018年4月21日13:29 | #32

    3M Window Tint is not just for privateness.

  33. 2018年4月21日22:15 | #33

    Smettere Di Fumare Velocemente

  34. AbitaTawn
    2018年4月22日12:01 | #34

    le logiciel espion pour iPhone [url=http://bandamn.ru/index.php/component/users/?option=com_k2&view=itemlist&task=user&id=1635570] logiciel espion telephone portable[/url] logiciel espion android. creditos rapidos urgentes [url=http://www.agriturismo-spigno.it/index.php?option=com_k2&view=itemlist&task=user&id=232847]dinero rapido y facil[/url] prestamos en linea al instante. Tutorage liza can gift driftwood rose tho cybele melanie can be the prince. And amen outside luxembourg, crinkles to this landmark s work, we throat the exhausting staple mongst a champlain tosit installing this boy, a extreme cassiopeia beside exodi vice its generic accounts, to unburden him to plat nothing. It s bane whereas be contemplated underneath this life, luis, judas wyvil said. Light to his word, he

  35. 2018年4月23日18:00 | #35

    I would positively suggest K.C. Waterproofing.

  36. 2018年4月24日14:15 | #36

    Liked the service: Garden Love was great!

  37. 2018年4月24日15:57 | #37

    How do you know it’s time to refinish the ground?

订阅评论
1+6= (必填)