python指令碼用來nagios發送郵件

來源:互聯網
上載者:User


python指令碼用來nagios發送郵件


 之前公司nagios發送郵件的指令碼是用expect來寫的,但是一直有一個弊端就是nagios郵件內文不能換行,只能在一行顯示,每次警示看起來都很費勁。一直想換了它,這次用python的指令碼就解決的這個換行的問題。

 廢話少說,上指令碼:

 
  1. #!/usr/bin/python

  2. import smtplib

  3. import string

  4. import sys

  5. import getopt

  6. def usage():

  7.   print """sendmail is a send mail Plugins

  8.   Usage:

  9.   sendmail [-h|--help][-t|--to][-s|--subject][-m|--message]

  10.   Options:

  11.          --help|-h)

  12.                 print sendmail help.

  13.          --to|-t)

  14.                 Sets sendmail to email.

  15.          --subject|-s)

  16.                  Sets the mail subject.

  17.          --message|-m)

  18.                  Sets the mail body

  19.    Example:

  20.           only one to email  user

  21.          ./sendmail -t 'eric@nginxs.com' -s 'hello eric' -m 'hello eric,this is sendmail test!

  22.           many to email  user

  23.          ./sendmail -t 'eric@nginxs.com,zhangsan@nginxs.com' -s 'hello eric' -m 'hello eric,this is sendmail test!"""

  24.   sys.exit(3)

  25. try:

  26.   options,args = getopt.getopt(sys.argv[1:],"ht:s:m:",["help","to=","subject=","message="])

  27. except getopt.GetoptError:

  28.   usage()

  29. for name,value in options:

  30.    if name in ("-h","--help"):

  31.       usage()

  32.    if name in ("-t","--to"):

  33. # accept message user

  34. TO = value

  35. TO = TO.split(",")

  36.    if name in ("-s","--title"):

  37. SUBJECT = value

  38.    if name in ("-m","--message"):

  39. MESSAGE = value

  40. MESSAGE = MESSAGE.split('\\n')

  41. MESSAGE = '\n'.join(MESSAGE)

  42. #smtp HOST

  43. HOST = "smtp.126.com"                #改為你的郵局SMTP 主機地址

  44. #smtp port

  45. PORT = "25"                          #改為你的郵局的SMTP 連接埠

  46. #FROM mail user

  47. USER = 'eric'                        # 改為你的信箱使用者名

  48. #FROM mail password

  49. PASSWD = '123456'                    # 改為你的郵箱密碼

  50. #FROM EMAIL

  51. FROM = "test@163.com"                # 改為你的郵箱 email

  52. try:

  53. BODY = string.join((

  54.      "From: %s" % FROM,

  55.      "To: %s" % TO,

  56.      "Subject: %s" % SUBJECT,

  57.      "",

  58.      MESSAGE),"\r\n")

  59. smtp = smtplib.SMTP()

  60.   smtp.connect(HOST,PORT)

  61.   smtp.login(USER,PASSWD)

  62.   smtp.sendmail(FROM,TO,BODY)

  63.   smtp.quit()

  64. except:

  65.   print "UNKNOWN ERROR"

  66.   print "please look help"

  67.   print "./sendmail -h"



使用方法:

只給一個使用者發:

nagios $> ./sendmail -t 'test@163.com' -s 'hello' -m 'hello,this is sendmail test!

給多個使用者發:

./sendmail -t ' test@163.com,test02@163.com' -s 'hello' -m 'hello,this is sendmail test!

應用到nagios上,用來警示:



[root@master ~]# vim /etc/nagios/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$"

       }

[root@master ~]# /etc/init.d/nagios reload

這樣就可以了。




本文出自 “夢想照進現實” 部落格,請務必保留此出處http://zhhmj.blog.51cto.com/1666742/990830

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.