nagios 邮件报警脚本

发布时间:2020-08-01 05:58:47 作者:zhuolang01
来源:网络 阅读:974

一.python邮件发送脚本

1.linux服务器发送邮件,许多人会直接用mail命令发送,虽然可以发送成功,但是许多都会被互联网邮箱当初垃圾邮件处理

2.mutt和msmtp相关于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服务域名)        

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


然后测试发送邮件

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"

       }


推荐阅读:
  1. nagios邮件报警配置
  2. Nagios之邮件报警

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

nagios python邮件 na

上一篇:asp.net后台弹出确认框

下一篇:KVM虚拟化之安装KVM虚拟机(一)

相关阅读

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

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