首页 > 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日21:01 | #1

    Undeniably believe that which you saiԁ. Your favorite justification appeared to bе on the net
    tthe ѕimplеst thing to be mindful ⲟf. I say tο you, I dеfinitely
    get irked even as folks consideг issueѕ that thesy just don’t гealize about.
    You managed to hhit the naіl ulon tһe highest as
    smartly as defined out the whole thing without having side effect ,
    folks could taқe a signal. Will likely be back to get more.
    Thank you http://www.1ru.it/?option=com_k2&view=itemlist&task=user&id=1078594

  2. 2018年5月5日21:03 | #2

    Pduiu
    Sqjatog2Joykriz4Iifgprh6Fcbqhio6Rfcsltg6

    google

  3. 2018年5月5日21:07 | #3

    Εхcеllent post. I used to ЬƄe checking continuously his ԝeblog
    and I aam inspired! Extremely helpful info specifically the ultimate section :) I
    handⅼe suϲh info a lot. I was seeking this certain infօ for a very long time.
    Thankѕ and best of lucҝ.

  4. 2018年5月5日21:18 | #4

    This is Boeing’s contribution to providing an ‘innovative, secure and flexible mobile
    solution,’ according to a Boeing spokesperson. For example, you maybe
    the boss of a company and suspect your employee is abusing his
    or her phone privileges. The top element may be the mobile spy software program performs on all phones which includes
    i – Phone, android, and blackberry.

  5. 2018年5月5日21:26 | #5

    This is the right site for everyone who really wants to find out about this topic.
    You know 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’s been discussed for a long time.
    Wonderful stuff, just great!

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

    After I initially commented I appear to have clicked the -Notify
    me when new comments are added- checkbox
    and now each time a comment is added I recieve four emails with the exact same comment.
    There has to be a means you can remove me from that service?
    Appreciate it!

  7. 2018年5月5日21:37 | #7

    Actually film is the display of some one’s feelings; it offers the lesson to the users.

  8. 2018年5月5日21:55 | #8

    Outstanding post ƅut І was wondering if you coսld wrіte
    a litte m᧐re on this subject? I’d bee very grateful if you coսld elaboate a lіttle
    bit morе. Thankѕ!

  9. 2018年5月5日21:59 | #9

    Wow! After all I got a weblog from where I be able
    to truly get helpful data concerning my study and knowledge.

  10. 2018年5月5日22:06 | #10

    Hello there! Quick question that’s entirely off topic.

    Do you know how to make your site mobile friendly?
    My weblog looks weird when viewing from my iphone. I’m trying to find a template
    or plugin that might be able to correct this issue.
    If you have any suggestions, please share. Appreciate it!

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

    Hi there, I would like to subscribe for this blog to get latest
    updates, therefore where can i do it please help out.

  12. 2018年5月5日23:10 | #12

    Amazing video, really a nice quality, this YouTube video touched me a lot in terms of quality.

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

    online pharmacies
    canadian pharmacies online prescriptions
    reputable canadian online pharmacies

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

    I was studying some of your content on this site and I believe this web site is
    very instructive! Continue putting up.

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

    Hmm is anyone else experiencing problems with the images on this blog loading?
    I’m trying to figure out if its a problem on my
    end or if it’s the blog. Any suggestions would be greatly appreciated.

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

    whoah this weblog is fantastic i really like reading your posts.
    Keep up the great work! You already know,
    many persons are searching around for this info, you could
    help them greatly.

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

    When someone writes an piece of writing he/she retains the image of a user in his/her mind that how a user can understand it.
    So that’s why this article is great. Thanks!

  18. 2018年5月5日23:59 | #18

    After looking over a few of the articles on your site,
    I seriously appreciate your technique of writing a blog.
    I saved as a favorite it to my bookmark webpage list and will be checking back in the near future.
    Take a look at my website as well and let me know how you feel.

  19. 2018年5月6日00:05 | #19

    I know this web page offers quality dependent articles or reviews and extra
    stuff, is there any other web page which offers these data in quality?

  20. 2018年5月6日00:06 | #20

    That is really attention-grabbing, You are an overly skilled blogger.

    I’ve joined your feed and sit up for in search of extra of your great post.
    Also, I’ve shared your site in my social networks

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

    Very shortly this website will be famous among all blogging viewers, due to it’s nice
    posts

  22. 2018年5月6日00:12 | #22

    Can use of vigrx plus enhance my penis size?

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

    Greetings I am so excited I found your webpage, I really
    found you by error, while I was browsing on Bing for something else, Anyways I am here now and would just like to say many thanks for a marvelous post and a all round thrilling blog (I also love the theme/design), I don’t have time
    to read through it all at the moment but I have bookmarked it and also
    added in your RSS feeds, so when I have time I will be back to read
    a great deal more, Please do keep up the fantastic
    work.

  24. 2018年5月6日00:16 | #24

    I do not even understand how I ended up right here, but I assumed this publish used to be good.
    I do not understand who you’re however definitely you’re going to a
    famous blogger in case you aren’t already. Cheers!

  25. 2018年5月6日00:16 | #25

    Hello, I enjoy reading through your article.
    I like to write a little comment to support you.

  26. 2018年5月6日00:27 | #26

    What’s up to all, because I am really keen of reading this website’s post
    to be updated regularly. It includes pleasant data.

  27. 2018年5月6日00:28 | #27

    I’m extremely impressed together with your writing talents
    and also with the layout for your weblog. Is this a paid subject matter or did you modify
    it yourself? Anyway keep up the excellent high quality writing,
    it is rare to see a great weblog like this one today..

  28. 2018年5月6日00:31 | #28

    Hey there! This post could not be written any better! Reading this post reminds me of my previous room
    mate! He always kept talking about this. I will forward this write-up
    to him. Fairly certain he will have a good read. Many thanks for sharing!

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

    After exploring a handful of the blog posts on your web
    page, I seriously like your technique of blogging. I book-marked it
    to my bookmark site list and will be checking back soon. Take a look at my web site as well
    and tell me your opinion.

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

    I like the valuable information you provide in your articles.
    I’ll bookmark your blog and check again here frequently.
    I am quite certain I’ll learn lots of new stuff right here!
    Best of luck for the next!

  31. 2018年5月6日00:37 | #31

    Ꭲһankѕ for finally writіng aabout >Eгic’s linux аnd nginx!

    > python插件做nagios发报警邮件 <Liked it! http://Www.lidun9hao.com/comment/html/?99556.html

  32. 2018年5月6日00:59 | #32

    Hey there! Quick question that’s entirely off topic.

    Do you know how to make your site mobile friendly?
    My weblog looks weird when browsing from my iphone
    4. I’m trying to find a template or plugin that might be able to fix this issue.
    If you have any suggestions, please share. Thanks!

  33. 2018年5月6日01:01 | #33

    Keep on writing, great job!

  34. 2018年5月6日01:07 | #34

    magnificent points altogether, you simply won a new reader.
    What would you suggest about your post that you just made a few days ago?
    Any certain?

  35. 2018年5月6日01:12 | #35

    Ich habe gewesen Surfen Online mehhr alls heute, abdr I keineswegs
    gefunden aufmerksamkeitsstarken Artikel wiie Ihres.
    Es ist pretty Wert genug asreichend für mich. Meinmer Meinung, wenn alle Web-Besitzer und Bloggerfn aus
    gute Inhalt Material wie Sie wahrscheinlich tat, diie Web wird
    wahrscheinlich mehr hilfreich als je zuvor.

  36. 2018年5月6日01:21 | #36

    When someone writes an article he/she retains the plan of a user in his/her brain that how a user can be
    aware of it. Therefore that’s why this paragraph is amazing.
    Thanks!

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

    Hey! I’m at work browsing your blog from my new
    iphone 3gs! Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the great work!

  38. 2018年5月6日01:40 | #38

    I’m not sure why but this web site is loading incredibly 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.

  39. 2018年5月6日01:46 | #39

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

  40. 2018年5月6日01:54 | #40

    I read this article completely on the topic
    of the difference of hottest and previous technologies, it’s remarkable article.

  41. 2018年5月6日02:01 | #41

    I have read so many posts regarding the blogger lovers but this piece of writing is really a
    pleasant piece of writing, keep it up.

  42. 2018年5月6日02:21 | #42

    Mаgnificent beat !I wish to apprentice whilst you amend
    yοur website, how could i subscribe for a ᴡeblog web site?

    The account helped me a acceptable deal. I had been a little bit familiar of this your broadcast
    provіded shiny clear ⅽoncept.

  43. 2018年5月6日02:21 | #43

    Ӏt’s going to be finish of mine day, but befⲟre finish Ӏ am reаding this
    enormous paragraph to increase my know-һow. http://Uaeclassifieds.com/author/hestershell/

  44. 2018年5月6日02:33 | #44

    I like the helpful info you supply to your articles.
    I will bookmark your weblog and check once more right here frequently.

    I’m moderately sure I’ll learn many new stuff right right here!
    Best of luck for the following!

  45. 2018年5月6日02:35 | #45

    I’m gone to convey my little brother, that he should also go to see this webpage on regular basis to take updated from
    latest news.

  46. 2018年5月6日02:50 | #46

    Very descriptive post, I liked that bit. Will there be a part 2?

  47. 2018年5月6日02:54 | #47

    I pay a quick visit daily some web pages and blogs to read articles or reviews,
    except this web site offers feature based articles.

  48. 2018年5月6日03:09 | #48

    Hi to every body, it’s my first pay a quick visit of this website; this web site
    includes awesome and truly excellent stuff for visitors.

  49. 2018年5月6日03:47 | #49

    Ι’d liie to find out more? I’d care to find outt moee details.

  50. 2018年5月6日03:55 | #50

    Hi! I simply want to offer you a huge thumbs up for the
    great info you have got right here on this post. I’ll
    be coming back to your web site for more soon.

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