zabbix发送报警的几种方法
最近有个朋友问我zabbix报警怎么做比较简单,灵活,方便。
其实目前zabbix的报警无怪乎下面几种方法:
1.通过action直接调用sendmail一类的脚本,传入zabbix的宏变量,优点:配置比较简单,缺点:缺乏灵活性。
2.通过action调用脚本,传入宏变量,并通过脚本来做后续的处理,这个方法比较简单也比较灵活,大部分情况下都适用。
3.抓取zabbix 数据库里面的报警信息并做报警处理,比如可以通过下面的sql抓取一段时间范围内的报警信息:
1
2
3
4
5
|
select s.host,h.ip,i.key_,a.subject,a.message,t.priority,a.clock from alerts a,events e,
triggers t,functions f,items i,interface h,hosts s where a.eventid=e.eventid
and e.objectid=t.triggerid and t.triggerid=f.triggerid and i.itemid=f.itemid
and i.hostid=h.hostid and e.object= '0' and e.source= '0' and s.hostid=i.hostid
and a.clock > 'xxxx'
|
这里简单说下第二种方法(对于第二种方法有比较多的扩展,比如通过脚本把消息发送至消息队列中,然后由后端的报警程序消费,后端的报警程序可以做一些扩展的功能,比如结合cmdb来获取业务负责人,机器的维护状态等,这个涉及到一定的开发量,我们这里说下简单的方法):
简单说下想要实现的目标
1)发送的邮件可以做一些基本的判断(比如获取内存使用,cpu使用,服务器负载等),并增加历史的图形展示
2)根据主机名,item名来判断收件人(属于谁的发送给谁,sample的脚本没有写这个功能)
首先,在action里面的operations设置如下:
注意target list/type,execute on项,以及脚本的参数(注意双引号)
创建报警脚本(写得比较挫,见谅):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/usr/bin/python # -*- coding: utf8 -*- #zabbix remote command import time,datetime
import re,os
import sys
import zabbix_sendmail
from zabbix_sendmail import SendmailError
import subprocess
import logging
from logging.handlers import RotatingFileHandler
reload (sys)
sys.setdefaultencoding( 'utf-8' )
LOGFILE = "/apps/svr/zabbix_server/scripts/logs/out.log"
MAXLOGSIZE = 100 * 1024 * 1024
BACKUPCOUNT = 4
log = logging.getLogger( 'zabbix_exec_command' )
log.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(levelname)s: %(message)s' )
fh = RotatingFileHandler(LOGFILE,maxBytes = MAXLOGSIZE,backupCount = BACKUPCOUNT)
ch = logging.StreamHandler()
fh.setFormatter(formatter) ch.setFormatter(formatter) log.addHandler(fh) log.addHandler(ch) def get_2hour_ago():
a = os.popen( "date +%Y%m%d%H -d '2 hours ago'" ).readlines()[ 0 ]
b = a.strip()
return b
def run_command(ip,command):
cmd = "zabbix_get -s " + ip + " -k 'system.run[\"" + command + "\"]'"
return os.popen(cmd).readlines()
mailcontent = ""
if __name__ = = "__main__" :
to_list = []
to_mobile = []
if len (sys.argv) ! = 8 :
print "show usage: %s serverip itemid itemname hostname itemkey itemvalue triggerstatus" % (sys.argv[ 0 ])
sys.exit( 1 )
mailcontent = """
<html>
<body>
<meta http-equiv="Content-Type" content="text/html";charset=utf-8>
<title>大数据监控邮件</title>
<style type="text/css">
.body { font-size: 14px; color: #333;background-color: #fff;}
.divtd {color:#E28E40;}
</style>
"""
ip,itemid,itemname,hostname = sys.argv[ 1 ],sys.argv[ 2 ],sys.argv[ 3 ],sys.argv[ 4 ]
itemkey,itemvalue,triggerstatus = str (sys.argv[ 5 ]).replace( " " ," "),str(sys.argv[6]).replace(" "," "),sys.argv[ 7 ]
log.info( "报警信息:IP:%s,监控项:%s,主机名:%s,监控key:%s" % (ip,itemname,hostname,itemkey))
time_start = get_2hour_ago()
mailcontent + = "<div class='divtd'> 监控项:%s , 主机名:%s ,IP:%s ,当前值: %s </div><br />" % (itemname,hostname,ip,itemvalue)
mailcontent + = "<div class='divtd'>内存信息:</div> <div class='.body'>"
for line in run_command(ip, "free -m" ):
line = line.strip()
mailcontent + = "%s<br />" % line
mailcontent + = "<br /></div>"
mailcontent + = "<div class='divtd'>IO信息:</div> <div class='.body'>"
for line in run_command(ip, "iostat -xn 1 1" ):
line = line.strip()
mailcontent + = "%s<br />" % line
mailcontent + = "<br /></div>"
mailcontent + = "<div class='divtd'>负载信息:</div> <div class='.body'>"
for line in run_command(ip, "sar -q 1 3" ):
line = line.strip()
mailcontent + = "%s<br />" % line
mailcontent + = "<br /></div>"
mailcontent + = "<div class='divtd'>CPU信息:</div> <div class='.body'>"
for line in run_command(ip, "mpstat -P ALL" ):
line = line.strip()
mailcontent + = "%s<br />" % line
mailcontent + = "<br /></div>"
mailcontent + = "<div class='divtd'>系统日志:</div> <div class='.body'>"
for line in run_command(ip, "/usr/bin/sudo tail -20 /var/log/messages" ):
line = line.strip()
mailcontent + = "%s<br />" % line
mailcontent + = "<br /></div>"
to_list = [ "xxxx" ]
mailcontent + = "<div class='divtd'>历史数据:</div> <div class='.body'>"
mailcontent + = "<table style='border-collapse: collapse; width: 96%;'>"
mailcontent + = """<tr><img src=http://xxxxxx/chart.php?itemid=%s&period=7200&stime=%s width="871" height="305"/>""" % (itemid,time_start)
mailcontent + = "</tr> </table>"
mailcontent + = """</html>
</body>"""
print mailcontent
log.debug( "监控邮件内容:%s" % mailcontent)
log.info( "邮件收件人:%s,短信收件人列表:%s" % (to_list,to_mobile))
mail_sub = "Zabbix监控报警|当前状态:%s,监控项:%s,监控主机:%s " % (triggerstatus,itemname,hostname)
log.info( "开始发送邮件,邮件主题:%s" % mail_sub)
try :
zabbix_sendmail.send_mail_withoutSSL(to_list,mail_sub,mailcontent)
log.info( "报警邮件发送成功" )
except SendmailError,e:
log.error( "报警邮件发送失败,失败信息:%s" % ( str (e)))
|
其中zabbix_sendmail的内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/usr/bin/python # -*- coding: utf8 -*- #to send zabbix alert mail import smtplib
from email.mime.text import MIMEText
import traceback
import exceptions
import sys
reload (sys)
sys.setdefaultencoding( 'utf-8' )
mail_host = "xxxxx"
mail_user = "xxxx"
mail_pass = "xxxx"
mail_address = "xxx"
mail_postfix = "xxxx"
mail_port = "xxx"
class SendmailError(Exception):
def __init__( self , errorlog):
self .errorlog = errorlog
def __str__( self ):
return "发送邮件错误,错误信息: %s" % ( self .errorlog)
def send_mail_withoutSSL(to_list,sub,content):
me = "BI Monitor" + "<" + mail_user + "@" + mail_postfix + ">"
msg = MIMEText(content, 'html' , 'utf8' )
msg[ 'Subject' ] = sub
msg[ 'From' ] = me
msg[ 'To' ] = ";" .join(to_list)
try :
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
raise SendmailError( str (e))
|
发送的报警邮件如下
关于报警问题的debug:
1.看看trigger有没有正常触发(dashboard上有没有对应的action)
2.发送报警的命令运行是否正常(手动验证)
3.结合server的日志和zabbix的数据库debug(比较常用的是alerts表)
本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1427504,如需转载请自行联系原作者