How to Set Up Mailx and Zabbix Alerts with Email and WeChat Integration
This guide walks you through installing and configuring mailx, creating Zabbix email alert scripts, and integrating WeChat notifications by setting up the necessary scripts, parameters, and media actions within the Zabbix web interface.
Install and Configure mailx
Install mailx
yum install -y mailxEdit mailx configuration
vim /etc/mail.rc <code>set [email protected] # define sender
set smtp=smtp.163.com # define SMTP server
set [email protected]
set smtp-auth-password=asdASD123
set smtp-auth=login</code>Send a test email
echo 'test1'|mail -s "testmail" [email protected]Create a Zabbix mail alert script
vim /usr/lib/zabbix/alertscripts/mail.shThis is the default script path; it can be changed in the Zabbix server configuration.
<code>#/bin/bash
to=$1
subject=$(echo $2|tr '\r\n' '\n')
message=$(echo $3|tr '\r\n' '\n')
echo "$message"|mail -s "$subject" $to >>/var/log/mailx.log 2>&1</code>touch /var/log/mailx.log chown -R zabbix.zabbix /var/log/mailx.log chmod +x /usr/lib/zabbix/alertscripts/mail.sh ./mail.sh [email protected] "Subject" "Content"
Edit Zabbix web UI to configure media
Macro variables are uppercase; see the official Zabbix macro documentation. Email media is also an option and is simpler.
Edit Zabbix web UI to configure users
Configure alert actions
Operations, recovery, or update actions must exist.
Condition configuration
Set trigger conditions as needed.
Operation configuration
Default title:
<code>{TRIGGER.STATUS}:{TRIGGER.NAME}</code>Message body:
<code>报警主机:{HOST.NAME}
报警IP:{HOST.IP}
报警时间:{EVENT.DATE}-{EVENT.TIME}
报警等级:{TRIGGER.SEVERITY}
报警信息:{TRIGGER.NAME}:{ITEM.VALUE}
事件ID:{EVENT.ID}</code>Recovery operation
Default title:
<code>{TRIGGER.STATUS}:{TRIGGER.NAME}</code>Message body:
<code>恢复主机:{HOST.NAME}
恢复IP:{HOST.IP}
恢复时间:{EVENT.DATE}-{EVENT.TIME}
恢复等级:{TRIGGER.SEVERITY}
恢复信息:{TRIGGER.NAME}:{ITEM.VALUE}
恢复ID:{EVENT.ID}</code>Stop agent and test alert
systemctl stop zabbix_agentsystemctl start zabbix_agentConfigure WeChat Alerts
Register Enterprise WeChat
Visit https://work.weixin.qq.com/wework_admin/ to create an account.
Obtain Enterprise ID
Navigate to “My Enterprise” and scroll to the bottom to find the ID.
Get AgentID and Secret
Create a new application under “Apps & Mini Programs”, note the AgentID and Secret.
Configure Script
Clone WeChat alert script from GitHub
<code>git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
cp Zabbix-Alert-WeChat/wechat.py /usr/local/zabbix34/alertscripts/
chmod +x wechat.py && chown zabbix:zabbix wechat.py</code>Install requests library
Install pip first, then install the library.
<code>python get-pip.py</code> <code>pip install requests</code>Modify wechat.py
Fill in Corpid, Secret, and AgentID with your Enterprise WeChat information; optionally set tag or department IDs.
Test the script:
<code>python wechat.py 企业微信ID test text
{u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}</code>Configure Media and Users in Zabbix Web
Media configuration
User configuration
Enter the Enterprise WeChat user ID as the recipient.
Configure Actions
Operation configuration
Message content and title are the same as email alerts; just change the media to WeChat.
Recovery operation
Set the media to WeChat for recovery notifications as well.
Test WeChat Alerts
Restart the VM; the Enterprise WeChat account should receive the alert.
Reference links: https://www.cnblogs.com/kumarhua/p/7645399.html http://blog.51cto.com/tryrus/1782062 https://www.cnblogs.com/winstom/p/8883796.html
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.