5

Zabbix阿里云短信告警

 1 year ago
source link: https://blog.51cto.com/u_13444271/5913117
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Zabbix阿里云短信告警

精选 原创

Genius_Kim 2022-12-05 17:20:04 博主文章分类:Zabbix监控自动化运维 ©著作权

文章标签 zabbix sms 文章分类 Linux 系统/运维 阅读数182

(不一定要这样子的环境,可根据具体的生产环境配置)

应用 版本
CentOS 7.4
Zabbix 6.0.4
Python v2
阿里云短信服务SDK 2.0.22

准备阿里云短信签名和模板

阿里云短信签名

添加完签名再添加模板,有不清楚的问阿里服务人员

阿里云短信模版

模版内容可以根据需求变更,这里使用的模版是以下内容

A warning has occurred ${title} 
Time:${datetime} 
Host:${name} 
Info:${info}

接收到的样子大概长这个样子

【签名】A warning has occurred Nginx Down
Time:2022.12.03 16:42:44 
Host:node.if010.com 127.0.0.1 
Info:nginx status:0

这里需要注意以下几点:
1、变量属性选择“其他”;
2、申请后要和客户沟通,及时修改,不然会被审核不通过

准备key和秘钥

鼠标放到右上角用户处,可以看到一个AccessKey管理,入不记得秘钥可以重新生成

AccessKey

编写sms.py脚本

vim /usr/local/zabbix/share/zabbix/alertscripts/sms.py
#!/usr/bin/env python
#coding=utf-8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import logging
import json

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkdysmsapi.request.v20170525.SendSmsRequest import SendSmsRequest

# 创建一个日志器,就是一个logger对象
logger = logging.getLogger('logger')
# 设置logger输入级别
logger.setLevel(logging.DEBUG)
# 创建日志格式器
formator = logging.Formatter(fmt="%(asctime)s [ %(filename)s ]  %(lineno)d line | [ %(levelname)s ] | [%(message)s]",
                             datefmt="%Y/%m/%d/%X")
# 创建一个输出的处理器,让它输入到控制台
sh = logging.StreamHandler()

# 1.创建一个把日志信息存储到文件中的处理器
# 要加编码,不然后可能会乱码
fh = logging.FileHandler("/var/log/sms.log", encoding="utf-8")

# 把输出处理器添加到日志器中
logger.addHandler(sh)

# 给处理器添加格式器
sh.setFormatter(formator)

# 2.把文件处理器,加载到logger中
logger.addHandler(fh)

# 3.给文件处理器添加格式器
fh.setFormatter(formator)

credentials = AccessKeyCredential('<这里填写你的AccessKey>', '<这里填写你的AccessSecret>')
client = AcsClient(region_id='cn-hangzhou', credential=credentials)

request = SendSmsRequest()
request.set_accept_format('json')
request.set_SignName("<这里填写你的短信签名名称>")
request.set_TemplateCode("<这里填写你的短信模版ID>")
request.set_PhoneNumbers(sys.argv[1])

message=json.loads(sys.argv[2])
logger.info("PhoneNumber:" + sys.argv[1])
logger.info("Title:" + message['title'])
logger.info("Datetime:" + message['datetime'])
logger.info("HostName:" + message['name'])
logger.info("Info:" + message['info'])

request.set_TemplateParam(sys.argv[2])

response = client.do_action_with_exception(request)
# python2:  print(response) 
# print(str(response, encoding='utf-8'))

# 输出日志信息
#logger.debug('输出debug级别的日志')
logger.info(sys.argv)
logger.info(response)
#logger.info(response)
#logger.warning('输出warning级别的日志')
#logger.error('输出error级别的日志')
#logger.critical('输出critical级别的日志')

此脚本的路径需要和你的zabbix_server.conf文件中AlertScriptsPath定义的路径一致,否则后面会报错找不到脚本执行!!!此外,还需要给脚本在/var/log/创建sms.log日志文件,最后给脚本赋予执行权限

chmod 777 /usr/local/zabbix/share/zabbix/alertscripts/sms.py
chmod 777 /var/log/sms.log

配置zabbix报警媒介类型

报警媒介
信息模版

信息模版编写成josn格式,方便脚本的读写

{"title":"{EVENT.NAME}","datetime":"{EVENT.DATE} {EVENT.TIME}","name":"{HOST.NAME} {HOST.IP}","info":"{ITEM.KEY1}:{ITEM.VALUE1}"}

关于阿里云JDK的环境

pip 安装 阿里云的JDK模块

pip install alibabacloud_dysmsapi20170525==2.0.23
  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK