zabbix配置騰訊雲簡訊報警

2021-09-27 05:37:08 字數 2591 閱讀 3332

單擊建立簽名

建立應用模板

模板內容使用{}作為變數,建立完模板之後會生成乙個模板id,這個後面接入的時候也會用到,由於個人使用者單個模板變數最大長度不超過12個字元,因此需要多定義幾個變數,最後用正則取出

注意:模板變數之間使用,分隔的話,那麼在觸發器裡名字就不要包含,了,否則會發生轉義,導致由於變數太長而傳送簡訊失敗

實現思路:

將獲取到的資訊前五行進行正則匹配,因為前五行的內容比較短,方便處理,將":"與換行符之間的內容進行匹配,並新增到列表中

將獲取到的資訊最後一行也就是事件資訊進行正則匹配,由於內容比較長,所以需要將字串分成五組,每組長度不超過十二個字元,並存放在列表中,將兩個列表進行相加,並作為模板引數傳給簡訊服務的api

將傳入列表中的元素作為引數post請求給api,平台傳送簡訊

**如下:

#!/usr/bin/python3

#coding=utf-8

#author:wanger

import requests,re

import time,sys,json,hashlib,random

rand=random.randint(100000,999999)

mobile=sys.ar**[1]

message=sys.ar**[2]

message="""%s""" %message

times=time.time()

times=int(times)

tpl_id=225686

sig=hashlib.sha256(raw_text.encode('utf-8')).hexdigest()

def rest(message):

#獲取報警內容,方便後續除錯

with open('/tmp/message','w',encoding='utf-8') as f:

s=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]

#獲取處理後的前五行內容,方便除錯

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'

a,b=a+11,b+11

var=alert+event

#獲取處理後的報警資訊,方便除錯

with open('/tmp/messages1','a',encoding='utf-8') as f:

for i in event:

s=json.dump(i,f,ensure_ascii=false)

return var

#print(sig)

def sendsms(remes):

data=,

"time": times,

"tpl_id": 387120

}request=requests.post(url=url,headers=header,json=data)

return json.loads(request.content)

rest=rest(message)

remes=remes(rest,message)

sendsms(remes)

將指令碼放到/usr/lib/zabbix/alertscripts/目錄下,並授予指令碼操作許可權,然後在zabbix頁面進行配置

定義報警媒介型別

配置使用者接收的手機號

新增動作停止zabbix-agent,使其產生報警

systemctl stop zabbix-agent
可以看到報警簡訊已經收到盡量使用python3,python2在linux環境下匹配資料可能會存在問題

簡訊模板中變數的分隔符盡量不要出現在觸發器中,否則會造成轉義

傳入的模板變數不能為空字元,可以先將空字串轉為"\r",模板變數之間也不能用空字元分隔

zabbix監控之簡訊報警

編寫簡訊報警指令碼 在zabbix安裝目錄這個資料夾裡 alertscripts 建立sendsms.sh指令碼 description zabbix簡訊告警指令碼 notes 簡訊閘道器使用了創藍簡訊 指令碼的日誌檔案 logfile tmp sms.log logfile exec 1 logf...

騰訊雲 簡訊

主要引數 簡訊簽名 模板id 1.發簡訊工具類 public class smsutil 簽名 傳送驗證碼 param phone 手機號 param smscode 驗證碼 return public static boolean sendsms string phone,string smsco...

Zabbix配置郵件報警

cd usr lib zabbix alertscripts vim zabbix email.sh 163的郵箱 usr bin python coding utf 8 import smtplib,sys from email.mime.text import mimetext from ema...