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

python插件做nagios发报警邮件

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

Ps: 完美的解决了python传入参数带有回车换行符,不换行问题。修改本文时间:2010年12月21号,脚本有变动·nagios commands.cfg无需修改·用了此版本·就解决了不用去读文本方式。

安装好nagios后,一直利用nagios服务器上的sendmail服务器去发邮件,修改配置文件可以伪造别的邮件地址发邮件,但是一直会被邮局视为垃圾邮件,我把email地址加在了QQ上,好处是一来邮件QQ右下角会弹提示。工作时间会第一时间知道服务器出状况(手机短信暂时不提),但是如果是垃圾邮件,QQ不会提醒的,当然可以在QQ邮箱把其添加为信任邮局,可以避免。我就不那么浪费时间了。自己写一个发邮件的插件来替换掉本机的sendmail服务,可以为服务器节省资源,大家也知道启动sendmail服务稍稍费点时间,如果主机名没有设置好的话sendmail服务会启动很久才能起来的哦。废话不多说了,发源代码。

nagios $> cat /usr/local/nagios/libexec/sendmail
#!/usr/bin/python
#Script is Sendmail
#Version 1.0.2
#BY:eric
#QQ:277057817
#coding=utf-8

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:
           only one to email  user
          ./sendmail -t 'eric@nginxs.com' -s 'hello eric' -m 'hello eric,this is sendmail test!
           many to email  user
          ./sendmail -t 'eric@nginxs.com,yangzi@nginxs.com,zhangsan@nginxs.com' -s 'hello eric' -m 'hello eric,this is sendmail test!"""
   sys.exit(3)

try:
   options,args = getopt.getopt(sys.argv[1:],"ht:s:m:",["help","to=","subject=","message="])
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 ("-m","--message"):
       MESSAGE = value
       MESSAGE = MESSAGE.split('\\n')        #以‘\n’字符切割字符串,分割后的字符串python就不会在加\
       MESSAGE = '\n'.join(MESSAGE)          #在每个字符串后加 '\n' 字符,这样python 就会认为字符'\n'是换行符

#smtp HOST
HOST = "smtp.126.com"               #改为你的邮局SMTP 主机地址
#smtp port
PORT = "25"                               #改为你的邮局的SMTP 端口
#FROM mail user
USER = 'eric'                              # 改为你的邮箱用户名
#FROM mail password
PASSWD = '123456'                    # 改为你的邮箱密码
#FROM EMAIL
FROM = "yangzi2008@126.com"    # 改为你的邮箱 email

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"

使用方法:
只给一个用户发:

nagios $> ./sendmail -t 'eric@nginxs.com' -s 'hello eric' -m 'hello eric,this is sendmail test!

给多个用户发:

./sendmail -t 'eric@nginxs.com,yangzi@nginxs.com,zhangsan@nginxs.com' -s 'hello eric' -m 'hello eric,this is sendmail test!

如果利用在nagios 上修改 commands.cfg
# 还为测试·马上就测试。怕的是换行符有问题·

nagios $> vim /usr/local/nagios/etc/objects/commands.cfg
define command{
        command_name    notify-host-by-email
        command_line    $USER1$/sendmail -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"  -m  "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"
        }

define command{
        command_name    notify-service-by-email
        command_line    $USER1$/sendmail -t  $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"  -m  "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$"
        }

有图有真相:

插件下载地址

sendmail

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

