溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用python 自寫nagios發(fā)送郵件小程序

發(fā)布時(shí)間:2020-08-08 21:32:03 來(lái)源:網(wǎng)絡(luò) 閱讀:561 作者:nginx2012 欄目:移動(dòng)開(kāi)發(fā)

樓主公司之前一直都有短信接口 報(bào)警都是自己寫腳本通過(guò)短信方式來(lái)報(bào)警,但今天領(lǐng)導(dǎo)突然要求 收到nagios郵件報(bào)警,樓主一時(shí)激動(dòng) 寫個(gè)python的小程序吧,希望對(duì)大家有幫助

vim /usr/local/nagios/libexec/sendmail

#!/usr/bin/python
#-*- 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')
      MESSAGE = '\n'.join(MESSAGE)

#smtp HOST
HOST = ""                    
#smtp port
PORT = ""                              
#FROM mail user
USER = ''                              
#FROM mail password
PASSWD = ''                    
#FROM EMAIL
FROM = ""    

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"



chmod +x /usr/local/nagios/libexec/sendmail


vim /usr/local/nagios/etc/object/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$"


即可收到郵件 簡(jiǎn)單吧 比網(wǎng)上的方法簡(jiǎn)單的多

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI