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

    This page definitely has all the information I wanted about this subject and didn?t know who to
    ask.

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

    I don’t know whether it’s just me or if everyone else encountering
    issues with your site. It appears like some of the text on your content
    are running off the screen. Can somebody else please provide feedback
    and let me know if this is happening to them as well? This might be a problem with my browser
    because I’ve had this happen previously. Appreciate
    it

  3. 2018年6月14日09:31 | #4

    I have read so many articles concerning the blogger lovers however
    this post is really a nice post, keep it up.

  4. 2018年6月14日09:49 | #5

    Best Online International Pharmacies
    canadian pharmacies online
    canadian drugs

  5. 2018年6月14日09:50 | #6

    Have you ever considered about including a little bit more than just
    your articles? I mean, what you say is important and all.

    But think of if you added some great graphics or video clips to
    give your posts more, “pop”! Your content is excellent but with images
    and videos, this blog could certainly be one of the most beneficial in its niche.
    Amazing blog!

  6. 2018年6月14日09:53 | #7

    Tremendous issues here. I’m very satisfied to look your article.
    Thank you so much and I’m looking forward to contact you.
    Will you kindly drop me a mail?

  7. 2018年6月14日10:05 | #8

    It’s great that you are getting thoughts from this article as well as from our discussion made at this time.

  8. 2018年6月14日10:11 | #9

    best us casinos online
    casino games free
    casino games real money
    casino online free

  9. 2018年6月14日10:16 | #10

    I am regular visitor, how are you everybody?
    This paragraph posted at this site is genuinely good.

  10. 2018年6月14日10:29 | #11

    Very nice article, exactly what I was looking for.

  11. 2018年6月14日10:30 | #12

    Have you ever considered creating an ebook or guest authoring
    on other websites? I have a blog based on the same subjects you discuss and would love to
    have you share some stories/information. I know my subscribers
    would value your work. If you’re even remotely interested, feel free
    to send me an email.

  12. 2018年6月14日10:33 | #13

    Useful info. Lucky me I found your website accidentally, and I’m shocked why this twist of fate did not happened earlier!
    I bookmarked it.

  13. 2018年6月14日10:38 | #14

    If you would like to take a great deal from this piece of
    writing then you have to apply such methods to your won website.

  14. 2018年6月14日10:40 | #15

    I do not even know how I ended up here, but I thought this post was great.

    I don’t know who you are but definitely you are going to
    a famous blogger if you aren’t already ;) Cheers!

  15. 2018年6月14日10:46 | #16

    I don’t know if it’s just me or if perhaps everyone else encountering problems with
    your site. It looks like some of the written text
    within your posts are running off the screen. Can someone
    else please provide feedback and let me know if this is happening to them too?
    This might be a issue with my web browser because I’ve had this happen previously.
    Thank you

  16. 2018年6月14日10:51 | #17

    Hi would you mind stating which blog platform
    you’re working with? I’m looking to start my own blog in the near future but I’m
    having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique.

    P.S My apologies for being off-topic but I had to ask!

  17. 2018年6月14日10:52 | #18

    Aw, this was an extremely nice post. Taking the time and actual effort to create a really good article…
    but what can I say… I procrastinate a lot and never seem to
    get anything done.

  18. 2018年6月14日11:07 | #19

    Hello! This is kind of off topic but I need some advice from an established blog.

    Is it tough to set up your own blog? I’m not very techincal but I can figure things out
    pretty fast. I’m thinking about creating my own but I’m not sure where to begin.
    Do you have any ideas or suggestions? Many thanks

  19. 2018年6月14日11:19 | #20

    Good day! I just would like to give you a huge thumbs up
    for the excellent info you’ve got here on this post. I am returning to your website for
    more soon.

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

    If this is true then results might be skewed or perhaps the writer could be can not draw any sensible conclusions.
    Cross out any irrelevant ones and make your better to put them in a logical order.
    Reading and writing wherever possible is the best method to
    develop a writing style.

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

    Hi there tⲟ all, how is the whole thing, I think every one is getting more from
    this site, and your views are fastidious in support of new visitors.

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

    Highly energetic blog, I enjoyed that a lot.
    Will there be a part 2?

  23. 2018年6月14日13:13 | #24

    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.

  24. 2018年6月14日13:28 | #25

    Grdat delivery. Outstanding arguments. Keep up the good effort.

  25. 2018年6月14日13:31 | #26

    Hi there! I simply would like to give you a huge
    thumbs up for the great info you have right here on this post.
    I am coming back to your blog for more soon.

  26. 2018年6月14日13:42 | #27

    Настоящие люди заходят в игру каждую секунду.

  27. 2018年6月14日13:43 | #28

    Hi! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m absolutely enjoying your blog and look forward to new updates.

  28. 2018年6月14日13:48 | #29

    nevertheless [cue for newness] bravery, constancy,
    and resolution [reverse of submission]. Each format pressupposes a certain formation plus design for citing rephrased and echoed resources and only all choices of printed, internet, and other forms of resources.

    Reading and writing whenever you can certainly is the best method to develop a writing style.

  29. 2018年6月14日13:49 | #30

    First off I want to say wonderful blog! I
    had a quick question in which I’d like to ask if you do not mind.
    I was interested to know how you center yourself and clear
    your thoughts prior to writing. I’ve had a difficult time clearing my mind in getting my ideas out.
    I do take pleasure in writing however it just
    seems like the first 10 to 15 minutes tend to be wasted simply just trying to figure out how to begin. Any recommendations or tips?
    Thanks!

  30. 2018年6月14日14:42 | #31

    Excellent blog here! Also your website loads up very fast!

    What web host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as fast as yours lol

  31. 2018年6月14日14:51 | #32

    I must thank you for the efforts you have put in penning this website.
    I really hope to see the same high-grade blog posts from you in the future as
    well. In fact, your creative writing abilities has inspired me to get my very
    own website now ;)

  32. 2018年6月14日14:51 | #33

    Hello, just wanted to mention, I liked this blog post.
    It was practical. Keep on posting!

  33. 2018年6月14日15:25 | #34

    Hi, just wanted to say, I enjoyed this article. It was practical.
    Keep on posting!

  34. 2018年6月14日15:44 | #35

    Good article! We will be linking to this particularly great post on our site.
    Keep up the good writing.

  35. 2018年6月14日15:54 | #36

    Great site you have got here.. It’s difficult to find high quality writing like yours
    nowadays. I truly appreciate individuals like you! Take care!!

  36. 2018年6月14日16:30 | #37

    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 wrote and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any points for rookie blog writers? I’d certainly appreciate it.

  37. 2018年6月14日16:51 | #38

    Fantastic beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog website?
    The account helped me a acceptable deal.
    I had been a little bit acquainted of this your broadcast provided bright clear concept

  38. 2018年6月14日17:03 | #39

    I am truly thankful to the owner of this web page
    who has shared this fantastic piece of writing at at this time.

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

    Hell᧐! I’m at work browsing your blog from my new iphone!
    Just wanted to ssay I love reading your blog and ⅼook foгward to all your posts!
    Caгry on the superb work!

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

    I’m not sure whewre you’re getting your information, but
    good topic. I needs to spehd some time learning much more or understanding more.
    Thanks forr excellent information I was looking for this info for my mission.

  41. 2018年6月14日17:27 | #42

    Keep on writing, great job!

  42. 2018年6月14日17:27 | #43

    Ι think the admin of this web page is really working hartⅾ foor his web
    paցе, because here every stuff is quality based information.

  43. 2018年6月14日17:37 | #44

    I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get got an shakiness over that you wish
    be delivering the following. unwell unquestionably come further
    formerly again since exactly the same nearly very often inside case you shield this hike.

  44. 2018年6月14日17:38 | #45

    Yes! Finally something about Marco Island Roofer.

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

    Eu não podia de sair do seu blog sem pelo menos publicar um comentário.
    Seu site é muito bom . me tornarei um visitante
    assíduo daqui . Parabéns http://jmht.journal.ipb.ac.id/index.php/jurnalilkom/comment/view/1070/0/134762

  46. 2018年6月14日18:16 | #47

    When I originally commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment
    is added I recieve 4 emails with the exact same
    comment. Perhaps there is a way you are able to remove me from that service?
    Thanks a lot!

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

    Scatters trigger free spins with bonus options.

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

    Hi! Do you know if they make any plugins to protect against hackers?

    I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?

  49. 2018年6月14日19:09 | #50

    Hey would you mind stating which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a
    hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something unique.

    P.S My apologies for getting off-topic but I had to ask!

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