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

    Sweet 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!
    Many thanks

  2. 2018年5月5日13:13 | #2

    Azupw
    Vekltrw5Dsvvvwc2Lhgawfq3Nkyyyvk5Xtjtvot8

    google

  3. 2018年5月5日13:15 | #3

    Hi there, I enjoy reading through your post. I wanted to write a little comment to support you.

  4. 2018年5月5日13:19 | #4

    Hi there, I found your blog by the use of Google while
    searching for a comparable subject, your site
    came up, it seems to be good. I have bookmarked it
    in my google bookmarks.
    Hi there, simply changed into alert to your weblog thru Google,
    and located that it’s really informative. I
    am going to watch out for brussels. I’ll appreciate in case you continue this in future.
    Many people will probably be benefited from
    your writing. Cheers!

  5. 2018年5月5日13:19 | #5

    Does your blog have a contact page? I’m having trouble locating it but, I’d like to send
    you an e-mail. I’ve got some creative ideas for your blog you might
    be interested in hearing. Either way, great site and
    I look forward to seeing it expand over time.

  6. 2018年5月5日13:25 | #6

    bookmarked!!, I like your blog!

  7. 2018年5月5日13:31 | #7

    Everything is very open with a precise description of the
    issues. It was really informative. Your site is useful.
    Many thanks for sharing!

  8. 2018年5月5日13:34 | #8

    Hello, the whole thing is going sound here
    and ofcourse every one is sharing information, that’s in fact fine, keep up writing.

  9. 2018年5月5日13:36 | #9

    I am really pleased to read this weblog posts which consists of tons of valuable data,
    thanks for providing such statistics.

  10. 2018年5月5日13:51 | #10

    It’s an amazing article designed for all the online users; they will take benefit from it I am sure.

  11. 2018年5月5日13:59 | #11

    Have you ever considered creating an ebook or guest authoring on other websites?
    I have a blog based upon on the same information you discuss and would really like to have you share some stories/information. I know my readers would appreciate your work.
    If you’re even remotely interested, feel free to send me an e-mail.

  12. 2018年5月5日14:02 | #12

    Everythhing is very open with a precise description of the
    issues. It was definitely informative. Your website is useful.
    Many thanks for sharing!

  13. 2018年5月5日14:05 | #13

    Aw, this was a really nice post. Spending some time and actual effort to produce a very good article…
    but what can I say… I procrastinate a whole lot and don’t seem to get nearly anything done.

  14. 2018年5月5日14:14 | #14

    Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking for a plug-in like
    this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

  15. 2018年5月5日14:18 | #15

    I used to be suggested this blog by way of my cousin. I am
    now not sure whether or not this publish is
    written by him as no one else know such particular approximately my trouble.
    You’re incredible! Thanks!

  16. 2018年5月5日14:23 | #16

    Ӏf some one desires toο be updated with newest tecһnologies afterward һe must bbe pay a quick visit this site and bee uⲣ to date aⅼl the time. http://nbdaq.com/comment/html/?124202.html

  17. 2018年5月5日14:25 | #17

    Hmm it seems like your site ate my first comment (it was super long) so I guess I’ll
    just sum it up what I submitted and say, I’m thoroughly enjoying
    your blog. I too am an aspiring blog writer but I’m still new
    to everything. Do you have any tips and hints for inexperienced blog writers?
    I’d certainly appreciate it.

  18. 2018年5月5日14:26 | #18

    I am curious to find out what blog system you have been using?

    I’m experiencing some minor security problems with my latest site and I would like to find something more risk-free.
    Do you have any suggestions?

  19. 2018年5月5日14:34 | #19

    If you are going for finest contents like I
    do, just pay a quick visit this site everyday for the reason that
    it gives quality contents, thanks

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

    I think this is among the so much important info
    for me. And i am glad studying your article. However want to commentary
    on some normal issues, The website taste is wonderful, the articles is truly great : D.
    Just right process, cheers

  21. 2018年5月5日14:41 | #21

    whoah this weblog is fantastic i really like reading your articles.
    Keep up the great work! You recognize, lots of persons are searching round for this
    info, you could aid them greatly.

  22. 2018年5月5日14:44 | #22

    Most commonly, however, is hyperpigmentation which occurs on an portion of the body being a reaction to where
    the skin may be injured or traumatized by scrape,
    cut, crush, or burn. Citrus fruits including oranges and lemons
    can hinder producing melanin. It should be noted
    that Vitiligo doesn’t in its self cause any physical
    problems for example pain or discomfort.

  23. 2018年5月5日14:52 | #23

    Hello there! I know this is kind of off topic but I was
    wondering if you knew where I could locate a captcha plugin for my
    comment form? I’m using the same blog platform
    as yours and I’m having problems finding one?
    Thanks a lot!

  24. 2018年5月5日15:08 | #24

    My brother suggested I might like this website. He was tootally right.
    This ost actually made myy day. You cann’t imagine just how much time I
    had spent for this information! Thanks!

  25. 2018年5月5日15:15 | #25

    lloyds pharmacy online
    top rated online canadian pharmacies
    canadian pharmacy online

  26. 2018年5月5日15:37 | #26

    Hey! I’m at wоrk browsing your blog from my new apple iphone!
    Jսst ԝanted to say I love reading your blog and look forwаrԀ to
    all your рosts! Carry on the outstanding work! http://airvac.aagroup.com.sg/index.php/component/k2/itemlist/user/516949

  27. 2018年5月5日15:46 | #27

    I think the admin of this web page is truly working hard for his site, as here every
    material is quality based stuff.

  28. 2018年5月5日15:55 | #28

    Hi! This is kind of off topic but I need some help from an established blog.
    Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about setting up my own but I’m not sure where to begin. Do you
    have any ideas or suggestions? Thank you

  29. 2018年5月5日16:02 | #29

    Hey there! This is my 1st comment here sso I just wanted to gkve a quick shout
    oout and say I genuinely enjoy rewading through your blog
    posts. Can yoou suggest any other blogs/websites/forums that go over the same topics?
    Thank you!

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

    Hi! Someone in my Myspace group shared this site with us so I came to
    take a look. I’m definitely enjoying the information. I’m
    bookmarking and will be tweeting this to my followers!
    Excellent blog and superb style and design.

  31. 2018年5月5日16:09 | #31

    I wanted to thank you for this wonderful read!!
    I absolutely enjoyed every bit of it. I’ve got you bookmarked
    to check out new things you post…

  32. 2018年5月5日16:30 | #32

    Ι loved as muhh as you will rеceive carried oout right һerе.

    The sketch is tasteful, yⅼur authored material styⅼish.
    nonetheless, you command get got an edginess over that you wish bе delivering the following.
    unwell uunquestionably come more formerly agzin since
    exactly the same nearly very often inside case you shield this increase. http://www.222pass.com/zbxe/?document_srl=56731958&USER=Pieroweb&mid=m2u

  33. 2018年5月5日16:55 | #33

    Good рost.Ne’er knew this, tһank you for letting me know.

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

    WOW jսst what I ᴡas searching for. Ꮯame heгe by searchіng ffor situs poker
    tеrƅaiқ 2018 http://www.1-Olive.com/comment/html/?132560.html

  35. 2018年5月5日17:56 | #35

    What i don’t realize is in fact how you are now not actually much more
    smartly-appreciated than you might be now. You are so intelligent.
    You know thus significantly relating to this topic, produced me for my part consider it from a lot of varied angles.
    Its like women and men are not involved except it is something to do with Woman gaga!
    Your individual stuffs nice. Always take care of it up!

  36. 2018年5月5日18:06 | #36

    These are genuinely wonderful ideas in concerning blogging.
    You have touched some nice things here. Any way keep up wrinting.

  37. 2018年5月5日18:10 | #37

    Can use of vigrx plus enhance my penis size?

  38. 2018年5月5日18:26 | #38

    Great weblog here! Additionally your site so much up fast!
    What web host are you the use of? Can I get your associate
    hyperlink to your host? I want my site loaded up as fast as yours lol

  39. 2018年5月5日18:28 | #39

    Its not my first time to visit this web page, i am visiting this website dailly and get nice data from
    here daily.

  40. 2018年5月5日19:11 | #40

    Thankѕ a lot for sharing this with all of us you rеally recognise what you are speaking approximately!
    Bookmarked.Please aⅾditionally discuss ѡitһ myy wеbsite =).
    We could have a link changе aցreement betwеen ᥙs http://www.bonousa.com/?option=com_k2&view=itemlist&task=user&id=3725739

  41. 2018年5月5日19:15 | #42

    Hi there! I know this is kinda off topic but I was wondering if
    you knew where I could get a captcha plugin for my comment form?
    I’m using the same blog platform as yours
    and I’m having difficulty finding one? Thanks a lot!

  42. 2018年5月5日19:45 | #43

    Any gaming enthusiast will be able to recognize superior graphics and visuals when they see them.
    We’ll kick off with a short review and then we’ll get into some tips and cheats that might help you to get more from it.
    It provides users with precise, real time blackjack situational odds and analysis.

  43. 2018年5月5日19:47 | #44

    It’s impressive that you are getting ideas from this paragraph as well
    as from our argument made at this time.

  44. 2018年5月5日20:08 | #45

    Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is valuable and everything. Nevertheless imagine if you added
    some great visuals or videos to give your posts more, “pop”!
    Your content is excellent but with pics and clips,
    this blog could undeniably be one of the greatest in its field.
    Great blog!

  45. 2018年5月5日20:31 | #46

    It’s really a nice and helpful piece of information. I’m satisfied that
    you just shared this useful information with us. Please keep us informed like this.

    Thanks for sharing.

  46. 2018年5月5日20:31 | #47

    Like other clapping games, it plays with syllables.

  47. 2018年5月5日20:50 | #48

    Hurrah, that’s what I was looking for, what a information! present here at
    this web site, thanks admin of this website.

  48. 2018年5月5日21:00 | #49

    Can use of vigrx plus improve my penis measurement?

  49. 2018年5月5日21:00 | #50

    Greetings! Very helpful advice within this article!
    It’s the little changes that produce the most significant changes.
    Thanks for sharing!

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