首页 > 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年6月13日16:37 | #1

    Wonderful blog! I found it while surfing around on Yahoo News.

    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Appreciate it

  2. 2018年6月13日16:54 | #2

    Excellent way of telling, and nice paragraph to get information regarding my presentation focus, which i am going
    to convey in school.

  3. 2018年6月13日16:59 | #3

    canadian online pharmacy
    canadian pharmacies online
    cvs online pharmacy

  4. 2018年6月13日17:16 | #4

    Thanks for your marvelous posting! I genuinely enjoyed
    reading it, you could be a great author. I will make sure to bookmark your blog and will eventually come back in the future.
    I want to encourage one to continue your great work, have
    a nice afternoon!

  5. 2018年6月13日17:29 | #5

    Wow, awesome blog structure! How lengthy have you ever been running a blog for?
    you made running a blog look easy. The total look of your web site
    is fantastic, as well as the content!

  6. 2018年6月13日17:51 | #6
  7. 2018年6月13日17:53 | #7

    Yes! Finally someone writes about Minecraft.

  8. 2018年6月13日18:04 | #8

    Heya i am for the first time here. I found
    this board and I find It really useful & it helped me out a lot.
    I hope to give something back and aid others like you aided me.

  9. 2018年6月13日18:11 | #9

    Excellent post. I definitely love this site. Stick with it!

  10. 2018年6月13日18:14 | #11

    Does your website have a contact page? I’m having trouble locating it but, I’d like to
    shoot you an email. I’ve got some suggestions for your blog you
    might be interested in hearing. Either way, great blog
    and I look forward to seeing it develop over time.

  11. 2018年6月13日18:22 | #12

    Normally I don’t learn post on blogs, but I wish to say that this write-up very pressured me to try and do so!
    Your writing style has been amazed me. Thank you, quite nice post.

  12. 2018年6月13日18:28 | #13

    Hello there, just became aware of your blog through Google, and found
    that it is really informative. I am going to watch out for brussels.

    I will be grateful if you continue this in future. A lot of people will be benefited from your
    writing. Cheers!

  13. 2018年6月13日18:32 | #14

    At this time it looks like Movable Type is the top blogging platform out
    there right now. (from what I’ve read) Is that what you’re using on your blog?

  14. 2018年6月13日18:36 | #15

    Appreciate this post. Let me try it out.

  15. 2018年6月13日18:42 | #16

    Its like you read my mind! You appear to know a lot about this,
    like you wrote the book in it or something. I think that you can do with a
    few pics to drive the message home a little bit,
    but other than that, this is wonderful blog. A fantastic read.
    I’ll certainly be back.

  16. 2018年6月13日18:58 | #17

    Howdy! Dօ yоu know if theʏ makе any plugins to assist with
    Search Engine Optimization? Ӏ’m trying to get my blog
    to rank for ѕome targeted keywords Ьut I’m not seеing very good success.
    Ӏf you know of any please share. Tһanks!

  17. 2018年6月13日19:03 | #18

    Hello there! This post could not be written any better! Reading
    through this post reminds me of my good old room mate! He always kept talking about this.
    I will forward this page to him. Fairly certain he will
    have a good read. Thanks for sharing!

  18. 2018年6月13日19:10 | #19

    If you wish for to improve your knowledge simply keep
    visiting this site and be updated with the newest news update posted here.

  19. 2018年6月13日19:15 | #20

    Appreciate this post. Let me try it out.

  20. 2018年6月13日19:21 | #21

    决战开区一条龙服务端www.119uc.com决战开区一条龙服务端www.119uc.com-客服咨询QQ1292124634(企鹅扣扣)-Email:1292124634@qq.com 烈焰私服架设www.119uc.com

  21. 2018年6月13日19:21 | #22

    This paragraph provides clear idea designed for the new visitors of blogging, that actually how to do running a blog.

  22. 2018年6月13日19:26 | #23

    You actually make it seem so easy with your presentation but
    I find this topic to be actually something which I think
    I would never understand. It seems too complicated and extremely broad for me.
    I’m looking forward for your next post, I will try to get the
    hang of it!

  23. 2018年6月13日19:33 | #24

    new usa online casinos
    casino online
    best online casino games free
    pogo casino slots

  24. 2018年6月13日19:38 | #25

    Good post. I am experiencing a few of these issues as well..

  25. 2018年6月13日19:56 | #26

    My brother suggested I may like this site. He was
    totally right. This post actually made my day.
    You can not consider just how so much time I had spent
    for this information! Thank you!

  26. 2018年6月13日20:02 | #27

    Nice blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your design. Cheers

  27. 2018年6月13日20:05 | #28

    free casino games and poker
    casino games roulette
    online casinos 2016
    new usa online casinos

  28. 2018年6月13日20:08 | #29

    Hi there, the whole thing is going nicely here and ofcourse every one is sharing information, that’s in fact good, keep up writing.

  29. 2018年6月13日20:17 | #30

    slot game
    kasino
    online casino gambling
    kasino
    online gambling casino

  30. 2018年6月13日20:39 | #31

    Hello, after reading this remarkable piece of writing i
    am also glad to share my knowledge here with friends.

  31. 2018年6月13日20:40 | #32

    Hi friends, good paragraph and nice urging commented here, I
    am truly enjoying by these.

  32. 2018年6月13日20:43 | #33

    It’s not my first time to visit this website, i
    am visiting this website dailly and take pleasant information from here daily.

  33. 2018年6月13日20:48 | #34

    Quality content is the secret to interest the users to visit the web
    site, that’s what this web site is providing.

  34. 2018年6月13日20:51 | #35

    Howdy very cool website!! Guy .. Excellent ..
    Superb .. I’ll bookmark your blog and take the feeds additionally?
    I’m glad to find a lot of helpful info right here in the submit, we want work out extra techniques in this regard, thank you for sharing.
    . . . . .

  35. 2018年6月13日20:53 | #36

    This design is incredible! You certainly know how to keep a reader amused.

    Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Fantastic job. I really enjoyed what you had to say, and more than that, how
    you presented it. Too cool!

  36. 2018年6月13日21:04 | #37

    I am really impressed with your writing skills
    and also with the layout on your weblog. Is this a paid theme or
    did you modify it yourself? Anyway keep up the excellent
    quality writing, it’s rare to see a nice blog like this one these days.

  37. 2018年6月13日21:13 | #38

    Eⲭcellent blog here! Also your site loads up very fast!
    What host ɑre ʏou using? Can Iget your associate ⅼink for your hoѕt?
    I desire my site loaded up as գuickly as yours lol.

  38. 2018年6月13日22:02 | #39

    Thanks to my father who shared with me regarding this website, this website is
    truly remarkable.

  39. 2018年6月13日22:09 | #40

    Hey there! I know this is somewhat off topic but I was wondering
    which blog platform are you using for this site?

    I’m getting sick and tired of WordPress because I’ve had problems with hackers
    and I’m looking at alternatives for another
    platform. I would be fantastic if you could point
    me in the direction of a good platform.

  40. 2018年6月13日22:19 | #41

    Link exchange is nothing else but it is simply placing the other person’s weblog link on your page at proper
    place and other person will also do same in favor
    of you.

  41. 2018年6月13日22:24 | #42

    top rated free online casino games
    casino online for fun
    casino games real money
    new usa online casinos 2017

  42. 2018年6月13日23:07 | #44

    Hi tһere Dear, are you genuinely visiting this website daily,
    if sⲟ after that you will definitely obtain fаstіdious experience.

  43. 2018年6月13日23:14 | #45

    online casino real money
    casino online free
    online casinos 2016
    casino games list

  44. 2018年6月13日23:38 | #46

    Hello just wanted to give you a quick heads up. The words in your content
    seem to be running off the screen in Firefox. I’m not sure
    if this is a formatting issue or something to do with web browser compatibility but
    I figured I’d post to let you know. The design and style
    look great though! Hope you get the problem fixed soon.
    Thanks

  45. 2018年6月13日23:52 | #47

    No matter if some one searches for his necessary thing, thus he/she wishes to be available that in detail, so that thing is maintained over here.

  46. 2018年6月13日23:53 | #48

    If some one wishes expert view about running a blog afterward i propose him/her to pay a quick
    visit this web site, Keep up the pleasant job.

  47. 2018年6月14日00:08 | #49

    hi!,I really like your ᴡriting so a lot!
    percentage we c᧐mmunicate extra approximately your post on AOL?
    I need an expert onn this area to resolve my problem.
    Mybe that’ѕ you! Looking aheaⅾ to peer you.

  48. 2018年6月14日00:11 | #50

    Great blog here! Also your website loads up fast! What web host are you
    using? Can I get your affiliate link to your host? I wish my site loaded up
    as fast as yours lol

评论分页
订阅评论
5+3= (必填)