首页 > 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月14日19:13 | #1

    Having read this I thought it was extremely enlightening.

    I appreciate you finding the time and energy to put this information together.
    I once again find myself personally spending a significant amount of time both reading and posting comments.
    But so what, it was still worth it!

  2. 2018年6月14日19:14 | #2

    Your means of describing everything in this piece of writing is truly pleasant, every one
    be capable of simply be aware of it, Thanks a lot.

  3. 2018年6月14日19:23 | #3

    Exceptional post Ƅut I was wanting to know
    iff you cοuld ѡrite a litte mогe on this subject?
    І’d ƅе very grateful if yoou c᧐uld elaborate a little Ƅіt furtһeг.
    Appreϲiate it!

  4. 2018年6月14日19:45 | #4

    Whats up this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors
    or if you have to manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  5. 2018年6月14日20:39 | #5

    My brother suggested I would possibly like this website.
    He used to be entirely right. This post truly made my day.
    You cann’t believe just how much time I had spent for this info!
    Thanks!

  6. 2018年6月14日20:48 | #6

    After looking over a few of the articles on your blog, I really appreciate your way of writing a blog.
    I book marked it to my bookmark website list and will be checking back soon. Take
    a look at my web site too and tell me what you think.

  7. 2018年6月14日21:11 | #7

    It’s actually very complex in this busy life to listen news on TV,
    so I simply use web for that purpose, and obtain the
    most recent news.

  8. 2018年6月14日21:16 | #8

    My developer is trying to convince me to move to .net from PHP.

    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on various websites for about a year and am worried about switching to
    another platform. I have heard good things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?
    Any kind of help would be really appreciated!

  9. 2018年6月14日21:25 | #9

    I have read so many content about the blogger lovers however this article is truly a nice post, keep
    it up.

  10. 2018年6月14日21:35 | #10

    Wonderful site. Plenty of helpful information here. I am sending it to some buddies ans additionally sharing in delicious.
    And of course, thank you on your effort!

  11. 2018年6月14日21:40 | #11

    My brother recommended I would possibly like this web site.
    He was totally right. This publish actually made my day.

    You cann’t believe simply how much time I had spent for this info!
    Thank you!

  12. 2018年6月14日22:09 | #12

    viagra without a doctor prescription
    viagra without a doctor prescription
    viagra without a doctor prescription
    viagra without a doctor prescription
    viagra without a doctor prescription

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

    I have learn a few good stuff here. Certainly worth bookmarking for revisiting.
    I surprise how much attempt you place to make one of these magnificent informative site.

  14. 2018年6月14日22:24 | #14

    Howdy! Do you know if they make any plugins to help with
    SEO? I’m trying to get my blog to rank for some targeted keywords but
    I’m not seeing very good success. If you know of any please share.
    Thank you!

  15. 2018年6月14日22:59 | #15

    Outstanding post however I was wanting to know if you could write a litte more on this subject?
    I’d be very grateful if you could elaborate a little bit more.
    Appreciate it!

  16. 2018年6月14日23:40 | #16

    You ought to take part in a contest for one of the finest blogs online.

    I most certainly will highly recommend this site!

  17. 2018年6月14日23:40 | #17

    May I simply just say what a relief to find an individual who truly knows
    what they are talking about on the web. You definitely realize how to bring
    an issue to light and make it important.
    More and more people have to look at this and understand this side
    of your story. I was surprised you’re not more popular because you most certainly have
    the gift.

  18. 2018年6月14日23:48 | #18

    Single woman searching for sex chat 917-776-5652.

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

    І have Ƅeen surfing online mоre than three houгs today, Ьut I neveг found any іnteresting article likе yօurs.
    It’s lovely worth sufficient fߋr me. In my opinion, if all webmasters and bloggers mаde
    excellent content as yоu Ԁid, the internet ԝill be a
    lߋt more uѕeful tһan ever beforе.

  20. 2018年6月15日00:34 | #20

    If you are going for most excellent contents like I do, only pay a visit this web page everyday for the reason that it presents feature contents, thanks

  21. 2018年6月15日00:38 | #21

    What’s up friendѕ, how is the whole tһing, and wһat you desire to
    say regarding thiѕ paragraph, in my view іts truly aѡesome
    in favor of me.

  22. 2018年6月15日00:59 | #22

    you get access for the materials now Exam date renowned keep a file with account numbers online exam will be addressed if so passing microsoft contact what you
    will college application viruses can do much severe i would like to speak to a doctor online

  23. 2018年6月15日01:04 | #23

    Thanks designed for sharing such a nice idea, article is
    nice, thats why i have read it completely

  24. 2018年6月15日01:19 | #24

    Be both a helpful guide through complex issues and an informed judge when choices should be made.
    Each format pressupposes a particular formation plus design for citing rephrased and echoed resources in favor of all various printed, internet,
    as well as other types of resources. Remember that should you
    be new at college you’ll only improve in case you practice, so give
    your very best on every single assignment as you may be improving your academic
    writing skills with each one.

  25. 2018年6月15日01:47 | #25

    My spouse and I stumbled over here coming from a different website and thought I may as well check things
    out. I like what I see so now i am following you. Look forward to looking at your web page for a second time.

  26. 2018年6月15日02:00 | #26

    Wow that was strange. I just wrote an incredibly long comment but
    after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Regardless,
    just wanted to say fantastic blog!

  27. 2018年6月15日02:11 | #27

    Hi there! I simply wish to offer you a huge thumbs up
    for your great info you have right here on this
    post. I am returning to your website for more soon.

  28. 2018年6月15日02:18 | #28

    If you might be inclined on concepts of life aand deasth then you can find different designs offered bby tattoo
    galleries. Ouija Boards hace been about forever, yeet they’re still being confused for a few a sort oof portala communication devise
    that alowss us to taalk to our passed household or spirits we don. In this Shahrukh
    Khan hass played role just as thhe one playewd in Super Hero.

  29. 2018年6月15日02:23 | #29

    casino slots free
    four winds casino
    casinos in iowa
    casino slot

  30. 2018年6月15日02:29 | #30

    Outstanding post, I believe people should larn a lot from this site its very user genial.
    So much fantastic info on here :D .

  31. 2018年6月15日02:56 | #31

    I don’t even understand how I ended up right here, however I believed this publish was once
    good. I don’t know who you’re but definitely you’re going to a well-known blogger when you
    are not already. Cheers!

  32. 2018年6月15日03:04 | #32

    wh0cd243872 [url=http://suhagra.us.com/]cheap suhagra[/url]

  33. 2018年6月15日03:09 | #33

    I all the time emailed this web site post page to all my associates,
    as if like to read it next my contacts will too.

  34. 2018年6月15日03:10 | #34

    Hi! I knoԝ this iѕ kind of off topic but Iwas wondering wһich blog ⲣlɑtform are you using foг this site?
    I’m getting tired off Worԁpress becauѕe I’ve haԀ issues with hackers and I’m loⲟking
    at alternatives for another platform. I would bee awesome if you could point me in the Ԁirection of a good
    рlatform.

  35. 2018年6月15日03:26 | #35

    Wow, marvelous blog layout! How lengthy have you been running
    a blog for? you made blogging glance easy. The whole glance of your website is
    magnificent, let alone the content!

  36. 2018年6月15日03:51 | #36

    Hello, Neat post. There is an issue along with your website in web explorer, would check this?
    IE nonetheless is the marketplace chief and a large element
    of people will omit your fantastic writing due to this problem.

  37. 2018年6月15日03:58 | #37

    Research has proven this before supplementation approach to be more effective
    than taking BCAAs only after having a workout. The USA won their match against Spain by 2-0 and took to try out
    the final and I must point out that by that game; I had trained my ear and also
    my mind to accept the sound of the Vuvuzela. The concept is
    just one of a minimization of human interference using the processes of nature.

  38. 2018年6月15日04:08 | #38

    Hello mates, its enormous article about cultureand entirely defined, keep it up all
    the time.

  39. 2018年6月15日04:09 | #39

    If some one needs to be updated with newest technologies after that he must be visit this site and be up to date everyday.

  40. 2018年6月15日04:33 | #40

    Nice post. I used to be checking continuously this webllog and I am inspired!
    Extremely usefcul info specifically the ultimate part :) I deal with such
    information a lot. I used to be seeking this certain info for a long time.
    Thanks and good luck.

  41. 2018年6月15日04:34 | #41

    It’s an amazing paragraph in support of all the internet visitors; they will get advantage from it
    I am sure.

  42. 2018年6月15日04:49 | #42

    I was recommended this website via my cousin. I’m now not
    certain whether or not this put up is written through him as nobody else recognize such particular approximately my problem.

    You are amazing! Thanks!

  43. 2018年6月15日04:51 | #43

    Greetings from Idaho! I’m bored to death at work so I decided to browse your website
    on my iphone during lunch break. I enjoy the information you provide here and can’t
    wait to take a look when I get home. I’m surprised at how fast your blog loaded on my mobile ..

    I’m not even using WIFI, just 3G .. Anyways, great site!

  44. 2018年6月15日05:10 | #44

    wh0cd243872 [url=http://suhagra.us.com/]Suhagra Online[/url]

  45. 2018年6月15日05:18 | #45

    Wow, superb blog layout! How long have you been blogging
    for? you made blogging look easy. The overall look of your website is fantastic, as well
    as the content!

  46. 2018年6月15日05:24 | #46

    Hi, I do believe this is an excellent site. I stumbledupon it ;) I’m going to return yet again since I bookmarked it.

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

  47. 2018年6月15日05:42 | #47

    wh0cd243872 [url=http://buylasix.us.com/]lasix by mail[/url]

  48. 2018年6月15日05:54 | #48

    slot online
    casino games
    casino games slots
    kasino
    online slots

  49. 2018年6月15日06:03 | #49

    magnificent put up, very informative. I wonder why
    the opposite experts of this sector don’t notice this.

    You should proceed your writing. I’m sure, you’ve a huge readers’ base
    already!

  50. 2018年6月15日06:19 | #50

    Wow, wonderful weblog layout! How lengthy have you been blogging for?
    you make running a blog glance easy. The total glance of your website is fantastic, let alone the content!

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