zabbix實時監控服務簡訊報警
阿新 • • 發佈:2018-11-12
一.監控服務通過埠的監聽狀態(批量監控)
批量監控埠(也可以自動發現埠,但是自動發現的監聽埠可能含我們不想監控的,這裡使用手動新增)
監控linux伺服器批量埠指令碼check_port.py:
#!/usr/bin/env python #coding:utf-8 import os, json portlist=["8089", "8080", "8001", "3306"] port_list=[] port_dict={"data":None} for port in portlist: pdict={} pdict["{#TCP_PORT}"]=port port_list.append(pdict) port_dict["data"]=port_list jsonStr = json.dumps(port_dict, sort_keys=True, indent=4) print jsonStr
監控window伺服器批量埠指令碼check_port.bat:
@echo off
echo {
echo "data": [
echo {
echo "{#TCP_PORT}": "80"
echo },
echo {
echo "{#TCP_PORT}": "3306"
echo },
echo {
echo "{#TCP_PORT}": "9001"
echo }
echo ]
echo }
可以建立統一監控模板:
1.新增自動發現規則:
2.新增監控項原型:
3.新增觸發器型別
4.報警媒介型別:
5.報警媒介使用者:
6.觸發動作:
二.報警媒介:呼叫簡訊平臺介面(sms.py)
#!/usr/bin/python #-*- coding:utf-8 -*- import httplib import urllib import sys text0 = sys.argv[1] text1 = text0.replace('^M','').split(":") text2 = text1[5].split("1.")[1] text3 = text2.split("(") #text4 = '告警主機:'+text3[1] #text5 = '主機埠: '+text3[0] host = "api.isms.ihuyi.com" sms_send_uri = "/webservice/isms.php?method=Submit" #使用者名稱APIID account = "APIID" #密碼 APIKEY password = "APIKEY" def send_sms(text, mobile): params = urllib.urlencode({'account': account, 'password' : password, 'content': text, 'mobile':mobile,'format':'json' }) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.HTTPConnection(host, port=80, timeout=30) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str if __name__ == '__main__': #列表新增報警手機號 mobilelist = ["86 13688888888","86 13677777777","86 1386666666"] text = "伺服器異常,異常主機:" + text3[1] + "(" + text3[2] + ")" + ",異常問題:埠" + text3[0] + ",請及時修復。" for mobile in mobilelist: print(send_sms(text, mobile))