分类: nagios 标签: , ,
  1. 2018年4月23日00:25 | #1

    Keep this going please, great job!

  2. 2018年4月23日00:26 | #2

    casino games slots
    pogo casino slots
    casino games slots
    casino games roulette

  3. 2018年4月23日00:28 | #3

    I’ve been reviewing online more than seven hours today for windows 7 starter oa latam iso
    download & Eric’s linux and nginx! > python插件做nagios发报警邮件, yet I never found
    any interesting article like yours. It is pretty worth enough for me.
    In my view, if all website owners and bloggers made good content
    as you did, the web will be much more useful than ever before.

  4. 2018年4月23日00:28 | #4

    you are actually a good webmaster. The website loading speed is amazing.
    It kind of feels that you’re doing any distinctive trick.
    Furthermore, The contents are masterwork. you’ve performed a
    great process on this topic!

  5. 2018年4月23日00:40 | #5

    casino g
    best us casinos online
    new usa online casinos 2017
    casino online application

  6. 2018年4月23日00:52 | #6

    It’s actually a nice and useful piece of information. I am
    glad that you shared this helpful info with us. Please keep us informed like this.
    Thank you for sharing.

  7. 2018年4月23日01:16 | #7

    Hi there i am kavin, its my first time to commenting anyplace, when i read this post i
    thought i could also create comment due to this good paragraph.

  8. 2018年4月23日01:28 | #8

    It’s very simple to find out any matter
    on net as compared to textbooks, as I found
    this post at this website.

  9. 2018年4月23日01:49 | #9

    online casino slots
    vegas casino games
    vegas casino games
    casino online subtitrat

  10. 2018年4月23日02:07 | #10

    Hmm is anyone else having problems with the pictures on this blog loading?
    I’m trying to figure out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  11. 2018年4月23日02:17 | #11

    This is very true when fishing with very lengthy leaders.

  12. 2018年4月23日02:19 | #12

    I was very pleased to discover this page. I need to to thank
    you for ones time for this fantastic read!! I definitely liked every part of it
    and i also have you saved as a favorite to check out new
    stuff on your site.

  13. 2018年4月23日02:28 | #13

    I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you?

    Plz respond as I’m looking to construct my own blog and would like to find out where u got
    this from. thanks

  14. 2018年4月23日02:47 | #15

    I have been surfing online more than three hours today, yet I never
    found any fascinating article like yours. It is pretty worth
    enough for me. Personally, if all webmasters and bloggers made just right content as you did, the net will likely be much more helpful than ever before.

  15. 2018年4月23日02:50 | #16

    Hi, I read your new stuff like every week. Your humoristic style is witty, keep doing what you’re doing!

  16. 2018年4月23日03:03 | #17

    If you are going for most excellent contents like me, simply visit this site daily since it gives
    feature contents, thanks

  17. 2018年4月23日03:12 | #18

    online casino real money
    casino online gambling
    best us casinos online
    online casino

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

    Can you tell us more about this? I’d want to find out some additional information.

  19. 2018年4月23日03:33 | #20

    It’s actually a great and useful piece of information. I
    am happy that you shared this useful information with us. Please keep us informed like this.
    Thanks for sharing.

  20. 2018年4月23日03:41 | #21

    I think this is among the most important information for me.
    And i am glad reading your article. But should remark on few general things, The site style is perfect, the articles is really great :
    D. Good job, cheers

  21. 2018年4月23日03:46 | #22

    I really wanted to jot down a briief comment in order to thank you ffor all of the superb strategies you are sharing at this website.
    My time-consuming internet lookup has now been honored wkth brilliant ideas to share
    with my best friends. I would repeat that many of us
    website visitors are rather blessed to dwell in a
    notable site with many special indivviduals wih beneficial points.
    I feel rather happy to have discovered your weblog and look forward to
    some more awesome minutes reading here. Thank you once again for everything.

  22. 2018年4月23日03:48 | #23

    It’s an awesome piece of writing in favor of all the online viewers; they will get advantage from it I am
    sure.

  23. 2018年4月23日03:59 | #24
  24. 2018年4月23日04:01 | #25

    Claro! Disfunção erétil.

    Problemas de relacionamento. http://issyklife.kz/redirect?url=http://goodfield.com/dissecting-business-plan/

  25. 2018年4月23日04:30 | #26

    boursorama parrainage

  26. 2018年4月23日04:32 | #27

    I will immediately snatch your rss as I can not in finding your email subscription link or newsletter service.
    Do you have any? Please allow me understand in order that I may just subscribe.
    Thanks.

  27. 2018年4月23日04:58 | #28

    Wonderful beat ! I would like to apprentice while you amend
    your website, how can i subscribe for a blog site?
    The account helped me a acceptable deal. I had been a little
    bit acquainted of this your broadcast offered bright clear
    idea

  28. 2018年4月23日05:33 | #29

    I just could not go away your web site prior to suggesting that I actually enjoyed
    the usual information a person provide to your
    guests? Is gonna be back frequently in order to check up on new posts

  29. 2018年4月23日05:34 | #30

    It’s appropriate time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I wish to suggest you few
    interesting things or suggestions. Perhaps you could
    write next articles referring to this article. I want to read
    even more things about it!

  30. 2018年4月23日05:43 | #31

    I needed to thank you for this good read!! I absolutely loved every little bit of it.

    I have you book marked to checkk out new things you post…

  31. 2018年4月23日05:52 | #32

    Everyone loves what you guys are usually up too. This sort of clever work and coverage!
    Keep up the fantastic works guys I’ve incorporated you
    guys to my own blogroll.

  32. 2018年4月23日06:09 | #33

    Fantastic goods frolm you, man. I’ve be aware your stuff previous tto and
    you’re simply extremely magnificent. I really like what
    you have obtainrd right here, cetainly like whazt
    you’re saying and the bestt waay during which you assert it.

    You makje itt entertaining and you continue to take care
    of to stay it sensible. I cant wait to read far more
    from you. This is really a terrific web site.

评论分页
1 ... 61 62 63 371
订阅评论
3+5= (必填)