首页 > 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年1月14日03:25 | #1

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

  2. 2018年1月15日15:40 | #2

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

  3. 2018年1月16日14:16 | #3

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

  4. 2018年1月16日15:52 | #4

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

  5. 2018年1月17日02:34 | #5

    payday lender fees
    [url=http://loansmart24.com/]speedy cash payday loans online[/url]
    best fast loans online
    payday lender fees

  6. 2018年1月19日01:50 | #6

    online casino bovada
    [url=http://bablcasinogames.com/]free casino games vegas world[/url]
    casino games online real money
    casinos in michigan with hotels

  7. 2018年1月19日04:49 | #7

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

  8. 2018年1月20日01:52 | #8

    free slot machines play
    [url=http://bablcasinogames.com/]us online casinos[/url]
    online casinos real money
    online casinos real money

  9. 2018年1月20日21:57 | #9

    absolutely free slots for fun
    [url=http://bablcasinogames.com/]456 free slots casino[/url]
    free casino games no download no registration
    sizzling 777 slots free online

  10. 2018年1月21日00:01 | #10

    online casino game
    [url=http://bablcasinogames.com/]online casino play for free[/url]
    real money online
    sizzling 777 slots free online

  11. 2018年1月21日00:13 | #11

    usa online slots real money
    [url=http://bablcasinogames.com/]free casino games no download no registration[/url]
    400 free slot games
    casino games online real money

评论分页
1 ... 4 5 6 374
订阅评论
4+3= (必填)