首页 > 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. 2017年12月6日07:38 | #1

    cialis 5mg para que serve
    erectile dysfunction medications
    reliable sites to buy viagra
    [url=http://pillshnembn.com/#]erectile dysfunction medications[/url]
    can viagra go generic

  2. 2017年12月7日22:53 | #2

    sildenafil 50mg
    generic viagra 100mg
    buy authentic viagra online
    [url=http://hqviagrajdr.com/]viagra[/url]
    600 mg viagra

  3. 2017年12月8日00:37 | #3

    free casino games
    [url=http://real777money.com/]slot machines[/url]
    play casino online
    free casino games

  4. 2017年12月8日19:38 | #4

    slot games
    [url=http://real777money.com/]slot games[/url]
    real money casino online usa
    best us casinos online

  5. 2017年12月9日01:12 | #5

    general auto insurance number
    texas car insurance companies
    cheap auto insurance quotes florida car insurance state farm
    [url=http://autocarins2018.com/free-auto-insurance-quotes/]memphis auto insurance[/url]

  6. 2017年12月9日16:11 | #6

    real money casino
    [url=http://real777money.com/]best us casinos online[/url]
    online casino
    slot games

  7. 2017年12月10日00:08 | #7

    cialis canedean pills
    canada brand cialis
    cialis canada with prescription
    [url=http://edpharmacybest.com/]viagra canadian drugstore[/url]
    online pharmacy cialis generic

  8. 2017年12月11日16:06 | #8

    casino online
    [url=http://real777money.com/]online casino[/url]
    slot machines
    best us casinos online

  9. 2017年12月12日15:47 | #9

    women viagras pills

    discount viagra

    buying viagras in new york

    [url=http://1stmedsusa.com/]how does viagra work[/url]

    viagra price

  10. 2017年12月13日07:16 | #10

    payday loans no credit check
    [url=https://smajloans.com/]cash loans[/url]
    payday loans online
    online loans

  11. 2017年12月14日11:07 | #11

    pay day loans
    [url=https://smajloans.com/]payday advance[/url]
    payday loans online
    online loans

  12. 2017年12月14日11:37 | #12

    order cialis
    use cialis
    cialis on line order cialis online
    [url=http://girlisus.com/members/kicknovel6/activity/17608/]cialis costs[/url]

  13. 2017年12月15日00:33 | #13

    buy cheap cialis online
    cialis
    best cialis prices cails
    [url=http://lowest-price-canadian-cialis.com/]cialis coupon[/url]

  14. 2017年12月15日08:39 | #14

    pharma from canada
    getcanadiandrugs .com
    canadian prescription drugs canadian pills
    [url=http://canadianpharmacyonline.us.com/]canadian pharmaceuticals[/url]

    canadapharmacyonline.com

  15. 2017年12月15日17:49 | #15

    payday loan online
    [url=https://smajloans.com/]payday loans online[/url]
    cash advance
    payday loans

  16. 2017年12月17日00:27 | #16

    payday loans online
    [url=https://smajloans.com/]payday loans online[/url]
    payday loans online
    payday loans

  17. 2017年12月18日05:39 | #17

    payday loans
    [url=https://smajloans.com/]payday loans online[/url]
    payday loans no credit check
    payday loans no credit check

  18. 2017年12月18日15:06 | #18

    44-466 green pill levitra canadian pharmacy
    canadian pharmacy for viagra canadian pharmacist
    [url=http://besttrustpharmacy.com/]cialis canadian pharmacy[/url]

  19. 2017年12月19日10:44 | #19

    payday loans no credit check
    [url=https://smajloans.com/]payday loans no credit check[/url]
    payday loans online
    payday loans no credit check

  20. 2017年12月21日13:08 | #20

    payday loans no credit check
    [url=https://smajloans.com/]payday loans no credit check[/url]
    payday loans no credit check
    payday loans online

  21. 2017年12月24日01:49 | #21

    payday loans no credit
    [url=http://paydaynock.com/]payday loans online no credit check[/url]
    payday loans online no credit check
    payday loans online no credit check

  22. 2017年12月24日09:08 | #22

    payday loans online no credit check
    [url=http://paydaynock.com/]payday loans no credit[/url]
    payday loans no credit check
    payday loans online no credit check

  23. 2017年12月24日21:20 | #23

    payday loans online no credit check
    [url=http://paydaynock.com/]payday loans no credit check[/url]
    payday loans online no credit check
    payday loans online no credit check

  24. 2017年12月25日21:38 | #24

    car insurance in az car insurance quotes young drivers do you need car insurance [url=http://topcarinsurancerate.com/]ms car insurance[/url] cheap new car insurance

  25. 2017年12月26日17:20 | #25

    compare prices car insurance car insurance companies in nj car insurance chicago il [url=http://3danarchy.com/]auto insurance for teenagers[/url] cheap car insurance in south carolina

  26. 2017年12月26日18:37 | #26

    casino online
    casino games
    online casino real money
    casino games
    casino games list

  27. 2017年12月28日14:11 | #27

    online casino
    [url=http://bom777casino.com/]online casino[/url]
    п»їcasino online
    real money casino

  28. 2017年12月28日19:19 | #28

    free casino games
    [url=http://bom777casino.com/]online casino[/url]
    online casino
    п»їcasino online

  29. 2017年12月29日12:17 | #29

    casino games
    [url=http://bom777casino.com/]casino games[/url]
    free casino games
    casino games

  30. 2017年12月29日15:07 | #30

    free casino games
    [url=http://bom777casino.com/]casino online[/url]
    free casino games
    real money casino

  31. 2017年12月29日16:04 | #31

    After checking out a handful of the blog articles on your web site, I really appreciate your
    way of blogging. I saved it to my bookmark webpage list and will be checking
    back in the near future. Take a look at my web site as well and
    tell me how you feel.

  32. 2017年12月30日05:20 | #32

    casino games
    [url=http://bom777casino.com/]free casino games[/url]
    online casino
    casino games

  33. 2017年12月30日16:37 | #33

    casino games
    [url=http://bom777casino.com/]online casino[/url]
    casino online
    online casino

  34. 2017年12月30日23:25 | #34

    casino online
    [url=http://bom777casino.com/]online casino[/url]
    casino games
    free casino games

  35. 2017年12月30日23:32 | #35

    casino games
    [url=http://bom777casino.com/]real money casino[/url]
    free casino games
    real money casino

  36. 2017年12月31日08:32 | #36

    casino games
    [url=http://bom777casino.com/]casino online[/url]
    online casino
    free casino games

  37. 2017年12月31日15:06 | #37

    casino online
    [url=http://bom777casino.com/]casino online[/url]
    casino online
    free casino games

  38. 2018年1月1日03:25 | #38

    online casino
    [url=http://bom777casino.com/]online casino[/url]
    casino online
    online casino

  39. 2018年1月1日09:03 | #39

    free casino games
    [url=http://bom777casino.com/]casino games[/url]
    casino games
    casino online

  40. 2018年1月1日21:30 | #40

    casino online
    [url=http://bom777casino.com/]free casino games[/url]
    real money casino
    casino online

  41. 2018年1月2日04:03 | #41

    real money casino
    [url=http://bom777casino.com/]free casino games[/url]
    real money casino
    free casino games

  42. 2018年1月2日08:39 | #42

    online casino
    [url=http://bom777casino.com/]real money casino[/url]
    casino online
    free casino games

  43. 2018年1月2日20:25 | #43

    casino games
    [url=http://bom777casino.com/]casino games[/url]
    casino online
    casino games

  44. 2018年1月7日03:13 | #44

    free casino games
    [url=http://casinobablogames.com/]free slot games[/url]
    free slot games
    free slots casino games

  45. 2018年1月7日04:46 | #45

    online casino games
    [url=http://casinobablogames.com/]online casino real money[/url]
    casino free games
    online casinos

  46. 2018年1月8日09:57 | #46

    casino games slots free
    [url=http://casinobablogames.com/]casino slots[/url]
    online casino games
    casino free games

  47. 2018年1月8日14:13 | #47

    online casinos
    [url=http://casinobablogames.com/]casino games slots free[/url]
    free slots online
    slotomania free slots

  48. 2018年1月9日00:58 | #48

    free casino games no download
    [url=http://casinobablogames.com/]casino games slots free[/url]
    free casino slots
    free slot games

  49. 2018年1月12日18:57 | #49

    loans for bad credit
    [url=http://paydayhjkfg.com/]payday loans online no credit check[/url]
    payday loans online no credit check
    payday loans online no credit check

  50. 2018年1月14日02:42 | #50

    online casino
    [url=http://casinobablogames.com/]real money casino[/url]
    online slots
    online casino

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