python程序实现nagios发送报警邮件
思韵闪耀
2013-03-18
0

楼主公司之前一直都有短信接口 报警都是自己写脚本通过短信方式来报警,但今天领导突然要求 收到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),"rn")

  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 *****nnNotification
Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/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
*****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SERVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$
nnAdditional Info:nn$SERVICEOUTPUT$"

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

expect批量同步数据


【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

Import or ins...
若要为一个或多个Exchange服务启用加密,Exchange服务...
2022-09-16
数字证书和加密Exchan...
加密和数字证书是所有组织的重要考虑因素。 默认情况下,Exchan...
2022-09-16
EXCHANGE 2016...
EXCHANGE 2016证书续期 1.exchange证书右侧点...
2022-09-16
SMTP 发送邮件错误码和...
SMTP错误码/建议解决方法 错误总表 420 1. Timeou...
2022-03-03
Exchange迁移用户命...
Exchange迁移用户命令及排查处理 查询统计 查看迁移进度 G...
2022-01-14
Nagios+zabbix...
最近开始接触Linux运维,安装了Nagios,zabbix和ga...
2021-11-05

热门资讯

Exchange 2013/2... Exchange 2013/2016/2019邮件大小限制 可以在三个位置上配置Exchange上的...
SMTP 发送邮件错误码和解决... SMTP错误码/建议解决方法 错误总表 420 1. Timeout Communication P...
Exchange 2013 O... 最近在Exchange 2013环境中,遇到OWA/ECP无法访问的问题;用户是可以正常通过...
Roundcube 配置过各提... Fileinfo/mime_content_type configuration: OK Mimet...
彻底卸载Exchange Se... 一般来说,我们安装完Exchange Server 2016之后,可以通过正常卸载程序来进行卸载,但...
Exchange 2013批量... 一批量删除特定主题的邮件 1.1批量删除所有数据库中特定主题的邮件 1)群发了几封主题为backup...
快速查看已安装Exchange... Exchange 2010的版本号在Exchange管理控制台(EMC)显示的,好像不准确了。比如,...
在 Exchange 2016... 用户邮箱是与人员关联的 Exchange 邮箱,通常为每人一个邮箱。每个用户邮箱都有一个关联的 Ac...
exchange2013域名(... 需要对 接受的域 和 电子邮件地址策略进行配置: 首先外网域名要做解析和增加mx记录,这里就不具体说...
如何为AD 添加多个UPN后缀 今天我在群里看到有群友在问User principal name (UPN)多后缀的问题,我在这里就...