温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用python 自写nagios发送邮件小程序

发布时间:2020-08-08 21:32:03 来源:网络 阅读:570 作者:nginx2012 栏目:移动开发

楼主公司之前一直都有短信接口 报警都是自己写脚本通过短信方式来报警,但今天领导突然要求 收到nagios邮件报警,楼主一时激动 写个python的小程序吧,希望对大家有帮助

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$"


即可收到邮件 简单吧 比网上的方法简单的多

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI