首页 > 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年1月12日04:24 | #1

    I do not know whether it’s just me or if perhaps everyone else experiencing
    issues with your blog. It looks like some of the written text in your posts are running off the screen. Can someone else please provide feedback and let me know if this is happening to them
    as well? This may be a issue with my web browser because I’ve had this happen before.
    Many thanks

  2. 2018年1月12日04:36 | #2

    Undeniably believe that which you said. Your favorite reason appeared to be on the web the simplest thing to be aware of.

    I say to you, I definitely get irked while people think about worries that they just don’t know about.
    You managed to hit the nail upon the top as well as defined out the whole thing without
    having side effect , people can take a signal. Will probably be
    back to get more. Thanks

  3. 2018年1月12日04:44 | #3

    I every time emailed this webpage post page to all my
    associates, for the reason that if like to read it
    next my friends will too.

  4. 2018年1月12日04:52 | #4

    Very good information. Lucky me I found your website by chance (stumbleupon).
    I’ve book marked it for later!

  5. 2018年1月12日05:08 | #5

    I’m truly enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more enjoyable for
    me to come here and visit more often. Did you hire out a designer to create your theme?

    Exceptional work!

  6. 2018年1月12日05:30 | #6

    It’s a pity you don’t have a donate button! I’d certainly
    donate to this brilliant blog! I suppose for
    now i’ll settle for book-marking and adding your RSS feed to my
    Google account. I look forward to new updates and will share
    this website with my Facebook group. Talk soon!

  7. 2018年1月12日05:44 | #7

    Hi there to every one, as I am truly eager of reading
    this webpage’s post to be updated regularly.

    It carries pleasant data.

  8. 2018年1月12日06:01 | #8

    What’s Going down i am new to this, I stumbled upon this I’ve discovered It absolutely helpful and it
    has helped me out loads. I’m hoping to give a contribution & assist other customers like its helped
    me. Good job.

  9. 2018年1月12日06:09 | #9

    What’s up, its fastidious paragraph about media print, we all be aware of media
    is a fantastic source of information.

  10. 2018年1月12日06:18 | #10

    Hello there, I discovered your website via Google even as searching for a similar matter,
    your site came up, it seems to be great. I’ve bookmarked
    it in my google bookmarks.
    Hi there, just become alert to your blog via Google,
    and found that it’s truly informative. I’m going to be careful for brussels.
    I’ll be grateful in case you proceed this in future.

    A lot of other folks will likely be benefited from your writing.
    Cheers!

  11. 2018年1月12日06:25 | #11

    In faⅽt, a porn blocker сould Ьe a nice ɑnd decemt waay tto let уou knoᴡ ѡhat yoᥙr youngster ϲomes
    tߋ ɑn end to. Τhe result iѕ thаt most ԝill
    attempt to quit it independently eҳactly ⅼike most smokers ԝhich never reqlly wοrks.
    Ꭺ comment ‘sexy’ is taken aѕ a compliment not realizing tһɑt ths
    is а bltant judgment passed oon һow yߋu looҝ and check, reducing ߋne
    to the tatus of the mere commodity.

  12. 2018年1月12日07:04 | #12

    Hi! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community
    in the same niche. Your blog provided us useful information to work on. You have done a marvellous job!

  13. 2018年1月12日07:24 | #13

    Every weekend i used to go to see this web site, because i wish for enjoyment, for the
    reason that this this website conations actually fastidious funny data too.

  14. 2018年1月12日07:35 | #14

    Appreciation to my father who told me regarding this webpage, this website is
    truly amazing.

  15. 2018年1月12日07:52 | #15

    Hmm it seems like your website ate my first comment (it
    was super long) so I guess I’ll just sum it up what I had written and say, I’m
    thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to everything.
    Do you have any tips for inexperienced blog writers?
    I’d really appreciate it.

  16. 2018年1月12日08:00 | #16

    who invented autocad software
    autocad download
    autocad ws price

  17. 2018年1月12日08:59 | #17

    Write more, thats all I have to say. Literally, it seems as though you relied
    on the video to make your point. You obviously know what
    youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?

  18. 2018年1月12日09:47 | #18

    I just couldn’t depart your web site prior to suggesting that
    I actually enjoyed the standard info a person provide on your guests?
    Is going to be again continuously to investigate cross-check
    new posts

  19. 2018年1月12日10:10 | #19

    I delight in, result in I discovered just what I was taking a look for.
    You’ve ended my 4 day long hunt! God Bless you man. Have a
    great day. Bye

  20. 2018年1月12日10:14 | #20

    Heya i’m for the primary time here. I found this board and
    I find It really useful & it helped me out much.
    I’m hoping to give something back and help others such
    as you aided me.

  21. 2018年1月12日10:20 | #21

    I simply could not go away your site prior to suggesting that I extremely enjoyed the usual info a person provide
    on your guests? Is going to be back ceaselessly to check out new posts

  22. 2018年1月12日10:21 | #22

    Wߋnderful goods fro you, man. I’ve understand your stuff previous to and you’re just
    too great. I really lіke what yoᥙ havе aϲquired here, really like what you’re stating
    and tһe way in which you say it. You make it entertaining and you ѕtill сare for to keep
    it smart. I cant wait to reqd far mоre from үou.
    This is really a tremendous website.

  23. 2018年1月12日10:49 | #23

    Hello, I check your blogs like every week. Your story-telling style is witty, keep
    up the good work!

  24. 2018年1月12日11:02 | #24

    Wow, this post is fastidious, my younger sister is analyzing these
    kinds of things, so I am going to inform her.

  25. 2018年1月12日11:10 | #25

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

  26. 2018年1月12日11:28 | #26

    I am forever thought about this, thanks for posting.

  27. 2018年1月12日11:29 | #27

    I wksh I ϲould enter additional іnformation wih this, but I wiⅼl be jeopardizing
    mуseⅼf and my family. Tһere аre plenty men who watch tһеm doing thheir thing on creen with
    a huge penis then decjde tһey would liкe ⲟne much
    like thɑt. Now most of the people who try to avoiod contact ᴡith porn аnd continue tօ resit ɑlways fail.

  28. 2018年1月12日11:31 | #28

    What’s up, yup this post is genuinely good and I have learned lot of things from it
    on the topic of blogging. thanks.

  29. 2018年1月12日11:51 | #29

    I used to be able to find good info from your content.

  30. 2018年1月12日12:04 | #30

    Heya i am for the first time here. I came across this board and I find
    It truly useful & it helped me out a lot. I hope to give something back and
    help others like you aided me.

  31. 2018年1月12日12:41 | #31

    Its such as you read my mind! You appear to grasp
    a lot approximately this, like you wrote the guide in it or something.
    I think that you simply can do with some p.c.
    to power the message home a bit, however other than that, this is wonderful blog.
    A great read. I’ll definitely be back.

  32. 2018年1月12日12:47 | #32

    John Wild is vice president of Absolute Access & Security.

    That is absolutely true, since you will be able to improve in each aspect.
    With a very good background to its credit, you will surely
    have a rewarding and ingenuous experience with a locksmith.

  33. 2018年1月12日13:23 | #33

    Saved as a favorite, I like your blog!

  34. 2018年1月12日13:27 | #34

    Good day! I know this is kinda off topic but I’d figured I’d
    ask. Would you be interested in trading links or maybe guest writing a blog article or vice-versa?

    My website discusses a lot of the same topics as
    yours and I feel we could greatly benefit from each other.
    If you are interested feel free to shoot me an email.
    I look forward to hearing from you! Superb blog by the way!

  35. 2018年1月12日13:58 | #35

    loans no credit check
    payday loans online no credit check
    payday loans no credit
    loans no credit check
    payday loans no credit

  36. 2018年1月12日14:04 | #36

    Hello to all, how is the whole thing, I think every one is getting more from this website,
    and your views are pleasant in favor of new users.

  37. 2018年1月12日14:05 | #37

    Therefore, the factories and sweatshops established overseas by Nike launched a debate
    regarding whether Nike was in compliance or violation of moral
    tips and regulations.

  38. 2018年1月12日14:24 | #38

    Ƭhis is the right website for everyone who wants to find out about this topic.
    You knoᴡ so much its almost tough to argue with you (not thаt I actuallү
    wouyld want tо?HaHa). You certainly put a new spin on ɑ toρic that’s beеn discussed for many years.
    Wonderful stuff, just wonderful!

  39. 2018年1月12日14:28 | #39

    Currently it seems like WordPress is the preferred blogging platform out there
    right now. (from what I’ve read) Is that what you’re using on your blog?

  40. 2018年1月12日14:34 | #40

    Most of all latest packages offer this.

  41. 2018年1月12日14:35 | #41

    Hi, all the time i used to check web site posts here in the early hours in the morning, since i enjoy
    to learn more and more.
    buy cheap viagra online

  42. 2018年1月12日14:50 | #42

    Awesome things here. I’m very satisfied to see your article.
    Thank you a lot and I’m having a look ahead to contact
    you. Will you kindly drop me a e-mail?

  43. 2018年1月12日15:03 | #43

    Hello, i believe that i noticed you visited my website so i came to go back the prefer?.I’m attempting to
    to find issues to improve my web site!I assume its good enough to
    make use of a few of your ideas!!

  44. 2018年1月12日15:06 | #44

    You made some good points there. I looked on the web to find
    out more about the issue and found most individuals will
    go along with your views on this website.

  45. 2018年1月12日15:20 | #45

    What a material of un-ambiguity and preserveness of valuable know-how about
    unexpected feelings.

  46. 2018年1月12日15:28 | #46

    Touche. Outstanding arguments. Keep up the amazing work.

  47. 2018年1月12日15:36 | #47

    Hello exceptional blog! Does running a blog like this require a great deal of work?

    I’ve absolutely no understanding of coding however
    I had been hoping to start my own blog soon. Anyhow, should you have any recommendations or techniques for new
    blog owners please share. I understand this is off subject but I just had to ask.
    Thank you!

  48. 2018年1月12日15:53 | #48

    I always spent my half an hour to read this blog’s posts daily along with a cup of coffee.

  49. 2018年1月12日15:53 | #49

    Thank you for the auspicious writeup. It in truth used to
    be a leisure account it. Look complex to more added agreeable from you!
    By the way, how could we communicate?

  50. 2018年1月12日16:04 | #50

    Having read this I believed it was rather enlightening. I appreciate you taking the time and energy to put this article together.

    I once again find myself spending a significant amount of time both reading and posting comments.
    But so what, it was still worthwhile!

评论分页
1 ... 93 94 95 96 97 ... 122 371
订阅评论
2+4= (必填)