zabbix运维系统怎么编写脚本
发布网友
发布时间:2022-04-14 05:18
我来回答
共1个回答
热心网友
时间:2022-04-14 06:47
1.修改zabbix server端配置文件启用AlertScriptsPath使其支持脚本功能
1.编辑zabbix server端配置文件并重启服务
# vim /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/local/zabbix/alertscripts
# /etc/init.d/zabbix_server restart
2.服务端添加邮件报警python脚本并给脚本执行权限
邮件报警并记录志
# vim /usr/local/zabbix/alertscripts/zabbix_sendmail.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pdb
import smtplib
import string
import time
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
#pdb.set_trace()
#HOST = "mail.gw.com.cn"
def S_Mail():
HOST = "smtp.exmail.qq.com" #邮件服务器
SUBJECT = sys.argv[2].decode(‘utf-8‘).encode(‘gbk‘) #主题
TO =sys.argv[1] #收件邮箱
FROM = "admin@qq.cn"
text = sys.argv[3].decode(‘utf-8‘).encode(‘gbk‘) #发件内容
BODY = string.join((
"FROM: %s" % FROM,
"To: %s" % TO,
"Subject: %s" %SUBJECT,
"",
text
),"\r\n")
server = smtplib.SMTP()
server.connect(HOST,25)
#server.starttls()
server.login("发件邮箱","密码")
server.sendmail(FROM,[TO],BODY)
server.quit()
# email log 记录志
with open(‘/data/logs/zabbix/Email.log‘, ‘a‘) as f:
date=time.strftime("%y-%m-%d %H:%M:%S")
str = date + " " + TO +" " + SUBJECT + "\r\n" + "\n"
str1 = str.decode(‘gbk‘).encode(‘utf-8‘)
# print("%s" %str1)
f.write(str1)
if __name__==‘__main__‘:
S_Mail()