zabbix使用短信告警高效、快捷,本次做个记录
一、zabbix服务器安装
https://blog.csdn.net/oToyix/article/details/106853849
二、zabbix服务器安装“阿里短信SDK核心库”
这里要先安装python3.6,及配置pip3
安装python3
wget -c https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
tar -xf Python-3.6.8.tar.xz
cd Python-3.6.8/
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline- devel sqlite-devel gcc gcc-c++ openssl-devel libffi-devel python-devel mariadb-devel
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
mkdir /usr/local/python3
./configure --prefix=/usr/local/python3
make
make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
注:
安装python3.5以上会附加安装了包管理工具setuptools和pip,下面是手动安装
setuptools
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26 --no-check-certificate
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6/
python3 setup.py build
python3 setup.py install
pip3
wget https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb --no-check-certificate
tar -zxvf pip-8.0.2.tar.gz
cd pip-8.0.2
python3 setup.py build
python3 setup.py install
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
升级pip3
ll /usr/bin/pip3
pip3 install --upgrade pip
pip3 install --upgrade setuptools
pip3 install --upgrade pip
pip3 install --default-timeout=1000 aliyun-python-sdk-core-v3
这里也可以yum来安装
安装“阿里短信SDK核心库”
pip3 install --default-timeout=1000 aliyun-python-sdk-core-v3
三、阿里云申请“短信签名”名“短信模板”
异常通知模板
恢复通知模板
四、创建异常告警及恢复通知脚本
1、告警脚本
[root@ecs-a71b ~]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@ecs-a71b alertscripts]# vim sendSMS.py
#!/usr/bin/python3
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import sys
client = AcsClient('accessKeyId ', 'accessKeySecret ', 'cn-hangzhou') 改这里
request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')
string = sys.argv[2]
phone_number = sys.argv[1]
message = string.split(";")
print(message)
dict1 = { }
dict1['host'] = message[0]
dict1['time'] = message[1]
dict1['item'] = message[2][0:18]
request.add_query_param('RegionId', 'cn-hangzhou')
request.add_query_param('PhoneNumbers', phone_number)
request.add_query_param('SignName', '短信签名名称') 改这里
request.add_query_param('TemplateCode', '短信模板Code') 改这里,共3个位置即可
request.add_query_param('TemplateParam', dict1)
response = client.do_action(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
2、恢复通知脚本
同上
[root@ecs-a71b alertscripts]# vim sendSMS_retore.py
#!/usr/bin/python3
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import sys
client = AcsClient('accessKeyId', 'accessKeySecret', 'cn-hangzhou') 改这里
request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')
string = sys.argv[2]
phone_number = sys.argv[1]
message = string.split(";")
print(message)
dict1 = { }
dict1['host'] = message[0]
dict1['time'] = message[1]
dict1['item'] = message[2][0:18]
request.add_query_param('RegionId', 'cn-hangzhou')
request.add_query_param('PhoneNumbers', phone_number)
request.add_query_param('SignName', '短信签名名称') 改这里
request.add_query_param('TemplateCode', '短信模板Code') 改这里
request.add_query_param('TemplateParam', dict1)
response = client.do_action(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
五、服务器短信配置
1、报警媒介类型
2、管理-用户 (异常告警用户)
3、管理-用户 (恢复告警用户)
新建一个群组adminsy
管理-用户(新建一个用户)
如果就有了两个报警媒介
4、创建触发器
省略。。。。。。
5、创建动作
省略。。。。。。
六、短信测试
------------------------------------end