首页 > 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月6日04:10 | #1

    This is really fascinating, You are an excessively professional
    blogger. I have joined your rss feed and stay up for in quest
    of extra of your fantastic post. Also, I have shared your web site in my social networks

  2. 2018年5月6日04:24 | #2

    I enjoy what you guys are up too. This type of clever work and coverage!
    Keep up the superb works guys I’ve added you guys
    to my personal blogroll.

  3. 2018年5月6日04:40 | #3

    I don’t eve know how I ended up here, but I thought
    this post was good. I doo not know who you are but certainly you’re going tto a famous blogger
    if you aren’t already ;) Cheers!

  4. 2018年5月6日04:41 | #4

    I’ve yet to seek out any comprehensive articles or evaluations thbat dig into the Fitbit
    vss Exerspy.

  5. 2018年5月6日04:58 | #5

    I know this web page offers quality depending articles or reviews and
    additional material, is there any other web page which provides
    such stuff in quality?

  6. 2018年5月6日05:04 | #6

    These are truly impressive ideas in about blogging.
    You have touched some fastidious points here. Any way keep up wrinting.

  7. 2018年5月6日05:05 | #7

    Нi there mates, pleasant paragrаph and nice arguments commented here, I am actually enjoying by these.

  8. 2018年5月6日05:12 | #8

    It’s an awesome post in favor of all the internet people; they will take advantage
    from it I am sure.

  9. 2018年5月6日05:23 | #9

    What’s up colleagues, how is everything, and what you would like to say concerning
    this article, in my view its really awesome in support of
    me.

  10. 2018年5月6日05:24 | #10

    I am not sure where you are getting your info, but great topic.

    I needs to spend some time learning much
    more or understanding more. Thanks for fantastic information I was looking for this info for my mission.

  11. 2018年5月6日05:24 | #11

    It’s in point of fact a great and useful piece of information. I am happy that you simply shared this useful information with us.
    Please keep us up to date like this. Thanks for sharing.

  12. 2018年5月6日05:32 | #12

    I was wondering if you ever considered changing the page layout of your website?
    Its very well written; I love what youve got to say. But maybe you could a little more
    in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having 1 or two pictures.
    Maybe you could space it out better?

  13. 2018年5月6日05:41 | #13

    An outstanding share! I have just forwarded this onto a coworker who had been doing
    a little homework on this. And he in fact ordered me dinner because I discovered it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending some time to talk about
    this matter here on your internet site.

  14. 2018年5月6日05:56 | #14

    When some one searches for his required thing, therefore he/she wants to be
    available that in detail, thus that thing is maintained over here.

  15. 2018年5月6日06:10 | #15

    Amazing! This blog looks exactly like my old one!
    It’s on a completely different topic but it has pretty much the same layout and design. Superb choice of
    colors!

  16. 2018年5月6日06:17 | #16

    Hello friends, its enormous paragraph about educationand completely
    explained, keep it up all the time.

  17. 2018年5月6日06:19 | #17

    I think the admin of this website is actually working hard in favor of
    his site, because here every data is quality based stuff.

  18. 2018年5月6日06:45 | #18

    Thank you for the auspicious writeup. It in reality was once a enjoyment account it.
    Glance complex to more introduced agreeable from you! However, how could we keep up
    a correspondence?

  19. 2018年5月6日06:53 | #19

    I love it when people come together and share
    views. Great blog, stick with it!

  20. 2018年5月6日07:28 | #20

    Hello my family member! I want to say that this post is
    awesome, great written and come with almost all important infos.
    I would like to peer extra posts like this .

  21. 2018年5月6日07:35 | #21

    Thank you, I’ve recently been looking for information about
    this subject for a while and yours is the greatest I have found out
    so far. But, what about the bottom line?
    Are you sure in regards to the source?

  22. 2018年5月6日07:38 | #22

    I constantly spent my half an hour to read this weblog’s posts
    all the time along with a cup of coffee.

  23. 2018年5月6日07:40 | #23

    Either they might choose it to be sold on a single sheet or this can be separated to each
    side coming out alone separate page. Really, it is not necessary an elegant
    new machine (that would wind up costing you
    a lot more). There are loads of things to think about so you might
    find it tough to make your final decision.

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

    I’m extremely impressed with your writing skills as well as with the layout on your
    weblog. Is this a paid theme or did you customize it yourself?
    Either way keep up the excellent quality writing, it is rare
    to see a great blog like this one these days.

  25. 2018年5月6日07:56 | #25

    What’sup to еvery ƅody, it’s my first visit of this webpage;
    this webpagye consists of amazing and acһtually good information in suрport of visitors. http://bonousa.com/?option=com_k2&view=itemlist&task=user&id=3727035

  26. 2018年5月6日08:07 | #26

    Can use of vigrx plus enhance my penis dimension?

  27. 2018年5月6日08:22 | #27

    I don’t even know how I ended up here, but I thought this
    post was good. I don’t know who you are but definitely you’re going to a famous
    blogger if you aren’t already ;) Cheers!

  28. 2018年5月6日08:28 | #28

    First off I want to say excellent blog! I had a quick question in which I’d like to ask if you don’t mind.
    I was interested to know how you center yourself and clear your mind
    prior to writing. I have had a difficult time clearing my mind in getting my thoughts out there.
    I do take pleasure in writing however it just seems like the first 10 to 15 minutes tend to be lost just
    trying to figure out how to begin. Any suggestions or
    hints? Many thanks!

  29. 2018年5月6日08:33 | #29

    My spouse and I stumbled over here by a different web address and thought I should
    check things out. I like what I see so now i’m following you.
    Look forward to checking out your web page again.

  30. 2018年5月6日08:35 | #30

    Hello There. I found your weblog the use of msn. That is an extremely
    neatly written article. I will be sure to bookmark it and
    return to read more of your useful information. Thanks for the
    post. I’ll definitely return.

  31. 2018年5月6日08:36 | #31

    It’s remarkable to go to see this web page and reading the views of all mates on the
    topic of this article, while I am also zealous of
    getting experience.

  32. 2018年5月6日09:07 | #32

    Have you ever thought about writing an ebook or guest
    authoring on other blogs? I have a blog based on the same subjects you discuss aand would love to have you share some stories/information. I know my audience would enjoy
    your work. If you are even rdmotely interested,
    feel free to shoot me an e mail.

  33. 2018年5月6日09:08 | #33

    Aw, this was a really nice post. Taking a few minutes and actual effort to
    make a top notch article… but what can I say… I procrastinate a whole lot and never seem to get nearly
    anything done.

  34. 2018年5月6日09:22 | #34

    I just couldn’t go away your web site before suggesting that I really
    loved the usual information a person supply in your
    guests? Is going to be again regularly to inspect new posts

  35. 2018年5月6日09:23 | #35

    Generally I don’t learn article on blogs, however I would like to say
    that this write-up very compelled me to check out and do so!
    Your writing taste has been amazed me. Thank you, very great post.

  36. 2018年5月6日09:27 | #36

    Tratamento para insuficiência sexual masculina. https://bbs.114good.cn/space-uid-58853.html?do=profile

  37. 2018年5月6日09:33 | #37

    Having read this I believed it was rather enlightening. I appreciate you spending some time and energy to put this informative article together.
    I once again find myself spending a lot of time both reading and posting comments.

    But so what, it was still worthwhile!

  38. 2018年5月6日09:34 | #38

    Great site you have here but I was wanting to know if you knew of any discussion boards that cover
    the same topics discussed here? I’d really like to be a part of online community where I can get feed-back
    from other experienced individuals that share the same interest.
    If you have any suggestions, please let me know. Bless you!

  39. 2018年5月6日09:35 | #39

    Hello There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more of your useful info.
    Thanks for the post. I will certainly return.

  40. 2018年5月6日09:57 | #40

    ich wirklijch Liebe Blog .. Groß Farbeen und Thema. Haben Sie Entwicklung Diese Webseite
    bewerten sich selbst? Bitrte antworten Sie mir zurück, als ich mich auf Planung bis erstellen meinem
    site unnd wollen lernen, wo Sie habe diese von oder genau das, was
    das Thema heißt. Danke!

  41. 2018年5月6日10:00 | #41

    These are truly wonderful ideas in about blogging.
    You have touched some good points here. Any way keep up wrinting.

  42. 2018年5月6日10:06 | #42

    With this Blackberry spy software you will not be left in the dark.
    They offer features like Two Wash Courses (Gentle & Normal Wash, twin water inlets,
    spin shower, two wash courses (Gentle and Normal wash), and wheels for easy mobility in some models.

    By reading their text messages, you can find if your child has a problem
    with drugs, anorexia, bulimia, alcoholism, or unwanted
    pregnancy.

  43. 2018年5月6日10:12 | #43

    Yes! Finally someone writes about personal branding
    communication.

  44. 2018年5月6日10:32 | #44

    Woᴡ! This can be оne particular of the most useful blogs We hɑve ever arrive aсross on this ѕubject.
    Aϲtualⅼy Magnificent. I am aⅼso an expert in this topic so I can understɑnd your effort.

  45. 2018年5月6日10:33 | #45

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

  46. 2018年5月6日10:34 | #46

    Pretty section of content. I just stumbled upon your website and in accession capital to assert
    that I get in fact enjoyed account your blog
    posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently fast.

  47. 2018年5月6日10:38 | #47

    Hi there, of course this paragraph is really
    fastidious and I have learned lot of things from it concerning
    blogging. thanks.

  48. 2018年5月6日10:38 | #48

    Your style is very unique compared to other people I have read stuff from.
    Many thanks for posting when you’ve got the opportunity, Guess I’ll
    just book mark this blog.

  49. 2018年5月6日10:44 | #49

    I think what you said was actually very logical.
    However, think about this, suppose you added a little information? I
    mean, I don’t want to tell you how to run your blog, however suppose you added something to possibly get a person’s attention? I mean Eric's linux and nginx!

    » python插件做nagios发报警邮件 is kinda vanilla.
    You might peek at Yahoo’s home page and see how they create news headlines to get people interested.
    You might add a video or a pic or two to get readers excited about everything’ve got to say.

    In my opinion, it would bring your posts a little bit more interesting.

  50. 2018年5月6日11:11 | #50

    Hi mates, pleasant post and nice arguments commented at this place, I am actually
    enjoying by these.

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