溫馨提示×

溫馨提示×

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

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

nagios 郵件報警腳本

發(fā)布時間:2020-08-01 05:58:47 來源:網(wǎng)絡(luò) 閱讀:965 作者:zhuolang01 欄目:移動開發(fā)

一.python郵件發(fā)送腳本

1.linux服務(wù)器發(fā)送郵件,許多人會直接用mail命令發(fā)送,雖然可以發(fā)送成功,但是許多都會被互聯(lián)網(wǎng)郵箱當(dāng)初垃圾郵件處理

2.mutt和msmtp相關(guān)于linux下的郵件客戶端,和windows上的outlook,foxmail之類有點類似

3.安裝mutt和msmtp雖然可以,但是通過python腳本也可以代替,如下

ubuntu@ubuntu:/etc/nagios3/scripts$ cat sendmail

#!/usr/bin/python

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.126.com"      (這個是126的smtp服務(wù)域名)        

#smtp port

PORT = "25"                 (126郵箱smtp端口號)            

#FROM mail user

USER = 'Czar_test'      (你注冊126郵箱的用戶名)                  

#FROM mail password

PASSWD = 'test123'       (你注冊126郵箱的密碼)            

#FROM EMAIL

FROM = "Czar_test@126.com"   (你的126郵箱地址)


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"


然后測試發(fā)送郵件

ubuntu@ubuntu:/etc/nagios3/scripts$ /etc/nagios3/scripts/sendmail -t "yongkang_tian@126.com" -s "Nagios Test" -m "Hello Wrold"

nagios 郵件報警腳本

二.nagios命令文件里面添加代碼

ubuntu@ubuntu:/etc/nagios3$ vim commands.cfg

define command{

       command_name    notify-host-by-email

       command_line    /etc/nagios3/scripts/sendmail  -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "%b" -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    /etc/nagios3/scripts/sendmail -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "%b" -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$\n"

       }


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

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

AI