潇洒紫焰 发表于 2019-1-26 06:01:33

zabbix通过脚本报警

  概述
  zabbix媒介类型包括mail、sms、自定义脚本,我们用的最多的还是脚本媒介,再次我们就不讲另外两个媒介了。当事件通知到脚本,会传递三个参数它,分别为$1(发送给谁) $2(标题) $3(内容)。
  媒介配置
  点击administrator->media types->create media types
http://s3.运维网.com/wyfs02/M02/59/52/wKioL1TQWfHAFqlSAACuQ4CqshM797.jpg
  Name:触发器名称
  Type:介质类型
  script name:脚本名称(需要先定义AlertScriptsPath,sendmail.py放在这个目录下,写绝路路径没用)
  Enabled:状态
  配置AlertScriptsPath
  # cat etc/zabbix_server.conf | grep "^AlertScriptsPath"
  AlertScriptsPath=/usr/local/zabbix/script#手动配置
  # pwd
  /usr/local/zabbix
  # ls script/
  mail.shsendmail.py
  编写脚本
  # cat script/sendmail.py
  #!/usr/bin/python
  #coding: utf-8
  import smtplib,sys
  from email.mime.text import MIMEText
  from email.header import Header
  sender = '***@163.com'   #发送邮件地址
  receiver = sys.argv
  subject = sys.argv
  smtpserver = 'smtp.163.com'
  username = '***@163.com' #发送邮件账号
  password = '******'    #发送邮件密码
  msg = MIMEText("%s" % sys.argv,'text','utf-8')#中文需参数‘utf-8',单字节字符不需要
  msg['Subject'] = Header(subject, 'utf-8')
  smtp = smtplib.SMTP()
  smtp.connect('smtp.163.com')
  smtp.login(username, password)
  smtp.sendmail(sender, receiver, msg.as_string())
  smtp.quit()
  用户媒介
  点击Administration→Users->打开用户属性表单->在Media tab点击Add
http://s3.运维网.com/wyfs02/M01/59/56/wKiom1TQWnPw4m0GAAGPFvhQP3Y932.jpg
  参数介绍
  Type:选择媒介类型,这边选自定义媒介
  Send to:发送到哪,例如monitor@ttlsa.com,他就是脚本中的$1
  When active:报警时间限定,例如1-5,09:00-18:00,只有工作日的9点到18点才会通知,实际工作中,我们应该是相反。
  Use if severity:严重性类型,只接收指定的类型,例如info不想接收,那我不勾选即可。
  Status:媒介状态Enabled – 启用中.Disabled – 已禁用.

页: [1]
查看完整版本: zabbix通过脚本报警