首页 > 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年5月16日22:15 | #1

    Nice blog right here! Additionally your web site a lot up very fast!
    What web host are you the use of? Can I am getting your affiliate link for your host?
    I want my site loaded up as quickly as yours lol

  2. 2018年5月16日22:18 | #2

    My brother recommended I would possibly like this website.
    He was totally right. This publish actually made my day. You can not imagine just how a lot time I had spent for this info!
    Thanks!

  3. 2018年5月16日22:44 | #3

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

  4. 2018年5月16日22:57 | #4

    At this time I am going away to do my breakfast, afterward
    having my breakfast coming yet again to read additional news.

  5. 2018年5月16日22:58 | #5

    1) White foods contain quercetin and allicin – maintains healthy bones, circulatory health, and supports arterial
    function. This might be referred to as “watering and oiling” in when you brush your teeth
    and wash see your face at night, then water
    and oil your hair. Yes, biotin is definitely an actual vitamin which has been discovered in the 1940s by scientists.

  6. 2018年5月16日23:10 | #6

    Stronger sperm in higher quantities raises the odds that pregnancy will occur.
    The USA won their match against Spain by 2-0 and continued to play the last and I must claim that by that game; I had trained my ear
    in addition to my thoughts to just accept the sound in the Vuvuzela.
    One with the most beautiful and popular places will be the Garden Route, this is the
    200km stretch of spectacular coastline that hosts an unbelievable selection of natural habitats.

  7. 2018年5月16日23:11 | #7

    I’m not sure exactly why but this blog is loading extremely slow
    for me. Is anyone else having this issue or is it a problem on my end?
    I’ll check back later on and see if the problem still exists.

  8. 2018年5月16日23:35 | #8

    Do you mind if I quote a couple of your articles as long as I
    provide credit and sources back to your blog? My website is in the exact same niche as yours
    and my users would really benefit from a lot of the information you provide here.

    Please let me know if this ok with you. Thanks a lot!

  9. 2018年5月17日00:08 | #9

    Maintenance is vitally vital to correct garden care.

  10. 2018年5月17日00:12 | #10

    You really make it seem so easy with your presentation but I find this
    topic to be really something that I think I
    would never understand. It seems too complicated and extremely broad for me.

    I’m looking forward for your next post, I’ll try to get the hang of it!

  11. 2018年5月17日00:42 | #11

    Galaxy S9 ve S9+|işletmek için gerçekleştirin.

  12. 2018年5月17日00:57 | #12

    Right here is the right web site for everyone
    who wants to understand this topic. You understand so much its almost hard to argue with you (not
    that I personally will need to…HaHa). You certainly put a fresh spin on a topic that has been written about for years.
    Great stuff, just excellent!

  13. 2018年5月17日01:37 | #13

    Wow that was unusual. I just wrote an extremely long
    comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Anyway,
    just wanted to say superb blog!

  14. 2018年5月17日02:07 | #14

    Woah! I’m really digging the template/theme of this website.

    It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and visual appeal.
    I must say you have done a excellent job with this.
    Also, the blog loads very quick for me on Chrome. Outstanding Blog!

  15. 2018年5月17日02:39 | #15

    Right here is the perfect website for anyone who wishes to find out about this topic.
    You realize so much its almost hard to argue with
    you (not that I really would want to…HaHa). You certainly put a fresh
    spin on a topic that has been written about for years. Wonderful stuff, just wonderful!

  16. 2018年5月17日03:03 | #16

    Many individuals may benefit from your writing. Cheers!

  17. 2018年5月17日03:11 | #17

    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? Either way keep up the nice quality writing, it’s rare to see a
    nice blog like this one these days.

  18. 2018年5月17日03:11 | #18

    This website was… how do I say it? Relevant!! Finally I have found something which helped me.
    Many thanks!

  19. 2018年5月17日03:14 | #19

    Hi there, just became aware of your blog through Google, and found that it is really
    informative. I’m gonna watch out for brussels.
    I’ll be grateful if you continue this in future.
    Lots of people will be benefited from your writing.

    Cheers!

  20. 2018年5月17日03:22 | #20

    I am regular visitor, how are you everybody?
    This paragraph posted at this website is genuinely nice.

  21. 2018年5月17日03:23 | #21

    Thanks for sharing your thoughts about https://atk.by/. Regards

  22. 2018年5月17日03:47 | #22

    Great post. I am going through many of these issues as well..

  23. 2018年5月17日03:50 | #23

    Hi, I do think this is an excellent website. I stumbledupon it
    ;) I may return once again since i have bookmarked it.

    Money and freedom is the best way to change,
    may you be rich and continue to guide others.

  24. 2018年5月17日03:51 | #24

    This design is steller! You obviously 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!) Wonderful job.
    I really loved what you had to say, and more than that, how you presented
    it. Too cool!

  25. 2018年5月17日04:12 | #25

    Remarkable! Its actually amazing paragraph, I have got
    much clear idea on the topic of from this piece of writing.

  26. 2018年5月17日04:13 | #26

    In fact when someone doesn’t know after that its up to other visitors that they will help,
    so here it takes place.

  27. 2018年5月17日04:18 | #27

    If you desire to get much from this piece of writing then you have
    to apply such methods to your won blog.

  28. 2018年5月17日04:31 | #28

    Great beat ! I wish to apprentice while you amend your website, how
    can i subscribe for a blog site? The account aided
    me a acceptable deal. I had been tiny bit acquainted
    of this your broadcast provided bright clear idea

  29. 2018年5月17日04:34 | #29

    What i don’t understood is actually how you’re not really a lot more smartly-preferred than you may
    be right now. You are very intelligent. You realize therefore
    considerably in relation to this subject, made me for my part consider it from a lot of numerous angles.
    Its like men and women aren’t fascinated except it is one thing to accomplish with Girl gaga!

    Your personal stuffs outstanding. At all times handle it up!

  30. 2018年5月17日04:46 | #30

    Hi! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up
    losing a few months of hard work due to no data backup.
    Do you have any solutions to prevent hackers?

  31. 2018年5月17日05:10 | #31

    Having read this I believed it was rather informative.
    I appreciate you spending some time annd energy to put this
    informatrion together. I once again find myself spending a significant amount
    of time both reading and leaving comments. But so what, it was still worth it!

  32. 2018年5月17日05:22 | #32

    I don’t even know how I stopped up here, however I thought this put up was good.
    I don’t know who you might be but definitely you’re going to a well-known blogger if you happen to aren’t already.
    Cheers!

  33. 2018年5月17日05:25 | #33

    Please let me know if you’re looking for a article author for your weblog.
    You have some really great articles and I think I would be a good asset.

    If you ever want to take some of the load off, I’d love to write some articles
    for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Regards!

  34. 2018年5月17日05:34 | #34

    Please let me know if you’re looking for a author
    for your site. You have some really good articles and I believe I would
    be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some content
    for your blog in exchange for a link back to mine. Please shoot me an email if
    interested. Thank you!

  35. 2018年5月17日05:40 | #35

    Link exchange is nothing else but iit is just placing
    tthe other person’s weblog link on your page at
    proper place and other person wipl also do similar in favopr of you.

  36. 2018年5月17日05:40 | #36

    My partner and I stumbled over here different web page
    and thought I might check things out. I like what I see so i am just following
    you. Look forward to looking into your web page again.

  37. 2018年5月17日05:45 | #37

    It’s amazing to go to see this site and reading the views of all
    friends concerning this paragraph, while I am also eager of getting knowledge.

  38. 2018年5月17日06:04 | #38

    This post gives clear idea for the new users of blogging,
    that in fact how to do blogging and site-building.

  39. 2018年5月17日06:14 | #39

    I truly treasure your work. Excellent post.

  40. 2018年5月17日06:35 | #40

    Is your plant-loving pal an avid vegetable gardener?

  41. 2018年5月17日06:35 | #41

    I was suggested this web site by my cousin. I am not sure whether
    this post is written by him as no one else know such detailed about my difficulty.
    You are wonderful! Thanks!

  42. 2018年5月17日07:16 | #42

    Woah! I’m really loving the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between user friendliness and visual appearance.
    I must say that you’ve done a awesome job with this.

    Also, the blog loads extremely quick for me on Chrome.
    Superb Blog!

  43. 2018年5月17日07:20 | #43

    If some one wants to be updated with most recent technologies then he must be pay a visit this website and
    be up to date daily.

  44. 2018年5月17日07:24 | #44

    whoah this blog is magnificent i like studying your articles.
    Keep up the great work! You know, many persons are searching round for this info, you could aid them greatly.

  45. 2018年5月17日07:25 | #45

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

  46. 2018年5月17日07:35 | #46

    Some genuinely nice stuff on this web site, I enjoy it.

  47. 2018年5月17日07:36 | #47

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

  48. 2018年5月17日07:41 | #48

    Normally I do not learn post on blogs, however I wish to say
    that this write-up very pressured me to try and do it! Your
    writing taste has been amazed me. Thanks, quite great post.

  49. 2018年5月17日07:42 | #49

    You made some good points there. I did research online on the subject and found
    most people will agree with your blog.

  50. 2018年5月17日07:54 | #50

    I must thank you for the efforts you’ve put in penning this
    website. I am hoping to see the same high-grade content from
    you in the future as well. In truth, your creative writing abilities has motivated me to get my very own website now ;)

评论分页
订阅评论
2+8= (必填)