CentOS 7.7 yum installation and configuration Zabbix 4.0 LTS details (3)
1. Configure mail alarm, Wechat alarm and nail alarm:
1. Configure email alarms:
(1) define the sender:
Administration-- > Media types-- > Email-- > Media type-- > Update
(2) define the recipient:
Administration-- > Users-- > Admin-- > Media-- > Add-- > Update
(3) define actions:
Configuration-- > Actions-- > Create action-- > Add
Remarks: custom alarm content
Https://www.zabbix.com/documentation/4.0/zh/manual/appendix/macros/supported_by_location
2. Configure Wechat alarm:
(1) Registration WeCom: https://work.weixin.qq.com/
(2) login by scanning Wechat code:
My enterprise-- > micro-workbench-- > invite follow (use Wechat to scan the QR code to follow directly)
Application Management-- > Application-- > self-built-- > create Zabbix Wechat alarm application
My Enterprise-- > Enterprise Information-- > Enterprise ID
Address Book:
(3) write Wechat alarm script:
# grep ^ AlertScriptsPath / etc/zabbix/zabbix_server.conf
# cd / usr/lib/zabbix/alertscripts
# vim wechat.py
#! / usr/bin/env python
#-*-coding: utf-8-*-
# comment: Zabbix Wechat alarm script
Import requests
Import sys
Import os
Import json
Import logging
Logging.basicConfig (level=logging.DEBUG, format='% (asctime) s,% (filename) s,% (levelname) s,% (levelname) s,% d% b% Y% HRV% MRV% filename.path.join ('/ tmp','wechat.log'), filemode='a')
Corpid='XXXXXXXX'
Appsecret='XXXXXXXX'
Agentid='XXXXXXXX'
# get accesstoken
Token_url=' https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid +'& corpsecret=' + appsecret
Req=requests.get (token_url)
Accesstoken=req.json () ['access_token']
# send messages
Msgsend_url=' https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
Touser=sys.argv [1]
Subject=sys.argv [2]
Message=sys.argv [2] + "\ n\ n" + sys.argv [3]
Params= {
"touser": touser
"msgtype": "text"
"agentid": agentid
"text": {
"content": message
}
"safe": 0
}
Req=requests.post (msgsend_url, data=json.dumps (params))
Logging.info ('sendto:' + touser +'; subject:' + subject +'; message:' + message)
# chmod + x wechat.py
Note: the XXXXXXXX in the above script needs to be replaced according to the actual situation.
(4) preparation before executing the wechat.py script:
A. Configure Epel source: # yum-y install epel-release
B. Install the python2-pip package:
# python-version
# yum-y install python2-pip
C. Modify the pip source to Aliyun image source:
# mkdir-pv ~ / .pip
# vim ~ / .pip/pip.conf
[global]
Index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
Trusted-host = mirrors.aliyun.com
D. Upgrade pip software package:
# pip-V
# pip install-upgrade pip
# pip-V
E. Install the requests module of Python: # pip install requests
F. Create a log file and modify it to belong to the master group:
# touch / tmp/wechat.log
# chown zabbix.zabbix / tmp/wechat.log
(5) Test the wechat.py script:
# / usr/lib/zabbix/alertscripts/wechat.py 'XXXX'' topic: test' 'content: wechat alert'
(6) Wechat alarm has been added to the media:
Administration-- > Media types-- > Create media type-- > Media type-- > Add
Three parameters are required: {ALERT.SENDTO}, {ALERT.SUBJECT}, {ALERT.MESSAGE}
(7) alarm media are set in the corresponding users:
Administration-- > Users-- > Admin-- > Media-- > Add-- > Update
(8) define actions:
Configuration-- > Actions-- > Email-- > Clone-- > Add
3. Configure nail alarm:
(1) IP for public network exit of server is required
(2) registered enterprise nails: https://oa.dingtalk.com/
(3) Nail scan code, enter the administrative password and log in:
Address Book-- > Internal address Book Management-- > Department personnel-- > invite members to join (use pin scan QR code to apply)
Workbench-- > Application Management-- > self-built Application-- > create Zabbix nail alarm application
Nail open platform: https://open-dev.dingtalk.com/
(4) write nail alarm script:
# cd / usr/lib/zabbix/alertscripts
# vim dingtalk.py
#! / usr/bin/python
#-*-coding: utf-8-*-
Import json,urllib2,sys
Appkey = 'XXXXXXXX'
Appsecret = 'XXXXXXXX'
Agentid = 'XXXXXXXX'
Touser = sys.argv [1]
Content = sys.argv [2]
Tockenurl = 'https://oapi.dingtalk.com/gettoken?corpid=' + appkey + "& corpsecret=" + appsecret
Tockenresponse = urllib2.urlopen (tockenurl)
Tockenresult = json.loads (tockenresponse.read () .decode ('utf-8'))
Tocken = tockenresult ['access_token']
Sendurl = 'https://oapi.dingtalk.com/message/send?access_token=' + tocken
Headers = {
'Content-Type':'application/json'
}
Main_content = {
"touser": touser
"toparty":
"agentid": agentid
"msgtype": "text"
"text": {
"content": content
}
}
Main_content = json.dumps (main_content)
Req = urllib2.Request (sendurl,headers=headers)
Response = urllib2.urlopen (req, main_content.encode ('utf8'))
Print (response.read () .decode ('utf-8'))
# chmod + x dingtalk.py
Note: the XXXXXXXX in the above script needs to be replaced according to the actual situation.
(5) Test the dingtalk.py script:
# / usr/lib/zabbix/alertscripts/dingtalk.py 'XXXX'' zabbix alert test'
(6) add nail alarm to the media:
Administration-- > Media types-- > Create media type-- > Media type-- > Add
Two parameters are required: {ALERT.SENDTO}, {ALERT.MESSAGE}
(7) alarm media are set in the corresponding users:
Administration-- > Users-- > Admin-- > Media-- > Add-- > Update
(8) define actions:
Configuration-- > Actions-- > Email-- > Clone-- > Add
4. Alarm test:
(1) stop vsftpd:# systemctl stop vsftpd on the node-122 node
Check Action log:Reports-- > Action log
(2) start vsftpd:# systemctl start vsftpd on the node-122 node
Check Action log:Reports-- > Action log