阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nagios 邮件报警脚本

110次阅读
没有评论

共计 3184 个字符,预计需要花费 8 分钟才能阅读完成。

一.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 命令文件里面添加代码
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”
      }

Nagios 的详细介绍 :请点这里
Nagios 的下载地址 :请点这里

相关阅读

网络监控器 Nagios 全攻略 http://www.linuxidc.com/Linux/2013-07/87067.htm

Nagios 搭建与配置详解 http://www.linuxidc.com/Linux/2013-05/84848.htm

Nginx 环境下构建 Nagios 监控平台 http://www.linuxidc.com/Linux/2011-07/38112.htm

在 RHEL5.3 上配置基本的 Nagios 系统 (使用 Nagios-3.1.2) http://www.linuxidc.com/Linux/2011-07/38129.htm

CentOS 5.5+Nginx+Nagios 监控端和被控端安装配置指南 http://www.linuxidc.com/Linux/2011-09/44018.htm

Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用 http://www.linuxidc.com/Linux/2013-11/93047.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计3184字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中