《Zabbix企业级分布式监控系统》学习笔记(二)——告警

本人学习《Zabbix企业级分布式监控系统》第6章的笔记,Linux版本Centos6.10,Zabbix版本2.2.22

1 Trigger配置

1.1 简单Trigger配置

在Linux主机上配置trigger

用ssh登陆该服务器后,在Web端看见告警信息

1.2 告警依赖

添加一个依赖,如果有人登陆并且/etc/passwd被修改就告警

修改密码后,断开ssh连接再重新登陆,收到告警

2 Action配置

配置一个Action,当出现告警信息就像邮箱发送邮件,首先需要准备两个邮箱,一个作为发件邮箱,一个作为接收邮箱

之前配置了一个Trigger监控服务器登陆状况,通过ssh的断开与登陆触发这个Trigger从而使Action工作

在本地撰写一个发送邮件脚本,我放在了/etc/zabbix/alertscripts下,配置好发件邮箱,这里使用了QQ邮箱

#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import os
import argparse
import logging
import datetime

mail_host = 'smtp.qq.com'
mail_user = '这里填写你的QQ号'
mail_pass = '这里填写你的密码,如果是QQ邮箱,在设置那里打开STMP服务后,获取授权码'
mail_postfix = 'qq.com'

def send_mail(mail_to, subject, content):
	me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
	msg = MIMEText(content)
	msg['Subject'] = subject
	msg['From'] = me
	msg['to'] = mail_to
	global sendstatus
	global senderr

	try:
		smtp = smtplib.SMTP()
		smtp.connect(mail_host)
		smtp.login(mail_user, mail_pass)
		smtp.sendmail(me, mail_to, msg.as_string())
		smtp.close()
		print 'send ok'
		sendstatus = True
	except Exception, e:
		senderr = str(e)
		print senderr
		sendstatus = False

def logwrite(sendstatus, mail_to, content):
	logpath = '/var/log/zabbix/alert'
	if not sendstatus:
		content = senderr
	if not os.path.isdir(logpath):
		os.makedirs(logpath)
		t = datetime.datetime.now()
	t = datetime.datetime.now()
        daytime = t.strftime('%Y-%m-%d')
	daylogfile = logpath + '/' + str(daytime) + '.log'
	logging.basicConfig(filename = daylogfile, level = logging.DEBUG)
	logging.info('*' * 130)
	logging.debug(str(t) + ' mail send to {0}, content is :\n {1}'.format(mail_to, content))

if __name__ == "__main__":
	parser = argparse.ArgumentParser(description = 'Send mail to user for zabbix alerting')
	parser.add_argument('mail_to', action = 'store', help = 'The address of the E-mail that send to user')
	parser.add_argument('subject', action = 'store', help = 'The subject of the E-mail')
	parser.add_argument('content', action = 'store', help = 'The content of the E-mail')
	args = parser.parse_args()
	mail_to = args.mail_to
	subject = args.subject
	content = args.content
                
	send_mail(mail_to, subject, content)
	logwrite(sendstatus, mail_to, content)

写完后运行一下,在参数写上目的邮箱、邮件主题、邮件内容,测试是否发送成功

配置一个新的媒介

给用户添加设定好的媒介,并配置收件邮箱 

配置一个新的Action,写上名字,其他都默认,Condition也默认

配置Operation,添加消息发送的用户 

配置好后,要点最下面的add,不能直接点Save,不然会报Field "operations" is mandatory.

然后ssh用户重新登陆,发现邮箱并没有邮件

查看Action的日志

原来是默认zabbix配置的脚本路径并不是我的脚本所存在的路径,打开/etc/zabbix/zabbix_server.conf文件,修改下面一行并重启服务

然后再次报错,Permission denied

权限问题,把脚本文件的权限修改为777就可以成功发送,不过这个做法是不对的,zabbix用户权限范围有待研究

但是看了看邮箱并没有邮件,脚本的日志中也没有日志记录,似乎脚本根本没有执行,之前测试的时候我都是用python sendmail.py执行的脚本,现在换成./执行发现报错:/usr/bin/python^M: bad interpreter

因为我的脚本是在Windows下编辑的,到了Linux下可能会有一些不可见的字符生成,用vim打开文件,输入命令set fileformat=unix,然后重新登陆一下ssh,搞一个Warning触发Action,终于收到了邮件,虽然在垃圾邮件里

猜你喜欢

转载自blog.csdn.net/jiangxuege/article/details/81279159