Integrate Tencent Cloud SMS with Zabbix for Automated Alerting (Python)
This guide walks through obtaining a free Tencent Cloud SMS quota, creating an application, signature, and template, writing a Python script to format Zabbix alerts, configuring Zabbix media types, and testing the end‑to‑end SMS notification workflow while highlighting common pitfalls.
When you first enable a Tencent Cloud SMS package, you receive 100 free SMS messages each month for testing.
After activation, add an application
Enter the created application
Remember the application ID and key; you will need them later.
Create application signature and template
Create signature
Tencent Cloud only supports certain signature types; for personal use you can use a registered website or a public account. Alibaba Cloud does not support personal public accounts, only enterprise accounts.
Create template
Use
{}as variables in the template. After creation you obtain a template ID, which is required during integration. Each variable may be at most 12 characters, so define multiple variables and extract them with regular expressions.
Note: If you separate template variables with commas, do not include commas in the variable names within the trigger, otherwise escaping issues may cause SMS sending failures.
Start integrating Tencent Cloud SMS service
Tencent Cloud provides SDKs for multiple languages; the Python SDK (qcloudsms_py) can be installed via
pip. This guide uses the raw API because template variables must be ≤12 characters and cannot contain spaces, requiring pre‑processing of the message.
Implementation idea
Extract the first five lines of the alert using a regular expression and add them to a list.
Extract the last line (event information), split it into groups of up to 12 characters, store them in a list, and concatenate with the first list to form the template parameters.
POST the assembled parameter list to the SMS API.
Code
<code>#!/usr/bin/python3
#coding=utf-8
#author:wanger
import requests,re
import time,sys,json,hashlib,random
rand=random.randint(100000,999999)
mobile=sys.argv[1]
message=sys.argv[2]
message="""%s""" %message
times=int(time.time())
tpl_id=225686
appkey='f545bc772b396c41df6da4c4442ce085'
raw_text="appkey={}&random={}&time={}&mobile={}".format(appkey,rand,times,mobile)
sig=hashlib.sha256(raw_text.encode('utf-8')).hexdigest()
def rest(message):
# get alert content for debugging
with open('/tmp/message','w',encoding='utf-8') as f:
json.dump(message,f,ensure_ascii=False)
res=re.findall(r':(.*)\r\n',message,re.M)
hostname=res[0]
ip1=re.match(r'(\d+\.\d+)\.(.*)',res[1]).group(1)
ip2=re.match(r'(\d+\.\d+)\.(.*)',res[1]).group(2)
date1=re.match(r'(.*)-(.*)',res[2]).group(1)
time1=re.match(r'(.*)-(.*)',res[2]).group(2)
level=res[3]
id1=res[4]
alert=[hostname,ip1,ip2,date1,time1,level,id1]
# write processed first five lines for debugging
with open('tmp/messages','a',encoding='utf-8') as f:
for i in alert:
f.write(i)
return alert
def remes(alert,message):
res=re.search('报警信息:(.*)$',message).group(1)
event=[]
a,b=0,11
for i in range(5):
s1=res[a:b]
if len(s1)==0:
s1='\r'
event.append(s1)
a,b=a+11,b+11
var=alert+event
# write processed event info for debugging
with open('/tmp/messages1','a',encoding='utf-8') as f:
for i in event:
json.dump(i,f,ensure_ascii=False)
return var
def sendsms(remes):
url='https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=1400238944&random={}'.format(rand)
header={"Content-Type": "application/json"}
data={
"ext": "123",
"extend": "",
"params": [
remes[0],remes[1],remes[2],remes[3],remes[4],remes[5],remes[6],remes[7],remes[8],remes[9],remes[10],remes[11]
],
"sig": sig,
"sign": "没有故事的陈师傅",
"tel": {"mobile": mobile, "nationcode": "86"},
"time": times,
"tpl_id": tpl_id
}
request=requests.post(url=url,headers=header,json=data)
return json.loads(request.content)
rest=rest(message)
remes=remes(rest,message)
sendsms(remes)
</code>Integrate with Zabbix
Place the script in
/usr/lib/zabbix/alertscripts/, grant execution permissions, and configure the media type in Zabbix.
Define alert media type
Configure recipient phone number
Add action
Message order must match the SMS template.
Test Zabbix alert
Stop the Zabbix agent to trigger an alert:
<code>systemctl stop zabbix-agent</code>You should receive the SMS alert.
Check error details in the Tencent Cloud SMS statistics page if needed.
Common pitfalls
Prefer Python 3; Python 2 may have regex issues on Linux.
Avoid using commas in template variables within triggers to prevent escaping problems.
One phone number can receive at most 1 SMS per 30 seconds, 5 per hour, and 10 per day; consider other providers or enterprise verification for higher limits.
Template variables cannot be empty; replace empty strings with "\r" and ensure no spaces separate variables.
Feel free to share improvements in the comments.
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.